1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-27 01:57:52 +00:00
transmission/libtransmission/json.c

190 lines
4.7 KiB
C
Raw Normal View History

2008-05-11 22:42:53 +00:00
/*
* This file Copyright (C) Mnemosyne LLC
2008-05-11 22:42:53 +00:00
*
* This file is licensed by the GPL version 2. Works owned by the
2008-05-11 22:42:53 +00:00
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
2008-05-11 22:42:53 +00:00
* This exemption does not extend to derived works not owned by
* the Transmission project.
*
2008-05-28 17:17:12 +00:00
* $Id$
2008-05-11 22:42:53 +00:00
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <errno.h> /* EILSEQ, EINVAL */
#include "JSON_parser.h"
2008-05-11 22:42:53 +00:00
#include "transmission.h"
#include "bencode.h"
#include "json.h"
2008-05-11 22:42:53 +00:00
#include "ptrarray.h"
#include "utils.h"
struct json_benc_data
{
bool hasContent;
tr_benc * top;
tr_ptrArray stack;
char * key;
2008-05-11 22:42:53 +00:00
};
static tr_benc*
getNode( struct json_benc_data * data )
{
tr_benc * parent;
tr_benc * node = NULL;
if( tr_ptrArrayEmpty( &data->stack ) )
2008-05-11 22:42:53 +00:00
parent = NULL;
else
parent = tr_ptrArrayBack( &data->stack );
2008-05-11 22:42:53 +00:00
if( !parent )
node = data->top;
else if( tr_bencIsList( parent ) )
node = tr_bencListAdd( parent );
else if( tr_bencIsDict( parent ) && data->key )
{
2008-05-11 22:42:53 +00:00
node = tr_bencDictAdd( parent, data->key );
tr_free( data->key );
data->key = NULL;
}
return node;
}
static int
callback( void * vdata,
int type,
const JSON_value * value )
2008-05-11 22:42:53 +00:00
{
struct json_benc_data * data = vdata;
tr_benc * node;
2008-05-11 22:42:53 +00:00
switch( type )
{
case JSON_T_ARRAY_BEGIN:
data->hasContent = true;
2008-05-11 22:42:53 +00:00
node = getNode( data );
tr_bencInitList( node, 0 );
tr_ptrArrayAppend( &data->stack, node );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_ARRAY_END:
tr_ptrArrayPop( &data->stack );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_OBJECT_BEGIN:
data->hasContent = true;
2008-05-11 22:42:53 +00:00
node = getNode( data );
tr_bencInitDict( node, 0 );
tr_ptrArrayAppend( &data->stack, node );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_OBJECT_END:
tr_ptrArrayPop( &data->stack );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_FLOAT:
data->hasContent = true;
tr_bencInitReal( getNode( data ), value->vu.float_value );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_NULL:
data->hasContent = true;
tr_bencInitStr( getNode( data ), "", 0 );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_INTEGER:
data->hasContent = true;
tr_bencInitInt( getNode( data ), value->vu.integer_value );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_TRUE:
data->hasContent = true;
tr_bencInitBool( getNode( data ), 1 );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_FALSE:
data->hasContent = true;
tr_bencInitBool( getNode( data ), 0 );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_STRING:
data->hasContent = true;
tr_bencInitStr( getNode( data ),
value->vu.str.value,
value->vu.str.length );
2008-05-11 22:42:53 +00:00
break;
case JSON_T_KEY:
data->hasContent = true;
2008-05-11 22:42:53 +00:00
assert( !data->key );
data->key = tr_strndup( value->vu.str.value, value->vu.str.length );
2008-05-11 22:42:53 +00:00
break;
}
return 1;
}
int
tr_jsonParse( const char * source,
const void * vbuf,
size_t len,
tr_benc * setme_benc,
const uint8_t ** setme_end )
2008-05-11 22:42:53 +00:00
{
int line = 1;
int column = 1;
int err = 0;
const unsigned char * buf = vbuf;
const void * bufend = buf + len;
JSON_config config;
struct JSON_parser_struct * checker;
struct json_benc_data data;
2008-05-11 22:42:53 +00:00
init_JSON_config( &config );
config.callback = callback;
config.callback_ctx = &data;
config.depth = -1;
data.hasContent = false;
2008-05-11 22:42:53 +00:00
data.key = NULL;
data.top = setme_benc;
data.stack = TR_PTR_ARRAY_INIT;
2008-05-11 22:42:53 +00:00
checker = new_JSON_parser( &config );
while( ( buf != bufend ) && JSON_parser_char( checker, *buf ) ) {
if( *buf != '\n' )
++column;
else {
++line;
column = 1;
}
++buf;
}
if( buf != bufend ) {
if( source )
tr_err( "JSON parser failed in %s at line %d, column %d: \"%.16s\"", source, line, column, buf );
else
tr_err( "JSON parser failed at line %d, column %d: \"%.16s\"", line, column, buf );
2008-10-03 04:49:06 +00:00
err = EILSEQ;
}
2008-05-11 22:42:53 +00:00
if( !data.hasContent )
err = EINVAL;
if( setme_end )
*setme_end = (const uint8_t*) buf;
delete_JSON_parser( checker );
tr_ptrArrayDestruct( &data.stack, NULL );
2008-05-11 22:42:53 +00:00
return err;
}