Changeset 5198


Ignore:
Timestamp:
Mar 4, 2008, 7:29:51 PM (14 years ago)
Author:
charles
Message:

new function: tr_bencSaveAsSerializedPHP()

Location:
trunk/libtransmission
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/bencode-test.c

    r5127 r5198  
    267267
    268268static int
     269testPHPSnippet( const char * benc_str, const char * expected )
     270{
     271    tr_benc top;
     272    char * serialized;
     273    tr_bencLoad( benc_str, strlen( benc_str ), &top, NULL );
     274    serialized = tr_bencSaveAsSerializedPHP( &top, NULL );
     275    check( !strcmp( serialized, expected ) );
     276    tr_free( serialized );
     277    tr_bencFree( &top );
     278    return 0;
     279}
     280
     281static int
     282testPHP( void )
     283{
     284    int val;
     285    const char * benc_str;
     286    const char * expected;
     287
     288    benc_str = "i6e";
     289    expected = "i:6;";
     290    if(( val = testPHPSnippet( benc_str, expected )))
     291        return val;
     292
     293    benc_str = "d3:cow3:moo4:spam4:eggse";
     294    expected = "a:2:{s:3:\"cow\";s:3:\"moo\";s:4:\"spam\";s:4:\"eggs\";}";
     295    if(( val = testPHPSnippet( benc_str, expected )))
     296        return val;
     297
     298    benc_str = "l3:cow3:moo4:spam4:eggse";
     299    expected = "a:4:{i:0;s:3:\"cow\";i:1;s:3:\"moo\";i:2;s:4:\"spam\";i:3;s:4:\"eggs\";}";
     300    if(( val = testPHPSnippet( benc_str, expected )))
     301        return val;
     302
     303    return 0;
     304}
     305
     306static int
    269307testStackSmash( void )
    270308{
     
    312350        return i;
    313351
     352    if(( i = testPHP( )))
     353        return i;
     354
    314355    if(( i = testStackSmash( )))
    315356        return i;
  • trunk/libtransmission/bencode.c

    r5193 r5198  
    3434#include "transmission.h"
    3535#include "bencode.h"
     36#include "list.h"
    3637#include "ptrarray.h"
    3738#include "utils.h" /* tr_new(), tr_free() */
     
    847848    bencWalk( val, &walkFuncs, &walkPrint );
    848849}
     850
     851/***
     852****
     853***/
     854
     855struct ParentState
     856{
     857    int type;
     858    int index;
     859};
     860 
     861struct phpWalk
     862{
     863    tr_list * parents;
     864    struct evbuffer * out;
     865};
     866
     867static void
     868phpChildFunc( struct phpWalk * data )
     869{
     870    if( data->parents )
     871    {
     872        struct ParentState * parentState = data->parents->data;
     873
     874        if( parentState->type == TYPE_LIST )
     875            evbuffer_add_printf( data->out, "i:%d;", parentState->index++ );
     876    }
     877}
     878
     879static void
     880phpPushParent( struct phpWalk * data, int type )
     881{
     882    struct ParentState * parentState = tr_new( struct ParentState, 1 );
     883    parentState->type = type;
     884    parentState->index = 0;
     885    tr_list_prepend( &data->parents, parentState );
     886}
     887
     888static void
     889phpPopParent( struct phpWalk * data )
     890{
     891    tr_free( tr_list_pop_front( &data->parents ) );
     892}
     893
     894static void
     895phpIntFunc( const tr_benc * val, void * vdata )
     896{
     897    struct phpWalk * data = vdata;
     898    phpChildFunc( data );
     899    evbuffer_add_printf( data->out, "i:%"PRId64";", tr_bencGetInt(val) );
     900}
     901static void
     902phpStringFunc( const tr_benc * val, void * vdata )
     903{
     904    struct phpWalk * data = vdata;
     905    phpChildFunc( data );
     906    evbuffer_add_printf( data->out, "s:%d:\"%s\";", val->val.s.i, val->val.s.s );
     907}
     908static void
     909phpDictBeginFunc( const tr_benc * val, void * vdata )
     910{
     911    struct phpWalk * data = vdata;
     912    phpChildFunc( data );
     913    phpPushParent( data, TYPE_DICT );
     914    evbuffer_add_printf( data->out, "a:%d:{", val->val.l.count/2 );
     915}
     916static void
     917phpListBeginFunc( const tr_benc * val, void * vdata )
     918{
     919    struct phpWalk * data = vdata;
     920    phpChildFunc( data );
     921    phpPushParent( data, TYPE_LIST );
     922    evbuffer_add_printf( data->out, "a:%d:{", val->val.l.count );
     923}
     924static void
     925phpContainerEndFunc( const tr_benc * val UNUSED, void * vdata )
     926{
     927    struct phpWalk * data = vdata;
     928    phpPopParent( data );
     929    evbuffer_add_printf( data->out, "}" );
     930}
     931char*
     932tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len )
     933{
     934    char * ret;
     935    struct WalkFuncs walkFuncs;
     936    struct phpWalk data;
     937
     938    data.out = evbuffer_new( );
     939    data.parents = NULL;
     940
     941    walkFuncs.intFunc = phpIntFunc;
     942    walkFuncs.stringFunc = phpStringFunc;
     943    walkFuncs.dictBeginFunc = phpDictBeginFunc;
     944    walkFuncs.listBeginFunc = phpListBeginFunc;
     945    walkFuncs.containerEndFunc = phpContainerEndFunc;
     946
     947    bencWalk( top, &walkFuncs, &data );
     948   
     949    if( len != NULL )
     950        *len = EVBUFFER_LENGTH( data.out );
     951    ret = tr_strndup( (char*) EVBUFFER_DATA( data.out ), EVBUFFER_LENGTH( data.out ) );
     952    evbuffer_free( data.out );
     953    return ret;
     954}
  • trunk/libtransmission/bencode.h

    r5127 r5198  
    9797
    9898char*  tr_bencSave( const tr_benc * val, int * len );
     99char*   tr_bencSaveAsSerializedPHP( const tr_benc * top, int * len );
     100
    99101
    100102int64_t tr_bencGetInt( const tr_benc * val );
Note: See TracChangeset for help on using the changeset viewer.