static int test_list( void ) { int64_t i; const char * str; tr_benc top; tr_rpc_parse_list_str( &top, "12", -1 ); check( tr_bencIsInt( &top ) ); check( tr_bencGetInt( &top, &i ) ); check( i == 12 ); tr_bencFree( &top ); tr_rpc_parse_list_str( &top, "12", 1 ); check( tr_bencIsInt( &top ) ); check( tr_bencGetInt( &top, &i ) ); check( i == 1 ); tr_bencFree( &top ); tr_rpc_parse_list_str( &top, "6,7", -1 ); check( tr_bencIsList( &top ) ); check( tr_bencListSize( &top ) == 2 ); check( tr_bencGetInt( tr_bencListChild( &top, 0 ), &i ) ); check( i == 6 ); check( tr_bencGetInt( tr_bencListChild( &top, 1 ), &i ) ); check( i == 7 ); tr_bencFree( &top ); tr_rpc_parse_list_str( &top, "asdf", -1 ); check( tr_bencIsString( &top ) ); check( tr_bencGetStr( &top, &str ) ); check( !strcmp( str, "asdf" ) ); tr_bencFree( &top ); tr_rpc_parse_list_str( &top, "1,3-5", -1 ); check( tr_bencIsList( &top ) ); check( tr_bencListSize( &top ) == 4 ); check( tr_bencGetInt( tr_bencListChild( &top, 0 ), &i ) ); check( i == 1 ); check( tr_bencGetInt( tr_bencListChild( &top, 1 ), &i ) ); check( i == 3 ); check( tr_bencGetInt( tr_bencListChild( &top, 2 ), &i ) ); check( i == 4 ); check( tr_bencGetInt( tr_bencListChild( &top, 3 ), &i ) ); check( i == 5 ); tr_bencFree( &top ); return 0; }
static tr_benc* get_node (struct jsonsl_st * jsn) { tr_benc * parent; tr_benc * node = NULL; struct json_wrapper_data * data = jsn->data; parent = tr_ptrArrayEmpty (&data->stack) ? NULL : tr_ptrArrayBack (&data->stack); if (!parent) { node = data->top; } else if (tr_bencIsList (parent)) { node = tr_bencListAdd (parent); } else if (tr_bencIsDict (parent) && (data->key!=NULL)) { node = tr_bencDictAdd (parent, data->key); tr_free (data->key); data->key = NULL; } return node; }
static const char* parseFiles (tr_info * inf, tr_benc * files, const tr_benc * length) { int64_t len; inf->totalSize = 0; if (tr_bencIsList (files)) /* multi-file mode */ { tr_file_index_t i; struct evbuffer * buf = evbuffer_new (); inf->isMultifile = 1; inf->fileCount = tr_bencListSize (files); inf->files = tr_new0 (tr_file, inf->fileCount); for (i=0; i<inf->fileCount; i++) { tr_benc * file; tr_benc * path; file = tr_bencListChild (files, i); if (!tr_bencIsDict (file)) return "files"; if (!tr_bencDictFindList (file, "path.utf-8", &path)) if (!tr_bencDictFindList (file, "path", &path)) return "path"; if (!getfile (&inf->files[i].name, inf->name, path, buf)) return "path"; if (!tr_bencDictFindInt (file, "length", &len)) return "length"; inf->files[i].length = len; inf->totalSize += len; } evbuffer_free (buf); } else if (tr_bencGetInt (length, &len)) /* single-file mode */ { if (path_is_suspicious (inf->name)) return "path"; inf->isMultifile = 0; inf->fileCount = 1; inf->files = tr_new0 (tr_file, 1); inf->files[0].name = tr_strdup (inf->name); inf->files[0].length = len; inf->totalSize += len; } else { return "length"; } return NULL; }
static tr_bool getfile( char ** setme, const char * root, tr_benc * path ) { tr_bool success = FALSE; if( tr_bencIsList( path ) ) { int i; char * tmp; const int n = tr_bencListSize( path ); struct evbuffer * buf = evbuffer_new( ); evbuffer_add( buf, root, strlen( root ) ); for( i = 0; i < n; ++i ) { const char * str; if( tr_bencGetStr( tr_bencListChild( path, i ), &str ) ) { evbuffer_add( buf, TR_PATH_DELIMITER_STR, 1 ); evbuffer_add( buf, str, strlen( str ) ); } } tmp = evbuffer_free_to_str( buf ); *setme = tr_utf8clean( tmp, -1 ); tr_free( tmp ); /* fprintf( stderr, "[%s]\n", *setme ); */ success = TRUE; } if( ( *setme != NULL ) && path_is_suspicious( *setme ) ) { tr_free( *setme ); *setme = NULL; success = FALSE; } return success; }
static bool getfile (char ** setme, const char * root, tr_benc * path, struct evbuffer * buf) { bool success = false; if (tr_bencIsList (path)) { int i; const int n = tr_bencListSize (path); evbuffer_drain (buf, evbuffer_get_length (buf)); evbuffer_add (buf, root, strlen (root)); for (i=0; i<n; i++) { const char * str; if (tr_bencGetStr (tr_bencListChild (path, i), &str)) { evbuffer_add (buf, TR_PATH_DELIMITER_STR, 1); evbuffer_add (buf, str, strlen (str)); } } *setme = tr_utf8clean ((char*)evbuffer_pullup (buf, -1), evbuffer_get_length (buf)); /* fprintf (stderr, "[%s]\n", *setme); */ success = true; } if ((*setme != NULL) && path_is_suspicious (*setme)) { tr_free (*setme); *setme = NULL; success = false; } return success; }
static int getfile( char ** setme, const char * root, tr_benc * path ) { int err; if( !tr_bencIsList( path ) ) { err = TR_EINVALID; } else { struct evbuffer * buf = tr_getBuffer( ); int n = tr_bencListSize( path ); int i; evbuffer_add( buf, root, strlen( root ) ); for( i = 0; i < n; ++i ) { const char * str; if( tr_bencGetStr( tr_bencListChild( path, i ), &str ) && strcmp( str, ".." ) ) { evbuffer_add( buf, TR_PATH_DELIMITER_STR, 1 ); evbuffer_add( buf, str, strlen( str ) ); } } *setme = tr_utf8clean( (char*)EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ), NULL ); /* fprintf( stderr, "[%s]\n", *setme ); */ tr_releaseBuffer( buf ); err = 0; } return err; }
static tr_benc* getNode( struct json_benc_data * data ) { tr_benc * parent; tr_benc * node = NULL; if( tr_ptrArrayEmpty( data->stack ) ) parent = NULL; else parent = tr_ptrArrayBack( data->stack ); if( !parent ) node = data->top; else if( tr_bencIsList( parent ) ) node = tr_bencListAdd( parent ); else if( tr_bencIsDict( parent ) && data->key ) { node = tr_bencDictAdd( parent, data->key ); tr_free( data->key ); data->key = NULL; } return node; }
static uint64_t loadProgress( tr_benc * dict, tr_torrent * tor ) { size_t i, n; uint64_t ret = 0; tr_benc * prog; const tr_info * inf = tr_torrentInfo( tor ); for( i=0, n=inf->pieceCount; i<n; ++i ) inf->pieces[i].timeChecked = 0; if( tr_bencDictFindDict( dict, KEY_PROGRESS, &prog ) ) { const char * err; const char * str; const uint8_t * raw; size_t rawlen; tr_benc * l; tr_benc * b; struct tr_bitset bitset = TR_BITSET_INIT; if( tr_bencDictFindList( prog, KEY_PROGRESS_CHECKTIME, &l ) ) { /* per-piece timestamps were added in 2.20. If some of a file's pieces have been checked more recently than the file's mtime, and some lest recently, then that file will have a list containing timestamps for each piece. However, the most common use case is that the file doesn't change after it's downloaded. To reduce overhead in the .resume file, only a single timestamp is saved for the file if *all* or *none* of the pieces were tested more recently than the file's mtime. */ tr_file_index_t fi; for( fi=0; fi<inf->fileCount; ++fi ) { tr_benc * b = tr_bencListChild( l, fi ); const tr_file * f = &inf->files[fi]; tr_piece * p = &inf->pieces[f->firstPiece]; const tr_piece * pend = &inf->pieces[f->lastPiece]+1; if( tr_bencIsInt( b ) ) { int64_t t; tr_bencGetInt( b, &t ); for( ; p!=pend; ++p ) p->timeChecked = (time_t)t; } else if( tr_bencIsList( b ) ) { int i = 0; int64_t offset = 0; const int pieces = f->lastPiece + 1 - f->firstPiece; tr_bencGetInt( tr_bencListChild( b, 0 ), &offset ); for( i=0; i<pieces; ++i ) { int64_t t = 0; tr_bencGetInt( tr_bencListChild( b, i+1 ), &t ); inf->pieces[f->firstPiece+i].timeChecked = (time_t)(t ? t + offset : 0); } } } } else if( tr_bencDictFindList( prog, KEY_PROGRESS_MTIMES, &l ) ) { tr_file_index_t fi; /* Before 2.20, we stored the files' mtimes in the .resume file. When loading the .resume file, a torrent's file would be flagged as untested if its stored mtime didn't match its real mtime. */ for( fi=0; fi<inf->fileCount; ++fi ) { int64_t t; if( tr_bencGetInt( tr_bencListChild( l, fi ), &t ) ) { const tr_file * f = &inf->files[fi]; tr_piece * p = &inf->pieces[f->firstPiece]; const tr_piece * pend = &inf->pieces[f->lastPiece]; const time_t mtime = tr_torrentGetFileMTime( tor, fi ); const time_t timeChecked = mtime==t ? mtime : 0; for( ; p!=pend; ++p ) p->timeChecked = timeChecked; } } } err = NULL; if(( b = tr_bencDictFind( prog, KEY_PROGRESS_BLOCKS ))) { if( !tr_bitsetFromBenc( &bitset, b ) ) err = "Invalid value for PIECES"; } else if( tr_bencDictFindStr( prog, KEY_PROGRESS_HAVE, &str ) ) { if( !strcmp( str, "all" ) ) tr_bitsetSetHaveAll( &bitset ); else err = "Invalid value for HAVE"; } else if( tr_bencDictFindRaw( prog, KEY_PROGRESS_BITFIELD, &raw, &rawlen ) ) { bitset.bitfield.bits = (void*) raw; bitset.bitfield.byteCount = rawlen; bitset.bitfield.bitCount = rawlen * 8; } else err = "Couldn't find 'pieces' or 'have' or 'bitfield'"; if( !err && !tr_cpBlockBitsetInit( &tor->completion, &bitset ) ) err = "Error loading bitfield"; if( err != NULL ) tr_tordbg( tor, "Torrent needs to be verified - %s", err ); ret = TR_FR_PROGRESS; } return ret; }