Skip to content

Commit

Permalink
Allow extended ASCII character set ISO 8859-1
Browse files Browse the repository at this point in the history
set on/off with "clean-json-utf-enabled" in settings.json
also fixed https://trac.transmissionbt.com/ticket/4882
  • Loading branch information
cfpp2p committed Jun 2, 2016
1 parent 6063e42 commit a7a259a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 23 deletions.
44 changes: 31 additions & 13 deletions libtransmission/bencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1392,20 +1392,16 @@ jsonStringFunc( const tr_benc * val, void * vdata )

for( ; it!=end; ++it )
{
switch( *it )
{
case '\b': *outwalk++ = '\\'; *outwalk++ = 'b'; break;
case '\f': *outwalk++ = '\\'; *outwalk++ = 'f'; break;
case '\n': *outwalk++ = '\\'; *outwalk++ = 'n'; break;
case '\r': *outwalk++ = '\\'; *outwalk++ = 'r'; break;
case '\t': *outwalk++ = '\\'; *outwalk++ = 't'; break;
case '"' : *outwalk++ = '\\'; *outwalk++ = '"'; break;
case '\\': *outwalk++ = '\\'; *outwalk++ = '\\'; break;
if( *it < 0x20 )
switch( *it )
{
case '\b': *outwalk++ = '\\'; *outwalk++ = 'b'; break;
case '\f': *outwalk++ = '\\'; *outwalk++ = 'f'; break;
case '\n': *outwalk++ = '\\'; *outwalk++ = 'n'; break;
case '\r': *outwalk++ = '\\'; *outwalk++ = 'r'; break;
case '\t': *outwalk++ = '\\'; *outwalk++ = 't'; break;

default:
if( isascii( *it ) )
*outwalk++ = *it;
else {
default: {
const UTF8 * tmp = it;
UTF32 buf[1] = { 0 };
UTF32 * u32 = buf;
Expand All @@ -1415,6 +1411,28 @@ jsonStringFunc( const tr_benc * val, void * vdata )
it = tmp - 1;
}
}
}
else if( *it < 0x80 )
switch( *it )
{
case '"' : *outwalk++ = '\\'; *outwalk++ = '"'; break;
case '\\': *outwalk++ = '\\'; *outwalk++ = '\\'; break;

default:
*outwalk++ = *it;
}
else
{
const UTF8 * tmp = it;
UTF32 buf[1] = { 0 };
UTF32 * u32 = buf;
ConversionResult result = ConvertUTF8toUTF32( &tmp, end, &u32, buf + 1, 0 );
if((( result==conversionOK ) || (result==targetExhausted)) && (tmp!=it)) {
outwalk += tr_snprintf( outwalk, outend-outwalk, "\\u%04x", (unsigned int)buf[0] );
it = tmp - 1;
}
else
*outwalk++ = *it;
}
}

Expand Down
29 changes: 21 additions & 8 deletions libtransmission/metainfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ path_is_harmful( const char * path )
}

static bool
getfile( char ** setme, const char * root, tr_benc * path, struct evbuffer * buf )
getfile( char ** setme, const char * root, tr_benc * path, struct evbuffer * buf, const bool cleanFiles )
{
bool success = false;

Expand All @@ -191,7 +191,11 @@ getfile( char ** setme, const char * root, tr_benc * path, struct evbuffer * buf
}
}

*setme = tr_utf8clean( (char*)evbuffer_pullup( buf, -1 ), evbuffer_get_length( buf ) );
if( cleanFiles )
*setme = tr_utf8clean( (char*)evbuffer_pullup( buf, -1 ), evbuffer_get_length( buf ) );
else
*setme = tr_strndup( (char*)evbuffer_pullup( buf, -1 ), evbuffer_get_length( buf ) );

/* fprintf( stderr, "[%s]\n", *setme ); */
success = true;
}
Expand All @@ -207,7 +211,7 @@ getfile( char ** setme, const char * root, tr_benc * path, struct evbuffer * buf
}

static const char*
parseFiles( tr_info * inf, tr_benc * files, const tr_benc * length )
parseFiles( tr_info * inf, tr_benc * files, const tr_benc * length, const bool cleanFiles )
{
int64_t len;

Expand Down Expand Up @@ -239,7 +243,7 @@ parseFiles( tr_info * inf, tr_benc * files, const tr_benc * length )
return "path";
}

