1 | /****************************************************************************** |
---|
2 | * $Id: metainfo.c 1613 2007-03-31 19:10:32Z joshe $ |
---|
3 | * |
---|
4 | * Copyright (c) 2005-2007 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 "transmission.h" |
---|
26 | |
---|
27 | #define TORRENT_MAX_SIZE (5*1024*1024) |
---|
28 | |
---|
29 | /*********************************************************************** |
---|
30 | * Local prototypes |
---|
31 | **********************************************************************/ |
---|
32 | static void savedname( char * name, size_t len, const char * hash, |
---|
33 | const char * tag ); |
---|
34 | static char * readtorrent( const char * path, const char * hash, |
---|
35 | const char * tag, size_t * len ); |
---|
36 | static int savetorrent( const char * hash, const char * tag, |
---|
37 | const uint8_t * buf, size_t buflen ); |
---|
38 | static int getfile( char * buf, int size, |
---|
39 | const char * prefix, benc_val_t * name ); |
---|
40 | static int getannounce( tr_info_t * inf, benc_val_t * meta ); |
---|
41 | static char * announceToScrape( const char * announce ); |
---|
42 | static int parseFiles( tr_info_t * inf, benc_val_t * name, |
---|
43 | benc_val_t * files, benc_val_t * length ); |
---|
44 | static void strcatUTF8( char *, int, const char *, int ); |
---|
45 | |
---|
46 | /*********************************************************************** |
---|
47 | * tr_metainfoParse |
---|
48 | *********************************************************************** |
---|
49 | * |
---|
50 | **********************************************************************/ |
---|
51 | int tr_metainfoParse( tr_info_t * inf, const char * tag, const char * path, |
---|
52 | const char * savedHash, int saveCopy ) |
---|
53 | { |
---|
54 | char * buf; |
---|
55 | benc_val_t meta, * beInfo, * val, * val2; |
---|
56 | int i; |
---|
57 | size_t len; |
---|
58 | |
---|
59 | assert( NULL == path || NULL == savedHash ); |
---|
60 | /* if savedHash isn't null, saveCopy should be false */ |
---|
61 | assert( NULL == savedHash || !saveCopy ); |
---|
62 | |
---|
63 | /* read the torrent data */ |
---|
64 | buf = readtorrent( path, savedHash, tag, &len ); |
---|
65 | if( NULL == buf ) |
---|
66 | { |
---|
67 | return 1; |
---|
68 | } |
---|
69 | |
---|
70 | /* Parse bencoded infos */ |
---|
71 | if( tr_bencLoad( buf, len, &meta, NULL ) ) |
---|
72 | { |
---|
73 | tr_err( "Error while parsing bencoded data" ); |
---|
74 | free( buf ); |
---|
75 | return 1; |
---|
76 | } |
---|
77 | |
---|
78 | /* Get info hash */ |
---|
79 | beInfo = tr_bencDictFind( &meta, "info" ); |
---|
80 | if( NULL == beInfo || TYPE_DICT != beInfo->type ) |
---|
81 | { |
---|
82 | tr_err( "%s \"info\" dictionary", ( beInfo ? "Invalid" : "Missing" ) ); |
---|
83 | tr_bencFree( &meta ); |
---|
84 | free( buf ); |
---|
85 | return 1; |
---|
86 | } |
---|
87 | SHA1( (uint8_t *) beInfo->begin, |
---|
88 | (long) beInfo->end - (long) beInfo->begin, inf->hash ); |
---|
89 | for( i = 0; i < SHA_DIGEST_LENGTH; i++ ) |
---|
90 | { |
---|
91 | snprintf( inf->hashString + i * 2, sizeof( inf->hashString ) - i * 2, |
---|
92 | "%02x", inf->hash[i] ); |
---|
93 | } |
---|
94 | |
---|
95 | /* Save a copy of the torrent file */ |
---|
96 | if( saveCopy ) |
---|
97 | { |
---|
98 | if( savetorrent( inf->hashString, tag, (uint8_t *) buf, len ) ) |
---|
99 | { |
---|
100 | tr_bencFree( &meta ); |
---|
101 | free( buf ); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /* We won't need this anymore */ |
---|
107 | free( buf ); |
---|
108 | |
---|
109 | /* Torrent file name */ |
---|
110 | if( NULL == path || saveCopy ) |
---|
111 | { |
---|
112 | savedname( inf->torrent, sizeof inf->torrent, inf->hashString, tag ); |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | snprintf( inf->torrent, MAX_PATH_LENGTH, "%s", path ); |
---|
117 | } |
---|
118 | |
---|
119 | /* Comment info */ |
---|
120 | val = tr_bencDictFindFirst( &meta, "comment.utf-8", "comment", NULL ); |
---|
121 | if( NULL != val && TYPE_STR == val->type ) |
---|
122 | { |
---|
123 | strcatUTF8( inf->comment, sizeof( inf->comment ), val->val.s.s, 0 ); |
---|
124 | } |
---|
125 | |
---|
126 | /* Creator info */ |
---|
127 | val = tr_bencDictFindFirst( &meta, "created by.utf-8", "created by", NULL ); |
---|
128 | if( NULL != val && TYPE_STR == val->type ) |
---|
129 | { |
---|
130 | strcatUTF8( inf->creator, sizeof( inf->creator ), val->val.s.s, 0 ); |
---|
131 | } |
---|
132 | |
---|
133 | /* Date created */ |
---|
134 | inf->dateCreated = 0; |
---|
135 | val = tr_bencDictFind( &meta, "creation date" ); |
---|
136 | if( NULL != val && TYPE_INT == val->type ) |
---|
137 | { |
---|
138 | inf->dateCreated = val->val.i; |
---|
139 | } |
---|
140 | |
---|
141 | /* Private torrent */ |
---|
142 | val = tr_bencDictFind( beInfo, "private" ); |
---|
143 | val2 = tr_bencDictFind( &meta, "private" ); |
---|
144 | if( ( NULL != val && ( TYPE_INT != val->type || 0 != val->val.i ) ) || |
---|
145 | ( NULL != val2 && ( TYPE_INT != val2->type || 0 != val2->val.i ) ) ) |
---|
146 | { |
---|
147 | inf->flags |= TR_FLAG_PRIVATE; |
---|
148 | } |
---|
149 | |
---|
150 | /* Piece length */ |
---|
151 | val = tr_bencDictFind( beInfo, "piece length" ); |
---|
152 | if( NULL == val || TYPE_INT != val->type ) |
---|
153 | { |
---|
154 | tr_err( "%s \"piece length\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
155 | goto fail; |
---|
156 | } |
---|
157 | inf->pieceSize = val->val.i; |
---|
158 | |
---|
159 | /* Hashes */ |
---|
160 | val = tr_bencDictFind( beInfo, "pieces" ); |
---|
161 | if( NULL == val || TYPE_STR != val->type ) |
---|
162 | { |
---|
163 | tr_err( "%s \"pieces\" entry", ( val ? "Invalid" : "Missing" ) ); |
---|
164 | goto fail; |
---|
165 | } |
---|
166 | if( val->val.s.i % SHA_DIGEST_LENGTH ) |
---|
167 | { |
---|
168 | tr_err( "Invalid \"piece\" string (size is %d)", val->val.s.i ); |
---|
169 | goto fail; |
---|
170 | } |
---|
171 | inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH; |
---|
172 | inf->pieces = (uint8_t *) tr_bencStealStr( val ); |
---|
173 | |
---|
174 | /* TODO add more tests so we don't crash on weird files */ |
---|
175 | |
---|
176 | /* get file or top directory name */ |
---|
177 | val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL ); |
---|
178 | if( parseFiles( inf, tr_bencDictFindFirst( beInfo, |
---|
179 | "name.utf-8", "name", NULL ), |
---|
180 | tr_bencDictFind( beInfo, "files" ), |
---|
181 | tr_bencDictFind( beInfo, "length" ) ) ) |
---|
182 | { |
---|
183 | goto fail; |
---|
184 | } |
---|
185 | |
---|
186 | if( (uint64_t) inf->pieceCount != |
---|
187 | ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize ) |
---|
188 | { |
---|
189 | tr_err( "Size of hashes and files don't match" ); |
---|
190 | goto fail; |
---|
191 | } |
---|
192 | |
---|
193 | /* get announce or announce-list */ |
---|
194 | if( getannounce( inf, &meta ) ) |
---|
195 | { |
---|
196 | goto fail; |
---|
197 | } |
---|
198 | |
---|
199 | tr_bencFree( &meta ); |
---|
200 | return 0; |
---|
201 | |
---|
202 | fail: |
---|
203 | tr_metainfoFree( inf ); |
---|
204 | tr_bencFree( &meta ); |
---|
205 | return 1; |
---|
206 | } |
---|
207 | |
---|
208 | void tr_metainfoFree( tr_info_t * inf ) |
---|
209 | { |
---|
210 | int ii, jj; |
---|
211 | |
---|
212 | free( inf->pieces ); |
---|
213 | free( inf->files ); |
---|
214 | |
---|
215 | for( ii = 0; ii < inf->trackerTiers; ii++ ) |
---|
216 | { |
---|
217 | for( jj = 0; jj < inf->trackerList[ii].count; jj++ ) |
---|
218 | { |
---|
219 | free( inf->trackerList[ii].list[jj].address ); |
---|
220 | free( inf->trackerList[ii].list[jj].announce ); |
---|
221 | free( inf->trackerList[ii].list[jj].scrape ); |
---|
222 | } |
---|
223 | free( inf->trackerList[ii].list ); |
---|
224 | } |
---|
225 | free( inf->trackerList ); |
---|
226 | } |
---|
227 | |
---|
228 | static int getfile( char * buf, int size, |
---|
229 | const char * prefix, benc_val_t * name ) |
---|
230 | { |
---|
231 | benc_val_t * dir; |
---|
232 | const char ** list; |
---|
233 | int ii, jj; |
---|
234 | |
---|
235 | if( TYPE_LIST != name->type ) |
---|
236 | { |
---|
237 | return 1; |
---|
238 | } |
---|
239 | |
---|
240 | list = calloc( name->val.l.count, sizeof( list[0] ) ); |
---|
241 | if( NULL == list ) |
---|
242 | { |
---|
243 | return 1; |
---|
244 | } |
---|
245 | |
---|
246 | for( ii = jj = 0; name->val.l.count > ii; ii++ ) |
---|
247 | { |
---|
248 | dir = &name->val.l.vals[ii]; |
---|
249 | if( TYPE_STR != dir->type ) |
---|
250 | { |
---|
251 | continue; |
---|
252 | } |
---|
253 | if( 0 == strcmp( "..", dir->val.s.s ) ) |
---|
254 | { |
---|
255 | if( 0 < jj ) |
---|
256 | { |
---|
257 | jj--; |
---|
258 | } |
---|
259 | } |
---|
260 | else if( 0 != strcmp( ".", dir->val.s.s ) ) |
---|
261 | { |
---|
262 | list[jj] = dir->val.s.s; |
---|
263 | jj++; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | if( 0 == jj ) |
---|
268 | { |
---|
269 | return 1; |
---|
270 | } |
---|
271 | |
---|
272 | strcatUTF8( buf, size, prefix, 0 ); |
---|
273 | for( ii = 0; jj > ii; ii++ ) |
---|
274 | { |
---|
275 | strcatUTF8( buf, size, "/", 0 ); |
---|
276 | strcatUTF8( buf, size, list[ii], 1 ); |
---|
277 | } |
---|
278 | free( list ); |
---|
279 | |
---|
280 | return 0; |
---|
281 | } |
---|
282 | |
---|
283 | static int getannounce( tr_info_t * inf, benc_val_t * meta ) |
---|
284 | { |
---|
285 | benc_val_t * val, * subval, * urlval; |
---|
286 | char * address, * announce; |
---|
287 | int ii, jj, port, random, subcount; |
---|
288 | tr_tracker_info_t * sublist; |
---|
289 | void * swapping; |
---|
290 | |
---|
291 | /* Announce-list */ |
---|
292 | val = tr_bencDictFind( meta, "announce-list" ); |
---|
293 | if( NULL != val && TYPE_LIST == val->type && 0 < val->val.l.count ) |
---|
294 | { |
---|
295 | inf->trackerTiers = 0; |
---|
296 | inf->trackerList = calloc( sizeof( inf->trackerList[0] ), |
---|
297 | val->val.l.count ); |
---|
298 | |
---|
299 | /* iterate through the announce-list's tiers */ |
---|
300 | for( ii = 0; ii < val->val.l.count; ii++ ) |
---|
301 | { |
---|
302 | subval = &val->val.l.vals[ii]; |
---|
303 | if( TYPE_LIST != subval->type || 0 >= subval->val.l.count ) |
---|
304 | { |
---|
305 | continue; |
---|
306 | } |
---|
307 | subcount = 0; |
---|
308 | sublist = calloc( sizeof( sublist[0] ), subval->val.l.count ); |
---|
309 | |
---|
310 | /* iterate through the tier's items */ |
---|
311 | for( jj = 0; jj < subval->val.l.count; jj++ ) |
---|
312 | { |
---|
313 | urlval = &subval->val.l.vals[jj]; |
---|
314 | if( TYPE_STR != urlval->type || |
---|
315 | tr_httpParseUrl( urlval->val.s.s, urlval->val.s.i, |
---|
316 | &address, &port, &announce ) ) |
---|
317 | { |
---|
318 | continue; |
---|
319 | } |
---|
320 | |
---|
321 | /* place the item info in a random location in the sublist */ |
---|
322 | random = tr_rand( subcount + 1 ); |
---|
323 | if( random != subcount ) |
---|
324 | { |
---|
325 | sublist[subcount] = sublist[random]; |
---|
326 | } |
---|
327 | sublist[random].address = address; |
---|
328 | sublist[random].port = port; |
---|
329 | sublist[random].announce = announce; |
---|
330 | sublist[random].scrape = announceToScrape( announce ); |
---|
331 | subcount++; |
---|
332 | } |
---|
333 | |
---|
334 | /* just use sublist as-is if it's full */ |
---|
335 | if( subcount == subval->val.l.count ) |
---|
336 | { |
---|
337 | inf->trackerList[inf->trackerTiers].list = sublist; |
---|
338 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
339 | inf->trackerTiers++; |
---|
340 | } |
---|
341 | /* if we skipped some of the tier's items then trim the sublist */ |
---|
342 | else if( 0 < subcount ) |
---|
343 | { |
---|
344 | inf->trackerList[inf->trackerTiers].list = calloc( sizeof( sublist[0] ), subcount ); |
---|
345 | memcpy( inf->trackerList[inf->trackerTiers].list, sublist, |
---|
346 | sizeof( sublist[0] ) * subcount ); |
---|
347 | inf->trackerList[inf->trackerTiers].count = subcount; |
---|
348 | inf->trackerTiers++; |
---|
349 | free( sublist ); |
---|
350 | } |
---|
351 | /* drop the whole sublist if we didn't use any items at all */ |
---|
352 | else |
---|
353 | { |
---|
354 | free( sublist ); |
---|
355 | } |
---|
356 | } |
---|
357 | |
---|
358 | /* did we use any of the tiers? */ |
---|
359 | if( 0 == inf->trackerTiers ) |
---|
360 | { |
---|
361 | tr_inf( "No valid entries in \"announce-list\"" ); |
---|
362 | free( inf->trackerList ); |
---|
363 | inf->trackerList = NULL; |
---|
364 | } |
---|
365 | /* trim unused sublist pointers */ |
---|
366 | else if( inf->trackerTiers < val->val.l.count ) |
---|
367 | { |
---|
368 | swapping = inf->trackerList; |
---|
369 | inf->trackerList = calloc( sizeof( inf->trackerList[0] ), |
---|
370 | inf->trackerTiers ); |
---|
371 | memcpy( inf->trackerList, swapping, |
---|
372 | sizeof( inf->trackerList[0] ) * inf->trackerTiers ); |
---|
373 | free( swapping ); |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | /* Regular announce value */ |
---|
378 | if( 0 == inf->trackerTiers ) |
---|
379 | { |
---|
380 | val = tr_bencDictFind( meta, "announce" ); |
---|
381 | if( NULL == val || TYPE_STR != val->type ) |
---|
382 | { |
---|
383 | tr_err( "No \"announce\" entry" ); |
---|
384 | return 1; |
---|
385 | } |
---|
386 | |
---|
387 | if( tr_httpParseUrl( val->val.s.s, val->val.s.i, |
---|
388 | &address, &port, &announce ) ) |
---|
389 | { |
---|
390 | tr_err( "Invalid announce URL (%s)", val->val.s.s ); |
---|
391 | return 1; |
---|
392 | } |
---|
393 | |
---|
394 | sublist = calloc( sizeof( sublist[0] ), 1 ); |
---|
395 | sublist[0].address = address; |
---|
396 | sublist[0].port = port; |
---|
397 | sublist[0].announce = announce; |
---|
398 | sublist[0].scrape = announceToScrape( announce ); |
---|
399 | inf->trackerList = calloc( sizeof( inf->trackerList[0] ), 1 ); |
---|
400 | inf->trackerList[0].list = sublist; |
---|
401 | inf->trackerList[0].count = 1; |
---|
402 | inf->trackerTiers = 1; |
---|
403 | } |
---|
404 | |
---|
405 | return 0; |
---|
406 | } |
---|
407 | |
---|
408 | static char * announceToScrape( const char * announce ) |
---|
409 | { |
---|
410 | char old[] = "announce"; |
---|
411 | int oldlen = 8; |
---|
412 | char new[] = "scrape"; |
---|
413 | int newlen = 6; |
---|
414 | char * slash, * scrape; |
---|
415 | size_t scrapelen, used; |
---|
416 | |
---|
417 | slash = strrchr( announce, '/' ); |
---|
418 | if( NULL == slash ) |
---|
419 | { |
---|
420 | return NULL; |
---|
421 | } |
---|
422 | slash++; |
---|
423 | |
---|
424 | if( 0 != strncmp( slash, old, oldlen ) ) |
---|
425 | { |
---|
426 | return NULL; |
---|
427 | } |
---|
428 | |
---|
429 | scrapelen = strlen( announce ) - oldlen + newlen; |
---|
430 | scrape = calloc( scrapelen + 1, 1 ); |
---|
431 | if( NULL == scrape ) |
---|
432 | { |
---|
433 | return NULL; |
---|
434 | } |
---|
435 | assert( ( size_t )( slash - announce ) < scrapelen ); |
---|
436 | memcpy( scrape, announce, slash - announce ); |
---|
437 | used = slash - announce; |
---|
438 | strncat( scrape, new, scrapelen - used ); |
---|
439 | used += newlen; |
---|
440 | assert( strlen( scrape ) == used ); |
---|
441 | if( used < scrapelen ) |
---|
442 | { |
---|
443 | assert( strlen( slash + oldlen ) == scrapelen - used ); |
---|
444 | strncat( scrape, slash + oldlen, scrapelen - used ); |
---|
445 | } |
---|
446 | |
---|
447 | return scrape; |
---|
448 | } |
---|
449 | |
---|
450 | void savedname( char * name, size_t len, const char * hash, const char * tag ) |
---|
451 | { |
---|
452 | if( NULL == tag ) |
---|
453 | { |
---|
454 | snprintf( name, len, "%s/%s", tr_getTorrentsDirectory(), hash ); |
---|
455 | } |
---|
456 | else |
---|
457 | { |
---|
458 | snprintf( name, len, "%s/%s-%s", |
---|
459 | tr_getTorrentsDirectory(), hash, tag ); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | void tr_metainfoRemoveSaved( const char * hashString, const char * tag ) |
---|
464 | { |
---|
465 | char file[MAX_PATH_LENGTH]; |
---|
466 | |
---|
467 | savedname( file, sizeof file, hashString, tag ); |
---|
468 | unlink(file); |
---|
469 | } |
---|
470 | |
---|
471 | char * readtorrent( const char * path, const char * hash, const char * tag, |
---|
472 | size_t * len ) |
---|
473 | { |
---|
474 | char hashpath[MAX_PATH_LENGTH], * buf; |
---|
475 | struct stat sb; |
---|
476 | FILE * file; |
---|
477 | int lasterr, save; |
---|
478 | |
---|
479 | if( NULL != hash ) |
---|
480 | { |
---|
481 | savedname( hashpath, sizeof hashpath, hash, tag ); |
---|
482 | path = hashpath; |
---|
483 | } |
---|
484 | |
---|
485 | /* try to stat the file */ |
---|
486 | save = 0; |
---|
487 | if( stat( path, &sb ) ) |
---|
488 | { |
---|
489 | if( ENOENT != errno || NULL == hash ) |
---|
490 | { |
---|
491 | tr_err( "Could not stat file (%s)", path ); |
---|
492 | return NULL; |
---|
493 | } |
---|
494 | /* it doesn't exist, check for an old file without a tag */ |
---|
495 | /* XXX this should go away at some point */ |
---|
496 | lasterr = errno; |
---|
497 | savedname( hashpath, sizeof hashpath, hash, NULL ); |
---|
498 | if( stat( path, &sb ) ) |
---|
499 | { |
---|
500 | savedname( hashpath, sizeof hashpath, hash, tag ); |
---|
501 | errno = lasterr; |
---|
502 | tr_err( "Could not stat file (%s)", path ); |
---|
503 | return NULL; |
---|
504 | } |
---|
505 | save = 1; |
---|
506 | } |
---|
507 | |
---|
508 | if( ( sb.st_mode & S_IFMT ) != S_IFREG ) |
---|
509 | { |
---|
510 | tr_err( "Not a regular file (%s)", path ); |
---|
511 | return NULL; |
---|
512 | } |
---|
513 | if( sb.st_size > TORRENT_MAX_SIZE ) |
---|
514 | { |
---|
515 | tr_err( "Torrent file is too big (%d bytes)", ( int )sb.st_size ); |
---|
516 | return NULL; |
---|
517 | } |
---|
518 | |
---|
519 | /* Load the torrent file into our buffer */ |
---|
520 | file = fopen( path, "rb" ); |
---|
521 | if( !file ) |
---|
522 | { |
---|
523 | tr_err( "Could not open file (%s)", path ); |
---|
524 | return NULL; |
---|
525 | } |
---|
526 | buf = malloc( sb.st_size ); |
---|
527 | if( NULL == buf ) |
---|
528 | { |
---|
529 | tr_err( "Could not allocate memory (%d bytes)", ( int )sb.st_size ); |
---|
530 | } |
---|
531 | fseek( file, 0, SEEK_SET ); |
---|
532 | if( fread( buf, sb.st_size, 1, file ) != 1 ) |
---|
533 | { |
---|
534 | tr_err( "Read error (%s)", path ); |
---|
535 | free( buf ); |
---|
536 | fclose( file ); |
---|
537 | return NULL; |
---|
538 | } |
---|
539 | fclose( file ); |
---|
540 | |
---|
541 | /* save a new tagged copy of the old untagged torrent */ |
---|
542 | if( save ) |
---|
543 | { |
---|
544 | savetorrent( hash, tag, (uint8_t *) buf, sb.st_size ); |
---|
545 | } |
---|
546 | |
---|
547 | *len = sb.st_size; |
---|
548 | |
---|
549 | return buf; |
---|
550 | } |
---|
551 | |
---|
552 | /* Save a copy of the torrent file in the saved torrent directory */ |
---|
553 | int savetorrent( const char * hash, const char * tag, |
---|
554 | const uint8_t * buf, size_t buflen ) |
---|
555 | { |
---|
556 | char path[MAX_PATH_LENGTH]; |
---|
557 | FILE * file; |
---|
558 | |
---|
559 | savedname( path, sizeof path, hash, tag ); |
---|
560 | file = fopen( path, "wb" ); |
---|
561 | if( !file ) |
---|
562 | { |
---|
563 | tr_err( "Could not open file (%s) (%s)", path, strerror( errno ) ); |
---|
564 | return 1; |
---|
565 | } |
---|
566 | fseek( file, 0, SEEK_SET ); |
---|
567 | if( fwrite( buf, 1, buflen, file ) != buflen ) |
---|
568 | { |
---|
569 | tr_err( "Could not write file (%s) (%s)", path, strerror( errno ) ); |
---|
570 | fclose( file ); |
---|
571 | return 1; |
---|
572 | } |
---|
573 | fclose( file ); |
---|
574 | |
---|
575 | return 0; |
---|
576 | } |
---|
577 | |
---|
578 | int |
---|
579 | parseFiles( tr_info_t * inf, benc_val_t * name, |
---|
580 | benc_val_t * files, benc_val_t * length ) |
---|
581 | { |
---|
582 | benc_val_t * item, * path; |
---|
583 | int ii; |
---|
584 | |
---|
585 | if( NULL == name || TYPE_STR != name->type ) |
---|
586 | { |
---|
587 | tr_err( "%s \"name\" string", ( name ? "Invalid" : "Missing" ) ); |
---|
588 | return 1; |
---|
589 | } |
---|
590 | |
---|
591 | strcatUTF8( inf->name, sizeof( inf->name ), name->val.s.s, 1 ); |
---|
592 | if( '\0' == inf->name[0] ) |
---|
593 | { |
---|
594 | tr_err( "Invalid \"name\" string" ); |
---|
595 | return 1; |
---|
596 | } |
---|
597 | inf->totalSize = 0; |
---|
598 | |
---|
599 | if( files && TYPE_LIST == files->type ) |
---|
600 | { |
---|
601 | /* Multi-file mode */ |
---|
602 | inf->multifile = 1; |
---|
603 | inf->fileCount = files->val.l.count; |
---|
604 | inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) ); |
---|
605 | |
---|
606 | if( NULL == inf->files ) |
---|
607 | { |
---|
608 | return 1; |
---|
609 | } |
---|
610 | |
---|
611 | for( ii = 0; files->val.l.count > ii; ii++ ) |
---|
612 | { |
---|
613 | item = &files->val.l.vals[ii]; |
---|
614 | path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL ); |
---|
615 | if( getfile( inf->files[ii].name, sizeof( inf->files[0].name ), |
---|
616 | inf->name, path ) ) |
---|
617 | { |
---|
618 | tr_err( "%s \"path\" entry", |
---|
619 | ( path ? "Invalid" : "Missing" ) ); |
---|
620 | return 1; |
---|
621 | } |
---|
622 | length = tr_bencDictFind( item, "length" ); |
---|
623 | if( NULL == length || TYPE_INT != length->type ) |
---|
624 | { |
---|
625 | tr_err( "%s \"length\" entry", |
---|
626 | ( length ? "Invalid" : "Missing" ) ); |
---|
627 | return 1; |
---|
628 | } |
---|
629 | inf->files[ii].length = length->val.i; |
---|
630 | inf->totalSize += length->val.i; |
---|
631 | } |
---|
632 | } |
---|
633 | else if( NULL != length && TYPE_INT == length->type ) |
---|
634 | { |
---|
635 | /* Single-file mode */ |
---|
636 | inf->multifile = 0; |
---|
637 | inf->fileCount = 1; |
---|
638 | inf->files = calloc( 1, sizeof( inf->files[0] ) ); |
---|
639 | |
---|
640 | if( NULL == inf->files ) |
---|
641 | { |
---|
642 | return 1; |
---|
643 | } |
---|
644 | |
---|
645 | strcatUTF8( inf->files[0].name, sizeof( inf->files[0].name ), |
---|
646 | name->val.s.s, 1 ); |
---|
647 | |
---|
648 | inf->files[0].length = length->val.i; |
---|
649 | inf->totalSize += length->val.i; |
---|
650 | } |
---|
651 | else |
---|
652 | { |
---|
653 | tr_err( "%s \"files\" entry and %s \"length\" entry", |
---|
654 | ( files ? "Invalid" : "Missing" ), |
---|
655 | ( length ? "invalid" : "missing" ) ); |
---|
656 | } |
---|
657 | |
---|
658 | return 0; |
---|
659 | } |
---|
660 | |
---|
661 | /*********************************************************************** |
---|
662 | * strcatUTF8 |
---|
663 | *********************************************************************** |
---|
664 | * According to the official specification, all strings in the torrent |
---|
665 | * file are supposed to be UTF-8 encoded. However, there are |
---|
666 | * non-compliant torrents around... If we encounter an invalid UTF-8 |
---|
667 | * character, we assume it is ISO 8859-1 and convert it to UTF-8. |
---|
668 | **********************************************************************/ |
---|
669 | #define WANTBYTES( want, got ) \ |
---|
670 | if( (want) > (got) ) { return; } else { (got) -= (want); } |
---|
671 | static void strcatUTF8( char * s, int len, const char * append, int deslash ) |
---|
672 | { |
---|
673 | const char * p; |
---|
674 | |
---|
675 | /* don't overwrite the nul at the end */ |
---|
676 | len--; |
---|
677 | |
---|
678 | /* Go to the end of the destination string */ |
---|
679 | while( s[0] ) |
---|
680 | { |
---|
681 | s++; |
---|
682 | len--; |
---|
683 | } |
---|
684 | |
---|
685 | /* Now start appending, converting on the fly if necessary */ |
---|
686 | for( p = append; p[0]; ) |
---|
687 | { |
---|
688 | /* skip over / if requested */ |
---|
689 | if( deslash && '/' == p[0] ) |
---|
690 | { |
---|
691 | p++; |
---|
692 | continue; |
---|
693 | } |
---|
694 | |
---|
695 | if( !( p[0] & 0x80 ) ) |
---|
696 | { |
---|
697 | /* ASCII character */ |
---|
698 | WANTBYTES( 1, len ); |
---|
699 | *(s++) = *(p++); |
---|
700 | continue; |
---|
701 | } |
---|
702 | |
---|
703 | if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 ) |
---|
704 | { |
---|
705 | /* 2-bytes UTF-8 character */ |
---|
706 | WANTBYTES( 2, len ); |
---|
707 | *(s++) = *(p++); *(s++) = *(p++); |
---|
708 | continue; |
---|
709 | } |
---|
710 | |
---|
711 | if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
712 | ( p[2] & 0xC0 ) == 0x80 ) |
---|
713 | { |
---|
714 | /* 3-bytes UTF-8 character */ |
---|
715 | WANTBYTES( 3, len ); |
---|
716 | *(s++) = *(p++); *(s++) = *(p++); |
---|
717 | *(s++) = *(p++); |
---|
718 | continue; |
---|
719 | } |
---|
720 | |
---|
721 | if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
722 | ( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 ) |
---|
723 | { |
---|
724 | /* 4-bytes UTF-8 character */ |
---|
725 | WANTBYTES( 4, len ); |
---|
726 | *(s++) = *(p++); *(s++) = *(p++); |
---|
727 | *(s++) = *(p++); *(s++) = *(p++); |
---|
728 | continue; |
---|
729 | } |
---|
730 | |
---|
731 | /* ISO 8859-1 -> UTF-8 conversion */ |
---|
732 | WANTBYTES( 2, len ); |
---|
733 | *(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 ); |
---|
734 | *(s++) = 0x80 | ( *(p++) & 0x3F ); |
---|
735 | } |
---|
736 | } |
---|