mirror of
https://github.com/transmission/transmission
synced 2024-12-25 01:03:01 +00:00
Correctly bencode strings containing nul characters.
This commit is contained in:
parent
3388b93542
commit
6ebeef7924
3 changed files with 36 additions and 12 deletions
|
@ -164,7 +164,8 @@ static void __bencPrint( benc_val_t * val, int space )
|
|||
break;
|
||||
|
||||
case TYPE_STR:
|
||||
fprintf( stderr, "%s\n", val->val.s.s );
|
||||
fwrite( val->val.s.s, 1, val->val.s.i, stderr );
|
||||
putc( '\n', stderr );
|
||||
break;
|
||||
|
||||
case TYPE_LIST:
|
||||
|
@ -265,12 +266,8 @@ int tr_bencSave( benc_val_t * val, char ** buf, int * used, int * max )
|
|||
break;
|
||||
|
||||
case TYPE_STR:
|
||||
if( (int)strlen(val->val.s.s) != val->val.s.i )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if( tr_sprintf( buf, used, max, "%i:%s",
|
||||
val->val.s.i, val->val.s.s ) )
|
||||
if( tr_sprintf( buf, used, max, "%i:", val->val.s.i ) ||
|
||||
tr_concat( buf, used, max, val->val.s.s, val->val.s.i ) )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -300,22 +300,46 @@ int tr_vsprintf( char ** buf, int * used, int * max, const char * fmt,
|
|||
va_list ap1, va_list ap2 )
|
||||
{
|
||||
int want;
|
||||
char * newbuf;
|
||||
|
||||
want = vsnprintf( NULL, 0, fmt, ap1 );
|
||||
|
||||
while( *used + want + 1 > *max )
|
||||
if( tr_concat( buf, used, max, NULL, want ) )
|
||||
{
|
||||
*max += SPRINTF_BUFSIZE;
|
||||
newbuf = realloc( *buf, *max );
|
||||
return 1;
|
||||
}
|
||||
assert( *used + want + 1 <= *max );
|
||||
|
||||
*used += vsnprintf( *buf + *used, *max - *used, fmt, ap2 );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tr_concat( char ** buf, int * used, int * max, const char * data, int len )
|
||||
{
|
||||
int newmax;
|
||||
char * newbuf;
|
||||
|
||||
newmax = *max;
|
||||
while( *used + len + 1 > newmax )
|
||||
{
|
||||
newmax += SPRINTF_BUFSIZE;
|
||||
}
|
||||
if( newmax > *max )
|
||||
{
|
||||
newbuf = realloc( *buf, newmax );
|
||||
if( NULL == newbuf )
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
*buf = newbuf;
|
||||
*max = newmax;
|
||||
}
|
||||
|
||||
*used += vsnprintf( *buf + *used, *max - *used, fmt, ap2 );
|
||||
if( NULL != data )
|
||||
{
|
||||
memcpy( *buf + *used, data, len );
|
||||
*used += len;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -62,6 +62,9 @@ int tr_sprintf( char ** buf, int * used, int * max,
|
|||
const char * format, ... ) PRINTF( 4, 5 );
|
||||
/* gee, it sure would be nice if BeOS had va_copy() */
|
||||
int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list );
|
||||
/* this concatenates some binary data onto the end of a buffer */
|
||||
int tr_concat( char ** buf, int * used, int * max,
|
||||
const char * data, int len );
|
||||
|
||||
/***********************************************************************
|
||||
* tr_dupstr
|
||||
|
|
Loading…
Reference in a new issue