Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / libtransmission / bencode.c
blob9fbec5d136ec550c62f6b28b2515a9ed5d377132
1 /*
2 * This file Copyright (C) 2008-2010 Mnemosyne LLC
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
10 * $Id: bencode.c 11445 2010-12-01 04:54:18Z charles $
13 #include <assert.h>
14 #include <ctype.h> /* isdigit() */
15 #include <errno.h>
16 #include <math.h> /* fabs() */
17 #include <stdio.h>
18 #include <stdlib.h> /* realpath() */
19 #include <string.h>
21 #ifdef WIN32 /* tr_mkstemp() */
22 #include <fcntl.h>
23 #define _S_IREAD 256
24 #define _S_IWRITE 128
25 #endif
27 #include <sys/types.h> /* stat() */
28 #include <sys/stat.h> /* stat() */
29 #include <locale.h>
30 #include <unistd.h> /* stat() */
32 #include <event.h> /* struct evbuffer */
34 #include "ConvertUTF.h"
36 #include "transmission.h"
37 #include "bencode.h"
38 #include "fdlimit.h" /* tr_close_file() */
39 #include "json.h"
40 #include "list.h"
41 #include "platform.h" /* TR_PATH_MAX */
42 #include "ptrarray.h"
43 #include "utils.h" /* tr_new(), tr_free() */
45 #ifndef ENODATA
46 #define ENODATA EIO
47 #endif
49 /**
50 ***
51 **/
53 static tr_bool
54 isContainer( const tr_benc * val )
56 return tr_bencIsList( val ) || tr_bencIsDict( val );
59 static tr_bool
60 isSomething( const tr_benc * val )
62 return isContainer( val ) || tr_bencIsInt( val )
63 || tr_bencIsString( val )
64 || tr_bencIsReal( val )
65 || tr_bencIsBool( val );
68 static void
69 tr_bencInit( tr_benc * val, char type )
71 memset( val, 0, sizeof( *val ) );
72 val->type = type;
75 /***
76 **** tr_bencParse()
77 **** tr_bencLoad()
78 ***/
80 /**
81 * The initial i and trailing e are beginning and ending delimiters.
82 * You can have negative numbers such as i-3e. You cannot prefix the
83 * number with a zero such as i04e. However, i0e is valid.
84 * Example: i3e represents the integer "3"
85 * NOTE: The maximum number of bit of this integer is unspecified,
86 * but to handle it as a signed 64bit integer is mandatory to handle
87 * "large files" aka .torrent for more that 4Gbyte
89 int
90 tr_bencParseInt( const uint8_t * buf,
91 const uint8_t * bufend,
92 const uint8_t ** setme_end,
93 int64_t * setme_val )
95 char * endptr;
96 const void * begin;
97 const void * end;
98 int64_t val;
100 if( buf >= bufend )
101 return EILSEQ;
102 if( *buf != 'i' )
103 return EILSEQ;
105 begin = buf + 1;
106 end = memchr( begin, 'e', ( bufend - buf ) - 1 );
107 if( end == NULL )
108 return EILSEQ;
110 errno = 0;
111 val = evutil_strtoll( begin, &endptr, 10 );
112 if( errno || ( endptr != end ) ) /* incomplete parse */
113 return EILSEQ;
114 if( val && *(const char*)begin == '0' ) /* no leading zeroes! */
115 return EILSEQ;
117 *setme_end = (const uint8_t*)end + 1;
118 *setme_val = val;
119 return 0;
123 * Byte strings are encoded as follows:
124 * <string length encoded in base ten ASCII>:<string data>
125 * Note that there is no constant beginning delimiter, and no ending delimiter.
126 * Example: 4:spam represents the string "spam"
129 tr_bencParseStr( const uint8_t * buf,
130 const uint8_t * bufend,
131 const uint8_t ** setme_end,
132 const uint8_t ** setme_str,
133 size_t * setme_strlen )
135 size_t len;
136 const void * end;
137 char * endptr;
139 if( buf >= bufend )
140 return EILSEQ;
142 if( !isdigit( *buf ) )
143 return EILSEQ;
145 end = memchr( buf, ':', bufend - buf );
146 if( end == NULL )
147 return EILSEQ;
149 errno = 0;
150 len = strtoul( (const char*)buf, &endptr, 10 );
151 if( errno || endptr != end )
152 return EILSEQ;
154 if( (const uint8_t*)end + 1 + len > bufend )
155 return EILSEQ;
157 *setme_end = (const uint8_t*)end + 1 + len;
158 *setme_str = (const uint8_t*)end + 1;
159 *setme_strlen = len;
160 return 0;
163 /* set to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */
164 #define LIST_SIZE 4 /* number of items to increment list/dict buffer by */
166 static int
167 makeroom( tr_benc * val,
168 size_t count )
170 assert( TR_TYPE_LIST == val->type || TR_TYPE_DICT == val->type );
172 if( val->val.l.count + count > val->val.l.alloc )
174 /* We need a bigger boat */
175 const int len = val->val.l.alloc + count +
176 ( count % LIST_SIZE ? LIST_SIZE -
177 ( count % LIST_SIZE ) : 0 );
178 void * tmp = realloc( val->val.l.vals, len * sizeof( tr_benc ) );
179 if( !tmp )
180 return 1;
182 val->val.l.alloc = len;
183 val->val.l.vals = tmp;
186 return 0;
189 static tr_benc*
190 getNode( tr_benc * top,
191 tr_ptrArray * parentStack,
192 int type )
194 tr_benc * parent;
196 assert( top );
197 assert( parentStack );
199 if( tr_ptrArrayEmpty( parentStack ) )
200 return top;
202 parent = tr_ptrArrayBack( parentStack );
203 assert( parent );
205 /* dictionary keys must be strings */
206 if( ( parent->type == TR_TYPE_DICT )
207 && ( type != TR_TYPE_STR )
208 && ( !( parent->val.l.count % 2 ) ) )
209 return NULL;
211 makeroom( parent, 1 );
212 return parent->val.l.vals + parent->val.l.count++;
216 * This function's previous recursive implementation was
217 * easier to read, but was vulnerable to a smash-stacking
218 * attack via maliciously-crafted bencoded data. (#667)
220 static int
221 tr_bencParseImpl( const void * buf_in,
222 const void * bufend_in,
223 tr_benc * top,
224 tr_ptrArray * parentStack,
225 const uint8_t ** setme_end )
227 int err;
228 const uint8_t * buf = buf_in;
229 const uint8_t * bufend = bufend_in;
231 tr_bencInit( top, 0 );
233 while( buf != bufend )
235 if( buf > bufend ) /* no more text to parse... */
236 return 1;
238 if( *buf == 'i' ) /* int */
240 int64_t val;
241 const uint8_t * end;
242 tr_benc * node;
244 if( ( err = tr_bencParseInt( buf, bufend, &end, &val ) ) )
245 return err;
247 node = getNode( top, parentStack, TR_TYPE_INT );
248 if( !node )
249 return EILSEQ;
251 tr_bencInitInt( node, val );
252 buf = end;
254 if( tr_ptrArrayEmpty( parentStack ) )
255 break;
257 else if( *buf == 'l' ) /* list */
259 tr_benc * node = getNode( top, parentStack, TR_TYPE_LIST );
260 if( !node )
261 return EILSEQ;
262 tr_bencInit( node, TR_TYPE_LIST );
263 tr_ptrArrayAppend( parentStack, node );
264 ++buf;
266 else if( *buf == 'd' ) /* dict */
268 tr_benc * node = getNode( top, parentStack, TR_TYPE_DICT );
269 if( !node )
270 return EILSEQ;
271 tr_bencInit( node, TR_TYPE_DICT );
272 tr_ptrArrayAppend( parentStack, node );
273 ++buf;
275 else if( *buf == 'e' ) /* end of list or dict */
277 tr_benc * node;
278 ++buf;
279 if( tr_ptrArrayEmpty( parentStack ) )
280 return EILSEQ;
282 node = tr_ptrArrayBack( parentStack );
283 if( tr_bencIsDict( node ) && ( node->val.l.count % 2 ) )
285 /* odd # of children in dict */
286 tr_bencFree( &node->val.l.vals[--node->val.l.count] );
287 return EILSEQ;
290 tr_ptrArrayPop( parentStack );
291 if( tr_ptrArrayEmpty( parentStack ) )
292 break;
294 else if( isdigit( *buf ) ) /* string? */
296 const uint8_t * end;
297 const uint8_t * str;
298 size_t str_len;
299 tr_benc * node;
301 if( ( err = tr_bencParseStr( buf, bufend, &end, &str, &str_len ) ) )
302 return err;
304 node = getNode( top, parentStack, TR_TYPE_STR );
305 if( !node )
306 return EILSEQ;
308 tr_bencInitStr( node, str, str_len );
309 buf = end;
311 if( tr_ptrArrayEmpty( parentStack ) )
312 break;
314 else /* invalid bencoded text... march past it */
316 ++buf;
320 err = !isSomething( top ) || !tr_ptrArrayEmpty( parentStack );
322 if( !err && setme_end )
323 *setme_end = buf;
325 return err;
329 tr_bencParse( const void * buf,
330 const void * end,
331 tr_benc * top,
332 const uint8_t ** setme_end )
334 int err;
335 tr_ptrArray parentStack = TR_PTR_ARRAY_INIT;
337 top->type = 0; /* set to `uninitialized' */
338 err = tr_bencParseImpl( buf, end, top, &parentStack, setme_end );
339 if( err )
340 tr_bencFree( top );
342 tr_ptrArrayDestruct( &parentStack, NULL );
343 return err;
347 tr_bencLoad( const void * buf_in,
348 size_t buflen,
349 tr_benc * setme_benc,
350 char ** setme_end )
352 const uint8_t * buf = buf_in;
353 const uint8_t * end;
354 const int ret = tr_bencParse( buf, buf + buflen, setme_benc, &end );
356 if( !ret && setme_end )
357 *setme_end = (char*) end;
358 return ret;
361 /***
362 ****
363 ***/
365 /* returns true if the benc's string was malloced.
366 * this occurs when the string is too long for our string buffer */
367 static inline int
368 stringIsAlloced( const tr_benc * val )
370 return val->val.s.len >= sizeof( val->val.s.str.buf );
373 /* returns a const pointer to the benc's string */
374 static inline const char*
375 getStr( const tr_benc* val )
377 return stringIsAlloced(val) ? val->val.s.str.ptr : val->val.s.str.buf;
380 static int
381 dictIndexOf( const tr_benc * val, const char * key )
383 if( tr_bencIsDict( val ) )
385 size_t i;
386 const size_t len = strlen( key );
388 for( i = 0; ( i + 1 ) < val->val.l.count; i += 2 )
390 const tr_benc * child = val->val.l.vals + i;
391 if( ( child->type == TR_TYPE_STR )
392 && ( child->val.s.len == len )
393 && !memcmp( getStr(child), key, len ) )
394 return i;
398 return -1;
401 tr_benc *
402 tr_bencDictFind( tr_benc * val, const char * key )
404 const int i = dictIndexOf( val, key );
406 return i < 0 ? NULL : &val->val.l.vals[i + 1];
409 static tr_bool
410 tr_bencDictFindType( tr_benc * dict, const char * key, int type, tr_benc ** setme )
412 return tr_bencIsType( *setme = tr_bencDictFind( dict, key ), type );
415 size_t
416 tr_bencListSize( const tr_benc * list )
418 return tr_bencIsList( list ) ? list->val.l.count : 0;
421 tr_benc*
422 tr_bencListChild( tr_benc * val,
423 size_t i )
425 tr_benc * ret = NULL;
427 if( tr_bencIsList( val ) && ( i < val->val.l.count ) )
428 ret = val->val.l.vals + i;
429 return ret;
433 tr_bencListRemove( tr_benc * list, size_t i )
435 if( tr_bencIsList( list ) && ( i < list->val.l.count ) )
437 tr_bencFree( &list->val.l.vals[i] );
438 tr_removeElementFromArray( list->val.l.vals, i, sizeof( tr_benc ), list->val.l.count-- );
439 return 1;
442 return 0;
445 static void
446 tr_benc_warning( const char * err )
448 fprintf( stderr, "warning: %s\n", err );
451 tr_bool
452 tr_bencGetInt( const tr_benc * val,
453 int64_t * setme )
455 tr_bool success = FALSE;
457 if( !success && (( success = tr_bencIsInt( val ))))
458 if( setme )
459 *setme = val->val.i;
461 if( !success && (( success = tr_bencIsBool( val )))) {
462 tr_benc_warning( "reading bool as an int" );
463 if( setme )
464 *setme = val->val.b ? 1 : 0;
467 return success;
470 tr_bool
471 tr_bencGetStr( const tr_benc * val, const char ** setme )
473 const tr_bool success = tr_bencIsString( val );
475 if( success )
476 *setme = getStr( val );
478 return success;
481 tr_bool
482 tr_bencGetBool( const tr_benc * val, tr_bool * setme )
484 const char * str;
485 tr_bool success = FALSE;
487 if(( success = tr_bencIsBool( val )))
488 *setme = val->val.b;
490 if( !success && tr_bencIsInt( val ) )
491 if(( success = ( val->val.i==0 || val->val.i==1 ) ))
492 *setme = val->val.i!=0;
494 if( !success && tr_bencGetStr( val, &str ) )
495 if(( success = ( !strcmp(str,"true") || !strcmp(str,"false"))))
496 *setme = !strcmp(str,"true");
498 return success;
501 tr_bool
502 tr_bencGetReal( const tr_benc * val, double * setme )
504 tr_bool success = FALSE;
506 if( !success && (( success = tr_bencIsReal( val ))))
507 *setme = val->val.d;
509 if( !success && (( success = tr_bencIsInt( val ))))
510 *setme = val->val.i;
512 if( !success && tr_bencIsString(val) )
514 char * endptr;
515 char locale[128];
516 double d;
518 /* the json spec requires a '.' decimal point regardless of locale */
519 tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
520 setlocale( LC_NUMERIC, "POSIX" );
521 d = strtod( getStr(val), &endptr );
522 setlocale( LC_NUMERIC, locale );
524 if(( success = ( getStr(val) != endptr ) && !*endptr ))
525 *setme = d;
529 return success;
532 tr_bool
533 tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme )
535 return tr_bencGetInt( tr_bencDictFind( dict, key ), setme );
538 tr_bool
539 tr_bencDictFindBool( tr_benc * dict, const char * key, tr_bool * setme )
541 return tr_bencGetBool( tr_bencDictFind( dict, key ), setme );
544 tr_bool
545 tr_bencDictFindReal( tr_benc * dict, const char * key, double * setme )
547 return tr_bencGetReal( tr_bencDictFind( dict, key ), setme );
550 tr_bool
551 tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme )
553 return tr_bencGetStr( tr_bencDictFind( dict, key ), setme );
556 tr_bool
557 tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme )
559 return tr_bencDictFindType( dict, key, TR_TYPE_LIST, setme );
562 tr_bool
563 tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme )
565 return tr_bencDictFindType( dict, key, TR_TYPE_DICT, setme );
568 tr_bool
569 tr_bencDictFindRaw( tr_benc * dict,
570 const char * key,
571 const uint8_t ** setme_raw,
572 size_t * setme_len )
574 tr_benc * child;
575 const tr_bool found = tr_bencDictFindType( dict, key, TR_TYPE_STR, &child );
577 if( found ) {
578 *setme_raw = (uint8_t*) getStr(child);
579 *setme_len = child->val.s.len;
582 return found;
585 /***
586 ****
587 ***/
589 void
590 tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount )
592 char * setme;
593 tr_bencInit( val, TR_TYPE_STR );
595 /* There's no way in benc notation to distinguish between
596 * zero-terminated strings and raw byte arrays.
597 * Because of this, tr_bencMergeDicts() and tr_bencListCopy()
598 * don't know whether or not a TR_TYPE_STR node needs a '\0'.
599 * Append one, een to the raw arrays, just to be safe. */
601 if( byteCount < sizeof( val->val.s.str.buf ) )
602 setme = val->val.s.str.buf;
603 else
604 setme = val->val.s.str.ptr = tr_new( char, byteCount + 1 );
606 memcpy( setme, src, byteCount );
607 setme[byteCount] = '\0';
608 val->val.s.len = byteCount;
611 void
612 tr_bencInitStr( tr_benc * val, const void * str, int len )
614 if( str == NULL )
615 len = 0;
616 else if( len < 0 )
617 len = strlen( str );
619 tr_bencInitRaw( val, str, len );
622 void
623 tr_bencInitBool( tr_benc * b, int value )
625 tr_bencInit( b, TR_TYPE_BOOL );
626 b->val.b = value != 0;
629 void
630 tr_bencInitReal( tr_benc * b, double value )
632 tr_bencInit( b, TR_TYPE_REAL );
633 b->val.d = value;
636 void
637 tr_bencInitInt( tr_benc * b, int64_t value )
639 tr_bencInit( b, TR_TYPE_INT );
640 b->val.i = value;
644 tr_bencInitList( tr_benc * b, size_t reserveCount )
646 tr_bencInit( b, TR_TYPE_LIST );
647 return tr_bencListReserve( b, reserveCount );
651 tr_bencListReserve( tr_benc * b, size_t count )
653 assert( tr_bencIsList( b ) );
654 return makeroom( b, count );
658 tr_bencInitDict( tr_benc * b, size_t reserveCount )
660 tr_bencInit( b, TR_TYPE_DICT );
661 return tr_bencDictReserve( b, reserveCount );
665 tr_bencDictReserve( tr_benc * b, size_t reserveCount )
667 assert( tr_bencIsDict( b ) );
668 return makeroom( b, reserveCount * 2 );
671 tr_benc *
672 tr_bencListAdd( tr_benc * list )
674 tr_benc * item;
676 assert( tr_bencIsList( list ) );
678 if( list->val.l.count == list->val.l.alloc )
679 tr_bencListReserve( list, LIST_SIZE );
681 assert( list->val.l.count < list->val.l.alloc );
683 item = &list->val.l.vals[list->val.l.count];
684 list->val.l.count++;
685 tr_bencInit( item, TR_TYPE_INT );
687 return item;
690 tr_benc *
691 tr_bencListAddInt( tr_benc * list, int64_t val )
693 tr_benc * node = tr_bencListAdd( list );
695 tr_bencInitInt( node, val );
696 return node;
699 tr_benc *
700 tr_bencListAddReal( tr_benc * list, double val )
702 tr_benc * node = tr_bencListAdd( list );
703 tr_bencInitReal( node, val );
704 return node;
707 tr_benc *
708 tr_bencListAddBool( tr_benc * list, tr_bool val )
710 tr_benc * node = tr_bencListAdd( list );
711 tr_bencInitBool( node, val );
712 return node;
715 tr_benc *
716 tr_bencListAddStr( tr_benc * list, const char * val )
718 tr_benc * node = tr_bencListAdd( list );
719 tr_bencInitStr( node, val, -1 );
720 return node;
723 tr_benc *
724 tr_bencListAddRaw( tr_benc * list, const uint8_t * val, size_t len )
726 tr_benc * node = tr_bencListAdd( list );
727 tr_bencInitRaw( node, val, len );
728 return node;
731 tr_benc*
732 tr_bencListAddList( tr_benc * list,
733 size_t reserveCount )
735 tr_benc * child = tr_bencListAdd( list );
737 tr_bencInitList( child, reserveCount );
738 return child;
741 tr_benc*
742 tr_bencListAddDict( tr_benc * list,
743 size_t reserveCount )
745 tr_benc * child = tr_bencListAdd( list );
747 tr_bencInitDict( child, reserveCount );
748 return child;
751 tr_benc *
752 tr_bencDictAdd( tr_benc * dict,
753 const char * key )
755 tr_benc * keyval, * itemval;
757 assert( tr_bencIsDict( dict ) );
758 if( dict->val.l.count + 2 > dict->val.l.alloc )
759 makeroom( dict, 2 );
760 assert( dict->val.l.count + 2 <= dict->val.l.alloc );
762 keyval = dict->val.l.vals + dict->val.l.count++;
763 tr_bencInitStr( keyval, key, -1 );
765 itemval = dict->val.l.vals + dict->val.l.count++;
766 tr_bencInit( itemval, TR_TYPE_INT );
768 return itemval;
771 static tr_benc*
772 dictFindOrAdd( tr_benc * dict, const char * key, int type )
774 tr_benc * child;
776 /* see if it already exists, and if so, try to reuse it */
777 if(( child = tr_bencDictFind( dict, key ))) {
778 if( !tr_bencIsType( child, type ) ) {
779 tr_bencDictRemove( dict, key );
780 child = NULL;
784 /* if it doesn't exist, create it */
785 if( child == NULL )
786 child = tr_bencDictAdd( dict, key );
788 return child;
791 tr_benc*
792 tr_bencDictAddInt( tr_benc * dict,
793 const char * key,
794 int64_t val )
796 tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_INT );
797 tr_bencInitInt( child, val );
798 return child;
801 tr_benc*
802 tr_bencDictAddBool( tr_benc * dict, const char * key, tr_bool val )
804 tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_BOOL );
805 tr_bencInitBool( child, val );
806 return child;
809 tr_benc*
810 tr_bencDictAddReal( tr_benc * dict, const char * key, double val )
812 tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_REAL );
813 tr_bencInitReal( child, val );
814 return child;
817 tr_benc*
818 tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val )
820 tr_benc * child;
822 /* see if it already exists, and if so, try to reuse it */
823 if(( child = tr_bencDictFind( dict, key ))) {
824 if( tr_bencIsString( child ) ) {
825 if( stringIsAlloced( child ) )
826 tr_free( child->val.s.str.ptr );
827 } else {
828 tr_bencDictRemove( dict, key );
829 child = NULL;
833 /* if it doesn't exist, create it */
834 if( child == NULL )
835 child = tr_bencDictAdd( dict, key );
837 /* set it */
838 tr_bencInitStr( child, val, -1 );
840 return child;
843 tr_benc*
844 tr_bencDictAddRaw( tr_benc * dict,
845 const char * key,
846 const void * src,
847 size_t len )
849 tr_benc * child;
851 /* see if it already exists, and if so, try to reuse it */
852 if(( child = tr_bencDictFind( dict, key ))) {
853 if( tr_bencIsString( child ) ) {
854 if( stringIsAlloced( child ) )
855 tr_free( child->val.s.str.ptr );
856 } else {
857 tr_bencDictRemove( dict, key );
858 child = NULL;
862 /* if it doesn't exist, create it */
863 if( child == NULL )
864 child = tr_bencDictAdd( dict, key );
866 /* set it */
867 tr_bencInitRaw( child, src, len );
869 return child;
872 tr_benc*
873 tr_bencDictAddList( tr_benc * dict,
874 const char * key,
875 size_t reserveCount )
877 tr_benc * child = tr_bencDictAdd( dict, key );
879 tr_bencInitList( child, reserveCount );
880 return child;
883 tr_benc*
884 tr_bencDictAddDict( tr_benc * dict,
885 const char * key,
886 size_t reserveCount )
888 tr_benc * child = tr_bencDictAdd( dict, key );
890 tr_bencInitDict( child, reserveCount );
891 return child;
895 tr_bencDictRemove( tr_benc * dict,
896 const char * key )
898 int i = dictIndexOf( dict, key );
900 if( i >= 0 )
902 const int n = dict->val.l.count;
903 tr_bencFree( &dict->val.l.vals[i] );
904 tr_bencFree( &dict->val.l.vals[i + 1] );
905 if( i + 2 < n )
907 dict->val.l.vals[i] = dict->val.l.vals[n - 2];
908 dict->val.l.vals[i + 1] = dict->val.l.vals[n - 1];
910 dict->val.l.count -= 2;
912 return i >= 0; /* return true if found */
915 /***
916 **** BENC WALKING
917 ***/
919 struct KeyIndex
921 const char * key;
922 int index;
925 static int
926 compareKeyIndex( const void * va,
927 const void * vb )
929 const struct KeyIndex * a = va;
930 const struct KeyIndex * b = vb;
932 return strcmp( a->key, b->key );
935 struct SaveNode
937 const tr_benc * val;
938 int valIsVisited;
939 int childCount;
940 int childIndex;
941 int * children;
944 static void
945 nodeInitDict( struct SaveNode * node, const tr_benc * val )
947 int i, j;
948 int nKeys;
949 struct KeyIndex * indices;
951 assert( tr_bencIsDict( val ) );
953 nKeys = val->val.l.count / 2;
954 node->val = val;
955 node->children = tr_new0( int, nKeys * 2 );
957 /* ugh, a dictionary's children have to be sorted by key... */
958 indices = tr_new( struct KeyIndex, nKeys );
959 for( i = j = 0; i < ( nKeys * 2 ); i += 2, ++j )
961 indices[j].key = getStr(&val->val.l.vals[i]);
962 indices[j].index = i;
964 qsort( indices, j, sizeof( struct KeyIndex ), compareKeyIndex );
965 for( i = 0; i < j; ++i )
967 const int index = indices[i].index;
968 node->children[node->childCount++] = index;
969 node->children[node->childCount++] = index + 1;
972 assert( node->childCount == nKeys * 2 );
973 tr_free( indices );
976 static void
977 nodeInitList( struct SaveNode * node, const tr_benc * val )
979 int i, n;
981 assert( tr_bencIsList( val ) );
983 n = val->val.l.count;
984 node->val = val;
985 node->childCount = n;
986 node->children = tr_new0( int, n );
987 for( i = 0; i < n; ++i ) /* a list's children don't need to be reordered */
988 node->children[i] = i;
991 static void
992 nodeInitLeaf( struct SaveNode * node, const tr_benc * val )
994 assert( !isContainer( val ) );
996 node->val = val;
999 static void
1000 nodeInit( struct SaveNode * node, const tr_benc * val )
1002 static const struct SaveNode INIT_NODE = { NULL, 0, 0, 0, NULL };
1003 *node = INIT_NODE;
1005 if( tr_bencIsList( val ) ) nodeInitList( node, val );
1006 else if( tr_bencIsDict( val ) ) nodeInitDict( node, val );
1007 else nodeInitLeaf( node, val );
1010 typedef void ( *BencWalkFunc )( const tr_benc * val, void * user_data );
1012 struct WalkFuncs
1014 BencWalkFunc intFunc;
1015 BencWalkFunc boolFunc;
1016 BencWalkFunc realFunc;
1017 BencWalkFunc stringFunc;
1018 BencWalkFunc dictBeginFunc;
1019 BencWalkFunc listBeginFunc;
1020 BencWalkFunc containerEndFunc;
1024 * This function's previous recursive implementation was
1025 * easier to read, but was vulnerable to a smash-stacking
1026 * attack via maliciously-crafted bencoded data. (#667)
1028 static void
1029 bencWalk( const tr_benc * top,
1030 const struct WalkFuncs * walkFuncs,
1031 void * user_data )
1033 int stackSize = 0;
1034 int stackAlloc = 64;
1035 struct SaveNode * stack = tr_new( struct SaveNode, stackAlloc );
1037 nodeInit( &stack[stackSize++], top );
1039 while( stackSize > 0 )
1041 struct SaveNode * node = &stack[stackSize-1];
1042 const tr_benc * val;
1044 if( !node->valIsVisited )
1046 val = node->val;
1047 node->valIsVisited = TRUE;
1049 else if( node->childIndex < node->childCount )
1051 const int index = node->children[node->childIndex++];
1052 val = node->val->val.l.vals + index;
1054 else /* done with this node */
1056 if( isContainer( node->val ) )
1057 walkFuncs->containerEndFunc( node->val, user_data );
1058 --stackSize;
1059 tr_free( node->children );
1060 continue;
1063 if( val ) switch( val->type )
1065 case TR_TYPE_INT:
1066 walkFuncs->intFunc( val, user_data );
1067 break;
1069 case TR_TYPE_BOOL:
1070 walkFuncs->boolFunc( val, user_data );
1071 break;
1073 case TR_TYPE_REAL:
1074 walkFuncs->realFunc( val, user_data );
1075 break;
1077 case TR_TYPE_STR:
1078 walkFuncs->stringFunc( val, user_data );
1079 break;
1081 case TR_TYPE_LIST:
1082 if( val == node->val )
1083 walkFuncs->listBeginFunc( val, user_data );
1084 else {
1085 if( stackAlloc == stackSize ) {
1086 stackAlloc *= 2;
1087 stack = tr_renew( struct SaveNode, stack, stackAlloc );
1089 nodeInit( &stack[stackSize++], val );
1091 break;
1093 case TR_TYPE_DICT:
1094 if( val == node->val )
1095 walkFuncs->dictBeginFunc( val, user_data );
1096 else {
1097 if( stackAlloc == stackSize ) {
1098 stackAlloc *= 2;
1099 stack = tr_renew( struct SaveNode, stack, stackAlloc );
1101 nodeInit( &stack[stackSize++], val );
1103 break;
1105 default:
1106 /* did caller give us an uninitialized val? */
1107 tr_err( "%s", _( "Invalid metadata" ) );
1108 break;
1112 tr_free( stack );
1115 /****
1116 *****
1117 ****/
1119 static void
1120 saveIntFunc( const tr_benc * val, void * evbuf )
1122 evbuffer_add_printf( evbuf, "i%" PRId64 "e", val->val.i );
1125 static void
1126 saveBoolFunc( const tr_benc * val, void * evbuf )
1128 if( val->val.b )
1129 evbuffer_add( evbuf, "i1e", 3 );
1130 else
1131 evbuffer_add( evbuf, "i0e", 3 );
1134 static void
1135 saveRealFunc( const tr_benc * val, void * evbuf )
1137 char buf[128];
1138 char locale[128];
1139 size_t len;
1141 /* always use a '.' decimal point s.t. locale-hopping doesn't bite us */
1142 tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
1143 setlocale( LC_NUMERIC, "POSIX" );
1144 tr_snprintf( buf, sizeof( buf ), "%f", val->val.d );
1145 setlocale( LC_NUMERIC, locale );
1147 len = strlen( buf );
1148 evbuffer_add_printf( evbuf, "%lu:", (unsigned long)len );
1149 evbuffer_add( evbuf, buf, len );
1152 static void
1153 saveStringFunc( const tr_benc * val, void * evbuf )
1155 evbuffer_add_printf( evbuf, "%lu:", (unsigned long)val->val.s.len );
1156 evbuffer_add( evbuf, getStr(val), val->val.s.len );
1159 static void
1160 saveDictBeginFunc( const tr_benc * val UNUSED, void * evbuf )
1162 evbuffer_add( evbuf, "d", 1 );
1165 static void
1166 saveListBeginFunc( const tr_benc * val UNUSED, void * evbuf )
1168 evbuffer_add( evbuf, "l", 1 );
1171 static void
1172 saveContainerEndFunc( const tr_benc * val UNUSED, void * evbuf )
1174 evbuffer_add( evbuf, "e", 1 );
1177 static const struct WalkFuncs saveFuncs = { saveIntFunc,
1178 saveBoolFunc,
1179 saveRealFunc,
1180 saveStringFunc,
1181 saveDictBeginFunc,
1182 saveListBeginFunc,
1183 saveContainerEndFunc };
1185 /***
1186 ****
1187 ***/
1189 static void
1190 freeDummyFunc( const tr_benc * val UNUSED, void * buf UNUSED )
1193 static void
1194 freeStringFunc( const tr_benc * val, void * unused UNUSED )
1196 if( stringIsAlloced( val ) )
1197 tr_free( val->val.s.str.ptr );
1200 static void
1201 freeContainerEndFunc( const tr_benc * val, void * unused UNUSED )
1203 tr_free( val->val.l.vals );
1206 static const struct WalkFuncs freeWalkFuncs = { freeDummyFunc,
1207 freeDummyFunc,
1208 freeDummyFunc,
1209 freeStringFunc,
1210 freeDummyFunc,
1211 freeDummyFunc,
1212 freeContainerEndFunc };
1214 void
1215 tr_bencFree( tr_benc * val )
1217 if( isSomething( val ) )
1218 bencWalk( val, &freeWalkFuncs, NULL );
1221 /***
1222 ****
1223 ***/
1225 /** @brief Implementation helper class for tr_bencToBuffer(TR_FMT_JSON) */
1226 struct ParentState
1228 int bencType;
1229 int childIndex;
1230 int childCount;
1233 /** @brief Implementation helper class for tr_bencToBuffer(TR_FMT_JSON) */
1234 struct jsonWalk
1236 tr_bool doIndent;
1237 tr_list * parents;
1238 struct evbuffer * out;
1241 static void
1242 jsonIndent( struct jsonWalk * data )
1244 if( data->doIndent )
1246 char buf[1024];
1247 const int width = tr_list_size( data->parents ) * 4;
1249 buf[0] = '\n';
1250 memset( buf+1, ' ', width );
1251 evbuffer_add( data->out, buf, 1+width );
1255 static void
1256 jsonChildFunc( struct jsonWalk * data )
1258 if( data->parents )
1260 struct ParentState * parentState = data->parents->data;
1262 switch( parentState->bencType )
1264 case TR_TYPE_DICT:
1266 const int i = parentState->childIndex++;
1267 if( !( i % 2 ) )
1268 evbuffer_add( data->out, ": ", data->doIndent ? 2 : 1 );
1269 else {
1270 const tr_bool isLast = parentState->childIndex == parentState->childCount;
1271 if( !isLast ) {
1272 evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
1273 jsonIndent( data );
1276 break;
1279 case TR_TYPE_LIST:
1281 const tr_bool isLast = ++parentState->childIndex == parentState->childCount;
1282 if( !isLast ) {
1283 evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 );
1284 jsonIndent( data );
1286 break;
1289 default:
1290 break;
1295 static void
1296 jsonPushParent( struct jsonWalk * data,
1297 const tr_benc * benc )
1299 struct ParentState * parentState = tr_new( struct ParentState, 1 );
1301 parentState->bencType = benc->type;
1302 parentState->childIndex = 0;
1303 parentState->childCount = benc->val.l.count;
1304 tr_list_prepend( &data->parents, parentState );
1307 static void
1308 jsonPopParent( struct jsonWalk * data )
1310 tr_free( tr_list_pop_front( &data->parents ) );
1313 static void
1314 jsonIntFunc( const tr_benc * val,
1315 void * vdata )
1317 struct jsonWalk * data = vdata;
1319 evbuffer_add_printf( data->out, "%" PRId64, val->val.i );
1320 jsonChildFunc( data );
1323 static void
1324 jsonBoolFunc( const tr_benc * val, void * vdata )
1326 struct jsonWalk * data = vdata;
1328 if( val->val.b )
1329 evbuffer_add( data->out, "true", 4 );
1330 else
1331 evbuffer_add( data->out, "false", 5 );
1333 jsonChildFunc( data );
1336 static void
1337 jsonRealFunc( const tr_benc * val, void * vdata )
1339 struct jsonWalk * data = vdata;
1340 char locale[128];
1342 if( fabs( val->val.d - (int)val->val.d ) < 0.00001 )
1343 evbuffer_add_printf( data->out, "%d", (int)val->val.d );
1344 else {
1345 /* json requires a '.' decimal point regardless of locale */
1346 tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) );
1347 setlocale( LC_NUMERIC, "POSIX" );
1348 evbuffer_add_printf( data->out, "%.4f", tr_truncd( val->val.d, 4 ) );
1349 setlocale( LC_NUMERIC, locale );
1352 jsonChildFunc( data );
1355 static void
1356 jsonStringFunc( const tr_benc * val, void * vdata )
1358 struct jsonWalk * data = vdata;
1359 const unsigned char * it = (const unsigned char *) getStr(val);
1360 const unsigned char * end = it + val->val.s.len;
1362 evbuffer_expand( data->out, val->val.s.len + 2 );
1363 evbuffer_add( data->out, "\"", 1 );
1365 for( ; it!=end; ++it )
1367 switch( *it )
1369 case '\b': evbuffer_add( data->out, "\\b", 2 ); break;
1370 case '\f': evbuffer_add( data->out, "\\f", 2 ); break;
1371 case '\n': evbuffer_add( data->out, "\\n", 2 ); break;
1372 case '\r': evbuffer_add( data->out, "\\r", 2 ); break;
1373 case '\t': evbuffer_add( data->out, "\\t", 2 ); break;
1374 case '"': evbuffer_add( data->out, "\\\"", 2 ); break;
1375 case '\\': evbuffer_add( data->out, "\\\\", 2 ); break;
1377 default:
1378 if( isascii( *it ) )
1379 evbuffer_add( data->out, it, 1 );
1380 else {
1381 const UTF8 * tmp = it;
1382 UTF32 buf = 0;
1383 UTF32 * u32 = &buf;
1384 ConversionResult result = ConvertUTF8toUTF32( &tmp, end, &u32, &buf + 1, 0 );
1385 if((( result==conversionOK ) || (result==targetExhausted)) && (tmp!=it)) {
1386 evbuffer_add_printf( data->out, "\\u%04x", (unsigned int)buf );
1387 it = tmp - 1;
1392 evbuffer_add( data->out, "\"", 1 );
1393 jsonChildFunc( data );
1396 static void
1397 jsonDictBeginFunc( const tr_benc * val,
1398 void * vdata )
1400 struct jsonWalk * data = vdata;
1402 jsonPushParent( data, val );
1403 evbuffer_add( data->out, "{", 1 );
1404 if( val->val.l.count )
1405 jsonIndent( data );
1408 static void
1409 jsonListBeginFunc( const tr_benc * val,
1410 void * vdata )
1412 const size_t nChildren = tr_bencListSize( val );
1413 struct jsonWalk * data = vdata;
1415 jsonPushParent( data, val );
1416 evbuffer_add( data->out, "[", 1 );
1417 if( nChildren )
1418 jsonIndent( data );
1421 static void
1422 jsonContainerEndFunc( const tr_benc * val,
1423 void * vdata )
1425 struct jsonWalk * data = vdata;
1426 int emptyContainer = FALSE;
1428 jsonPopParent( data );
1429 if( !emptyContainer )
1430 jsonIndent( data );
1431 if( tr_bencIsDict( val ) )
1432 evbuffer_add( data->out, "}", 1 );
1433 else /* list */
1434 evbuffer_add( data->out, "]", 1 );
1435 jsonChildFunc( data );
1438 static const struct WalkFuncs jsonWalkFuncs = { jsonIntFunc,
1439 jsonBoolFunc,
1440 jsonRealFunc,
1441 jsonStringFunc,
1442 jsonDictBeginFunc,
1443 jsonListBeginFunc,
1444 jsonContainerEndFunc };
1446 /***
1447 ****
1448 ***/
1450 static void
1451 tr_bencListCopy( tr_benc * target, const tr_benc * src )
1453 int i = 0;
1454 const tr_benc * val;
1456 while(( val = tr_bencListChild( (tr_benc*)src, i++ )))
1458 if( tr_bencIsBool( val ) )
1460 tr_bool boolVal = 0;
1461 tr_bencGetBool( val, &boolVal );
1462 tr_bencListAddBool( target, boolVal );
1464 else if( tr_bencIsReal( val ) )
1466 double realVal = 0;
1467 tr_bencGetReal( val, &realVal );
1468 tr_bencListAddReal( target, realVal );
1470 else if( tr_bencIsInt( val ) )
1472 int64_t intVal = 0;
1473 tr_bencGetInt( val, &intVal );
1474 tr_bencListAddInt( target, intVal );
1476 else if( tr_bencIsString( val ) )
1478 tr_bencListAddRaw( target, (const uint8_t*)getStr( val ), val->val.s.len );
1480 else if( tr_bencIsDict( val ) )
1482 tr_bencMergeDicts( tr_bencListAddDict( target, 0 ), val );
1484 else if ( tr_bencIsList( val ) )
1486 tr_bencListCopy( tr_bencListAddList( target, 0 ), val );
1488 else
1490 tr_err( "tr_bencListCopy skipping item" );
1495 static size_t
1496 tr_bencDictSize( const tr_benc * dict )
1498 size_t count = 0;
1500 if( tr_bencIsDict( dict ) )
1501 count = dict->val.l.count / 2;
1503 return count;
1506 tr_bool
1507 tr_bencDictChild( tr_benc * dict, size_t n, const char ** key, tr_benc ** val )
1509 tr_bool success = 0;
1511 assert( tr_bencIsDict( dict ) );
1513 if( tr_bencIsDict( dict ) && (n*2)+1 <= dict->val.l.count )
1515 tr_benc * k = dict->val.l.vals + (n*2);
1516 tr_benc * v = dict->val.l.vals + (n*2) + 1;
1517 if(( success = tr_bencGetStr( k, key ) && isSomething( v )))
1518 *val = v;
1521 return success;
1524 void
1525 tr_bencMergeDicts( tr_benc * target, const tr_benc * source )
1527 size_t i;
1528 const size_t sourceCount = tr_bencDictSize( source );
1530 assert( tr_bencIsDict( target ) );
1531 assert( tr_bencIsDict( source ) );
1533 for( i=0; i<sourceCount; ++i )
1535 const char * key;
1536 tr_benc * val;
1537 tr_benc * t;
1539 if( tr_bencDictChild( (tr_benc*)source, i, &key, &val ) )
1541 if( tr_bencIsBool( val ) )
1543 tr_bool boolVal;
1544 tr_bencGetBool( val, &boolVal );
1545 tr_bencDictAddBool( target, key, boolVal );
1547 else if( tr_bencIsReal( val ) )
1549 double realVal = 0;
1550 tr_bencGetReal( val, &realVal );
1551 tr_bencDictAddReal( target, key, realVal );
1553 else if( tr_bencIsInt( val ) )
1555 int64_t intVal = 0;
1556 tr_bencGetInt( val, &intVal );
1557 tr_bencDictAddInt( target, key, intVal );
1559 else if( tr_bencIsString( val ) )
1561 tr_bencDictAddRaw( target, key, getStr( val ), val->val.s.len );
1563 else if( tr_bencIsDict( val ) && tr_bencDictFindDict( target, key, &t ) )
1565 tr_bencMergeDicts( t, val );
1567 else if( tr_bencIsList( val ) )
1569 if( tr_bencDictFind( target, key ) == NULL )
1571 tr_bencListCopy( tr_bencDictAddList( target, key, tr_bencListSize( val ) ), val );
1574 else
1576 tr_dbg( "tr_bencMergeDicts skipping \"%s\"", key );
1582 /***
1583 ****
1584 ***/
1586 void
1587 tr_bencToBuf( const tr_benc * top, tr_fmt_mode mode, struct evbuffer * buf )
1589 evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
1590 evbuffer_expand( buf, 4096 ); /* alloc a little memory to start off with */
1592 switch( mode )
1594 case TR_FMT_BENC:
1595 bencWalk( top, &saveFuncs, buf );
1596 break;
1598 case TR_FMT_JSON:
1599 case TR_FMT_JSON_LEAN: {
1600 struct jsonWalk data;
1601 data.doIndent = mode==TR_FMT_JSON;
1602 data.out = buf;
1603 data.parents = NULL;
1604 bencWalk( top, &jsonWalkFuncs, &data );
1605 if( EVBUFFER_LENGTH( buf ) )
1606 evbuffer_add_printf( buf, "\n" );
1607 break;
1612 char*
1613 tr_bencToStr( const tr_benc * top, tr_fmt_mode mode, int * len )
1615 char * ret;
1616 struct evbuffer * buf = evbuffer_new( );
1617 tr_bencToBuf( top, mode, buf );
1618 ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
1619 if( len != NULL )
1620 *len = (int) EVBUFFER_LENGTH( buf );
1621 evbuffer_free( buf );
1622 return ret;
1625 /* portability wrapper for mkstemp(). */
1626 static int
1627 tr_mkstemp( char * template )
1629 #ifdef WIN32
1630 const int flags = O_RDWR | O_BINARY | O_CREAT | O_EXCL | _O_SHORT_LIVED;
1631 const mode_t mode = _S_IREAD | _S_IWRITE;
1632 mktemp( template );
1633 return open( template, flags, mode );
1634 #else
1635 return mkstemp( template );
1636 #endif
1639 /* portability wrapper for fsync(). */
1640 static void
1641 tr_fsync( int fd )
1643 #ifdef WIN32
1644 _commit( fd );
1645 #else
1646 fsync( fd );
1647 #endif
1651 tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename )
1653 char * tmp;
1654 int fd;
1655 int err = 0;
1656 char buf[TR_PATH_MAX];
1658 /* follow symlinks to find the "real" file, to make sure the temporary
1659 * we build with tr_mkstemp() is created on the right partition */
1660 if( tr_realpath( filename, buf ) != NULL )
1661 filename = buf;
1663 /* if the file already exists, try to move it out of the way & keep it as a backup */
1664 tmp = tr_strdup_printf( "%s.tmp.XXXXXX", filename );
1665 fd = tr_mkstemp( tmp );
1666 if( fd >= 0 )
1668 int len;
1669 char * str = tr_bencToStr( top, mode, &len );
1671 if( write( fd, str, len ) == (ssize_t)len )
1673 struct stat sb;
1674 const tr_bool already_exists = !stat( filename, &sb ) && S_ISREG( sb.st_mode );
1676 tr_fsync( fd );
1677 tr_close_file( fd );
1679 if( !already_exists || !unlink( filename ) )
1681 if( !rename( tmp, filename ) )
1683 tr_inf( _( "Saved \"%s\"" ), filename );
1685 else
1687 err = errno;
1688 tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
1689 unlink( tmp );
1692 else
1694 err = errno;
1695 tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) );
1696 unlink( tmp );
1699 else
1701 err = errno;
1702 tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
1703 tr_close_file( fd );
1704 unlink( tmp );
1707 tr_free( str );
1709 else
1711 err = errno;
1712 tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) );
1715 tr_free( tmp );
1716 return err;
1719 /***
1720 ****
1721 ***/
1724 tr_bencLoadFile( tr_benc * setme, tr_fmt_mode mode, const char * filename )
1726 int err;
1727 size_t contentLen;
1728 uint8_t * content;
1730 content = tr_loadFile( filename, &contentLen );
1731 if( !content && errno )
1732 err = errno;
1733 else if( !content )
1734 err = ENODATA;
1735 else {
1736 if( mode == TR_FMT_BENC )
1737 err = tr_bencLoad( content, contentLen, setme, NULL );
1738 else
1739 err = tr_jsonParse( filename, content, contentLen, setme, NULL );
1742 tr_free( content );
1743 return err;