Changeset 6582


Ignore:
Timestamp:
Aug 18, 2008, 3:10:59 AM (13 years ago)
Author:
charles
Message:

(libT) better handling of non-ascii characters when parsing JSON files

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode.c

    r6568 r6582  
    13151315    size_t contentLen;
    13161316    uint8_t * content = tr_loadFile( filename, &contentLen );
    1317     ret = content ? tr_jsonParse( content, contentLen, b, NULL )
     1317    ret = content ? tr_jsonParse( content, contentLen, b )
    13181318                  : TR_ERROR_IO_OTHER;
    13191319    tr_free( content );
  • trunk/libtransmission/json-test.c

    r6490 r6582  
    2222
    2323static int
     24test_utf8( void )
     25{
     26    const char * in = "{ \"key\": \"Letöltések\" }";
     27    tr_benc top;
     28    const int err = tr_jsonParse( in, strlen(in), &top );
     29
     30    check( !err );
     31    check( tr_bencIsDict( &top ) );
     32
     33    if( !err )
     34        tr_bencFree( &top );
     35
     36    return 0;
     37}
     38
     39static int
    2440test1( void )
    2541{
     
    4056    const char * str;
    4157    int64_t i;
    42     const uint8_t * end = NULL;
    43     const int err = tr_jsonParse( in, strlen(in), &top, &end );
     58    const int err = tr_jsonParse( in, strlen(in), &top );
    4459
    4560    check( !err );
     
    7388    int i;
    7489
     90    if(( i = test_utf8( )))
     91        return i;
     92
    7593    if(( i = test1( )))
    7694        return i;
  • trunk/libtransmission/json.c

    r6490 r6582  
    1818#include <event.h> /* evbuffer */
    1919
     20#include "ConvertUTF.h"
    2021#include "JSON_parser.h"
    2122
     
    108109
    109110        case JSON_T_STRING:
    110             tr_bencInitStrDup( getNode( data ), value->vu.str.value );
     111            tr_bencInitRaw( getNode( data ), value->vu.str.value, value->vu.str.length );
    111112            break;
    112113
    113114        case JSON_T_KEY:
    114115            assert( !data->key );
    115             data->key = tr_strdup( value->vu.str.value );
     116            data->key = tr_strndup( value->vu.str.value, value->vu.str.length );
    116117            break;
    117118    }
     
    123124tr_jsonParse( const void      * vbuf,
    124125              size_t            len,
    125               tr_benc         * setme_benc,
    126               const uint8_t  ** setme_end )
     126              tr_benc         * setme_benc )
    127127{
    128     int err = 0;
    129     const char * buf = vbuf;
    130     const void * bufend = buf + len;
    131128    struct JSON_config_struct config;
    132129    struct JSON_parser_struct * checker;
    133130    struct json_benc_data data;
     131    const UTF8 * utf8_begin;
     132    const UTF8 * utf8_end;
     133    UTF32 * utf32_begin;
     134    UTF32 * utf32_end;
     135    UTF32 * utf32_walk;
     136    int err = 0;
    134137
    135138    init_JSON_config( &config );
     
    142145    data.stack = tr_ptrArrayNew( );
    143146
     147    /* convert the utf8 that was passed in, into utf32 so that
     148     * we can be certain that each call to JSON_parser_char()
     149     * passes through a complete character */
     150    utf8_begin = vbuf;
     151    utf8_end = utf8_begin; /* inout argument */
     152    utf32_begin = tr_new0( UTF32, len );   
     153    utf32_end = utf32_begin; /* inout argument */
     154    ConvertUTF8toUTF32( &utf8_end, utf8_begin+len,
     155                        &utf32_end, utf32_begin+len, 0 );
     156
    144157    checker = new_JSON_parser( &config );
    145     while( ( buf != bufend ) && JSON_parser_char( checker, *buf ) )
    146         ++buf;
    147     if( buf != bufend )
     158    utf32_walk = utf32_begin;
     159    while( ( utf32_walk != utf32_end ) && JSON_parser_char( checker, *utf32_walk ) )
     160        ++utf32_walk;
     161    if( utf32_walk != utf32_end )
    148162        err = TR_ERROR;
    149 
    150     if( setme_end )
    151         *setme_end = (const uint8_t*) buf;
    152163
    153164    delete_JSON_parser( checker );
    154165    tr_ptrArrayFree( data.stack, NULL );
     166    tr_free( utf32_begin );
    155167    return err;
    156168}
  • trunk/libtransmission/json.h

    r5959 r6582  
    1515int tr_jsonParse( const void       * vbuf,
    1616                  size_t             len,
    17                   struct tr_benc   * setme_benc,
    18                   const uint8_t   ** setme_end );
     17                  struct tr_benc   * setme_benc );
    1918
    2019#endif
  • trunk/libtransmission/rpc.c

    r6521 r6582  
    718718        request_len = strlen( request_json );
    719719
    720     have_content = !tr_jsonParse( request_json, request_len, &top, NULL );
     720    have_content = !tr_jsonParse( request_json, request_len, &top );
    721721    ret = request_exec( handle, have_content ? &top : NULL, response_len );
    722722
Note: See TracChangeset for help on using the changeset viewer.