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