Changeset 10964 for branches


Ignore:
Timestamp:
Jul 7, 2010, 4:48:24 PM (12 years ago)
Author:
charles
Message:

(2.0x libT) #3397 "checksum errors when downloading files whose names are encoded in iso-8859-1" -- fixed

Location:
branches/2.0x
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0x/configure.ac

    r10888 r10964  
    105105AC_HEADER_TIME
    106106
    107 AC_CHECK_FUNCS([pread pwrite lrintf strlcpy daemon dirname basename strcasecmp localtime_r fallocate64 posix_fallocate memmem strtold syslog valloc getpagesize posix_memalign clearenv])
     107AC_CHECK_FUNCS([iconv_open pread pwrite lrintf strlcpy daemon dirname basename strcasecmp localtime_r fallocate64 posix_fallocate memmem strtold syslog valloc getpagesize posix_memalign clearenv])
    108108AC_PROG_INSTALL
    109109AC_PROG_MAKE_SET
  • branches/2.0x/libtransmission/metainfo.c

    r10774 r10964  
    170170        }
    171171
    172         *setme = tr_utf8clean( (char*)EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ), NULL );
     172        *setme = tr_utf8clean( (char*)EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
    173173        /* fprintf( stderr, "[%s]\n", *setme ); */
    174174        evbuffer_free( buf );
     
    413413    tr_benc *       infoDict = NULL;
    414414    tr_benc *       meta = (tr_benc *) meta_in;
    415     tr_bool         err;
    416415    tr_bool         b;
    417416    tr_bool         isMagnet = FALSE;
     
    472471            return "name";
    473472        tr_free( inf->name );
    474         inf->name = tr_utf8clean( str, -1, &err );
     473        inf->name = tr_utf8clean( str, -1 );
    475474    }
    476475
     
    480479            str = "";
    481480    tr_free( inf->comment );
    482     inf->comment = tr_utf8clean( str, -1, &err );
     481    inf->comment = tr_utf8clean( str, -1 );
    483482
    484483    /* created by */
     
    487486            str = "";
    488487    tr_free( inf->creator );
    489     inf->creator = tr_utf8clean( str, -1, &err );
     488    inf->creator = tr_utf8clean( str, -1 );
    490489
    491490    /* creation date */
  • branches/2.0x/libtransmission/utils-test.c

    r10239 r10964  
    153153    const char * in;
    154154    char * out;
    155     tr_bool err;
    156155
    157156    in = "hello world";
    158     out = tr_utf8clean( in, -1, &err );
    159     check( err == FALSE )
     157    out = tr_utf8clean( in, -1 );
    160158    check( out != NULL )
    161159    check( !strcmp( out, in ) )
     
    163161
    164162    in = "hello world";
    165     out = tr_utf8clean( in, 5, &err );
    166     check( err == FALSE )
     163    out = tr_utf8clean( in, 5 );
    167164    check( out != NULL )
    168165    check( !strcmp( out, "hello" ) )
     
    171168    /* this version is not utf-8 */
    172169    in = "Òðóäíî áûòü Áîãîì";
    173     out = tr_utf8clean( in, 17, &err );
     170    out = tr_utf8clean( in, 17 );
    174171    check( out != NULL )
    175     check( err != 0 )
    176172    check( strlen( out ) == 17 )
    177173    check( tr_utf8_validate( out, -1, NULL ) )
     
    180176    /* same string, but utf-8 clean */
    181177    in = "ÒðóÀíî áûòÌ Áîãîì";
    182     out = tr_utf8clean( in, -1, &err );
     178    out = tr_utf8clean( in, -1 );
    183179    check( out != NULL )
    184     check( !err );
    185180    check( tr_utf8_validate( out, -1, NULL ) )
    186181    check ( !strcmp( in, out ) )
  • branches/2.0x/libtransmission/utils.c

    r10929 r10964  
    1717#if defined(SYS_DARWIN)
    1818 #define HAVE_GETPAGESIZE
     19 #define HAVE_ICONV_OPEN
    1920 #define HAVE_VALLOC
    2021 #undef HAVE_POSIX_MEMALIGN /* not supported on OS X 10.5 and lower */
     
    3132#include <time.h> /* nanosleep() */
    3233
     34#ifdef HAVE_ICONV_OPEN
     35 #include <iconv.h>
     36#endif
    3337#include <libgen.h> /* basename */
    3438#include <sys/time.h>
     
    11801184***/
    11811185
    1182 char*
    1183 tr_utf8clean( const char * str, int max_len, tr_bool * err )
     1186static char*
     1187strip_non_utf8( const char * in, size_t inlen )
    11841188{
    11851189    char * ret;
    11861190    const char * end;
     1191    const char zero = '\0';
     1192    struct evbuffer * buf = evbuffer_new( );
     1193 
     1194    while( !tr_utf8_validate( in, inlen, &end ) )
     1195    {
     1196        const int good_len = end - in;
     1197
     1198        evbuffer_add( buf, in, good_len );
     1199        inlen -= ( good_len + 1 );
     1200        in += ( good_len + 1 );
     1201        evbuffer_add( buf, "?", 1 );
     1202    }
     1203 
     1204    evbuffer_add( buf, in, inlen );
     1205    evbuffer_add( buf, &zero, 1 );
     1206    ret = tr_memdup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
     1207    evbuffer_free( buf );
     1208    return ret;
     1209}
     1210
     1211static char*
     1212to_utf8( const char * in, size_t inlen )
     1213{
     1214    char * ret = NULL;
     1215
     1216#ifdef HAVE_ICONV_OPEN
     1217    int i;
     1218    const char * encodings[] = { "CURRENT", "ISO-8859-15" };
     1219    const int encoding_count = sizeof(encodings) / sizeof(encodings[1]);
     1220    const size_t buflen = inlen*4 + 10;
     1221    char * out = tr_new( char, buflen );
     1222
     1223    for( i=0; !ret && i<encoding_count; ++i )
     1224    {
     1225        char * inbuf = (char*) in;
     1226        char * outbuf = out;
     1227        size_t inbytesleft = inlen;
     1228        size_t outbytesleft = buflen;
     1229        const char * test_encoding = encodings[i];
     1230
     1231        iconv_t cd = iconv_open( "UTF-8", test_encoding );
     1232        if( cd != (iconv_t)-1 ) {
     1233            if( iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ) != (size_t)-1 )
     1234                ret = tr_strndup( out, buflen-outbytesleft );
     1235            iconv_close( cd );
     1236        }
     1237    }
     1238#endif
     1239
     1240    if( ret == NULL )
     1241        ret = strip_non_utf8( in, inlen );
     1242
     1243    return ret;
     1244}
     1245
     1246char*
     1247tr_utf8clean( const char * str, int max_len )
     1248{
     1249    char * ret;
     1250    const char * end;
    11871251
    11881252    if( max_len < 0 )
    11891253        max_len = (int) strlen( str );
    11901254
    1191     if( err != NULL )
    1192         *err = FALSE;
    1193 
    11941255    if( tr_utf8_validate( str, max_len, &end  ) )
    1195     {
    11961256        ret = tr_strndup( str, max_len );
    1197     }
    11981257    else
    1199     {
    1200         const char zero = '\0';
    1201         struct evbuffer * buf = evbuffer_new( );
    1202 
    1203         while( !tr_utf8_validate ( str, max_len, &end ) )
    1204         {
    1205             const int good_len = end - str;
    1206 
    1207             evbuffer_add( buf, str, good_len );
    1208             max_len -= ( good_len + 1 );
    1209             str += ( good_len + 1 );
    1210             evbuffer_add( buf, "?", 1 );
    1211 
    1212             if( err != NULL )
    1213                 *err = TRUE;
    1214         }
    1215 
    1216         evbuffer_add( buf, str, max_len );
    1217         evbuffer_add( buf, &zero, 1 );
    1218         ret = tr_memdup( EVBUFFER_DATA( buf ), EVBUFFER_LENGTH( buf ) );
    1219         evbuffer_free( buf );
    1220     }
     1258        ret = to_utf8( str, max_len );
    12211259
    12221260    assert( tr_utf8_validate( ret, -1, NULL ) );
  • branches/2.0x/libtransmission/utils.h

    r10929 r10964  
    273273 * @param str the string to make a clean copy of
    274274 * @param len the length of the string to copy.  If -1, the entire string is used.
    275  * @param err if an error occurs and err is non-NULL, it's set to TRUE.
    276  */
    277 char* tr_utf8clean( const char * str, int len, tr_bool * err ) TR_GNUC_MALLOC;
     275 */
     276char* tr_utf8clean( const char * str, int len ) TR_GNUC_MALLOC;
    278277
    279278
Note: See TracChangeset for help on using the changeset viewer.