Changeset 10091
- Timestamp:
- Feb 3, 2010, 2:31:12 PM (12 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/bencode-test.c
r8889 r10091 11 11 #include "utils.h" /* tr_free */ 12 12 13 #undef VERBOSE 13 /* #define VERBOSE */ 14 14 15 15 static int test = 0; … … 171 171 #if 0 172 172 fprintf( stderr, "in: [%s]\n", str ); 173 fprintf( stderr, "out:\n%s", tr_benc SaveAsJSON( &val, NULL ) );173 fprintf( stderr, "out:\n%s", tr_bencToStr( &val, TR_FMT_JSON, NULL ) ); 174 174 #endif 175 175 check( end == (const uint8_t*)str + len ); -
trunk/libtransmission/bencode.c
r10084 r10091 356 356 ***/ 357 357 358 /* returns true if the given string length would fit in our string buffer */359 static inline int360 stringFitsInBuffer( const tr_benc * val, int len )361 {362 return len < (int)sizeof( val->val.s.str.buf );363 }364 365 358 /* returns true if the benc's string was malloced. 366 359 * this occurs when the string is too long for our string buffer */ … … 368 361 stringIsAlloced( const tr_benc * val ) 369 362 { 370 return !stringFitsInBuffer( val, val->val.s.len);363 return val->val.s.len >= sizeof( val->val.s.str.buf ); 371 364 } 372 365 … … 578 571 tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount ) 579 572 { 573 char * setme; 580 574 tr_bencInit( val, TR_TYPE_STR ); 581 575 582 if( stringFitsInBuffer( val, val->val.s.len = byteCount )) 583 memcpy( val->val.s.str.buf, src, byteCount ); 576 /* There's no way in benc notation to distinguish between 577 * zero-terminated strings and raw byte arrays. 578 * Because of this, tr_bencMergeDicts() and tr_bencListCopy() 579 * don't know whether or not a TR_TYPE_STR node needs a '\0'. 580 * Append one, een to the raw arrays, just to be safe. */ 581 582 if( byteCount < sizeof( val->val.s.str.buf ) ) 583 setme = val->val.s.str.buf; 584 584 else 585 val->val.s.str.ptr = tr_memdup( src, byteCount ); 585 setme = val->val.s.str.ptr = tr_new( char, byteCount + 1 ); 586 587 memcpy( setme, src, byteCount ); 588 setme[byteCount] = '\0'; 589 val->val.s.len = byteCount; 586 590 } 587 591 … … 589 593 tr_bencInitStr( tr_benc * val, const void * str, int len ) 590 594 { 591 tr_bencInit( val, TR_TYPE_STR );592 593 595 if( str == NULL ) 594 596 len = 0; … … 596 598 len = strlen( str ); 597 599 598 if( stringFitsInBuffer( val, val->val.s.len = len )) { 599 memcpy( val->val.s.str.buf, str, len ); 600 val->val.s.str.buf[len] = '\0'; 601 } else 602 val->val.s.str.ptr = tr_strndup( str, len ); 600 tr_bencInitRaw( val, str, len ); 603 601 } 604 602
Note: See TracChangeset
for help on using the changeset viewer.