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