if( !getfile( &inf->files[i].name, inf->name, path, buf ) ) {
if( !getfile( &inf->files[i].name, inf->name, path, buf, cleanFiles ) ) {
evbuffer_free( buf );
return "path";
}
Expand Down Expand Up @@ -535,22 +539,31 @@ tr_metainfoParseImpl( const tr_session * session,
if( !str || !*str )
return "name";
tr_free( inf->name );
inf->name = tr_utf8clean( str, -1 );
if( session && session->cleanUTFenabled )
inf->name = tr_utf8clean( str, -1 );
else
inf->name = tr_strdup( str );
}

/* comment */
if( !tr_bencDictFindStr( meta, "comment.utf-8", &str ) )
if( !tr_bencDictFindStr( meta, "comment", &str ) )
str = "";
tr_free( inf->comment );
inf->comment = tr_utf8clean( str, -1 );
if( session && session->cleanUTFenabled )
inf->comment = tr_utf8clean( str, -1 );
else
inf->comment = tr_strdup( str );

/* created by */
if( !tr_bencDictFindStr( meta, "created by.utf-8", &str ) )
if( !tr_bencDictFindStr( meta, "created by", &str ) )
str = "";
tr_free( inf->creator );
inf->creator = tr_utf8clean( str, -1 );
if( session && session->cleanUTFenabled )
inf->creator = tr_utf8clean( str, -1 );
else
inf->creator = tr_strdup( str );

/* creation date */
if( !tr_bencDictFindInt( meta, "creation date", &i ) )
Expand Down Expand Up @@ -586,7 +599,7 @@ tr_metainfoParseImpl( const tr_session * session,
/* files */
if( !isMagnet ) {
if( ( str = parseFiles( inf, tr_bencDictFind( infoDict, "files" ),
tr_bencDictFind( infoDict, "length" ) ) ) )
tr_bencDictFind( infoDict, "length" ), session->cleanUTFenabled ) ) )
return str;
if( !inf->fileCount || !inf->totalSize )
return "files";
Expand Down
3 changes: 3 additions & 0 deletions libtransmission/rpcimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,8 @@ sessionSet( tr_session * session,
tr_sessionSetMaxMultiscrape( session, i );
if( tr_bencDictFindInt( args_in, TR_PREFS_KEY_CONCURRENT_ANNOUNCE_MAXIMUM, &i ) )
tr_sessionSetMaxConcurrentAnnounces( session, i );
if( tr_bencDictFindBool( args_in, TR_PREFS_KEY_CLEAN_JSON_UTF, &boolVal ) )
tr_sessionSetCleanJsonUtf( session, boolVal );

notify( session, TR_RPC_SESSION_CHANGED, NULL );

Expand Down Expand Up @@ -1934,6 +1936,7 @@ sessionGet( tr_session * s,
tr_bencDictAddInt ( d, TR_PREFS_KEY_REDIRECT_MAXIMUM, tr_sessionGetMaxRedirect( s ) );
tr_bencDictAddInt ( d, TR_PREFS_KEY_MULTISCRAPE_MAXIMUM, tr_sessionGetMaxMultiscrape( s ) );
tr_bencDictAddInt ( d, TR_PREFS_KEY_CONCURRENT_ANNOUNCE_MAXIMUM, tr_sessionGetMaxConcurrentAnnounces( s ) );
tr_bencDictAddBool( d, TR_PREFS_KEY_CLEAN_JSON_UTF, tr_sessionGetCleanJsonUtf( s ) );

return NULL;
}
Expand Down
25 changes: 23 additions & 2 deletions libtransmission/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ tr_sessionGetDefaultSettings( tr_benc * d )

assert( tr_bencIsDict( d ) );

tr_bencDictReserve( d, 90);
tr_bencDictReserve( d, 91);
tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, false );
tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_WEBSEEDS, false );
tr_bencDictAddBool( d, TR_PREFS_KEY_IPV6_ENABLED, false );
Expand Down Expand Up @@ -454,6 +454,7 @@ tr_sessionGetDefaultSettings( tr_benc * d )
tr_bencDictAddInt ( d, TR_PREFS_KEY_REDIRECT_MAXIMUM, 16 );
tr_bencDictAddInt ( d, TR_PREFS_KEY_MULTISCRAPE_MAXIMUM, 64 );
tr_bencDictAddInt ( d, TR_PREFS_KEY_CONCURRENT_ANNOUNCE_MAXIMUM, 48 );
tr_bencDictAddBool( d, TR_PREFS_KEY_CLEAN_JSON_UTF, false );

