1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-25 17:17:31 +00:00

Correctly bencode strings containing nul characters.

This commit is contained in:
Josh Elsasser 2007-01-28 23:26:57 +00:00
parent 3388b93542
commit 6ebeef7924
3 changed files with 36 additions and 12 deletions

View file

@ -164,7 +164,8 @@ static void __bencPrint( benc_val_t * val, int space )
break; break;
case TYPE_STR: 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; break;
case TYPE_LIST: case TYPE_LIST:
@ -265,12 +266,8 @@ int tr_bencSave( benc_val_t * val, char ** buf, int * used, int * max )
break; break;
case TYPE_STR: case TYPE_STR:
if( (int)strlen(val->val.s.s) != val->val.s.i ) 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;
}
if( tr_sprintf( buf, used, max, "%i:%s",
val->val.s.i, val->val.s.s ) )
{ {
return 1; return 1;
} }

View file

@ -300,22 +300,46 @@ int tr_vsprintf( char ** buf, int * used, int * max, const char * fmt,
va_list ap1, va_list ap2 ) va_list ap1, va_list ap2 )
{ {
int want; int want;
char * newbuf;
want = vsnprintf( NULL, 0, fmt, ap1 ); want = vsnprintf( NULL, 0, fmt, ap1 );
while( *used + want + 1 > *max ) if( tr_concat( buf, used, max, NULL, want ) )
{ {
*max += SPRINTF_BUFSIZE; return 1;
newbuf = realloc( *buf, *max ); }
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 ) if( NULL == newbuf )
{ {
return 1; return 1;
} }
*buf = newbuf; *buf = newbuf;
*max = newmax;
} }
*used += vsnprintf( *buf + *used, *max - *used, fmt, ap2 ); if( NULL != data )
{
memcpy( *buf + *used, data, len );
*used += len;
}
return 0; return 0;
} }

View file

@ -62,6 +62,9 @@ int tr_sprintf( char ** buf, int * used, int * max,
const char * format, ... ) PRINTF( 4, 5 ); const char * format, ... ) PRINTF( 4, 5 );
/* gee, it sure would be nice if BeOS had va_copy() */ /* gee, it sure would be nice if BeOS had va_copy() */
int tr_vsprintf( char **, int *, int *, const char *, va_list, va_list ); 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 * tr_dupstr