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