tr_bencDictAddStr (d, TR_PREFS_KEY_DOWNLOAD_GROUP_DEFAULT, tr_getDefaultDownloadGroupDefault ());
knownGroups = tr_getDefaultDownloadGroups ();
Expand All @@ -469,7 +470,7 @@ tr_sessionGetSettings( tr_session * s, struct tr_benc * d )

assert( tr_bencIsDict( d ) );

tr_bencDictReserve( d, 89 );
tr_bencDictReserve( d, 90 );
tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_ENABLED, tr_blocklistIsEnabled( s ) );
tr_bencDictAddBool( d, TR_PREFS_KEY_BLOCKLIST_WEBSEEDS, s->blockListWebseeds );
tr_bencDictAddBool( d, TR_PREFS_KEY_IPV6_ENABLED, s->ipv6Enabled );
Expand Down Expand Up @@ -555,6 +556,7 @@ tr_sessionGetSettings( tr_session * s, struct tr_benc * d )
tr_bencDictAddInt ( d, TR_PREFS_KEY_REDIRECT_MAXIMUM, s->maxRedirect );
tr_bencDictAddInt ( d, TR_PREFS_KEY_MULTISCRAPE_MAXIMUM, s->maxMultiscrape );
tr_bencDictAddInt ( d, TR_PREFS_KEY_CONCURRENT_ANNOUNCE_MAXIMUM, s->maxConcurrentAnnounces );
tr_bencDictAddBool( d, TR_PREFS_KEY_CLEAN_JSON_UTF, tr_sessionGetCleanJsonUtf( s ) );

tr_bencDictAddStr (d, TR_PREFS_KEY_DOWNLOAD_GROUP_DEFAULT, tr_sessionGetDownloadGroupDefault (s));
knownGroups = tr_sessionGetDownloadGroups (s);
Expand Down Expand Up @@ -957,6 +959,8 @@ sessionSetImpl( void * vdata )
session->maxMultiscrape = ( ( i >= 0 ) && ( i < 65 ) ) ? i : 64 ;
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_CONCURRENT_ANNOUNCE_MAXIMUM, &i ) )
session->maxConcurrentAnnounces = ( i >= 0 ) ? i : -1 ;
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_CLEAN_JSON_UTF, &boolVal ) )
session->cleanUTFenabled = boolVal;

if (tr_bencDictFindList (settings, TR_PREFS_KEY_DOWNLOAD_GROUPS, &groups))
{
Expand Down Expand Up @@ -3340,6 +3344,23 @@ tr_sessionGetQueueStalledEnabled( const tr_session * session )
return session->stalledEnabled;
}

void
tr_sessionSetCleanJsonUtf( tr_session * session, bool is_enabled )
{
assert( tr_isSession( session ) );
assert( tr_isBool( is_enabled ) );

session->cleanUTFenabled = is_enabled;
}

bool
tr_sessionGetCleanJsonUtf( const tr_session * session )
{
assert( tr_isSession( session ) );

return session->cleanUTFenabled;
}

int
tr_sessionGetQueueStalledMinutes( const tr_session * session )
{
Expand Down
1 change: 1 addition & 0 deletions libtransmission/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ struct tr_session
bool prefetchMagnets;
bool dropInterruptedWebseeds;
bool blockListWebseeds;
bool cleanUTFenabled;

int reverifyTorrents;
tr_cheatMode_t cheatModeDefault;
Expand Down
4 changes: 4 additions & 0 deletions libtransmission/transmission.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ const char* tr_getDefaultDownloadGroupDefault (void);
#define TR_PREFS_KEY_REDIRECT_MAXIMUM "redirect-maximum"
#define TR_PREFS_KEY_MULTISCRAPE_MAXIMUM "multiscrape-maximum"
#define TR_PREFS_KEY_CONCURRENT_ANNOUNCE_MAXIMUM "concurrent-announces-maximum"
#define TR_PREFS_KEY_CLEAN_JSON_UTF "clean-json-utf-enabled"


/**
Expand Down Expand Up @@ -990,6 +991,9 @@ int tr_sessionGetMaxMultiscrape( const tr_session * );
void tr_sessionSetMaxConcurrentAnnounces( tr_session *, int maxConcurrentAnnounces );
int tr_sessionGetMaxConcurrentAnnounces( const tr_session * );

void tr_sessionSetCleanJsonUtf( tr_session *, bool );
bool tr_sessionGetCleanJsonUtf( const tr_session * );

/**
***
**/
Expand Down

0 comments on commit a7a259a

Please sign in to comment.