1 | /* |
---|
2 | * This file Copyright (C) 2008-2010 Mnemosyne LLC |
---|
3 | * |
---|
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. |
---|
9 | * |
---|
10 | * $Id: bencode.c 10633 2010-05-06 15:07:18Z charles $ |
---|
11 | */ |
---|
12 | |
---|
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> |
---|
20 | |
---|
21 | #include <sys/types.h> /* stat() */ |
---|
22 | #include <sys/stat.h> /* stat() */ |
---|
23 | #include <locale.h> |
---|
24 | #include <unistd.h> /* stat(), close() */ |
---|
25 | |
---|
26 | #include <event.h> /* struct evbuffer */ |
---|
27 | |
---|
28 | #include "ConvertUTF.h" |
---|
29 | |
---|
30 | #include "transmission.h" |
---|
31 | #include "bencode.h" |
---|
32 | #include "json.h" |
---|
33 | #include "list.h" |
---|
34 | #include "platform.h" /* MAX_PATH_LEN */ |
---|
35 | #include "ptrarray.h" |
---|
36 | #include "utils.h" /* tr_new(), tr_free() */ |
---|
37 | |
---|
38 | #ifndef ENODATA |
---|
39 | #define ENODATA EIO |
---|
40 | #endif |
---|
41 | |
---|
42 | /** |
---|
43 | *** |
---|
44 | **/ |
---|
45 | |
---|
46 | static tr_bool |
---|
47 | isContainer( const tr_benc * val ) |
---|
48 | { |
---|
49 | return tr_bencIsList( val ) || tr_bencIsDict( val ); |
---|
50 | } |
---|
51 | |
---|
52 | static tr_bool |
---|
53 | isSomething( const tr_benc * val ) |
---|
54 | { |
---|
55 | return isContainer( val ) || tr_bencIsInt( val ) |
---|
56 | || tr_bencIsString( val ) |
---|
57 | || tr_bencIsReal( val ) |
---|
58 | || tr_bencIsBool( val ); |
---|
59 | } |
---|
60 | |
---|
61 | static void |
---|
62 | tr_bencInit( tr_benc * val, |
---|
63 | int type ) |
---|
64 | { |
---|
65 | memset( val, 0, sizeof( *val ) ); |
---|
66 | val->type = type; |
---|
67 | } |
---|
68 | |
---|
69 | /*** |
---|
70 | **** tr_bencParse() |
---|
71 | **** tr_bencLoad() |
---|
72 | ***/ |
---|
73 | |
---|
74 | /** |
---|
75 | * The initial i and trailing e are beginning and ending delimiters. |
---|
76 | * You can have negative numbers such as i-3e. You cannot prefix the |
---|
77 | * number with a zero such as i04e. However, i0e is valid. |
---|
78 | * Example: i3e represents the integer "3" |
---|
79 | * NOTE: The maximum number of bit of this integer is unspecified, |
---|
80 | * but to handle it as a signed 64bit integer is mandatory to handle |
---|
81 | * "large files" aka .torrent for more that 4Gbyte |
---|
82 | */ |
---|
83 | int |
---|
84 | tr_bencParseInt( const uint8_t * buf, |
---|
85 | const uint8_t * bufend, |
---|
86 | const uint8_t ** setme_end, |
---|
87 | int64_t * setme_val ) |
---|
88 | { |
---|
89 | char * endptr; |
---|
90 | const void * begin; |
---|
91 | const void * end; |
---|
92 | int64_t val; |
---|
93 | |
---|
94 | if( buf >= bufend ) |
---|
95 | return EILSEQ; |
---|
96 | if( *buf != 'i' ) |
---|
97 | return EILSEQ; |
---|
98 | |
---|
99 | begin = buf + 1; |
---|
100 | end = memchr( begin, 'e', ( bufend - buf ) - 1 ); |
---|
101 | if( end == NULL ) |
---|
102 | return EILSEQ; |
---|
103 | |
---|
104 | errno = 0; |
---|
105 | val = evutil_strtoll( begin, &endptr, 10 ); |
---|
106 | if( errno || ( endptr != end ) ) /* incomplete parse */ |
---|
107 | return EILSEQ; |
---|
108 | if( val && *(const char*)begin == '0' ) /* no leading zeroes! */ |
---|
109 | return EILSEQ; |
---|
110 | |
---|
111 | *setme_end = (const uint8_t*)end + 1; |
---|
112 | *setme_val = val; |
---|
113 | return 0; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Byte strings are encoded as follows: |
---|
118 | * <string length encoded in base ten ASCII>:<string data> |
---|
119 | * Note that there is no constant beginning delimiter, and no ending delimiter. |
---|
120 | * Example: 4:spam represents the string "spam" |
---|
121 | */ |
---|
122 | int |
---|
123 | tr_bencParseStr( const uint8_t * buf, |
---|
124 | const uint8_t * bufend, |
---|
125 | const uint8_t ** setme_end, |
---|
126 | const uint8_t ** setme_str, |
---|
127 | size_t * setme_strlen ) |
---|
128 | { |
---|
129 | size_t len; |
---|
130 | const void * end; |
---|
131 | char * endptr; |
---|
132 | |
---|
133 | if( buf >= bufend ) |
---|
134 | return EILSEQ; |
---|
135 | |
---|
136 | if( !isdigit( *buf ) ) |
---|
137 | return EILSEQ; |
---|
138 | |
---|
139 | end = memchr( buf, ':', bufend - buf ); |
---|
140 | if( end == NULL ) |
---|
141 | return EILSEQ; |
---|
142 | |
---|
143 | errno = 0; |
---|
144 | len = strtoul( (const char*)buf, &endptr, 10 ); |
---|
145 | if( errno || endptr != end ) |
---|
146 | return EILSEQ; |
---|
147 | |
---|
148 | if( (const uint8_t*)end + 1 + len > bufend ) |
---|
149 | return EILSEQ; |
---|
150 | |
---|
151 | *setme_end = (const uint8_t*)end + 1 + len; |
---|
152 | *setme_str = (const uint8_t*)end + 1; |
---|
153 | *setme_strlen = len; |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | /* set to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */ |
---|
158 | #define LIST_SIZE 4 /* number of items to increment list/dict buffer by */ |
---|
159 | |
---|
160 | static int |
---|
161 | makeroom( tr_benc * val, |
---|
162 | size_t count ) |
---|
163 | { |
---|
164 | assert( TR_TYPE_LIST == val->type || TR_TYPE_DICT == val->type ); |
---|
165 | |
---|
166 | if( val->val.l.count + count > val->val.l.alloc ) |
---|
167 | { |
---|
168 | /* We need a bigger boat */ |
---|
169 | const int len = val->val.l.alloc + count + |
---|
170 | ( count % LIST_SIZE ? LIST_SIZE - |
---|
171 | ( count % LIST_SIZE ) : 0 ); |
---|
172 | void * tmp = realloc( val->val.l.vals, len * sizeof( tr_benc ) ); |
---|
173 | if( !tmp ) |
---|
174 | return 1; |
---|
175 | |
---|
176 | val->val.l.alloc = len; |
---|
177 | val->val.l.vals = tmp; |
---|
178 | } |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|
183 | static tr_benc* |
---|
184 | getNode( tr_benc * top, |
---|
185 | tr_ptrArray * parentStack, |
---|
186 | int type ) |
---|
187 | { |
---|
188 | tr_benc * parent; |
---|
189 | |
---|
190 | assert( top ); |
---|
191 | assert( parentStack ); |
---|
192 | |
---|
193 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
194 | return top; |
---|
195 | |
---|
196 | parent = tr_ptrArrayBack( parentStack ); |
---|
197 | assert( parent ); |
---|
198 | |
---|
199 | /* dictionary keys must be strings */ |
---|
200 | if( ( parent->type == TR_TYPE_DICT ) |
---|
201 | && ( type != TR_TYPE_STR ) |
---|
202 | && ( !( parent->val.l.count % 2 ) ) ) |
---|
203 | return NULL; |
---|
204 | |
---|
205 | makeroom( parent, 1 ); |
---|
206 | return parent->val.l.vals + parent->val.l.count++; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * This function's previous recursive implementation was |
---|
211 | * easier to read, but was vulnerable to a smash-stacking |
---|
212 | * attack via maliciously-crafted bencoded data. (#667) |
---|
213 | */ |
---|
214 | static int |
---|
215 | tr_bencParseImpl( const void * buf_in, |
---|
216 | const void * bufend_in, |
---|
217 | tr_benc * top, |
---|
218 | tr_ptrArray * parentStack, |
---|
219 | const uint8_t ** setme_end ) |
---|
220 | { |
---|
221 | int err; |
---|
222 | const uint8_t * buf = buf_in; |
---|
223 | const uint8_t * bufend = bufend_in; |
---|
224 | |
---|
225 | tr_bencInit( top, 0 ); |
---|
226 | |
---|
227 | while( buf != bufend ) |
---|
228 | { |
---|
229 | if( buf > bufend ) /* no more text to parse... */ |
---|
230 | return 1; |
---|
231 | |
---|
232 | if( *buf == 'i' ) /* int */ |
---|
233 | { |
---|
234 | int64_t val; |
---|
235 | const uint8_t * end; |
---|
236 | tr_benc * node; |
---|
237 | |
---|
238 | if( ( err = tr_bencParseInt( buf, bufend, &end, &val ) ) ) |
---|
239 | return err; |
---|
240 | |
---|
241 | node = getNode( top, parentStack, TR_TYPE_INT ); |
---|
242 | if( !node ) |
---|
243 | return EILSEQ; |
---|
244 | |
---|
245 | tr_bencInitInt( node, val ); |
---|
246 | buf = end; |
---|
247 | |
---|
248 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
249 | break; |
---|
250 | } |
---|
251 | else if( *buf == 'l' ) /* list */ |
---|
252 | { |
---|
253 | tr_benc * node = getNode( top, parentStack, TR_TYPE_LIST ); |
---|
254 | if( !node ) |
---|
255 | return EILSEQ; |
---|
256 | tr_bencInit( node, TR_TYPE_LIST ); |
---|
257 | tr_ptrArrayAppend( parentStack, node ); |
---|
258 | ++buf; |
---|
259 | } |
---|
260 | else if( *buf == 'd' ) /* dict */ |
---|
261 | { |
---|
262 | tr_benc * node = getNode( top, parentStack, TR_TYPE_DICT ); |
---|
263 | if( !node ) |
---|
264 | return EILSEQ; |
---|
265 | tr_bencInit( node, TR_TYPE_DICT ); |
---|
266 | tr_ptrArrayAppend( parentStack, node ); |
---|
267 | ++buf; |
---|
268 | } |
---|
269 | else if( *buf == 'e' ) /* end of list or dict */ |
---|
270 | { |
---|
271 | tr_benc * node; |
---|
272 | ++buf; |
---|
273 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
274 | return EILSEQ; |
---|
275 | |
---|
276 | node = tr_ptrArrayBack( parentStack ); |
---|
277 | if( tr_bencIsDict( node ) && ( node->val.l.count % 2 ) ) |
---|
278 | { |
---|
279 | /* odd # of children in dict */ |
---|
280 | tr_bencFree( &node->val.l.vals[--node->val.l.count] ); |
---|
281 | return EILSEQ; |
---|
282 | } |
---|
283 | |
---|
284 | tr_ptrArrayPop( parentStack ); |
---|
285 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
286 | break; |
---|
287 | } |
---|
288 | else if( isdigit( *buf ) ) /* string? */ |
---|
289 | { |
---|
290 | const uint8_t * end; |
---|
291 | const uint8_t * str; |
---|
292 | size_t str_len; |
---|
293 | tr_benc * node; |
---|
294 | |
---|
295 | if( ( err = tr_bencParseStr( buf, bufend, &end, &str, &str_len ) ) ) |
---|
296 | return err; |
---|
297 | |
---|
298 | node = getNode( top, parentStack, TR_TYPE_STR ); |
---|
299 | if( !node ) |
---|
300 | return EILSEQ; |
---|
301 | |
---|
302 | tr_bencInitStr( node, str, str_len ); |
---|
303 | buf = end; |
---|
304 | |
---|
305 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
306 | break; |
---|
307 | } |
---|
308 | else /* invalid bencoded text... march past it */ |
---|
309 | { |
---|
310 | ++buf; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | err = !isSomething( top ) || !tr_ptrArrayEmpty( parentStack ); |
---|
315 | |
---|
316 | if( !err && setme_end ) |
---|
317 | *setme_end = buf; |
---|
318 | |
---|
319 | return err; |
---|
320 | } |
---|
321 | |
---|
322 | int |
---|
323 | tr_bencParse( const void * buf, |
---|
324 | const void * end, |
---|
325 | tr_benc * top, |
---|
326 | const uint8_t ** setme_end ) |
---|
327 | { |
---|
328 | int err; |
---|
329 | tr_ptrArray parentStack = TR_PTR_ARRAY_INIT; |
---|
330 | |
---|
331 | top->type = 0; /* set to `uninitialized' */ |
---|
332 | err = tr_bencParseImpl( buf, end, top, &parentStack, setme_end ); |
---|
333 | if( err ) |
---|
334 | tr_bencFree( top ); |
---|
335 | |
---|
336 | tr_ptrArrayDestruct( &parentStack, NULL ); |
---|
337 | return err; |
---|
338 | } |
---|
339 | |
---|
340 | int |
---|
341 | tr_bencLoad( const void * buf_in, |
---|
342 | size_t buflen, |
---|
343 | tr_benc * setme_benc, |
---|
344 | char ** setme_end ) |
---|
345 | { |
---|
346 | const uint8_t * buf = buf_in; |
---|
347 | const uint8_t * end; |
---|
348 | const int ret = tr_bencParse( buf, buf + buflen, setme_benc, &end ); |
---|
349 | |
---|
350 | if( !ret && setme_end ) |
---|
351 | *setme_end = (char*) end; |
---|
352 | return ret; |
---|
353 | } |
---|
354 | |
---|
355 | /*** |
---|
356 | **** |
---|
357 | ***/ |
---|
358 | |
---|
359 | /* returns true if the benc's string was malloced. |
---|
360 | * this occurs when the string is too long for our string buffer */ |
---|
361 | static inline int |
---|
362 | stringIsAlloced( const tr_benc * val ) |
---|
363 | { |
---|
364 | return val->val.s.len >= sizeof( val->val.s.str.buf ); |
---|
365 | } |
---|
366 | |
---|
367 | /* returns a const pointer to the benc's string */ |
---|
368 | static inline const char* |
---|
369 | getStr( const tr_benc* val ) |
---|
370 | { |
---|
371 | return stringIsAlloced(val) ? val->val.s.str.ptr : val->val.s.str.buf; |
---|
372 | } |
---|
373 | |
---|
374 | static int |
---|
375 | dictIndexOf( const tr_benc * val, const char * key ) |
---|
376 | { |
---|
377 | if( tr_bencIsDict( val ) ) |
---|
378 | { |
---|
379 | size_t i; |
---|
380 | const size_t len = strlen( key ); |
---|
381 | |
---|
382 | for( i = 0; ( i + 1 ) < val->val.l.count; i += 2 ) |
---|
383 | { |
---|
384 | const tr_benc * child = val->val.l.vals + i; |
---|
385 | if( ( child->type == TR_TYPE_STR ) |
---|
386 | && ( child->val.s.len == len ) |
---|
387 | && !memcmp( getStr(child), key, len ) ) |
---|
388 | return i; |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | return -1; |
---|
393 | } |
---|
394 | |
---|
395 | tr_benc * |
---|
396 | tr_bencDictFind( tr_benc * val, const char * key ) |
---|
397 | { |
---|
398 | const int i = dictIndexOf( val, key ); |
---|
399 | |
---|
400 | return i < 0 ? NULL : &val->val.l.vals[i + 1]; |
---|
401 | } |
---|
402 | |
---|
403 | static tr_bool |
---|
404 | tr_bencDictFindType( tr_benc * dict, const char * key, int type, tr_benc ** setme ) |
---|
405 | { |
---|
406 | return tr_bencIsType( *setme = tr_bencDictFind( dict, key ), type ); |
---|
407 | } |
---|
408 | |
---|
409 | size_t |
---|
410 | tr_bencListSize( const tr_benc * list ) |
---|
411 | { |
---|
412 | return tr_bencIsList( list ) ? list->val.l.count : 0; |
---|
413 | } |
---|
414 | |
---|
415 | tr_benc* |
---|
416 | tr_bencListChild( tr_benc * val, |
---|
417 | size_t i ) |
---|
418 | { |
---|
419 | tr_benc * ret = NULL; |
---|
420 | |
---|
421 | if( tr_bencIsList( val ) && ( i < val->val.l.count ) ) |
---|
422 | ret = val->val.l.vals + i; |
---|
423 | return ret; |
---|
424 | } |
---|
425 | |
---|
426 | static void |
---|
427 | tr_benc_warning( const char * err ) |
---|
428 | { |
---|
429 | fprintf( stderr, "warning: %s\n", err ); |
---|
430 | } |
---|
431 | |
---|
432 | tr_bool |
---|
433 | tr_bencGetInt( const tr_benc * val, |
---|
434 | int64_t * setme ) |
---|
435 | { |
---|
436 | tr_bool success = FALSE; |
---|
437 | |
---|
438 | if( !success && (( success = tr_bencIsInt( val )))) |
---|
439 | if( setme ) |
---|
440 | *setme = val->val.i; |
---|
441 | |
---|
442 | if( !success && (( success = tr_bencIsBool( val )))) { |
---|
443 | tr_benc_warning( "reading bool as an int" ); |
---|
444 | if( setme ) |
---|
445 | *setme = val->val.b ? 1 : 0; |
---|
446 | } |
---|
447 | |
---|
448 | return success; |
---|
449 | } |
---|
450 | |
---|
451 | tr_bool |
---|
452 | tr_bencGetStr( const tr_benc * val, |
---|
453 | const char ** setme ) |
---|
454 | { |
---|
455 | const int success = tr_bencIsString( val ); |
---|
456 | |
---|
457 | if( success ) |
---|
458 | *setme = getStr( val ); |
---|
459 | |
---|
460 | return success; |
---|
461 | } |
---|
462 | |
---|
463 | tr_bool |
---|
464 | tr_bencGetBool( const tr_benc * val, tr_bool * setme ) |
---|
465 | { |
---|
466 | const char * str; |
---|
467 | tr_bool success = FALSE; |
---|
468 | |
---|
469 | if(( success = tr_bencIsBool( val ))) |
---|
470 | *setme = val->val.b; |
---|
471 | |
---|
472 | if( !success && tr_bencIsInt( val ) ) |
---|
473 | if(( success = ( val->val.i==0 || val->val.i==1 ) )) |
---|
474 | *setme = val->val.i!=0; |
---|
475 | |
---|
476 | if( !success && tr_bencGetStr( val, &str ) ) |
---|
477 | if(( success = ( !strcmp(str,"true") || !strcmp(str,"false")))) |
---|
478 | *setme = !strcmp(str,"true"); |
---|
479 | |
---|
480 | return success; |
---|
481 | } |
---|
482 | |
---|
483 | tr_bool |
---|
484 | tr_bencGetReal( const tr_benc * val, double * setme ) |
---|
485 | { |
---|
486 | tr_bool success = FALSE; |
---|
487 | |
---|
488 | if( !success && (( success = tr_bencIsReal( val )))) |
---|
489 | *setme = val->val.d; |
---|
490 | |
---|
491 | if( !success && (( success = tr_bencIsInt( val )))) |
---|
492 | *setme = val->val.i; |
---|
493 | |
---|
494 | if( !success && tr_bencIsString(val) ) |
---|
495 | { |
---|
496 | char * endptr; |
---|
497 | char locale[128]; |
---|
498 | double d; |
---|
499 | |
---|
500 | /* the json spec requires a '.' decimal point regardless of locale */ |
---|
501 | tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) ); |
---|
502 | setlocale( LC_NUMERIC, "POSIX" ); |
---|
503 | d = strtod( getStr(val), &endptr ); |
---|
504 | setlocale( LC_NUMERIC, locale ); |
---|
505 | |
---|
506 | if(( success = ( getStr(val) != endptr ) && !*endptr )) |
---|
507 | *setme = d; |
---|
508 | } |
---|
509 | |
---|
510 | |
---|
511 | return success; |
---|
512 | } |
---|
513 | |
---|
514 | tr_bool |
---|
515 | tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme ) |
---|
516 | { |
---|
517 | return tr_bencGetInt( tr_bencDictFind( dict, key ), setme ); |
---|
518 | } |
---|
519 | |
---|
520 | tr_bool |
---|
521 | tr_bencDictFindBool( tr_benc * dict, const char * key, tr_bool * setme ) |
---|
522 | { |
---|
523 | return tr_bencGetBool( tr_bencDictFind( dict, key ), setme ); |
---|
524 | } |
---|
525 | |
---|
526 | tr_bool |
---|
527 | tr_bencDictFindReal( tr_benc * dict, const char * key, double * setme ) |
---|
528 | { |
---|
529 | return tr_bencGetReal( tr_bencDictFind( dict, key ), setme ); |
---|
530 | } |
---|
531 | |
---|
532 | tr_bool |
---|
533 | tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme ) |
---|
534 | { |
---|
535 | return tr_bencGetStr( tr_bencDictFind( dict, key ), setme ); |
---|
536 | } |
---|
537 | |
---|
538 | tr_bool |
---|
539 | tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme ) |
---|
540 | { |
---|
541 | return tr_bencDictFindType( dict, key, TR_TYPE_LIST, setme ); |
---|
542 | } |
---|
543 | |
---|
544 | tr_bool |
---|
545 | tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme ) |
---|
546 | { |
---|
547 | return tr_bencDictFindType( dict, key, TR_TYPE_DICT, setme ); |
---|
548 | } |
---|
549 | |
---|
550 | tr_bool |
---|
551 | tr_bencDictFindRaw( tr_benc * dict, |
---|
552 | const char * key, |
---|
553 | const uint8_t ** setme_raw, |
---|
554 | size_t * setme_len ) |
---|
555 | { |
---|
556 | tr_benc * child; |
---|
557 | const tr_bool found = tr_bencDictFindType( dict, key, TR_TYPE_STR, &child ); |
---|
558 | |
---|
559 | if( found ) { |
---|
560 | *setme_raw = (uint8_t*) getStr(child); |
---|
561 | *setme_len = child->val.s.len; |
---|
562 | } |
---|
563 | |
---|
564 | return found; |
---|
565 | } |
---|
566 | |
---|
567 | /*** |
---|
568 | **** |
---|
569 | ***/ |
---|
570 | |
---|
571 | void |
---|
572 | tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount ) |
---|
573 | { |
---|
574 | char * setme; |
---|
575 | tr_bencInit( val, TR_TYPE_STR ); |
---|
576 | |
---|
577 | /* There's no way in benc notation to distinguish between |
---|
578 | * zero-terminated strings and raw byte arrays. |
---|
579 | * Because of this, tr_bencMergeDicts() and tr_bencListCopy() |
---|
580 | * don't know whether or not a TR_TYPE_STR node needs a '\0'. |
---|
581 | * Append one, een to the raw arrays, just to be safe. */ |
---|
582 | |
---|
583 | if( byteCount < sizeof( val->val.s.str.buf ) ) |
---|
584 | setme = val->val.s.str.buf; |
---|
585 | else |
---|
586 | setme = val->val.s.str.ptr = tr_new( char, byteCount + 1 ); |
---|
587 | |
---|
588 | memcpy( setme, src, byteCount ); |
---|
589 | setme[byteCount] = '\0'; |
---|
590 | val->val.s.len = byteCount; |
---|
591 | } |
---|
592 | |
---|
593 | void |
---|
594 | tr_bencInitStr( tr_benc * val, const void * str, int len ) |
---|
595 | { |
---|
596 | if( str == NULL ) |
---|
597 | len = 0; |
---|
598 | else if( len < 0 ) |
---|
599 | len = strlen( str ); |
---|
600 | |
---|
601 | tr_bencInitRaw( val, str, len ); |
---|
602 | } |
---|
603 | |
---|
604 | void |
---|
605 | tr_bencInitBool( tr_benc * b, int value ) |
---|
606 | { |
---|
607 | tr_bencInit( b, TR_TYPE_BOOL ); |
---|
608 | b->val.b = value != 0; |
---|
609 | } |
---|
610 | |
---|
611 | void |
---|
612 | tr_bencInitReal( tr_benc * b, double value ) |
---|
613 | { |
---|
614 | tr_bencInit( b, TR_TYPE_REAL ); |
---|
615 | b->val.d = value; |
---|
616 | } |
---|
617 | |
---|
618 | void |
---|
619 | tr_bencInitInt( tr_benc * b, int64_t value ) |
---|
620 | { |
---|
621 | tr_bencInit( b, TR_TYPE_INT ); |
---|
622 | b->val.i = value; |
---|
623 | } |
---|
624 | |
---|
625 | int |
---|
626 | tr_bencInitList( tr_benc * b, size_t reserveCount ) |
---|
627 | { |
---|
628 | tr_bencInit( b, TR_TYPE_LIST ); |
---|
629 | return tr_bencListReserve( b, reserveCount ); |
---|
630 | } |
---|
631 | |
---|
632 | int |
---|
633 | tr_bencListReserve( tr_benc * b, size_t count ) |
---|
634 | { |
---|
635 | assert( tr_bencIsList( b ) ); |
---|
636 | return makeroom( b, count ); |
---|
637 | } |
---|
638 | |
---|
639 | int |
---|
640 | tr_bencInitDict( tr_benc * b, size_t reserveCount ) |
---|
641 | { |
---|
642 | tr_bencInit( b, TR_TYPE_DICT ); |
---|
643 | return tr_bencDictReserve( b, reserveCount ); |
---|
644 | } |
---|
645 | |
---|
646 | int |
---|
647 | tr_bencDictReserve( tr_benc * b, size_t reserveCount ) |
---|
648 | { |
---|
649 | assert( tr_bencIsDict( b ) ); |
---|
650 | return makeroom( b, reserveCount * 2 ); |
---|
651 | } |
---|
652 | |
---|
653 | tr_benc * |
---|
654 | tr_bencListAdd( tr_benc * list ) |
---|
655 | { |
---|
656 | tr_benc * item; |
---|
657 | |
---|
658 | assert( tr_bencIsList( list ) ); |
---|
659 | |
---|
660 | if( list->val.l.count == list->val.l.alloc ) |
---|
661 | tr_bencListReserve( list, LIST_SIZE ); |
---|
662 | |
---|
663 | assert( list->val.l.count < list->val.l.alloc ); |
---|
664 | |
---|
665 | item = &list->val.l.vals[list->val.l.count]; |
---|
666 | list->val.l.count++; |
---|
667 | tr_bencInit( item, TR_TYPE_INT ); |
---|
668 | |
---|
669 | return item; |
---|
670 | } |
---|
671 | |
---|
672 | tr_benc * |
---|
673 | tr_bencListAddInt( tr_benc * list, int64_t val ) |
---|
674 | { |
---|
675 | tr_benc * node = tr_bencListAdd( list ); |
---|
676 | |
---|
677 | tr_bencInitInt( node, val ); |
---|
678 | return node; |
---|
679 | } |
---|
680 | |
---|
681 | tr_benc * |
---|
682 | tr_bencListAddReal( tr_benc * list, double val ) |
---|
683 | { |
---|
684 | tr_benc * node = tr_bencListAdd( list ); |
---|
685 | tr_bencInitReal( node, val ); |
---|
686 | return node; |
---|
687 | } |
---|
688 | |
---|
689 | tr_benc * |
---|
690 | tr_bencListAddBool( tr_benc * list, tr_bool val ) |
---|
691 | { |
---|
692 | tr_benc * node = tr_bencListAdd( list ); |
---|
693 | tr_bencInitBool( node, val ); |
---|
694 | return node; |
---|
695 | } |
---|
696 | |
---|
697 | tr_benc * |
---|
698 | tr_bencListAddStr( tr_benc * list, const char * val ) |
---|
699 | { |
---|
700 | tr_benc * node = tr_bencListAdd( list ); |
---|
701 | tr_bencInitStr( node, val, -1 ); |
---|
702 | return node; |
---|
703 | } |
---|
704 | |
---|
705 | tr_benc * |
---|
706 | tr_bencListAddRaw( tr_benc * list, const uint8_t * val, size_t len ) |
---|
707 | { |
---|
708 | tr_benc * node = tr_bencListAdd( list ); |
---|
709 | tr_bencInitRaw( node, val, len ); |
---|
710 | return node; |
---|
711 | } |
---|
712 | |
---|
713 | tr_benc* |
---|
714 | tr_bencListAddList( tr_benc * list, |
---|
715 | size_t reserveCount ) |
---|
716 | { |
---|
717 | tr_benc * child = tr_bencListAdd( list ); |
---|
718 | |
---|
719 | tr_bencInitList( child, reserveCount ); |
---|
720 | return child; |
---|
721 | } |
---|
722 | |
---|
723 | tr_benc* |
---|
724 | tr_bencListAddDict( tr_benc * list, |
---|
725 | size_t reserveCount ) |
---|
726 | { |
---|
727 | tr_benc * child = tr_bencListAdd( list ); |
---|
728 | |
---|
729 | tr_bencInitDict( child, reserveCount ); |
---|
730 | return child; |
---|
731 | } |
---|
732 | |
---|
733 | tr_benc * |
---|
734 | tr_bencDictAdd( tr_benc * dict, |
---|
735 | const char * key ) |
---|
736 | { |
---|
737 | tr_benc * keyval, * itemval; |
---|
738 | |
---|
739 | assert( tr_bencIsDict( dict ) ); |
---|
740 | if( dict->val.l.count + 2 > dict->val.l.alloc ) |
---|
741 | makeroom( dict, 2 ); |
---|
742 | assert( dict->val.l.count + 2 <= dict->val.l.alloc ); |
---|
743 | |
---|
744 | keyval = dict->val.l.vals + dict->val.l.count++; |
---|
745 | tr_bencInitStr( keyval, key, -1 ); |
---|
746 | |
---|
747 | itemval = dict->val.l.vals + dict->val.l.count++; |
---|
748 | tr_bencInit( itemval, TR_TYPE_INT ); |
---|
749 | |
---|
750 | return itemval; |
---|
751 | } |
---|
752 | |
---|
753 | static tr_benc* |
---|
754 | dictFindOrAdd( tr_benc * dict, const char * key, int type ) |
---|
755 | { |
---|
756 | tr_benc * child; |
---|
757 | |
---|
758 | /* see if it already exists, and if so, try to reuse it */ |
---|
759 | if(( child = tr_bencDictFind( dict, key ))) { |
---|
760 | if( !tr_bencIsType( child, type ) ) { |
---|
761 | tr_bencDictRemove( dict, key ); |
---|
762 | child = NULL; |
---|
763 | } |
---|
764 | } |
---|
765 | |
---|
766 | /* if it doesn't exist, create it */ |
---|
767 | if( child == NULL ) |
---|
768 | child = tr_bencDictAdd( dict, key ); |
---|
769 | |
---|
770 | return child; |
---|
771 | } |
---|
772 | |
---|
773 | tr_benc* |
---|
774 | tr_bencDictAddInt( tr_benc * dict, |
---|
775 | const char * key, |
---|
776 | int64_t val ) |
---|
777 | { |
---|
778 | tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_INT ); |
---|
779 | tr_bencInitInt( child, val ); |
---|
780 | return child; |
---|
781 | } |
---|
782 | |
---|
783 | tr_benc* |
---|
784 | tr_bencDictAddBool( tr_benc * dict, const char * key, tr_bool val ) |
---|
785 | { |
---|
786 | tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_BOOL ); |
---|
787 | tr_bencInitBool( child, val ); |
---|
788 | return child; |
---|
789 | } |
---|
790 | |
---|
791 | tr_benc* |
---|
792 | tr_bencDictAddReal( tr_benc * dict, const char * key, double val ) |
---|
793 | { |
---|
794 | tr_benc * child = dictFindOrAdd( dict, key, TR_TYPE_REAL ); |
---|
795 | tr_bencInitReal( child, val ); |
---|
796 | return child; |
---|
797 | } |
---|
798 | |
---|
799 | tr_benc* |
---|
800 | tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val ) |
---|
801 | { |
---|
802 | tr_benc * child; |
---|
803 | |
---|
804 | /* see if it already exists, and if so, try to reuse it */ |
---|
805 | if(( child = tr_bencDictFind( dict, key ))) { |
---|
806 | if( tr_bencIsString( child ) ) { |
---|
807 | if( stringIsAlloced( child ) ) |
---|
808 | tr_free( child->val.s.str.ptr ); |
---|
809 | } else { |
---|
810 | tr_bencDictRemove( dict, key ); |
---|
811 | child = NULL; |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | /* if it doesn't exist, create it */ |
---|
816 | if( child == NULL ) |
---|
817 | child = tr_bencDictAdd( dict, key ); |
---|
818 | |
---|
819 | /* set it */ |
---|
820 | tr_bencInitStr( child, val, -1 ); |
---|
821 | |
---|
822 | return child; |
---|
823 | } |
---|
824 | |
---|
825 | tr_benc* |
---|
826 | tr_bencDictAddRaw( tr_benc * dict, |
---|
827 | const char * key, |
---|
828 | const void * src, |
---|
829 | size_t len ) |
---|
830 | { |
---|
831 | tr_benc * child; |
---|
832 | |
---|
833 | /* see if it already exists, and if so, try to reuse it */ |
---|
834 | if(( child = tr_bencDictFind( dict, key ))) { |
---|
835 | if( tr_bencIsString( child ) ) { |
---|
836 | if( stringIsAlloced( child ) ) |
---|
837 | tr_free( child->val.s.str.ptr ); |
---|
838 | } else { |
---|
839 | tr_bencDictRemove( dict, key ); |
---|
840 | child = NULL; |
---|
841 | } |
---|
842 | } |
---|
843 | |
---|
844 | /* if it doesn't exist, create it */ |
---|
845 | if( child == NULL ) |
---|
846 | child = tr_bencDictAdd( dict, key ); |
---|
847 | |
---|
848 | /* set it */ |
---|
849 | tr_bencInitRaw( child, src, len ); |
---|
850 | |
---|
851 | return child; |
---|
852 | } |
---|
853 | |
---|
854 | tr_benc* |
---|
855 | tr_bencDictAddList( tr_benc * dict, |
---|
856 | const char * key, |
---|
857 | size_t reserveCount ) |
---|
858 | { |
---|
859 | tr_benc * child = tr_bencDictAdd( dict, key ); |
---|
860 | |
---|
861 | tr_bencInitList( child, reserveCount ); |
---|
862 | return child; |
---|
863 | } |
---|
864 | |
---|
865 | tr_benc* |
---|
866 | tr_bencDictAddDict( tr_benc * dict, |
---|
867 | const char * key, |
---|
868 | size_t reserveCount ) |
---|
869 | { |
---|
870 | tr_benc * child = tr_bencDictAdd( dict, key ); |
---|
871 | |
---|
872 | tr_bencInitDict( child, reserveCount ); |
---|
873 | return child; |
---|
874 | } |
---|
875 | |
---|
876 | int |
---|
877 | tr_bencDictRemove( tr_benc * dict, |
---|
878 | const char * key ) |
---|
879 | { |
---|
880 | int i = dictIndexOf( dict, key ); |
---|
881 | |
---|
882 | if( i >= 0 ) |
---|
883 | { |
---|
884 | const int n = dict->val.l.count; |
---|
885 | tr_bencFree( &dict->val.l.vals[i] ); |
---|
886 | tr_bencFree( &dict->val.l.vals[i + 1] ); |
---|
887 | if( i + 2 < n ) |
---|
888 | { |
---|
889 | dict->val.l.vals[i] = dict->val.l.vals[n - 2]; |
---|
890 | dict->val.l.vals[i + 1] = dict->val.l.vals[n - 1]; |
---|
891 | } |
---|
892 | dict->val.l.count -= 2; |
---|
893 | } |
---|
894 | return i >= 0; /* return true if found */ |
---|
895 | } |
---|
896 | |
---|
897 | /*** |
---|
898 | **** BENC WALKING |
---|
899 | ***/ |
---|
900 | |
---|
901 | struct KeyIndex |
---|
902 | { |
---|
903 | const char * key; |
---|
904 | int index; |
---|
905 | }; |
---|
906 | |
---|
907 | static int |
---|
908 | compareKeyIndex( const void * va, |
---|
909 | const void * vb ) |
---|
910 | { |
---|
911 | const struct KeyIndex * a = va; |
---|
912 | const struct KeyIndex * b = vb; |
---|
913 | |
---|
914 | return strcmp( a->key, b->key ); |
---|
915 | } |
---|
916 | |
---|
917 | struct SaveNode |
---|
918 | { |
---|
919 | const tr_benc * val; |
---|
920 | int valIsVisited; |
---|
921 | int childCount; |
---|
922 | int childIndex; |
---|
923 | int * children; |
---|
924 | }; |
---|
925 | |
---|
926 | static struct SaveNode* |
---|
927 | nodeNewDict( const tr_benc * val ) |
---|
928 | { |
---|
929 | int i, j; |
---|
930 | int nKeys; |
---|
931 | struct SaveNode * node; |
---|
932 | struct KeyIndex * indices; |
---|
933 | |
---|
934 | assert( tr_bencIsDict( val ) ); |
---|
935 | |
---|
936 | nKeys = val->val.l.count / 2; |
---|
937 | node = tr_new0( struct SaveNode, 1 ); |
---|
938 | node->val = val; |
---|
939 | node->children = tr_new0( int, nKeys * 2 ); |
---|
940 | |
---|
941 | /* ugh, a dictionary's children have to be sorted by key... */ |
---|
942 | indices = tr_new( struct KeyIndex, nKeys ); |
---|
943 | for( i = j = 0; i < ( nKeys * 2 ); i += 2, ++j ) |
---|
944 | { |
---|
945 | indices[j].key = getStr(&val->val.l.vals[i]); |
---|
946 | indices[j].index = i; |
---|
947 | } |
---|
948 | qsort( indices, j, sizeof( struct KeyIndex ), compareKeyIndex ); |
---|
949 | for( i = 0; i < j; ++i ) |
---|
950 | { |
---|
951 | const int index = indices[i].index; |
---|
952 | node->children[node->childCount++] = index; |
---|
953 | node->children[node->childCount++] = index + 1; |
---|
954 | } |
---|
955 | |
---|
956 | assert( node->childCount == nKeys * 2 ); |
---|
957 | tr_free( indices ); |
---|
958 | return node; |
---|
959 | } |
---|
960 | |
---|
961 | static struct SaveNode* |
---|
962 | nodeNewList( const tr_benc * val ) |
---|
963 | { |
---|
964 | int i, n; |
---|
965 | struct SaveNode * node; |
---|
966 | |
---|
967 | assert( tr_bencIsList( val ) ); |
---|
968 | |
---|
969 | n = val->val.l.count; |
---|
970 | node = tr_new0( struct SaveNode, 1 ); |
---|
971 | node->val = val; |
---|
972 | node->childCount = n; |
---|
973 | node->children = tr_new0( int, n ); |
---|
974 | for( i = 0; i < n; ++i ) /* a list's children don't need to be reordered */ |
---|
975 | node->children[i] = i; |
---|
976 | |
---|
977 | return node; |
---|
978 | } |
---|
979 | |
---|
980 | static struct SaveNode* |
---|
981 | nodeNewLeaf( const tr_benc * val ) |
---|
982 | { |
---|
983 | struct SaveNode * node; |
---|
984 | |
---|
985 | assert( !isContainer( val ) ); |
---|
986 | |
---|
987 | node = tr_new0( struct SaveNode, 1 ); |
---|
988 | node->val = val; |
---|
989 | return node; |
---|
990 | } |
---|
991 | |
---|
992 | static struct SaveNode* |
---|
993 | nodeNew( const tr_benc * val ) |
---|
994 | { |
---|
995 | struct SaveNode * node; |
---|
996 | |
---|
997 | if( tr_bencIsList( val ) ) |
---|
998 | node = nodeNewList( val ); |
---|
999 | else if( tr_bencIsDict( val ) ) |
---|
1000 | node = nodeNewDict( val ); |
---|
1001 | else |
---|
1002 | node = nodeNewLeaf( val ); |
---|
1003 | |
---|
1004 | return node; |
---|
1005 | } |
---|
1006 | |
---|
1007 | typedef void ( *BencWalkFunc )( const tr_benc * val, void * user_data ); |
---|
1008 | |
---|
1009 | struct WalkFuncs |
---|
1010 | { |
---|
1011 | BencWalkFunc intFunc; |
---|
1012 | BencWalkFunc boolFunc; |
---|
1013 | BencWalkFunc realFunc; |
---|
1014 | BencWalkFunc stringFunc; |
---|
1015 | BencWalkFunc dictBeginFunc; |
---|
1016 | BencWalkFunc listBeginFunc; |
---|
1017 | BencWalkFunc containerEndFunc; |
---|
1018 | }; |
---|
1019 | |
---|
1020 | /** |
---|
1021 | * This function's previous recursive implementation was |
---|
1022 | * easier to read, but was vulnerable to a smash-stacking |
---|
1023 | * attack via maliciously-crafted bencoded data. (#667) |
---|
1024 | */ |
---|
1025 | static void |
---|
1026 | bencWalk( const tr_benc * top, |
---|
1027 | const struct WalkFuncs * walkFuncs, |
---|
1028 | void * user_data ) |
---|
1029 | { |
---|
1030 | tr_ptrArray stack = TR_PTR_ARRAY_INIT; |
---|
1031 | |
---|
1032 | tr_ptrArrayAppend( &stack, nodeNew( top ) ); |
---|
1033 | |
---|
1034 | while( !tr_ptrArrayEmpty( &stack ) ) |
---|
1035 | { |
---|
1036 | struct SaveNode * node = tr_ptrArrayBack( &stack ); |
---|
1037 | const tr_benc * val; |
---|
1038 | |
---|
1039 | if( !node->valIsVisited ) |
---|
1040 | { |
---|
1041 | val = node->val; |
---|
1042 | node->valIsVisited = TRUE; |
---|
1043 | } |
---|
1044 | else if( node->childIndex < node->childCount ) |
---|
1045 | { |
---|
1046 | const int index = node->children[node->childIndex++]; |
---|
1047 | val = node->val->val.l.vals + index; |
---|
1048 | } |
---|
1049 | else /* done with this node */ |
---|
1050 | { |
---|
1051 | if( isContainer( node->val ) ) |
---|
1052 | walkFuncs->containerEndFunc( node->val, user_data ); |
---|
1053 | tr_ptrArrayPop( &stack ); |
---|
1054 | tr_free( node->children ); |
---|
1055 | tr_free( node ); |
---|
1056 | continue; |
---|
1057 | } |
---|
1058 | |
---|
1059 | if( val ) switch( val->type ) |
---|
1060 | { |
---|
1061 | case TR_TYPE_INT: |
---|
1062 | walkFuncs->intFunc( val, user_data ); |
---|
1063 | break; |
---|
1064 | |
---|
1065 | case TR_TYPE_BOOL: |
---|
1066 | walkFuncs->boolFunc( val, user_data ); |
---|
1067 | break; |
---|
1068 | |
---|
1069 | case TR_TYPE_REAL: |
---|
1070 | walkFuncs->realFunc( val, user_data ); |
---|
1071 | break; |
---|
1072 | |
---|
1073 | case TR_TYPE_STR: |
---|
1074 | walkFuncs->stringFunc( val, user_data ); |
---|
1075 | break; |
---|
1076 | |
---|
1077 | case TR_TYPE_LIST: |
---|
1078 | if( val != node->val ) |
---|
1079 | tr_ptrArrayAppend( &stack, nodeNew( val ) ); |
---|
1080 | else |
---|
1081 | walkFuncs->listBeginFunc( val, user_data ); |
---|
1082 | break; |
---|
1083 | |
---|
1084 | case TR_TYPE_DICT: |
---|
1085 | if( val != node->val ) |
---|
1086 | tr_ptrArrayAppend( &stack, nodeNew( val ) ); |
---|
1087 | else |
---|
1088 | walkFuncs->dictBeginFunc( val, user_data ); |
---|
1089 | break; |
---|
1090 | |
---|
1091 | default: |
---|
1092 | /* did caller give us an uninitialized val? */ |
---|
1093 | tr_err( "%s", _( "Invalid metadata" ) ); |
---|
1094 | break; |
---|
1095 | } |
---|
1096 | } |
---|
1097 | |
---|
1098 | tr_ptrArrayDestruct( &stack, NULL ); |
---|
1099 | } |
---|
1100 | |
---|
1101 | /**** |
---|
1102 | ***** |
---|
1103 | ****/ |
---|
1104 | |
---|
1105 | static void |
---|
1106 | saveIntFunc( const tr_benc * val, void * evbuf ) |
---|
1107 | { |
---|
1108 | evbuffer_add_printf( evbuf, "i%" PRId64 "e", val->val.i ); |
---|
1109 | } |
---|
1110 | |
---|
1111 | static void |
---|
1112 | saveBoolFunc( const tr_benc * val, void * evbuf ) |
---|
1113 | { |
---|
1114 | if( val->val.b ) |
---|
1115 | evbuffer_add( evbuf, "i1e", 3 ); |
---|
1116 | else |
---|
1117 | evbuffer_add( evbuf, "i0e", 3 ); |
---|
1118 | } |
---|
1119 | |
---|
1120 | static void |
---|
1121 | saveRealFunc( const tr_benc * val, void * evbuf ) |
---|
1122 | { |
---|
1123 | char buf[128]; |
---|
1124 | char locale[128]; |
---|
1125 | size_t len; |
---|
1126 | |
---|
1127 | /* always use a '.' decimal point s.t. locale-hopping doesn't bite us */ |
---|
1128 | tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) ); |
---|
1129 | setlocale( LC_NUMERIC, "POSIX" ); |
---|
1130 | tr_snprintf( buf, sizeof( buf ), "%f", val->val.d ); |
---|
1131 | setlocale( LC_NUMERIC, locale ); |
---|
1132 | |
---|
1133 | len = strlen( buf ); |
---|
1134 | evbuffer_add_printf( evbuf, "%lu:", (unsigned long)len ); |
---|
1135 | evbuffer_add( evbuf, buf, len ); |
---|
1136 | } |
---|
1137 | |
---|
1138 | static void |
---|
1139 | saveStringFunc( const tr_benc * val, void * evbuf ) |
---|
1140 | { |
---|
1141 | evbuffer_add_printf( evbuf, "%lu:", (unsigned long)val->val.s.len ); |
---|
1142 | evbuffer_add( evbuf, getStr(val), val->val.s.len ); |
---|
1143 | } |
---|
1144 | |
---|
1145 | static void |
---|
1146 | saveDictBeginFunc( const tr_benc * val UNUSED, void * evbuf ) |
---|
1147 | { |
---|
1148 | evbuffer_add( evbuf, "d", 1 ); |
---|
1149 | } |
---|
1150 | |
---|
1151 | static void |
---|
1152 | saveListBeginFunc( const tr_benc * val UNUSED, void * evbuf ) |
---|
1153 | { |
---|
1154 | evbuffer_add( evbuf, "l", 1 ); |
---|
1155 | } |
---|
1156 | |
---|
1157 | static void |
---|
1158 | saveContainerEndFunc( const tr_benc * val UNUSED, void * evbuf ) |
---|
1159 | { |
---|
1160 | evbuffer_add( evbuf, "e", 1 ); |
---|
1161 | } |
---|
1162 | |
---|
1163 | static const struct WalkFuncs saveFuncs = { saveIntFunc, |
---|
1164 | saveBoolFunc, |
---|
1165 | saveRealFunc, |
---|
1166 | saveStringFunc, |
---|
1167 | saveDictBeginFunc, |
---|
1168 | saveListBeginFunc, |
---|
1169 | saveContainerEndFunc }; |
---|
1170 | |
---|
1171 | /*** |
---|
1172 | **** |
---|
1173 | ***/ |
---|
1174 | |
---|
1175 | static void |
---|
1176 | freeDummyFunc( const tr_benc * val UNUSED, |
---|
1177 | void * buf UNUSED ) |
---|
1178 | {} |
---|
1179 | |
---|
1180 | static void |
---|
1181 | freeStringFunc( const tr_benc * val, |
---|
1182 | void * freeme ) |
---|
1183 | { |
---|
1184 | if( stringIsAlloced( val ) ) |
---|
1185 | tr_ptrArrayAppend( freeme, val->val.s.str.ptr ); |
---|
1186 | } |
---|
1187 | |
---|
1188 | static void |
---|
1189 | freeContainerBeginFunc( const tr_benc * val, |
---|
1190 | void * freeme ) |
---|
1191 | { |
---|
1192 | tr_ptrArrayAppend( freeme, val->val.l.vals ); |
---|
1193 | } |
---|
1194 | |
---|
1195 | static const struct WalkFuncs freeWalkFuncs = { freeDummyFunc, |
---|
1196 | freeDummyFunc, |
---|
1197 | freeDummyFunc, |
---|
1198 | freeStringFunc, |
---|
1199 | freeContainerBeginFunc, |
---|
1200 | freeContainerBeginFunc, |
---|
1201 | freeDummyFunc }; |
---|
1202 | |
---|
1203 | void |
---|
1204 | tr_bencFree( tr_benc * val ) |
---|
1205 | { |
---|
1206 | if( isSomething( val ) ) |
---|
1207 | { |
---|
1208 | tr_ptrArray a = TR_PTR_ARRAY_INIT; |
---|
1209 | bencWalk( val, &freeWalkFuncs, &a ); |
---|
1210 | tr_ptrArrayDestruct( &a, tr_free ); |
---|
1211 | } |
---|
1212 | } |
---|
1213 | |
---|
1214 | /*** |
---|
1215 | **** |
---|
1216 | ***/ |
---|
1217 | |
---|
1218 | /** @brief Implementation helper class for tr_bencToBuffer(TR_FMT_JSON) */ |
---|
1219 | struct ParentState |
---|
1220 | { |
---|
1221 | int bencType; |
---|
1222 | int childIndex; |
---|
1223 | int childCount; |
---|
1224 | }; |
---|
1225 | |
---|
1226 | /** @brief Implementation helper class for tr_bencToBuffer(TR_FMT_JSON) */ |
---|
1227 | struct jsonWalk |
---|
1228 | { |
---|
1229 | tr_bool doIndent; |
---|
1230 | tr_list * parents; |
---|
1231 | struct evbuffer * out; |
---|
1232 | }; |
---|
1233 | |
---|
1234 | static void |
---|
1235 | jsonIndent( struct jsonWalk * data ) |
---|
1236 | { |
---|
1237 | if( data->doIndent ) |
---|
1238 | { |
---|
1239 | char buf[1024]; |
---|
1240 | const int width = tr_list_size( data->parents ) * 4; |
---|
1241 | |
---|
1242 | buf[0] = '\n'; |
---|
1243 | memset( buf+1, ' ', width ); |
---|
1244 | evbuffer_add( data->out, buf, 1+width ); |
---|
1245 | } |
---|
1246 | } |
---|
1247 | |
---|
1248 | static void |
---|
1249 | jsonChildFunc( struct jsonWalk * data ) |
---|
1250 | { |
---|
1251 | if( data->parents ) |
---|
1252 | { |
---|
1253 | struct ParentState * parentState = data->parents->data; |
---|
1254 | |
---|
1255 | switch( parentState->bencType ) |
---|
1256 | { |
---|
1257 | case TR_TYPE_DICT: |
---|
1258 | { |
---|
1259 | const int i = parentState->childIndex++; |
---|
1260 | if( !( i % 2 ) ) |
---|
1261 | evbuffer_add( data->out, ": ", data->doIndent ? 2 : 1 ); |
---|
1262 | else { |
---|
1263 | const tr_bool isLast = parentState->childIndex == parentState->childCount; |
---|
1264 | if( !isLast ) { |
---|
1265 | evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 ); |
---|
1266 | jsonIndent( data ); |
---|
1267 | } |
---|
1268 | } |
---|
1269 | break; |
---|
1270 | } |
---|
1271 | |
---|
1272 | case TR_TYPE_LIST: |
---|
1273 | { |
---|
1274 | const tr_bool isLast = ++parentState->childIndex == parentState->childCount; |
---|
1275 | if( !isLast ) { |
---|
1276 | evbuffer_add( data->out, ", ", data->doIndent ? 2 : 1 ); |
---|
1277 | jsonIndent( data ); |
---|
1278 | } |
---|
1279 | break; |
---|
1280 | } |
---|
1281 | |
---|
1282 | default: |
---|
1283 | break; |
---|
1284 | } |
---|
1285 | } |
---|
1286 | } |
---|
1287 | |
---|
1288 | static void |
---|
1289 | jsonPushParent( struct jsonWalk * data, |
---|
1290 | const tr_benc * benc ) |
---|
1291 | { |
---|
1292 | struct ParentState * parentState = tr_new( struct ParentState, 1 ); |
---|
1293 | |
---|
1294 | parentState->bencType = benc->type; |
---|
1295 | parentState->childIndex = 0; |
---|
1296 | parentState->childCount = benc->val.l.count; |
---|
1297 | tr_list_prepend( &data->parents, parentState ); |
---|
1298 | } |
---|
1299 | |
---|
1300 | static void |
---|
1301 | jsonPopParent( struct jsonWalk * data ) |
---|
1302 | { |
---|
1303 | tr_free( tr_list_pop_front( &data->parents ) ); |
---|
1304 | } |
---|
1305 | |
---|
1306 | static void |
---|
1307 | jsonIntFunc( const tr_benc * val, |
---|
1308 | void * vdata ) |
---|
1309 | { |
---|
1310 | struct jsonWalk * data = vdata; |
---|
1311 | |
---|
1312 | evbuffer_add_printf( data->out, "%" PRId64, val->val.i ); |
---|
1313 | jsonChildFunc( data ); |
---|
1314 | } |
---|
1315 | |
---|
1316 | static void |
---|
1317 | jsonBoolFunc( const tr_benc * val, void * vdata ) |
---|
1318 | { |
---|
1319 | struct jsonWalk * data = vdata; |
---|
1320 | |
---|
1321 | if( val->val.b ) |
---|
1322 | evbuffer_add( data->out, "true", 4 ); |
---|
1323 | else |
---|
1324 | evbuffer_add( data->out, "false", 5 ); |
---|
1325 | |
---|
1326 | jsonChildFunc( data ); |
---|
1327 | } |
---|
1328 | |
---|
1329 | static void |
---|
1330 | jsonRealFunc( const tr_benc * val, void * vdata ) |
---|
1331 | { |
---|
1332 | struct jsonWalk * data = vdata; |
---|
1333 | char locale[128]; |
---|
1334 | |
---|
1335 | if( fabs( val->val.d ) < 0.00001 ) |
---|
1336 | evbuffer_add( data->out, "0", 1 ); |
---|
1337 | else { |
---|
1338 | /* json requires a '.' decimal point regardless of locale */ |
---|
1339 | tr_strlcpy( locale, setlocale( LC_NUMERIC, NULL ), sizeof( locale ) ); |
---|
1340 | setlocale( LC_NUMERIC, "POSIX" ); |
---|
1341 | evbuffer_add_printf( data->out, "%.4f", tr_truncd( val->val.d, 4 ) ); |
---|
1342 | setlocale( LC_NUMERIC, locale ); |
---|
1343 | } |
---|
1344 | |
---|
1345 | jsonChildFunc( data ); |
---|
1346 | } |
---|
1347 | |
---|
1348 | static void |
---|
1349 | jsonStringFunc( const tr_benc * val, void * vdata ) |
---|
1350 | { |
---|
1351 | struct jsonWalk * data = vdata; |
---|
1352 | const unsigned char * it = (const unsigned char *) getStr(val); |
---|
1353 | const unsigned char * end = it + val->val.s.len; |
---|
1354 | |
---|
1355 | evbuffer_expand( data->out, val->val.s.len + 2 ); |
---|
1356 | evbuffer_add( data->out, "\"", 1 ); |
---|
1357 | |
---|
1358 | for( ; it!=end; ++it ) |
---|
1359 | { |
---|
1360 | switch( *it ) |
---|
1361 | { |
---|
1362 | case '\b': evbuffer_add( data->out, "\\b", 2 ); break; |
---|
1363 | case '\f': evbuffer_add( data->out, "\\f", 2 ); break; |
---|
1364 | case '\n': evbuffer_add( data->out, "\\n", 2 ); break; |
---|
1365 | case '\r': evbuffer_add( data->out, "\\r", 2 ); break; |
---|
1366 | case '\t': evbuffer_add( data->out, "\\t", 2 ); break; |
---|
1367 | case '"': evbuffer_add( data->out, "\\\"", 2 ); break; |
---|
1368 | case '\\': evbuffer_add( data->out, "\\\\", 2 ); break; |
---|
1369 | |
---|
1370 | default: |
---|
1371 | if( isascii( *it ) ) |
---|
1372 | evbuffer_add( data->out, it, 1 ); |
---|
1373 | else { |
---|
1374 | const UTF8 * tmp = it; |
---|
1375 | UTF32 buf = 0; |
---|
1376 | UTF32 * u32 = &buf; |
---|
1377 | ConversionResult result = ConvertUTF8toUTF32( &tmp, end, &u32, &buf + 1, 0 ); |
---|
1378 | if((( result==conversionOK ) || (result==targetExhausted)) && (tmp!=it)) { |
---|
1379 | evbuffer_add_printf( data->out, "\\u%04x", (unsigned int)buf ); |
---|
1380 | it = tmp - 1; |
---|
1381 | } |
---|
1382 | } |
---|
1383 | } |
---|
1384 | } |
---|
1385 | evbuffer_add( data->out, "\"", 1 ); |
---|
1386 | jsonChildFunc( data ); |
---|
1387 | } |
---|
1388 | |
---|
1389 | static void |
---|
1390 | jsonDictBeginFunc( const tr_benc * val, |
---|
1391 | void * vdata ) |
---|
1392 | { |
---|
1393 | struct jsonWalk * data = vdata; |
---|
1394 | |
---|
1395 | jsonPushParent( data, val ); |
---|
1396 | evbuffer_add( data->out, "{", 1 ); |
---|
1397 | if( val->val.l.count ) |
---|
1398 | jsonIndent( data ); |
---|
1399 | } |
---|
1400 | |
---|
1401 | static void |
---|
1402 | jsonListBeginFunc( const tr_benc * val, |
---|
1403 | void * vdata ) |
---|
1404 | { |
---|
1405 | const size_t nChildren = tr_bencListSize( val ); |
---|
1406 | struct jsonWalk * data = vdata; |
---|
1407 | |
---|
1408 | jsonPushParent( data, val ); |
---|
1409 | evbuffer_add( data->out, "[", 1 ); |
---|
1410 | if( nChildren ) |
---|
1411 | jsonIndent( data ); |
---|
1412 | } |
---|
1413 | |
---|
1414 | static void |
---|
1415 | jsonContainerEndFunc( const tr_benc * val, |
---|
1416 | void * vdata ) |
---|
1417 | { |
---|
1418 | struct jsonWalk * data = vdata; |
---|
1419 | int emptyContainer = FALSE; |
---|
1420 | |
---|
1421 | jsonPopParent( data ); |
---|
1422 | if( !emptyContainer ) |
---|
1423 | jsonIndent( data ); |
---|
1424 | if( tr_bencIsDict( val ) ) |
---|
1425 | evbuffer_add( data->out, "}", 1 ); |
---|
1426 | else /* list */ |
---|
1427 | evbuffer_add( data->out, "]", 1 ); |
---|
1428 | jsonChildFunc( data ); |
---|
1429 | } |
---|
1430 | |
---|
1431 | static const struct WalkFuncs jsonWalkFuncs = { jsonIntFunc, |
---|
1432 | jsonBoolFunc, |
---|
1433 | jsonRealFunc, |
---|
1434 | jsonStringFunc, |
---|
1435 | jsonDictBeginFunc, |
---|
1436 | jsonListBeginFunc, |
---|
1437 | jsonContainerEndFunc }; |
---|
1438 | |
---|
1439 | /*** |
---|
1440 | **** |
---|
1441 | ***/ |
---|
1442 | |
---|
1443 | static void |
---|
1444 | tr_bencListCopy( tr_benc * target, const tr_benc * src ) |
---|
1445 | { |
---|
1446 | int i = 0; |
---|
1447 | const tr_benc * val; |
---|
1448 | |
---|
1449 | while(( val = tr_bencListChild( (tr_benc*)src, i++ ))) |
---|
1450 | { |
---|
1451 | if( tr_bencIsBool( val ) ) |
---|
1452 | { |
---|
1453 | tr_bool boolVal = 0; |
---|
1454 | tr_bencGetBool( val, &boolVal ); |
---|
1455 | tr_bencListAddBool( target, boolVal ); |
---|
1456 | } |
---|
1457 | else if( tr_bencIsReal( val ) ) |
---|
1458 | { |
---|
1459 | double realVal = 0; |
---|
1460 | tr_bencGetReal( val, &realVal ); |
---|
1461 | tr_bencListAddReal( target, realVal ); |
---|
1462 | } |
---|
1463 | else if( tr_bencIsInt( val ) ) |
---|
1464 | { |
---|
1465 | int64_t intVal = 0; |
---|
1466 | tr_bencGetInt( val, &intVal ); |
---|
1467 | tr_bencListAddInt( target, intVal ); |
---|
1468 | } |
---|
1469 | else if( tr_bencIsString( val ) ) |
---|
1470 | { |
---|
1471 | tr_bencListAddRaw( target, (const uint8_t*)getStr( val ), val->val.s.len ); |
---|
1472 | } |
---|
1473 | else if( tr_bencIsDict( val ) ) |
---|
1474 | { |
---|
1475 | tr_bencMergeDicts( tr_bencListAddDict( target, 0 ), val ); |
---|
1476 | } |
---|
1477 | else if ( tr_bencIsList( val ) ) |
---|
1478 | { |
---|
1479 | tr_bencListCopy( tr_bencListAddList( target, 0 ), val ); |
---|
1480 | } |
---|
1481 | else |
---|
1482 | { |
---|
1483 | tr_err( "tr_bencListCopy skipping item" ); |
---|
1484 | } |
---|
1485 | } |
---|
1486 | } |
---|
1487 | |
---|
1488 | static size_t |
---|
1489 | tr_bencDictSize( const tr_benc * dict ) |
---|
1490 | { |
---|
1491 | size_t count = 0; |
---|
1492 | |
---|
1493 | if( tr_bencIsDict( dict ) ) |
---|
1494 | count = dict->val.l.count / 2; |
---|
1495 | |
---|
1496 | return count; |
---|
1497 | } |
---|
1498 | |
---|
1499 | tr_bool |
---|
1500 | tr_bencDictChild( tr_benc * dict, size_t n, const char ** key, tr_benc ** val ) |
---|
1501 | { |
---|
1502 | tr_bool success = 0; |
---|
1503 | |
---|
1504 | assert( tr_bencIsDict( dict ) ); |
---|
1505 | |
---|
1506 | if( tr_bencIsDict( dict ) && (n*2)+1 <= dict->val.l.count ) |
---|
1507 | { |
---|
1508 | tr_benc * k = dict->val.l.vals + (n*2); |
---|
1509 | tr_benc * v = dict->val.l.vals + (n*2) + 1; |
---|
1510 | if(( success = tr_bencGetStr( k, key ) && isSomething( v ))) |
---|
1511 | *val = v; |
---|
1512 | } |
---|
1513 | |
---|
1514 | return success; |
---|
1515 | } |
---|
1516 | |
---|
1517 | void |
---|
1518 | tr_bencMergeDicts( tr_benc * target, const tr_benc * source ) |
---|
1519 | { |
---|
1520 | size_t i; |
---|
1521 | const size_t sourceCount = tr_bencDictSize( source ); |
---|
1522 | |
---|
1523 | assert( tr_bencIsDict( target ) ); |
---|
1524 | assert( tr_bencIsDict( source ) ); |
---|
1525 | |
---|
1526 | for( i=0; i<sourceCount; ++i ) |
---|
1527 | { |
---|
1528 | const char * key; |
---|
1529 | tr_benc * val; |
---|
1530 | tr_benc * t; |
---|
1531 | |
---|
1532 | if( tr_bencDictChild( (tr_benc*)source, i, &key, &val ) ) |
---|
1533 | { |
---|
1534 | if( tr_bencIsBool( val ) ) |
---|
1535 | { |
---|
1536 | tr_bool boolVal; |
---|
1537 | tr_bencGetBool( val, &boolVal ); |
---|
1538 | tr_bencDictAddBool( target, key, boolVal ); |
---|
1539 | } |
---|
1540 | else if( tr_bencIsReal( val ) ) |
---|
1541 | { |
---|
1542 | double realVal = 0; |
---|
1543 | tr_bencGetReal( val, &realVal ); |
---|
1544 | tr_bencDictAddReal( target, key, realVal ); |
---|
1545 | } |
---|
1546 | else if( tr_bencIsInt( val ) ) |
---|
1547 | { |
---|
1548 | int64_t intVal = 0; |
---|
1549 | tr_bencGetInt( val, &intVal ); |
---|
1550 | tr_bencDictAddInt( target, key, intVal ); |
---|
1551 | } |
---|
1552 | else if( tr_bencIsString( val ) ) |
---|
1553 | { |
---|
1554 | tr_bencDictAddRaw( target, key, getStr( val ), val->val.s.len ); |
---|
1555 | } |
---|
1556 | else if( tr_bencIsDict( val ) && tr_bencDictFindDict( target, key, &t ) ) |
---|
1557 | { |
---|
1558 | tr_bencMergeDicts( t, val ); |
---|
1559 | } |
---|
1560 | else if( tr_bencIsList( val ) ) |
---|
1561 | { |
---|
1562 | if( tr_bencDictFind( target, key ) == NULL ) |
---|
1563 | { |
---|
1564 | tr_bencListCopy( tr_bencDictAddList( target, key, tr_bencListSize( val ) ), val ); |
---|
1565 | } |
---|
1566 | } |
---|
1567 | else |
---|
1568 | { |
---|
1569 | tr_dbg( "tr_bencMergeDicts skipping \"%s\"", key ); |
---|
1570 | } |
---|
1571 | } |
---|
1572 | } |
---|
1573 | } |
---|
1574 | |
---|
1575 | /*** |
---|
1576 | **** |
---|
1577 | ***/ |
---|
1578 | |
---|
1579 | void |
---|
1580 | tr_bencToBuf( const tr_benc * top, tr_fmt_mode mode, struct evbuffer * buf ) |
---|
1581 | { |
---|
1582 | evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) ); |
---|
1583 | evbuffer_expand( buf, 4096 ); /* alloc a little memory to start off with */ |
---|
1584 | |
---|
1585 | switch( mode ) |
---|
1586 | { |
---|
1587 | case TR_FMT_BENC: |
---|
1588 | bencWalk( top, &saveFuncs, buf ); |
---|
1589 | break; |
---|
1590 | |
---|
1591 | case TR_FMT_JSON: |
---|
1592 | case TR_FMT_JSON_LEAN: { |
---|
1593 | struct jsonWalk data; |
---|
1594 | data.doIndent = mode==TR_FMT_JSON; |
---|
1595 | data.out = buf; |
---|
1596 | data.parents = NULL; |
---|
1597 | bencWalk( top, &jsonWalkFuncs, &data ); |
---|
1598 | if( EVBUFFER_LENGTH( buf ) ) |
---|
1599 | evbuffer_add_printf( buf, "\n" ); |
---|
1600 | break; |
---|
1601 | } |
---|
1602 | } |
---|
1603 | } |
---|
1604 | |
---|
1605 | char* |
---|
1606 | tr_bencToStr( const tr_benc * top, tr_fmt_mode mode, int * len ) |
---|
1607 | { |
---|
1608 | char * ret; |
---|
1609 | struct evbuffer * buf = evbuffer_new( ); |
---|
1610 | tr_bencToBuf( top, mode, buf ); |
---|
1611 | ret = tr_strndup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) ); |
---|
1612 | if( len != NULL ) |
---|
1613 | *len = (int) EVBUFFER_LENGTH( buf ); |
---|
1614 | evbuffer_free( buf ); |
---|
1615 | return ret; |
---|
1616 | } |
---|
1617 | |
---|
1618 | int |
---|
1619 | tr_bencToFile( const tr_benc * top, tr_fmt_mode mode, const char * filename ) |
---|
1620 | { |
---|
1621 | char * tmp; |
---|
1622 | int fd; |
---|
1623 | int err = 0; |
---|
1624 | char buf[TR_MAX_PATH]; |
---|
1625 | |
---|
1626 | /* follow symlinks to find the "real" file, to make sure the temporary |
---|
1627 | * we build with mkstemp() is created on the right partition */ |
---|
1628 | if( realpath( filename, buf ) != NULL ) |
---|
1629 | filename = buf; |
---|
1630 | |
---|
1631 | /* if the file already exists, try to move it out of the way & keep it as a backup */ |
---|
1632 | tmp = tr_strdup_printf( "%s.tmp.XXXXXX", filename ); |
---|
1633 | fd = mkstemp( tmp ); |
---|
1634 | if( fd >= 0 ) |
---|
1635 | { |
---|
1636 | int len; |
---|
1637 | char * str = tr_bencToStr( top, mode, &len ); |
---|
1638 | tr_dbg( "Writing %d bytes to temporary file \"%s\"", (int)len, tmp ); |
---|
1639 | |
---|
1640 | if( write( fd, str, len ) == (ssize_t)len ) |
---|
1641 | { |
---|
1642 | fsync( fd ); |
---|
1643 | close( fd ); |
---|
1644 | |
---|
1645 | if( !unlink( filename ) || ( errno == ENOENT ) ) |
---|
1646 | { |
---|
1647 | tr_dbg( "Renaming \"%s\" as \"%s\"", tmp, filename ); |
---|
1648 | |
---|
1649 | if( !rename( tmp, filename ) ) |
---|
1650 | { |
---|
1651 | tr_inf( _( "Saved \"%s\"" ), filename ); |
---|
1652 | } |
---|
1653 | else |
---|
1654 | { |
---|
1655 | err = errno; |
---|
1656 | tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) ); |
---|
1657 | unlink( tmp ); |
---|
1658 | } |
---|
1659 | } |
---|
1660 | else |
---|
1661 | { |
---|
1662 | err = errno; |
---|
1663 | tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), filename, tr_strerror( err ) ); |
---|
1664 | unlink( tmp ); |
---|
1665 | } |
---|
1666 | } |
---|
1667 | else |
---|
1668 | { |
---|
1669 | err = errno; |
---|
1670 | tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) ); |
---|
1671 | close( fd ); |
---|
1672 | unlink( tmp ); |
---|
1673 | } |
---|
1674 | |
---|
1675 | tr_free( str ); |
---|
1676 | } |
---|
1677 | else |
---|
1678 | { |
---|
1679 | err = errno; |
---|
1680 | tr_err( _( "Couldn't save temporary file \"%1$s\": %2$s" ), tmp, tr_strerror( err ) ); |
---|
1681 | } |
---|
1682 | |
---|
1683 | tr_free( tmp ); |
---|
1684 | return err; |
---|
1685 | } |
---|
1686 | |
---|
1687 | /*** |
---|
1688 | **** |
---|
1689 | ***/ |
---|
1690 | |
---|
1691 | int |
---|
1692 | tr_bencLoadFile( tr_benc * setme, tr_fmt_mode mode, const char * filename ) |
---|
1693 | { |
---|
1694 | int err; |
---|
1695 | size_t contentLen; |
---|
1696 | uint8_t * content; |
---|
1697 | |
---|
1698 | content = tr_loadFile( filename, &contentLen ); |
---|
1699 | if( !content && errno ) |
---|
1700 | err = errno; |
---|
1701 | else if( !content ) |
---|
1702 | err = ENODATA; |
---|
1703 | else { |
---|
1704 | if( mode == TR_FMT_BENC ) |
---|
1705 | err = tr_bencLoad( content, contentLen, setme, NULL ); |
---|
1706 | else |
---|
1707 | err = tr_jsonParse( filename, content, contentLen, setme, NULL ); |
---|
1708 | } |
---|
1709 | |
---|
1710 | tr_free( content ); |
---|
1711 | return err; |
---|
1712 | } |
---|