1 | /****************************************************************************** |
---|
2 | * $Id: metainfo.c 5852 2008-05-19 14:06: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> /* isspace */ |
---|
27 | #include <errno.h> |
---|
28 | #include <stdio.h> |
---|
29 | #include <stdlib.h> |
---|
30 | |
---|
31 | #include <sys/types.h> |
---|
32 | #include <sys/stat.h> |
---|
33 | #include <unistd.h> /* unlink, stat */ |
---|
34 | |
---|
35 | #include <event.h> /* struct evbuffer */ |
---|
36 | |
---|
37 | #include "transmission.h" |
---|
38 | #include "bencode.h" |
---|
39 | #include "crypto.h" /* tr_sha1 */ |
---|
40 | #include "metainfo.h" |
---|
41 | #include "platform.h" |
---|
42 | #include "utils.h" |
---|
43 | |
---|
44 | /*********************************************************************** |
---|
45 | * Local prototypes |
---|
46 | **********************************************************************/ |
---|
47 | static int parseFiles( tr_info * inf, tr_benc * name, |
---|
48 | tr_benc * files, tr_benc * length ); |
---|
49 | |
---|
50 | /*** |
---|
51 | **** |
---|
52 | ***/ |
---|
53 | |
---|
54 | #define WANTBYTES( want, got ) \ |
---|
55 | if( (want) > (got) ) { return; } else { (got) -= (want); } |
---|
56 | static void |
---|
57 | strlcat_utf8( void * dest, const void * src, size_t len, char skip ) |
---|
58 | { |
---|
59 | char * s = dest; |
---|
60 | const char * append = src; |
---|
61 | const char * p; |
---|
62 | |
---|
63 | /* don't overwrite the nul at the end */ |
---|
64 | len--; |
---|
65 | |
---|
66 | /* Go to the end of the destination string */ |
---|
67 | while( s[0] ) |
---|
68 | { |
---|
69 | s++; |
---|
70 | len--; |
---|
71 | } |
---|
72 | |
---|
73 | /* Now start appending, converting on the fly if necessary */ |
---|
74 | for( p = append; p[0]; ) |
---|
75 | { |
---|
76 | /* skip over the requested character */ |
---|
77 | if( skip == p[0] ) |
---|
78 | { |
---|
79 | p++; |
---|
80 | continue; |
---|
81 | } |
---|
82 | |
---|
83 | if( !( p[0] & 0x80 ) ) |
---|
84 | { |
---|
85 | /* ASCII character */ |
---|
86 | WANTBYTES( 1, len ); |
---|
87 | *(s++) = *(p++); |
---|
88 | continue; |
---|
89 | } |
---|
90 | |
---|
91 | if( ( p[0] & 0xE0 ) == 0xC0 && ( p[1] & 0xC0 ) == 0x80 ) |
---|
92 | { |
---|
93 | /* 2-bytes UTF-8 character */ |
---|
94 | WANTBYTES( 2, len ); |
---|
95 | *(s++) = *(p++); *(s++) = *(p++); |
---|
96 | continue; |
---|
97 | } |
---|
98 | |
---|
99 | if( ( p[0] & 0xF0 ) == 0xE0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
100 | ( p[2] & 0xC0 ) == 0x80 ) |
---|
101 | { |
---|
102 | /* 3-bytes UTF-8 character */ |
---|
103 | WANTBYTES( 3, len ); |
---|
104 | *(s++) = *(p++); *(s++) = *(p++); |
---|
105 | *(s++) = *(p++); |
---|
106 | continue; |
---|
107 | } |
---|
108 | |
---|
109 | if( ( p[0] & 0xF8 ) == 0xF0 && ( p[1] & 0xC0 ) == 0x80 && |
---|
110 | ( p[2] & 0xC0 ) == 0x80 && ( p[3] & 0xC0 ) == 0x80 ) |
---|
111 | { |
---|
112 | /* 4-bytes UTF-8 character */ |
---|
113 | WANTBYTES( 4, len ); |
---|
114 | *(s++) = *(p++); *(s++) = *(p++); |
---|
115 | *(s++) = *(p++); *(s++) = *(p++); |
---|
116 | continue; |
---|
117 | } |
---|
118 | |
---|
119 | /* ISO 8859-1 -> UTF-8 conversion */ |
---|
120 | WANTBYTES( 2, len ); |
---|
121 | *(s++) = 0xC0 | ( ( *p & 0xFF ) >> 6 ); |
---|
122 | *(s++) = 0x80 | ( *(p++) & 0x3F ); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | static void |
---|
127 | getTorrentFilename( const tr_handle * handle, |
---|
128 | const tr_info * inf, |
---|
129 | char * buf, |
---|
130 | size_t buflen ) |
---|
131 | { |
---|
132 | const char * dir = tr_getTorrentDir( handle ); |
---|
133 | char base[MAX_PATH_LENGTH]; |
---|
134 | snprintf( base, sizeof( base ), "%s.%16.16s.torrent", inf->name, inf->hashString ); |
---|
135 | tr_buildPath( buf, buflen, dir, base, NULL ); |
---|
136 | } |
---|
137 | |
---|
138 | static void |
---|
139 | getTorrentOldFilename( const tr_handle * handle, |
---|
140 | const tr_info * info, |
---|
141 | char * name, |
---|
142 | size_t len ) |
---|
143 | { |
---|
144 | const char * torDir = tr_getTorrentDir( handle ); |
---|
145 | |
---|
146 | if( !handle->tag ) |
---|
147 | { |
---|
148 | tr_buildPath( name, len, torDir, info->hashString, NULL ); |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | char base[1024]; |
---|
153 | snprintf( base, sizeof(base), "%s-%s", info->hashString, handle->tag ); |
---|
154 | tr_buildPath( name, len, torDir, base, NULL ); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | void |
---|
159 | tr_metainfoMigrate( tr_handle * handle, |
---|
160 | tr_info * inf ) |
---|
161 | { |
---|
162 | struct stat new_sb; |
---|
163 | char new_name[MAX_PATH_LENGTH]; |
---|
164 | |
---|
165 | getTorrentFilename( handle, inf, new_name, sizeof( new_name ) ); |
---|
166 | |
---|
167 | if( stat( new_name, &new_sb ) || ( ( new_sb.st_mode & S_IFMT ) != S_IFREG ) ) |
---|
168 | { |
---|
169 | char old_name[MAX_PATH_LENGTH]; |
---|
170 | size_t contentLen; |
---|
171 | uint8_t * content; |
---|
172 | |
---|
173 | tr_mkdirp( tr_getTorrentDir( handle ), 0777 ); |
---|
174 | getTorrentOldFilename( handle, inf, old_name, sizeof( old_name ) ); |
---|
175 | if(( content = tr_loadFile( old_name, &contentLen ))) |
---|
176 | { |
---|
177 | FILE * out; |
---|
178 | errno = 0; |
---|
179 | out = fopen( new_name, "wb+" ); |
---|
180 | if( !out ) |
---|
181 | { |
---|
182 | tr_nerr( inf->name, _( "Couldn't create \"%1$s\": %2$s" ), new_name, tr_strerror( errno ) ); |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | if( fwrite( content, sizeof( uint8_t ), contentLen, out ) == contentLen ) |
---|
187 | { |
---|
188 | tr_free( inf->torrent ); |
---|
189 | inf->torrent = tr_strdup( new_name ); |
---|
190 | tr_sessionSetTorrentFile( handle, inf->hashString, new_name ); |
---|
191 | unlink( old_name ); |
---|
192 | } |
---|
193 | fclose( out ); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | tr_free( content ); |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | static char * |
---|
202 | announceToScrape( const char * announce ) |
---|
203 | { |
---|
204 | char * scrape = NULL; |
---|
205 | const char * s; |
---|
206 | |
---|
207 | /* To derive the scrape URL use the following steps: |
---|
208 | * Begin with the announce URL. Find the last '/' in it. |
---|
209 | * If the text immediately following that '/' isn't 'announce' |
---|
210 | * it will be taken as a sign that that tracker doesn't support |
---|
211 | * the scrape convention. If it does, substitute 'scrape' for |
---|
212 | * 'announce' to find the scrape page. */ |
---|
213 | if((( s = strrchr( announce, '/' ))) && !strncmp( ++s, "announce", 8 )) |
---|
214 | { |
---|
215 | struct evbuffer * buf = evbuffer_new( ); |
---|
216 | evbuffer_add( buf, announce, s-announce ); |
---|
217 | evbuffer_add( buf, "scrape", 6 ); |
---|
218 | evbuffer_add_printf( buf, "%s", s+8 ); |
---|
219 | scrape = tr_strdup( ( char * ) EVBUFFER_DATA( buf ) ); |
---|
220 | evbuffer_free( buf ); |
---|
221 | } |
---|
222 | |
---|
223 | return scrape; |
---|
224 | } |
---|
225 | |
---|
226 | static int |
---|
227 | getannounce( tr_info * inf, tr_benc * meta ) |
---|
228 | { |
---|
229 | const char * str; |
---|
230 | tr_tracker_info * trackers = NULL; |
---|
231 | int trackerCount = 0; |
---|
232 | tr_benc * tiers; |
---|
233 | |
---|
234 | /* Announce-list */ |
---|
235 | if( tr_bencDictFindList( meta, "announce-list", &tiers ) ) |
---|
236 | { |
---|
237 | int n; |
---|
238 | int i, j; |
---|
239 | |
---|
240 | n = 0; |
---|
241 | for( i=0; i<tiers->val.l.count; ++i ) |
---|
242 | n += tiers->val.l.vals[i].val.l.count; |
---|
243 | |
---|
244 | trackers = tr_new0( tr_tracker_info, n ); |
---|
245 | trackerCount = 0; |
---|
246 | |
---|
247 | for( i=0; i<tiers->val.l.count; ++i ) { |
---|
248 | const tr_benc * tier = &tiers->val.l.vals[i]; |
---|
249 | for( j=0; tr_bencIsList(tier) && j<tier->val.l.count; ++j ) { |
---|
250 | const tr_benc * a = &tier->val.l.vals[j]; |
---|
251 | if( tr_bencIsString( a ) && tr_httpIsValidURL( a->val.s.s ) ) { |
---|
252 | tr_tracker_info * t = trackers + trackerCount++; |
---|
253 | t->tier = i; |
---|
254 | t->announce = tr_strndup( a->val.s.s, a->val.s.i ); |
---|
255 | t->scrape = announceToScrape( a->val.s.s ); |
---|
256 | /*fprintf( stderr, "tier %d: %s\n", i, a->val.s.s );*/ |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | /* did we use any of the tiers? */ |
---|
262 | if( !trackerCount ) { |
---|
263 | tr_inf( _( "Invalid metadata entry \"%s\"" ), "announce-list" ); |
---|
264 | tr_free( trackers ); |
---|
265 | trackers = NULL; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /* Regular announce value */ |
---|
270 | if( !trackerCount |
---|
271 | && tr_bencDictFindStr( meta, "announce", &str ) |
---|
272 | && tr_httpIsValidURL( str ) ) |
---|
273 | { |
---|
274 | trackers = tr_new0( tr_tracker_info, 1 ); |
---|
275 | trackers[trackerCount].tier = 0; |
---|
276 | trackers[trackerCount].announce = tr_strdup( str ); |
---|
277 | trackers[trackerCount++].scrape = announceToScrape( str ); |
---|
278 | /*fprintf( stderr, "single announce: [%s]\n", str );*/ |
---|
279 | } |
---|
280 | |
---|
281 | inf->trackers = trackers; |
---|
282 | inf->trackerCount = trackerCount; |
---|
283 | |
---|
284 | return inf->trackerCount ? TR_OK : TR_ERROR; |
---|
285 | } |
---|
286 | |
---|
287 | int |
---|
288 | tr_metainfoParse( const tr_handle * handle, |
---|
289 | tr_info * inf, |
---|
290 | const tr_benc * meta_in ) |
---|
291 | { |
---|
292 | tr_piece_index_t i; |
---|
293 | tr_benc * beInfo, * val, * val2; |
---|
294 | tr_benc * meta = (tr_benc *) meta_in; |
---|
295 | char buf[4096]; |
---|
296 | |
---|
297 | /* info_hash: urlencoded 20-byte SHA1 hash of the value of the info key |
---|
298 | * from the Metainfo file. Note that the value will be a bencoded |
---|
299 | * dictionary, given the definition of the info key above. */ |
---|
300 | if(( beInfo = tr_bencDictFindType( meta, "info", TYPE_DICT ))) |
---|
301 | { |
---|
302 | int len; |
---|
303 | char * str = tr_bencSave( beInfo, &len ); |
---|
304 | tr_sha1( inf->hash, str, len, NULL ); |
---|
305 | tr_free( str ); |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | tr_err( _( "Missing metadata entry \"%s\"" ), "info" ); |
---|
310 | return TR_EINVALID; |
---|
311 | } |
---|
312 | |
---|
313 | tr_sha1_to_hex( inf->hashString, inf->hash ); |
---|
314 | |
---|
315 | /* comment */ |
---|
316 | memset( buf, '\0', sizeof( buf ) ); |
---|
317 | val = tr_bencDictFindFirst( meta, "comment.utf-8", "comment", NULL ); |
---|
318 | if( tr_bencIsString( val ) ) |
---|
319 | strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 ); |
---|
320 | tr_free( inf->comment ); |
---|
321 | inf->comment = tr_strdup( buf ); |
---|
322 | |
---|
323 | /* creator */ |
---|
324 | memset( buf, '\0', sizeof( buf ) ); |
---|
325 | val = tr_bencDictFindFirst( meta, "created by.utf-8", "created by", NULL ); |
---|
326 | if( tr_bencIsString( val ) ) |
---|
327 | strlcat_utf8( buf, val->val.s.s, sizeof( buf ), 0 ); |
---|
328 | tr_free( inf->creator ); |
---|
329 | inf->creator = tr_strdup( buf ); |
---|
330 | |
---|
331 | /* Date created */ |
---|
332 | inf->dateCreated = 0; |
---|
333 | val = tr_bencDictFind( meta, "creation date" ); |
---|
334 | if( tr_bencIsInt( val ) ) |
---|
335 | inf->dateCreated = val->val.i; |
---|
336 | |
---|
337 | /* Private torrent */ |
---|
338 | val = tr_bencDictFind( beInfo, "private" ); |
---|
339 | val2 = tr_bencDictFind( meta, "private" ); |
---|
340 | if( ( tr_bencIsInt(val) && val->val.i ) || |
---|
341 | ( tr_bencIsInt(val2) && val2->val.i ) ) |
---|
342 | { |
---|
343 | inf->isPrivate = 1; |
---|
344 | } |
---|
345 | |
---|
346 | /* Piece length */ |
---|
347 | val = tr_bencDictFind( beInfo, "piece length" ); |
---|
348 | if( !tr_bencIsInt( val ) ) |
---|
349 | { |
---|
350 | if( val ) |
---|
351 | tr_err( _( "Invalid metadata entry \"%s\"" ), "piece length" ); |
---|
352 | else |
---|
353 | tr_err( _( "Missing metadata entry \"%s\"" ), "piece length" ); |
---|
354 | goto fail; |
---|
355 | } |
---|
356 | inf->pieceSize = val->val.i; |
---|
357 | |
---|
358 | /* Hashes */ |
---|
359 | val = tr_bencDictFind( beInfo, "pieces" ); |
---|
360 | if( !tr_bencIsString( val ) ) |
---|
361 | { |
---|
362 | if( val ) |
---|
363 | tr_err( _( "Invalid metadata entry \"%s\"" ), "pieces" ); |
---|
364 | else |
---|
365 | tr_err( _( "Missing metadata entry \"%s\"" ), "pieces" ); |
---|
366 | goto fail; |
---|
367 | } |
---|
368 | if( val->val.s.i % SHA_DIGEST_LENGTH ) |
---|
369 | { |
---|
370 | tr_err( _( "Invalid metadata entry \"%s\"" ), "pieces" ); |
---|
371 | goto fail; |
---|
372 | } |
---|
373 | inf->pieceCount = val->val.s.i / SHA_DIGEST_LENGTH; |
---|
374 | |
---|
375 | inf->pieces = calloc ( inf->pieceCount, sizeof(tr_piece) ); |
---|
376 | |
---|
377 | for ( i=0; i<inf->pieceCount; ++i ) |
---|
378 | { |
---|
379 | memcpy (inf->pieces[i].hash, &val->val.s.s[i*SHA_DIGEST_LENGTH], SHA_DIGEST_LENGTH); |
---|
380 | } |
---|
381 | |
---|
382 | /* get file or top directory name */ |
---|
383 | val = tr_bencDictFindFirst( beInfo, "name.utf-8", "name", NULL ); |
---|
384 | if( parseFiles( inf, tr_bencDictFindFirst( beInfo, |
---|
385 | "name.utf-8", "name", NULL ), |
---|
386 | tr_bencDictFind( beInfo, "files" ), |
---|
387 | tr_bencDictFind( beInfo, "length" ) ) ) |
---|
388 | { |
---|
389 | goto fail; |
---|
390 | } |
---|
391 | |
---|
392 | if( !inf->fileCount || !inf->totalSize ) |
---|
393 | { |
---|
394 | tr_err( _( "Torrent is corrupt" ) ); /* the content is missing! */ |
---|
395 | goto fail; |
---|
396 | } |
---|
397 | |
---|
398 | /* TODO add more tests so we don't crash on weird files */ |
---|
399 | |
---|
400 | if( (uint64_t) inf->pieceCount != |
---|
401 | ( inf->totalSize + inf->pieceSize - 1 ) / inf->pieceSize ) |
---|
402 | { |
---|
403 | tr_err( _( "Torrent is corrupt" ) ); /* size of hashes and files don't match */ |
---|
404 | goto fail; |
---|
405 | } |
---|
406 | |
---|
407 | /* get announce or announce-list */ |
---|
408 | if( getannounce( inf, meta ) ) |
---|
409 | goto fail; |
---|
410 | |
---|
411 | /* filename of Transmission's copy */ |
---|
412 | getTorrentFilename( handle, inf, buf, sizeof( buf ) ); |
---|
413 | tr_free( inf->torrent ); |
---|
414 | inf->torrent = tr_strdup( buf ); |
---|
415 | |
---|
416 | return TR_OK; |
---|
417 | |
---|
418 | fail: |
---|
419 | tr_metainfoFree( inf ); |
---|
420 | return TR_EINVALID; |
---|
421 | } |
---|
422 | |
---|
423 | void tr_metainfoFree( tr_info * inf ) |
---|
424 | { |
---|
425 | tr_file_index_t ff; |
---|
426 | int i; |
---|
427 | |
---|
428 | for( ff=0; ff<inf->fileCount; ++ff ) |
---|
429 | tr_free( inf->files[ff].name ); |
---|
430 | |
---|
431 | tr_free( inf->pieces ); |
---|
432 | tr_free( inf->files ); |
---|
433 | tr_free( inf->comment ); |
---|
434 | tr_free( inf->creator ); |
---|
435 | tr_free( inf->torrent ); |
---|
436 | tr_free( inf->name ); |
---|
437 | |
---|
438 | for( i=0; i<inf->trackerCount; ++i ) { |
---|
439 | tr_free( inf->trackers[i].announce ); |
---|
440 | tr_free( inf->trackers[i].scrape ); |
---|
441 | } |
---|
442 | tr_free( inf->trackers ); |
---|
443 | |
---|
444 | memset( inf, '\0', sizeof(tr_info) ); |
---|
445 | } |
---|
446 | |
---|
447 | static int |
---|
448 | getfile( char ** setme, const char * prefix, tr_benc * name ) |
---|
449 | { |
---|
450 | const char ** list; |
---|
451 | int ii, jj; |
---|
452 | char buf[4096]; |
---|
453 | |
---|
454 | if( !tr_bencIsList( name ) ) |
---|
455 | return TR_EINVALID; |
---|
456 | |
---|
457 | list = calloc( name->val.l.count, sizeof( list[0] ) ); |
---|
458 | if( !list ) |
---|
459 | return TR_EINVALID; |
---|
460 | |
---|
461 | for( ii = jj = 0; name->val.l.count > ii; ii++ ) |
---|
462 | { |
---|
463 | tr_benc * dir = &name->val.l.vals[ii]; |
---|
464 | |
---|
465 | if( !tr_bencIsString( dir ) ) |
---|
466 | continue; |
---|
467 | |
---|
468 | if( 0 == strcmp( "..", dir->val.s.s ) ) |
---|
469 | { |
---|
470 | if( 0 < jj ) |
---|
471 | { |
---|
472 | jj--; |
---|
473 | } |
---|
474 | } |
---|
475 | else if( 0 != strcmp( ".", dir->val.s.s ) ) |
---|
476 | { |
---|
477 | list[jj] = dir->val.s.s; |
---|
478 | jj++; |
---|
479 | } |
---|
480 | } |
---|
481 | |
---|
482 | if( 0 == jj ) |
---|
483 | { |
---|
484 | free( list ); |
---|
485 | return TR_EINVALID; |
---|
486 | } |
---|
487 | |
---|
488 | memset( buf, 0, sizeof( buf ) ); |
---|
489 | strlcat_utf8( buf, prefix, sizeof(buf), 0 ); |
---|
490 | for( ii = 0; jj > ii; ii++ ) |
---|
491 | { |
---|
492 | strlcat_utf8( buf, TR_PATH_DELIMITER_STR, sizeof(buf), 0 ); |
---|
493 | strlcat_utf8( buf, list[ii], sizeof(buf), TR_PATH_DELIMITER ); |
---|
494 | } |
---|
495 | free( list ); |
---|
496 | |
---|
497 | tr_free( *setme ); |
---|
498 | *setme = tr_strdup( buf ); |
---|
499 | |
---|
500 | return TR_OK; |
---|
501 | } |
---|
502 | |
---|
503 | void |
---|
504 | tr_metainfoRemoveSaved( const tr_handle * handle, |
---|
505 | const tr_info * inf ) |
---|
506 | { |
---|
507 | char filename[MAX_PATH_LENGTH]; |
---|
508 | |
---|
509 | getTorrentFilename( handle, inf, filename, sizeof( filename ) ); |
---|
510 | unlink( filename ); |
---|
511 | |
---|
512 | getTorrentOldFilename( handle, inf, filename, sizeof( filename ) ); |
---|
513 | unlink( filename ); |
---|
514 | } |
---|
515 | |
---|
516 | static int |
---|
517 | parseFiles( tr_info * inf, tr_benc * name, |
---|
518 | tr_benc * files, tr_benc * length ) |
---|
519 | { |
---|
520 | tr_benc * item, * path; |
---|
521 | int ii; |
---|
522 | char buf[4096]; |
---|
523 | |
---|
524 | if( !tr_bencIsString( name ) ) |
---|
525 | { |
---|
526 | if( name ) |
---|
527 | tr_err( _( "Invalid metadata entry \"%s\"" ), "name" ); |
---|
528 | else |
---|
529 | tr_err( _( "Missing metadata entry \"%s\"" ), "name" ); |
---|
530 | return TR_EINVALID; |
---|
531 | } |
---|
532 | |
---|
533 | memset( buf, 0, sizeof( buf ) ); |
---|
534 | strlcat_utf8( buf, name->val.s.s, sizeof( buf ), 0 ); |
---|
535 | tr_free( inf->name ); |
---|
536 | inf->name = tr_strdup( buf ); |
---|
537 | if( !inf->name || !*inf->name ) |
---|
538 | { |
---|
539 | tr_err( _( "Invalid metadata entry \"%s\"" ), "name" ); |
---|
540 | return TR_EINVALID; |
---|
541 | } |
---|
542 | inf->totalSize = 0; |
---|
543 | |
---|
544 | if( tr_bencIsList( files ) ) |
---|
545 | { |
---|
546 | /* Multi-file mode */ |
---|
547 | inf->isMultifile = 1; |
---|
548 | inf->fileCount = files->val.l.count; |
---|
549 | inf->files = calloc( inf->fileCount, sizeof( inf->files[0] ) ); |
---|
550 | |
---|
551 | if( !inf->files ) |
---|
552 | return TR_EINVALID; |
---|
553 | |
---|
554 | for( ii = 0; files->val.l.count > ii; ii++ ) |
---|
555 | { |
---|
556 | item = &files->val.l.vals[ii]; |
---|
557 | path = tr_bencDictFindFirst( item, "path.utf-8", "path", NULL ); |
---|
558 | if( getfile( &inf->files[ii].name, inf->name, path ) ) |
---|
559 | { |
---|
560 | if( path ) |
---|
561 | tr_err( _( "Invalid metadata entry \"%s\"" ), "path" ); |
---|
562 | else |
---|
563 | tr_err( _( "Missing metadata entry \"%s\"" ), "path" ); |
---|
564 | return TR_EINVALID; |
---|
565 | } |
---|
566 | length = tr_bencDictFind( item, "length" ); |
---|
567 | if( !tr_bencIsInt( length ) ) |
---|
568 | { |
---|
569 | if( length ) |
---|
570 | tr_err( _( "Invalid metadata entry \"%s\"" ), "length" ); |
---|
571 | else |
---|
572 | tr_err( _( "Missing metadata entry \"%s\"" ), "length" ); |
---|
573 | return TR_EINVALID; |
---|
574 | } |
---|
575 | inf->files[ii].length = length->val.i; |
---|
576 | inf->totalSize += length->val.i; |
---|
577 | } |
---|
578 | } |
---|
579 | else if( tr_bencIsInt( length ) ) |
---|
580 | { |
---|
581 | char buf[4096]; |
---|
582 | |
---|
583 | /* Single-file mode */ |
---|
584 | inf->isMultifile = 0; |
---|
585 | inf->fileCount = 1; |
---|
586 | inf->files = calloc( 1, sizeof( inf->files[0] ) ); |
---|
587 | |
---|
588 | if( !inf->files ) |
---|
589 | return TR_EINVALID; |
---|
590 | |
---|
591 | memset( buf, 0, sizeof( buf ) ); |
---|
592 | strlcat_utf8( buf, name->val.s.s, sizeof(buf), TR_PATH_DELIMITER ); |
---|
593 | tr_free( inf->files[0].name ); |
---|
594 | inf->files[0].name = tr_strdup( buf ); |
---|
595 | |
---|
596 | inf->files[0].length = length->val.i; |
---|
597 | inf->totalSize += length->val.i; |
---|
598 | } |
---|
599 | else |
---|
600 | { |
---|
601 | tr_err( _( "Invalid or missing metadata entries \"length\" and \"files\"" ) ); |
---|
602 | } |
---|
603 | |
---|
604 | return TR_OK; |
---|
605 | } |
---|