1 | /****************************************************************************** |
---|
2 | * $Id: bencode.c 5627 2008-04-15 17:00:44Z charles $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #include <assert.h> |
---|
26 | #include <ctype.h> /* isdigit, isprint */ |
---|
27 | #include <errno.h> |
---|
28 | #include <stdarg.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <stdlib.h> |
---|
31 | |
---|
32 | #include <event.h> /* evbuffer */ |
---|
33 | |
---|
34 | #include "transmission.h" |
---|
35 | #include "bencode.h" |
---|
36 | #include "list.h" |
---|
37 | #include "ptrarray.h" |
---|
38 | #include "utils.h" /* tr_new(), tr_free() */ |
---|
39 | |
---|
40 | /** |
---|
41 | *** |
---|
42 | **/ |
---|
43 | |
---|
44 | int |
---|
45 | tr_bencIsType( const tr_benc * val, int type ) |
---|
46 | { |
---|
47 | return ( ( val != NULL ) && ( val->type == type ) ); |
---|
48 | } |
---|
49 | |
---|
50 | static int |
---|
51 | isContainer( const tr_benc * val ) |
---|
52 | { |
---|
53 | return tr_bencIsList(val) || tr_bencIsDict(val); |
---|
54 | } |
---|
55 | static int |
---|
56 | isSomething( const tr_benc * val ) |
---|
57 | { |
---|
58 | return isContainer(val) || tr_bencIsInt(val) || tr_bencIsString(val); |
---|
59 | } |
---|
60 | |
---|
61 | /*** |
---|
62 | **** tr_bencParse() |
---|
63 | **** tr_bencLoad() |
---|
64 | ***/ |
---|
65 | |
---|
66 | /** |
---|
67 | * The initial i and trailing e are beginning and ending delimiters. |
---|
68 | * You can have negative numbers such as i-3e. You cannot prefix the |
---|
69 | * number with a zero such as i04e. However, i0e is valid. |
---|
70 | * Example: i3e represents the integer "3" |
---|
71 | * NOTE: The maximum number of bit of this integer is unspecified, |
---|
72 | * but to handle it as a signed 64bit integer is mandatory to handle |
---|
73 | * "large files" aka .torrent for more that 4Gbyte |
---|
74 | */ |
---|
75 | int |
---|
76 | tr_bencParseInt( const uint8_t * buf, |
---|
77 | const uint8_t * bufend, |
---|
78 | const uint8_t ** setme_end, |
---|
79 | int64_t * setme_val ) |
---|
80 | { |
---|
81 | int err = TR_OK; |
---|
82 | char * endptr; |
---|
83 | const void * begin; |
---|
84 | const void * end; |
---|
85 | int64_t val; |
---|
86 | |
---|
87 | if( buf >= bufend ) |
---|
88 | return TR_ERROR; |
---|
89 | if( *buf != 'i' ) |
---|
90 | return TR_ERROR; |
---|
91 | |
---|
92 | begin = buf + 1; |
---|
93 | end = memchr( begin, 'e', (bufend-buf)-1 ); |
---|
94 | if( end == NULL ) |
---|
95 | return TR_ERROR; |
---|
96 | |
---|
97 | errno = 0; |
---|
98 | val = strtoll( begin, &endptr, 10 ); |
---|
99 | if( errno || ( endptr != end ) ) /* incomplete parse */ |
---|
100 | err = TR_ERROR; |
---|
101 | else if( val && *(const char*)begin=='0' ) /* no leading zeroes! */ |
---|
102 | err = TR_ERROR; |
---|
103 | else { |
---|
104 | *setme_end = end + 1; |
---|
105 | *setme_val = val; |
---|
106 | } |
---|
107 | |
---|
108 | return err; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | * Byte strings are encoded as follows: |
---|
114 | * <string length encoded in base ten ASCII>:<string data> |
---|
115 | * Note that there is no constant beginning delimiter, and no ending delimiter. |
---|
116 | * Example: 4:spam represents the string "spam" |
---|
117 | */ |
---|
118 | int |
---|
119 | tr_bencParseStr( const uint8_t * buf, |
---|
120 | const uint8_t * bufend, |
---|
121 | const uint8_t ** setme_end, |
---|
122 | uint8_t ** setme_str, |
---|
123 | size_t * setme_strlen ) |
---|
124 | { |
---|
125 | size_t len; |
---|
126 | const void * end; |
---|
127 | char * endptr; |
---|
128 | |
---|
129 | if( buf >= bufend ) |
---|
130 | return TR_ERROR; |
---|
131 | |
---|
132 | if( !isdigit( *buf ) ) |
---|
133 | return TR_ERROR; |
---|
134 | |
---|
135 | end = memchr( buf, ':', bufend-buf ); |
---|
136 | if( end == NULL ) |
---|
137 | return TR_ERROR; |
---|
138 | |
---|
139 | errno = 0; |
---|
140 | len = strtoul( (const char*)buf, &endptr, 10 ); |
---|
141 | if( errno || endptr!=end ) |
---|
142 | return TR_ERROR; |
---|
143 | |
---|
144 | if( (const uint8_t*)end + 1 + len > bufend ) |
---|
145 | return TR_ERROR; |
---|
146 | |
---|
147 | *setme_end = end + 1 + len; |
---|
148 | *setme_str = (uint8_t*) tr_strndup( end + 1, len ); |
---|
149 | *setme_strlen = len; |
---|
150 | return TR_OK; |
---|
151 | } |
---|
152 | |
---|
153 | /* setting to 1 to help expose bugs with tr_bencListAdd and tr_bencDictAdd */ |
---|
154 | #define LIST_SIZE 8 /* number of items to increment list/dict buffer by */ |
---|
155 | |
---|
156 | static int |
---|
157 | makeroom( tr_benc * val, int count ) |
---|
158 | { |
---|
159 | assert( TYPE_LIST == val->type || TYPE_DICT == val->type ); |
---|
160 | |
---|
161 | if( val->val.l.count + count > val->val.l.alloc ) |
---|
162 | { |
---|
163 | /* We need a bigger boat */ |
---|
164 | const int len = val->val.l.alloc + count + |
---|
165 | ( count % LIST_SIZE ? LIST_SIZE - ( count % LIST_SIZE ) : 0 ); |
---|
166 | void * new = realloc( val->val.l.vals, len * sizeof( tr_benc ) ); |
---|
167 | if( NULL == new ) |
---|
168 | return 1; |
---|
169 | |
---|
170 | val->val.l.alloc = len; |
---|
171 | val->val.l.vals = new; |
---|
172 | } |
---|
173 | |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | static tr_benc* |
---|
178 | getNode( tr_benc * top, tr_ptrArray * parentStack, int type ) |
---|
179 | { |
---|
180 | tr_benc * parent; |
---|
181 | |
---|
182 | assert( top != NULL ); |
---|
183 | assert( parentStack != NULL ); |
---|
184 | |
---|
185 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
186 | return top; |
---|
187 | |
---|
188 | parent = tr_ptrArrayBack( parentStack ); |
---|
189 | assert( parent != NULL ); |
---|
190 | |
---|
191 | /* dictionary keys must be strings */ |
---|
192 | if( ( parent->type == TYPE_DICT ) |
---|
193 | && ( type != TYPE_STR ) |
---|
194 | && ( ! ( parent->val.l.count % 2 ) ) ) |
---|
195 | return NULL; |
---|
196 | |
---|
197 | makeroom( parent, 1 ); |
---|
198 | return parent->val.l.vals + parent->val.l.count++; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * This function's previous recursive implementation was |
---|
203 | * easier to read, but was vulnerable to a smash-stacking |
---|
204 | * attack via maliciously-crafted bencoded data. (#667) |
---|
205 | */ |
---|
206 | int |
---|
207 | tr_bencParse( const void * buf_in, |
---|
208 | const void * bufend_in, |
---|
209 | tr_benc * top, |
---|
210 | const uint8_t ** setme_end ) |
---|
211 | { |
---|
212 | int err; |
---|
213 | const uint8_t * buf = buf_in; |
---|
214 | const uint8_t * bufend = bufend_in; |
---|
215 | tr_ptrArray * parentStack = tr_ptrArrayNew( ); |
---|
216 | |
---|
217 | tr_bencInit( top, 0 ); |
---|
218 | |
---|
219 | while( buf != bufend ) |
---|
220 | { |
---|
221 | if( buf > bufend ) /* no more text to parse... */ |
---|
222 | return 1; |
---|
223 | |
---|
224 | if( *buf=='i' ) /* int */ |
---|
225 | { |
---|
226 | int64_t val; |
---|
227 | const uint8_t * end; |
---|
228 | int err; |
---|
229 | tr_benc * node; |
---|
230 | |
---|
231 | if(( err = tr_bencParseInt( buf, bufend, &end, &val ))) |
---|
232 | return err; |
---|
233 | |
---|
234 | node = getNode( top, parentStack, TYPE_INT ); |
---|
235 | if( !node ) |
---|
236 | return TR_ERROR; |
---|
237 | |
---|
238 | tr_bencInitInt( node, val ); |
---|
239 | buf = end; |
---|
240 | |
---|
241 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
242 | break; |
---|
243 | } |
---|
244 | else if( *buf=='l' ) /* list */ |
---|
245 | { |
---|
246 | tr_benc * node = getNode( top, parentStack, TYPE_LIST ); |
---|
247 | if( !node ) |
---|
248 | return TR_ERROR; |
---|
249 | tr_bencInit( node, TYPE_LIST ); |
---|
250 | tr_ptrArrayAppend( parentStack, node ); |
---|
251 | ++buf; |
---|
252 | } |
---|
253 | else if( *buf=='d' ) /* dict */ |
---|
254 | { |
---|
255 | tr_benc * node = getNode( top, parentStack, TYPE_DICT ); |
---|
256 | if( !node ) |
---|
257 | return TR_ERROR; |
---|
258 | tr_bencInit( node, TYPE_DICT ); |
---|
259 | tr_ptrArrayAppend( parentStack, node ); |
---|
260 | ++buf; |
---|
261 | } |
---|
262 | else if( *buf=='e' ) /* end of list or dict */ |
---|
263 | { |
---|
264 | tr_benc * node; |
---|
265 | ++buf; |
---|
266 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
267 | return TR_ERROR; |
---|
268 | |
---|
269 | node = tr_ptrArrayBack( parentStack ); |
---|
270 | if( tr_bencIsDict( node ) && ( node->val.l.count % 2 ) ) |
---|
271 | return TR_ERROR; /* odd # of children in dict */ |
---|
272 | |
---|
273 | tr_ptrArrayPop( parentStack ); |
---|
274 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
275 | break; |
---|
276 | } |
---|
277 | else if( isdigit(*buf) ) /* string? */ |
---|
278 | { |
---|
279 | const uint8_t * end; |
---|
280 | uint8_t * str; |
---|
281 | size_t str_len; |
---|
282 | int err; |
---|
283 | tr_benc * node; |
---|
284 | |
---|
285 | if(( err = tr_bencParseStr( buf, bufend, &end, &str, &str_len ))) |
---|
286 | return err; |
---|
287 | |
---|
288 | node = getNode( top, parentStack, TYPE_STR ); |
---|
289 | if( !node ) |
---|
290 | return TR_ERROR; |
---|
291 | |
---|
292 | tr_bencInitStr( node, str, str_len, 0 ); |
---|
293 | buf = end; |
---|
294 | |
---|
295 | if( tr_ptrArrayEmpty( parentStack ) ) |
---|
296 | break; |
---|
297 | } |
---|
298 | else /* invalid bencoded text... march past it */ |
---|
299 | { |
---|
300 | ++buf; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | err = !isSomething( top ) || !tr_ptrArrayEmpty( parentStack ); |
---|
305 | |
---|
306 | if( !err && ( setme_end != NULL ) ) |
---|
307 | *setme_end = buf; |
---|
308 | |
---|
309 | tr_ptrArrayFree( parentStack, NULL ); |
---|
310 | return err; |
---|
311 | } |
---|
312 | |
---|
313 | int |
---|
314 | tr_bencLoad( const void * buf_in, |
---|
315 | int buflen, |
---|
316 | tr_benc * setme_benc, |
---|
317 | char ** setme_end ) |
---|
318 | { |
---|
319 | const uint8_t * buf = buf_in; |
---|
320 | const uint8_t * end; |
---|
321 | const int ret = tr_bencParse( buf, buf+buflen, setme_benc, &end ); |
---|
322 | if( !ret && setme_end ) |
---|
323 | *setme_end = (char*) end; |
---|
324 | return ret; |
---|
325 | } |
---|
326 | |
---|
327 | /*** |
---|
328 | **** |
---|
329 | ***/ |
---|
330 | |
---|
331 | tr_benc * |
---|
332 | tr_bencDictFind( tr_benc * val, const char * key ) |
---|
333 | { |
---|
334 | int len, ii; |
---|
335 | |
---|
336 | if( !tr_bencIsDict( val ) ) |
---|
337 | return NULL; |
---|
338 | |
---|
339 | len = strlen( key ); |
---|
340 | |
---|
341 | for( ii = 0; ii + 1 < val->val.l.count; ii += 2 ) |
---|
342 | { |
---|
343 | if( TYPE_STR != val->val.l.vals[ii].type || |
---|
344 | len != val->val.l.vals[ii].val.s.i || |
---|
345 | 0 != memcmp( val->val.l.vals[ii].val.s.s, key, len ) ) |
---|
346 | { |
---|
347 | continue; |
---|
348 | } |
---|
349 | return &val->val.l.vals[ii+1]; |
---|
350 | } |
---|
351 | |
---|
352 | return NULL; |
---|
353 | } |
---|
354 | |
---|
355 | tr_benc* |
---|
356 | tr_bencDictFindType( tr_benc * val, const char * key, int type ) |
---|
357 | { |
---|
358 | tr_benc * ret = tr_bencDictFind( val, key ); |
---|
359 | return ret && ret->type == type ? ret : NULL; |
---|
360 | } |
---|
361 | |
---|
362 | tr_benc * |
---|
363 | tr_bencDictFindFirst( tr_benc * val, ... ) |
---|
364 | { |
---|
365 | const char * key; |
---|
366 | tr_benc * ret; |
---|
367 | va_list ap; |
---|
368 | |
---|
369 | ret = NULL; |
---|
370 | va_start( ap, val ); |
---|
371 | while(( key = va_arg( ap, const char * ))) |
---|
372 | if(( ret = tr_bencDictFind( val, key ))) |
---|
373 | break; |
---|
374 | va_end( ap ); |
---|
375 | |
---|
376 | return ret; |
---|
377 | } |
---|
378 | |
---|
379 | tr_benc* |
---|
380 | tr_bencListGetNthChild( tr_benc * val, int i ) |
---|
381 | { |
---|
382 | tr_benc * ret = NULL; |
---|
383 | if( tr_bencIsList( val ) && ( i >= 0 ) && ( i < val->val.l.count ) ) |
---|
384 | ret = val->val.l.vals + i; |
---|
385 | return ret; |
---|
386 | } |
---|
387 | |
---|
388 | int |
---|
389 | tr_bencGetInt ( const tr_benc * val, int64_t * setme ) |
---|
390 | { |
---|
391 | int success = FALSE; |
---|
392 | if( tr_bencIsInt( val )) { |
---|
393 | *setme = val->val.i ; |
---|
394 | success = TRUE; |
---|
395 | } |
---|
396 | return success; |
---|
397 | } |
---|
398 | |
---|
399 | char * |
---|
400 | tr_bencStealStr( tr_benc * val ) |
---|
401 | { |
---|
402 | assert( tr_bencIsString( val ) ); |
---|
403 | val->val.s.nofree = 1; |
---|
404 | return val->val.s.s; |
---|
405 | } |
---|
406 | |
---|
407 | int |
---|
408 | tr_bencDictFindInt( tr_benc * dict, const char * key, int64_t * setme ) |
---|
409 | { |
---|
410 | int found = FALSE; |
---|
411 | tr_benc * child = tr_bencDictFindType( dict, key, TYPE_INT ); |
---|
412 | if( child ) |
---|
413 | found = tr_bencGetInt( child, setme ); |
---|
414 | return found; |
---|
415 | } |
---|
416 | |
---|
417 | int |
---|
418 | tr_bencDictFindList( tr_benc * dict, const char * key, tr_benc ** setme ) |
---|
419 | { |
---|
420 | int found = FALSE; |
---|
421 | tr_benc * child = tr_bencDictFindType( dict, key, TYPE_LIST ); |
---|
422 | if( child ) { |
---|
423 | *setme = child; |
---|
424 | found = TRUE; |
---|
425 | } |
---|
426 | return found; |
---|
427 | } |
---|
428 | |
---|
429 | int |
---|
430 | tr_bencDictFindDict( tr_benc * dict, const char * key, tr_benc ** setme ) |
---|
431 | { |
---|
432 | int found = FALSE; |
---|
433 | tr_benc * child = tr_bencDictFindType( dict, key, TYPE_DICT ); |
---|
434 | if( child ) { |
---|
435 | *setme = child; |
---|
436 | found = TRUE; |
---|
437 | } |
---|
438 | return found; |
---|
439 | } |
---|
440 | |
---|
441 | int |
---|
442 | tr_bencDictFindStr( tr_benc * dict, const char * key, const char ** setme ) |
---|
443 | { |
---|
444 | int found = FALSE; |
---|
445 | tr_benc * child = tr_bencDictFindType( dict, key, TYPE_STR ); |
---|
446 | if( child ) { |
---|
447 | *setme = child->val.s.s; |
---|
448 | found = TRUE; |
---|
449 | } |
---|
450 | return found; |
---|
451 | } |
---|
452 | |
---|
453 | tr_benc* |
---|
454 | tr_bencDictAddInt( tr_benc * dict, const char * key, int64_t val ) |
---|
455 | { |
---|
456 | tr_benc * child = tr_bencDictAdd( dict, key ); |
---|
457 | tr_bencInitInt( child, val ); |
---|
458 | return child; |
---|
459 | } |
---|
460 | |
---|
461 | tr_benc* |
---|
462 | tr_bencDictAddStr( tr_benc * dict, const char * key, const char * val ) |
---|
463 | { |
---|
464 | tr_benc * child = tr_bencDictAdd( dict, key ); |
---|
465 | tr_bencInitStrDup( child, val ); |
---|
466 | return child; |
---|
467 | } |
---|
468 | |
---|
469 | tr_benc* |
---|
470 | tr_bencDictAddList( tr_benc * dict, const char * key, int reserveCount ) |
---|
471 | { |
---|
472 | tr_benc * child = tr_bencDictAdd( dict, key ); |
---|
473 | tr_bencInitList( child, reserveCount ); |
---|
474 | return child; |
---|
475 | } |
---|
476 | |
---|
477 | tr_benc* |
---|
478 | tr_bencDictAddDict( tr_benc * dict, const char * key, int reserveCount ) |
---|
479 | { |
---|
480 | tr_benc * child = tr_bencDictAdd( dict, key ); |
---|
481 | tr_bencInitDict( child, reserveCount ); |
---|
482 | return child; |
---|
483 | } |
---|
484 | |
---|
485 | /*** |
---|
486 | **** |
---|
487 | ***/ |
---|
488 | |
---|
489 | void |
---|
490 | _tr_bencInitStr( tr_benc * val, char * str, int len, int nofree ) |
---|
491 | { |
---|
492 | tr_bencInit( val, TYPE_STR ); |
---|
493 | val->val.s.s = str; |
---|
494 | val->val.s.nofree = nofree; |
---|
495 | if( 0 >= len ) |
---|
496 | { |
---|
497 | len = ( NULL == str ? 0 : strlen( str ) ); |
---|
498 | } |
---|
499 | val->val.s.i = len; |
---|
500 | } |
---|
501 | |
---|
502 | void |
---|
503 | tr_bencInitRaw( tr_benc * val, const void * src, size_t byteCount ) |
---|
504 | { |
---|
505 | tr_bencInit( val, TYPE_STR ); |
---|
506 | val->val.s.i = byteCount; |
---|
507 | val->val.s.s = tr_new( char, byteCount ); |
---|
508 | val->val.s.nofree = 0; |
---|
509 | memcpy( val->val.s.s, src, byteCount ); |
---|
510 | } |
---|
511 | |
---|
512 | int |
---|
513 | tr_bencInitStrDup( tr_benc * val, const char * str ) |
---|
514 | { |
---|
515 | char * newStr = tr_strdup( str ); |
---|
516 | if( newStr == NULL ) |
---|
517 | return 1; |
---|
518 | |
---|
519 | _tr_bencInitStr( val, newStr, 0, 0 ); |
---|
520 | return 0; |
---|
521 | } |
---|
522 | |
---|
523 | void |
---|
524 | tr_bencInitInt( tr_benc * val, int64_t num ) |
---|
525 | { |
---|
526 | tr_bencInit( val, TYPE_INT ); |
---|
527 | val->val.i = num; |
---|
528 | } |
---|
529 | |
---|
530 | int |
---|
531 | tr_bencInitList( tr_benc * val, int reserveCount ) |
---|
532 | { |
---|
533 | tr_bencInit( val, TYPE_LIST ); |
---|
534 | return tr_bencListReserve( val, reserveCount ); |
---|
535 | } |
---|
536 | |
---|
537 | int |
---|
538 | tr_bencListReserve( tr_benc * val, int count ) |
---|
539 | { |
---|
540 | assert( tr_bencIsList( val ) ); |
---|
541 | return makeroom( val, count ); |
---|
542 | } |
---|
543 | |
---|
544 | int |
---|
545 | tr_bencInitDict( tr_benc * val, int reserveCount ) |
---|
546 | { |
---|
547 | tr_bencInit( val, TYPE_DICT ); |
---|
548 | return tr_bencDictReserve( val, reserveCount ); |
---|
549 | } |
---|
550 | |
---|
551 | int |
---|
552 | tr_bencDictReserve( tr_benc * val, int count ) |
---|
553 | { |
---|
554 | assert( tr_bencIsDict( val ) ); |
---|
555 | return makeroom( val, count * 2 ); |
---|
556 | } |
---|
557 | |
---|
558 | tr_benc * |
---|
559 | tr_bencListAdd( tr_benc * list ) |
---|
560 | { |
---|
561 | tr_benc * item; |
---|
562 | |
---|
563 | assert( tr_bencIsList( list ) ); |
---|
564 | |
---|
565 | if( list->val.l.count == list->val.l.alloc ) |
---|
566 | tr_bencListReserve( list, LIST_SIZE ); |
---|
567 | |
---|
568 | assert( list->val.l.count < list->val.l.alloc ); |
---|
569 | |
---|
570 | item = &list->val.l.vals[list->val.l.count]; |
---|
571 | list->val.l.count++; |
---|
572 | tr_bencInit( item, TYPE_INT ); |
---|
573 | |
---|
574 | return item; |
---|
575 | } |
---|
576 | |
---|
577 | tr_benc * |
---|
578 | tr_bencListAddInt( tr_benc * list, int64_t i ) |
---|
579 | { |
---|
580 | tr_benc * node = tr_bencListAdd( list ); |
---|
581 | tr_bencInitInt( node, i ); |
---|
582 | return node; |
---|
583 | } |
---|
584 | |
---|
585 | tr_benc * |
---|
586 | tr_bencDictAdd( tr_benc * dict, const char * key ) |
---|
587 | { |
---|
588 | tr_benc * keyval, * itemval; |
---|
589 | |
---|
590 | assert( tr_bencIsDict( dict ) ); |
---|
591 | assert( dict->val.l.count + 2 <= dict->val.l.alloc ); |
---|
592 | |
---|
593 | keyval = dict->val.l.vals + dict->val.l.count++; |
---|
594 | tr_bencInitStr( keyval, (char*)key, -1, 1 ); |
---|
595 | |
---|
596 | itemval = dict->val.l.vals + dict->val.l.count++; |
---|
597 | tr_bencInit( itemval, TYPE_INT ); |
---|
598 | |
---|
599 | return itemval; |
---|
600 | } |
---|
601 | |
---|
602 | /*** |
---|
603 | **** BENC WALKING |
---|
604 | ***/ |
---|
605 | |
---|
606 | struct KeyIndex |
---|
607 | { |
---|
608 | const char * key; |
---|
609 | int index; |
---|
610 | }; |
---|
611 | |
---|
612 | static int |
---|
613 | compareKeyIndex( const void * va, const void * vb ) |
---|
614 | { |
---|
615 | const struct KeyIndex * a = va; |
---|
616 | const struct KeyIndex * b = vb; |
---|
617 | return strcmp( a->key, b->key ); |
---|
618 | } |
---|
619 | |
---|
620 | struct SaveNode |
---|
621 | { |
---|
622 | const tr_benc * val; |
---|
623 | int valIsVisited; |
---|
624 | int childCount; |
---|
625 | int childIndex; |
---|
626 | int * children; |
---|
627 | }; |
---|
628 | |
---|
629 | static struct SaveNode* |
---|
630 | nodeNewDict( const tr_benc * val ) |
---|
631 | { |
---|
632 | int i, j; |
---|
633 | int nKeys; |
---|
634 | struct SaveNode * node; |
---|
635 | struct KeyIndex * indices; |
---|
636 | |
---|
637 | assert( tr_bencIsDict( val ) ); |
---|
638 | |
---|
639 | nKeys = val->val.l.count / 2; |
---|
640 | node = tr_new0( struct SaveNode, 1 ); |
---|
641 | node->val = val; |
---|
642 | node->children = tr_new0( int, nKeys * 2 ); |
---|
643 | |
---|
644 | /* ugh, a dictionary's children have to be sorted by key... */ |
---|
645 | indices = tr_new( struct KeyIndex, nKeys ); |
---|
646 | for( i=j=0; i<(nKeys*2); i+=2, ++j ) { |
---|
647 | indices[j].key = val->val.l.vals[i].val.s.s; |
---|
648 | indices[j].index = i; |
---|
649 | } |
---|
650 | qsort( indices, j, sizeof(struct KeyIndex), compareKeyIndex ); |
---|
651 | for( i=0; i<j; ++i ) { |
---|
652 | const int index = indices[i].index; |
---|
653 | node->children[ node->childCount++ ] = index; |
---|
654 | node->children[ node->childCount++ ] = index + 1; |
---|
655 | } |
---|
656 | |
---|
657 | assert( node->childCount == nKeys * 2 ); |
---|
658 | tr_free( indices ); |
---|
659 | return node; |
---|
660 | } |
---|
661 | |
---|
662 | static struct SaveNode* |
---|
663 | nodeNewList( const tr_benc * val ) |
---|
664 | { |
---|
665 | int i, n; |
---|
666 | struct SaveNode * node; |
---|
667 | |
---|
668 | assert( tr_bencIsList( val ) ); |
---|
669 | |
---|
670 | n = val->val.l.count; |
---|
671 | node = tr_new0( struct SaveNode, 1 ); |
---|
672 | node->val = val; |
---|
673 | node->childCount = n; |
---|
674 | node->children = tr_new0( int, n ); |
---|
675 | for( i=0; i<n; ++i ) /* a list's children don't need to be reordered */ |
---|
676 | node->children[i] = i; |
---|
677 | |
---|
678 | return node; |
---|
679 | } |
---|
680 | |
---|
681 | static struct SaveNode* |
---|
682 | nodeNewLeaf( const tr_benc * val ) |
---|
683 | { |
---|
684 | struct SaveNode * node; |
---|
685 | |
---|
686 | assert( !isContainer( val ) ); |
---|
687 | |
---|
688 | node = tr_new0( struct SaveNode, 1 ); |
---|
689 | node->val = val; |
---|
690 | return node; |
---|
691 | } |
---|
692 | |
---|
693 | static struct SaveNode* |
---|
694 | nodeNew( const tr_benc * val ) |
---|
695 | { |
---|
696 | struct SaveNode * node; |
---|
697 | |
---|
698 | if( tr_bencIsList( val ) ) |
---|
699 | node = nodeNewList( val ); |
---|
700 | else if( tr_bencIsDict( val ) ) |
---|
701 | node = nodeNewDict( val ); |
---|
702 | else |
---|
703 | node = nodeNewLeaf( val ); |
---|
704 | |
---|
705 | return node; |
---|
706 | } |
---|
707 | |
---|
708 | typedef void (*BencWalkFunc)( const tr_benc * val, void * user_data ); |
---|
709 | |
---|
710 | struct WalkFuncs |
---|
711 | { |
---|
712 | BencWalkFunc intFunc; |
---|
713 | BencWalkFunc stringFunc; |
---|
714 | BencWalkFunc dictBeginFunc; |
---|
715 | BencWalkFunc listBeginFunc; |
---|
716 | BencWalkFunc containerEndFunc; |
---|
717 | }; |
---|
718 | |
---|
719 | /** |
---|
720 | * This function's previous recursive implementation was |
---|
721 | * easier to read, but was vulnerable to a smash-stacking |
---|
722 | * attack via maliciously-crafted bencoded data. (#667) |
---|
723 | */ |
---|
724 | static void |
---|
725 | bencWalk( const tr_benc * top, |
---|
726 | struct WalkFuncs * walkFuncs, |
---|
727 | void * user_data ) |
---|
728 | { |
---|
729 | tr_ptrArray * stack = tr_ptrArrayNew( ); |
---|
730 | tr_ptrArrayAppend( stack, nodeNew( top ) ); |
---|
731 | |
---|
732 | while( !tr_ptrArrayEmpty( stack ) ) |
---|
733 | { |
---|
734 | struct SaveNode * node = tr_ptrArrayBack( stack ); |
---|
735 | const tr_benc * val; |
---|
736 | |
---|
737 | if( !node->valIsVisited ) |
---|
738 | { |
---|
739 | val = node->val; |
---|
740 | node->valIsVisited = TRUE; |
---|
741 | } |
---|
742 | else if( node->childIndex < node->childCount ) |
---|
743 | { |
---|
744 | const int index = node->children[ node->childIndex++ ]; |
---|
745 | val = node->val->val.l.vals + index; |
---|
746 | } |
---|
747 | else /* done with this node */ |
---|
748 | { |
---|
749 | if( isContainer( node->val ) ) |
---|
750 | walkFuncs->containerEndFunc( node->val, user_data ); |
---|
751 | tr_ptrArrayPop( stack ); |
---|
752 | tr_free( node->children ); |
---|
753 | tr_free( node ); |
---|
754 | continue; |
---|
755 | } |
---|
756 | |
---|
757 | switch( val->type ) |
---|
758 | { |
---|
759 | case TYPE_INT: |
---|
760 | walkFuncs->intFunc( val, user_data ); |
---|
761 | break; |
---|
762 | |
---|
763 | case TYPE_STR: |
---|
764 | walkFuncs->stringFunc( val, user_data ); |
---|
765 | break; |
---|
766 | |
---|
767 | case TYPE_LIST: |
---|
768 | if( val != node->val ) |
---|
769 | tr_ptrArrayAppend( stack, nodeNew( val ) ); |
---|
770 | else |
---|
771 | walkFuncs->listBeginFunc( val, user_data ); |
---|
772 | break; |
---|
773 | |
---|
774 | case TYPE_DICT: |
---|
775 | if( val != node->val ) |
---|
776 | tr_ptrArrayAppend( stack, nodeNew( val ) ); |
---|
777 | else |
---|
778 | walkFuncs->dictBeginFunc( val, user_data ); |
---|
779 | break; |
---|
780 | |
---|
781 | default: |
---|
782 | /* did caller give us an uninitialized val? */ |
---|
783 | tr_err( _( "Invalid metadata" ) ); |
---|
784 | break; |
---|
785 | } |
---|
786 | } |
---|
787 | |
---|
788 | tr_ptrArrayFree( stack, NULL ); |
---|
789 | } |
---|
790 | |
---|
791 | /**** |
---|
792 | ***** |
---|
793 | ****/ |
---|
794 | |
---|
795 | static void |
---|
796 | saveIntFunc( const tr_benc * val, void * evbuf ) |
---|
797 | { |
---|
798 | evbuffer_add_printf( evbuf, "i%"PRId64"e", val->val.i ); |
---|
799 | } |
---|
800 | static void |
---|
801 | saveStringFunc( const tr_benc * val, void * vevbuf ) |
---|
802 | { |
---|
803 | struct evbuffer * evbuf = vevbuf; |
---|
804 | evbuffer_add_printf( evbuf, "%d:", val->val.s.i ); |
---|
805 | evbuffer_add( evbuf, val->val.s.s, val->val.s.i ); |
---|
806 | } |
---|
807 | static void |
---|
808 | saveDictBeginFunc( const tr_benc * val UNUSED, void * evbuf ) |
---|
809 | { |
---|
810 | evbuffer_add_printf( evbuf, "d" ); |
---|
811 | } |
---|
812 | static void |
---|
813 | saveListBeginFunc( const tr_benc * val UNUSED, void * evbuf ) |
---|
814 | { |
---|
815 | evbuffer_add_printf( evbuf, "l" ); |
---|
816 | } |
---|
817 | static void |
---|
818 | saveContainerEndFunc( const tr_benc * val UNUSED, void * evbuf ) |
---|
819 | { |
---|
820 | evbuffer_add_printf( evbuf, "e" ); |
---|
821 | } |
---|
822 | char* |
---|
823 | tr_bencSave( const tr_benc * top, int * len ) |
---|
824 | { |
---|
825 | char * ret; |
---|
826 | struct WalkFuncs walkFuncs; |
---|
827 | struct evbuffer * out = evbuffer_new( ); |
---|
828 | |
---|
829 | walkFuncs.intFunc = saveIntFunc; |
---|
830 | walkFuncs.stringFunc = saveStringFunc; |
---|
831 | walkFuncs.dictBeginFunc = saveDictBeginFunc; |
---|
832 | walkFuncs.listBeginFunc = saveListBeginFunc; |
---|
833 | walkFuncs.containerEndFunc = saveContainerEndFunc; |
---|
834 | bencWalk( top, &walkFuncs, out ); |
---|
835 | |
---|
836 | if( len != NULL ) |
---|
837 | *len = EVBUFFER_LENGTH( out ); |
---|
838 | ret = tr_strndup( (char*) EVBUFFER_DATA( out ), EVBUFFER_LENGTH( out ) ); |
---|
839 | evbuffer_free( out ); |
---|
840 | return ret; |
---|
841 | } |
---|
842 | |
---|
843 | /*** |
---|
844 | **** |
---|
845 | ***/ |
---|
846 | |
---|
847 | static void |
---|
848 | freeDummyFunc( const tr_benc * val UNUSED, void * buf UNUSED ) |
---|
849 | { |
---|
850 | } |
---|
851 | static void |
---|
852 | freeStringFunc( const tr_benc * val, void * freeme ) |
---|
853 | { |
---|
854 | if( !val->val.s.nofree ) |
---|
855 | tr_ptrArrayAppend( freeme, val->val.s.s ); |
---|
856 | } |
---|
857 | static void |
---|
858 | freeContainerBeginFunc( const tr_benc * val, void * freeme ) |
---|
859 | { |
---|
860 | tr_ptrArrayAppend( freeme, val->val.l.vals ); |
---|
861 | } |
---|
862 | void |
---|
863 | tr_bencFree( tr_benc * val ) |
---|
864 | { |
---|
865 | if( val != NULL ) |
---|
866 | { |
---|
867 | tr_ptrArray * freeme = tr_ptrArrayNew( ); |
---|
868 | struct WalkFuncs walkFuncs; |
---|
869 | |
---|
870 | walkFuncs.intFunc = freeDummyFunc; |
---|
871 | walkFuncs.stringFunc = freeStringFunc; |
---|
872 | walkFuncs.dictBeginFunc = freeContainerBeginFunc; |
---|
873 | walkFuncs.listBeginFunc = freeContainerBeginFunc; |
---|
874 | walkFuncs.containerEndFunc = freeDummyFunc; |
---|
875 | bencWalk( val, &walkFuncs, freeme ); |
---|
876 | |
---|
877 | tr_ptrArrayFree( freeme, tr_free ); |
---|
878 | } |
---|
879 | } |
---|
880 | |
---|
881 | /*** |
---|
882 | **** |
---|
883 | ***/ |
---|
884 | |
---|
885 | struct WalkPrint |
---|
886 | { |
---|
887 | int depth; |
---|
888 | FILE * out; |
---|
889 | }; |
---|
890 | static void |
---|
891 | printLeadingSpaces( struct WalkPrint * data ) |
---|
892 | { |
---|
893 | const int width = data->depth * 2; |
---|
894 | fprintf( data->out, "%*.*s", width, width, " " ); |
---|
895 | } |
---|
896 | static void |
---|
897 | printIntFunc( const tr_benc * val, void * vdata ) |
---|
898 | { |
---|
899 | struct WalkPrint * data = vdata; |
---|
900 | printLeadingSpaces( data ); |
---|
901 | fprintf( data->out, "int: %"PRId64"\n", val->val.i ); |
---|
902 | } |
---|
903 | static void |
---|
904 | printStringFunc( const tr_benc * val, void * vdata ) |
---|
905 | { |
---|
906 | int ii; |
---|
907 | struct WalkPrint * data = vdata; |
---|
908 | printLeadingSpaces( data ); |
---|
909 | fprintf( data->out, "string: " ); |
---|
910 | for( ii = 0; val->val.s.i > ii; ii++ ) |
---|
911 | { |
---|
912 | if( '\\' == val->val.s.s[ii] ) { |
---|
913 | putc( '\\', data->out ); |
---|
914 | putc( '\\', data->out ); |
---|
915 | } else if( isprint( val->val.s.s[ii] ) ) { |
---|
916 | putc( val->val.s.s[ii], data->out ); |
---|
917 | } else { |
---|
918 | fprintf( data->out, "\\x%02x", val->val.s.s[ii] ); |
---|
919 | } |
---|
920 | } |
---|
921 | fprintf( data->out, "\n" ); |
---|
922 | } |
---|
923 | static void |
---|
924 | printListBeginFunc( const tr_benc * val UNUSED, void * vdata ) |
---|
925 | { |
---|
926 | struct WalkPrint * data = vdata; |
---|
927 | printLeadingSpaces( data ); |
---|
928 | fprintf( data->out, "list\n" ); |
---|
929 | ++data->depth; |
---|
930 | } |
---|
931 | static void |
---|
932 | printDictBeginFunc( const tr_benc * val UNUSED, void * vdata ) |
---|
933 | { |
---|
934 | struct WalkPrint * data = vdata; |
---|
935 | printLeadingSpaces( data ); |
---|
936 | fprintf( data->out, "dict\n" ); |
---|
937 | ++data->depth; |
---|
938 | } |
---|
939 | static void |
---|
940 | printContainerEndFunc( const tr_benc * val UNUSED, void * vdata ) |
---|
941 | { |
---|
942 | struct WalkPrint * data = vdata; |
---|
943 | --data->depth; |
---|
944 | } |
---|
945 | void |
---|
946 | tr_bencPrint( const tr_benc * val ) |
---|
947 | { |
---|
948 | struct WalkFuncs walkFuncs; |
---|
949 | struct WalkPrint walkPrint; |
---|
950 | |
---|
951 | walkFuncs.intFunc = printIntFunc; |
---|
952 | walkFuncs.stringFunc = printStringFunc; |
---|
953 | walkFuncs.dictBeginFunc = printDictBeginFunc; |
---|
954 | walkFuncs.listBeginFunc = printListBeginFunc; |
---|
955 | walkFuncs.containerEndFunc = printContainerEndFunc; |
---|
956 | |
---|
957 | walkPrint.out = stderr; |
---|
958 | walkPrint.depth = 0; |
---|
959 | bencWalk( val, &walkFuncs, &walkPrint ); |
---|
960 | } |
---|
961 | |
---|
962 | /*** |
---|
963 | **** |
---|
964 | ***/ |
---|
965 | |
---|
966 | struct ParentState |
---|
967 | { |
---|
968 | int type; |
---|
969 | int index; |
---|
970 | }; |
---|
971 | |
---|
972 | struct phpWalk |
---|
973 | { |
---|
974 | tr_list * parents; |
---|
975 | struct evbuffer * out; |
---|
976 | }; |
---|
977 | |
---|
978 | static void |
---|
979 | phpChildFunc( struct phpWalk * data ) |
---|
980 | { |
---|
981 | if( data->parents ) |
---|
982 | { |
---|
983 | struct ParentState * parentState = data->parents->data; |
---|
984 | |
---|
985 | if( parentState->type == TYPE_LIST ) |
---|
986 | evbuffer_add_printf( data->out, "i:%d;", parentState->index++ ); |
---|
987 | } |
---|
988 | } |
---|
989 | |
---|
990 | static void |
---|
991 | phpPushParent( struct phpWalk * data, int type ) |
---|
992 | { |
---|
993 | struct ParentState * parentState = tr_new( struct ParentState, 1 ); |
---|
994 | parentState->type = type; |
---|
995 | parentState->index = 0; |
---|
996 | tr_list_prepend( &data->parents, parentState ); |
---|
997 | } |
---|
998 | |
---|
999 | static void |
---|
1000 | phpPopParent( struct phpWalk * data ) |
---|
1001 | { |
---|
1002 | tr_free( tr_list_pop_front( &data->parents ) ); |
---|
1003 | } |
---|
1004 | |
---|
1005 | static void |
---|
1006 | phpIntFunc( const tr_benc * val, void * vdata ) |
---|
1007 | { |
---|
1008 | struct phpWalk * data = vdata; |
---|
1009 | phpChildFunc( data ); |
---|
1010 | evbuffer_add_printf( data->out, "i:%"PRId64";", val->val.i ); |
---|
1011 | } |
---|
1012 | static void |
---|
1013 | phpStringFunc( const tr_benc * val, void * vdata ) |
---|
1014 | { |
---|
1015 | struct phpWalk * data = vdata; |
---|
1016 | phpChildFunc( data ); |
---|
1017 | evbuffer_add_printf( data->out, "s:%d:\"%s\";", val->val.s.i, val->val.s.s ); |
---|
1018 | } |
---|
1019 | static void |
---|
1020 | phpDictBeginFunc( const tr_benc * val, void * vdata ) |
---|
1021 | { |
---|
1022 | struct phpWalk * data = vdata; |
---|
1023 | phpChildFunc( data ); |
---|
1024 | phpPushParent( data, TYPE_DICT ); |
---|
1025 | evbuffer_add_printf( data->out, "a:%d:{", val->val.l.count/2 ); |
---|
1026 | } |
---|
1027 | static void |
---|
1028 | phpListBeginFunc( const tr_benc * val, void * vdata ) |
---|
1029 | { |
---|
1030 | struct phpWalk * data = vdata; |
---|
1031 | phpChildFunc( data ); |
---|
1032 | phpPushParent( data, TYPE_LIST ); |
---|
1033 | evbuffer_add_printf( data->out, "a:%d:{", val->val.l.count ); |
---|
1034 | } |
---|
1035 | static void |
---|
1036 | phpContainerEndFunc( const tr_benc * val UNUSED, void * vdata ) |
---|
1037 | { |
---|
1038 | struct phpWalk * data = vdata; |
---|
1039 | phpPopParent( data ); |
---|
1040 | evbuffer_add_printf( data->out, "}" ); |
---|
1041 | } |
---|
1042 | char* |
---|
1043 | tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len ) |
---|
1044 | { |
---|
1045 | char * ret; |
---|
1046 | struct WalkFuncs walkFuncs; |
---|
1047 | struct phpWalk data; |
---|
1048 | |
---|
1049 | data.out = evbuffer_new( ); |
---|
1050 | data.parents = NULL; |
---|
1051 | |
---|
1052 | walkFuncs.intFunc = phpIntFunc; |
---|
1053 | walkFuncs.stringFunc = phpStringFunc; |
---|
1054 | walkFuncs.dictBeginFunc = phpDictBeginFunc; |
---|
1055 | walkFuncs.listBeginFunc = phpListBeginFunc; |
---|
1056 | walkFuncs.containerEndFunc = phpContainerEndFunc; |
---|
1057 | |
---|
1058 | bencWalk( top, &walkFuncs, &data ); |
---|
1059 | |
---|
1060 | if( len != NULL ) |
---|
1061 | *len = EVBUFFER_LENGTH( data.out ); |
---|
1062 | ret = tr_strndup( (char*) EVBUFFER_DATA( data.out ), EVBUFFER_LENGTH( data.out ) ); |
---|
1063 | evbuffer_free( data.out ); |
---|
1064 | return ret; |
---|
1065 | } |
---|
1066 | |
---|
1067 | /*** |
---|
1068 | **** |
---|
1069 | ***/ |
---|
1070 | |
---|
1071 | int |
---|
1072 | tr_bencSaveFile( const char * filename, const tr_benc * b ) |
---|
1073 | { |
---|
1074 | int err = TR_OK; |
---|
1075 | int len; |
---|
1076 | char * content = tr_bencSave( b, &len ); |
---|
1077 | FILE * out = NULL; |
---|
1078 | |
---|
1079 | out = fopen( filename, "wb+" ); |
---|
1080 | if( !out ) |
---|
1081 | { |
---|
1082 | tr_err( _( "Couldn't open \"%1$s\": %2$s" ), |
---|
1083 | filename, tr_strerror( errno ) ); |
---|
1084 | err = TR_EINVALID; |
---|
1085 | } |
---|
1086 | else if( fwrite( content, sizeof( char ), len, out ) != (size_t)len ) |
---|
1087 | { |
---|
1088 | tr_err( _( "Couldn't save file \"%1$s\": %2$s" ), |
---|
1089 | filename, tr_strerror( errno ) ); |
---|
1090 | err = TR_EINVALID; |
---|
1091 | } |
---|
1092 | |
---|
1093 | if( !err ) |
---|
1094 | tr_dbg( "tr_bencSaveFile saved \"%s\"", filename ); |
---|
1095 | tr_free( content ); |
---|
1096 | if( out ) |
---|
1097 | fclose( out ); |
---|
1098 | return err; |
---|
1099 | } |
---|
1100 | |
---|
1101 | int |
---|
1102 | tr_bencLoadFile( const char * filename, tr_benc * b ) |
---|
1103 | { |
---|
1104 | int ret = TR_ERROR; |
---|
1105 | size_t contentLen; |
---|
1106 | uint8_t * content = tr_loadFile( filename, &contentLen ); |
---|
1107 | ret = content ? tr_bencLoad( content, contentLen, b, NULL ) |
---|
1108 | : TR_ERROR_IO_OTHER; |
---|
1109 | tr_free( content ); |
---|
1110 | return ret; |
---|
1111 | } |
---|