Esempio n. 1
0
cell *sexpression_from_json_string(const char* json_string) {
    int r;
    jsmn_parser p;
    jsmntok_t t[1024];
    
    jsmn_init(&p);
    r = jsmn_parse(&p, json_string, strlen(json_string), t, sizeof(t)/sizeof(t[0]));
    if (r < 0) {
        printf("Failed to parse JSON: %d\n", r);
        return NULL;
    }
    
    if (r < 1 || t[0].type != JSMN_ARRAY) {
        printf("Array expected\n");
        return NULL;
    }
    int json_element_size;
    return sexpression_from_json_token(t, json_string, &json_element_size);
  
}
bool isReceivedJsonValid(const char *pJsonDocument) {
	int32_t tokenCount;

	jsmn_init(&shadowJsonParser);

	tokenCount = jsmn_parse(&shadowJsonParser, pJsonDocument, strlen(pJsonDocument), jsonTokenStruct,
							sizeof(jsonTokenStruct) / sizeof(jsonTokenStruct[0]));

	if(tokenCount < 0) {
		IOT_WARN("Failed to parse JSON: %d\n", tokenCount);
		return false;
	}

	/* Assume the top-level element is an object */
	if(tokenCount < 1 || jsonTokenStruct[0].type != JSMN_OBJECT) {
		return false;
	}

	return true;
}
Esempio n. 3
0
int do_parse_json(char * buffer,int* clock)
{
	int i;
	int type_flag = -1;
	int clock_flag = -1;
	
	int parser_return_code;
	jsmntok_t token[10];
	jsmn_parser p;
	
	for(i=0; i<10; i++) token[i].type=-1;

	jsmn_init(&p);
	parser_return_code = jsmn_parse(&p, buffer, token, 10);
	if(parser_return_code != JSMN_SUCCESS) return ERROR;
	
	//SZUKAJ TYPU I ZEGARA
	for(i=0; i<10; i++)
	{
		if(token[i].type == -1) break;
		if( token_string(buffer, token[i], "type") || token_string(buffer, token[i], "Type") || token_string(buffer, token[i], "TYPE") ) type_flag = i+1;
		if( token_string(buffer, token[i], "clock") || token_string(buffer, token[i], "Clock") || token_string(buffer, token[i], "CLOCK") ) clock_flag = i+1;
	}

	fprintf(stderr,"### type_flag: %d, clock_flag: %d\n", type_flag, clock_flag);

	//NIE ZNALEZIONO! BLAD
	if(type_flag == -1 || clock_flag == -1 ) return ERROR;
	//ZLE TYPY! BLAD
	if(token[type_flag].type != JSMN_STRING && token[clock_flag].type != JSMN_PRIMITIVE) return ERROR;

	int rec_clock = strtol( buffer+token[clock_flag].start, NULL, 0 );

	update_clock(rec_clock);
	(*clock) = rec_clock;
	
	if( token_string(buffer, token[type_flag], "order") ) return ORDER;
	else if( token_string(buffer, token[type_flag], "ok") ) return OK;
	else return ERROR;
	
}
Esempio n. 4
0
int test_unquoted_keys() {
#ifndef JSMN_STRICT
	int r;
	jsmn_parser p;
	jsmntok_t tok[10];
	const char *js;

	jsmn_init(&p);
	js = "key1: \"value\"\nkey2 : 123";

	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_SUCCESS && tok[0].type == JSMN_PRIMITIVE
			&& tok[1].type == JSMN_STRING && tok[2].type == JSMN_PRIMITIVE
			&& tok[3].type == JSMN_PRIMITIVE);
	check(TOKEN_STRING(js, tok[0], "key1"));
	check(TOKEN_STRING(js, tok[1], "value"));
	check(TOKEN_STRING(js, tok[2], "key2"));
	check(TOKEN_STRING(js, tok[3], "123"));
#endif
	return 0;
}
Esempio n. 5
0
int test_unicode_characters() {
	jsmn_parser p;
	jsmntok_t tokens[10];
	const char *js;

	int r;
	js = "{\"a\":\"\\uAbcD\"}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r >= 0);

	js = "{\"a\":\"str\\u0000\"}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r >= 0);

	js = "{\"a\":\"\\uFFFFstr\"}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r >= 0);

	js = "{\"a\":\"str\\uFFGFstr\"}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);

	js = "{\"a\":\"str\\u@FfF\"}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);

	js = "{\"a\":[\"\\u028\"]}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);

	js = "{\"a\":[\"\\u0280\"]}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r >= 0);

	return 0;
}
Esempio n. 6
0
int test_simple() {
	const char *js;
	int r;
	jsmn_parser p;
	jsmntok_t tokens[10];

	js = "{\"a\": 0}";

	jsmn_init(&p);
	r = jsmn_parse(&p, js, tokens, 10);
	check(r == JSMN_SUCCESS);
	check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT));
	check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING));
	check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE));

	check(TOKEN_STRING(js, tokens[0], js));
	check(TOKEN_STRING(js, tokens[1], "a"));
	check(TOKEN_STRING(js, tokens[2], "0"));

	return 0;
}
Esempio n. 7
0
static jsmntok_t *json_tokenise(char *js, size_t len, int *arr_size)
{
    int ret;
    unsigned int n = 256;
    jsmntok_t *tokens;
    jsmn_parser parser;

    jsmn_init(&parser);
    tokens = calloc(1, sizeof(jsmntok_t) * n);
    ret = jsmn_parse(&parser, js, len, tokens, n);

    while (ret == JSMN_ERROR_NOMEM) {
        n = n * 2 + 1;
        tokens = realloc(tokens, sizeof(jsmntok_t) * n);
        if (!tokens) {
            goto error;
        }
        ret = jsmn_parse(&parser, js, len, tokens, n);
    }

    if (ret == JSMN_ERROR_INVAL) {
        flb_utils_error(FLB_ERR_JSON_INVAL);
        goto error;
    }

    if (ret == JSMN_ERROR_PART) {
        /* Hmm, should we error for partial JSONs ? */
        flb_utils_error(FLB_ERR_JSON_PART);
        goto error;
    }

    /* Store the array length */
    *arr_size = n;
    return tokens;

 error:
    free(tokens);
    return NULL;
}
int main(int argc, char **argv)
{
    jsmntok_t tokens[1024] = {0};
    jsmn_parser parser;
    jsmnerr_t err;
    int i,total,g=0;
    group *groups;
    char *data= load_file(argv[1]);
    
    jsmn_init(&parser);
    err = jsmn_parse(&parser,data,tokens,sizeof(tokens));
    total = tokens[0].size;
    
    if(err != JSMN_SUCCESS || total <= 0) 
        return 1;
        
    groups = malloc(total * sizeof(group));
    printf("total:%d\n",tokens[0].size);
    
    for(i = 1; i < total ;i++) {
    printf("%d\n",tokens[i].size);
       if (tokens[i].size == 3 && tokens[i].type == JSMN_ARRAY) {
        /*next three tokens have the group data*/
         groups[g].from = atoi(&data[tokens[i+1].start]);
         groups[g].to = atoi(&data[tokens[i+2].start]);
         groups[g].key = &data[tokens[i+3].start];
         data[tokens[i+3].end] = '\0';
         group *gr = &groups[g];
         g++;
         printf("%d %d %s\n",gr->from,gr->to,gr->key);
       }
       if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
            total += tokens[i].size;
            i+= tokens[i].size;
        }

    }
}
Esempio n. 9
0
static STSStatus parse_sts_result(int bufferSize, const char* buffer, STSData* result)
{
    STSData* stsData = (STSData*)result;
    jsmntok_t json[256];
    jsmn_parser parser;
    jsmnerr_t r;
    int i = 0;
    int keyLen = 0;
    int valueLen = 0;
    const char* pKey;
    const char* pValue;
    
    jsmn_init(&parser);
    r = jsmn_parse(&parser, buffer, bufferSize, json, 256);
    
    if (r >= 0) {
        for (i = 0; i < r; ++i) {
            if (JSMN_STRING == json[i].type) {
                keyLen = json[i].end - json[i].start;
                valueLen = json[i+1].end - json[i+1].start;
                pKey = &buffer[json[i].start];
                pValue = &buffer[json[i+1].start];
                
                if (0 == strncmp("AccessKeyId", pKey, keyLen)) {
                    snprintf(stsData->tmpAccessKeyId, valueLen+1, "%s", pValue);
                }
                else if (0 == strncmp("AccessKeySecret", pKey, keyLen)) {
                    snprintf(stsData->tmpAccessKeySecret, valueLen+1, "%s", pValue);
                }
                else if (0 == strncmp("SecurityToken", pKey, keyLen)) {
                    snprintf(stsData->securityToken, valueLen+1, "%s", pValue);
                }
            }
        }
    }
    
    return STSStatusOK;
}
Esempio n. 10
0
int test_partial_string() {
	int r;
	jsmn_parser p;
	jsmntok_t tok[10];
	const char *js;

	jsmn_init(&p);
	js = "\"x\": \"va";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_ERROR_PART && tok[0].type == JSMN_STRING);
	check(TOKEN_STRING(js, tok[0], "x"));
	check(p.toknext == 1);

	js = "\"x\": \"valu";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_ERROR_PART && tok[0].type == JSMN_STRING);
	check(TOKEN_STRING(js, tok[0], "x"));
	check(p.toknext == 1);

	js = "\"x\": \"value\"";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
			&& tok[1].type == JSMN_STRING);
	check(TOKEN_STRING(js, tok[0], "x"));
	check(TOKEN_STRING(js, tok[1], "value"));

	js = "\"x\": \"value\", \"y\": \"value y\"";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
			&& tok[1].type == JSMN_STRING && tok[2].type == JSMN_STRING
			&& tok[3].type == JSMN_STRING);
	check(TOKEN_STRING(js, tok[0], "x"));
	check(TOKEN_STRING(js, tok[1], "value"));
	check(TOKEN_STRING(js, tok[2], "y"));
	check(TOKEN_STRING(js, tok[3], "value y"));

	return 0;
}
Esempio n. 11
0
int test_partial_array() {
	int r;
	jsmn_parser p;
	jsmntok_t tok[10];
	const char *js;

	jsmn_init(&p);
	js = "  [ 1, true, ";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY 
			&& tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE);

	js = "  [ 1, true, [123, \"hello";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY 
			&& tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
			&& tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE);

	js = "  [ 1, true, [123, \"hello\"]";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY 
			&& tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
			&& tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE
			&& tok[5].type == JSMN_STRING);
	/* check child nodes of the 2nd array */
	check(tok[3].size == 2);

	js = "  [ 1, true, [123, \"hello\"]]";
	r = jsmn_parse(&p, js, tok, 10);
	check(r == JSMN_SUCCESS && tok[0].type == JSMN_ARRAY 
			&& tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
			&& tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE
			&& tok[5].type == JSMN_STRING);
	check(tok[3].size == 2);
	check(tok[0].size == 3);
	return 0;
}
Esempio n. 12
0
struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
{
	int n;
	jsmn_parser p;
	int toks_num = 100;
	jsmntok_t *toks = NULL;
	struct hyper_container *c = NULL;

realloc:
	toks = realloc(toks, toks_num * sizeof(jsmntok_t));
	if (toks == NULL) {
		fprintf(stderr, "allocate tokens for new container failed\n");
		goto fail;
	}

	jsmn_init(&p);
	n = jsmn_parse(&p, json, length, toks, toks_num);
	if (n < 0) {
		fprintf(stdout, "jsmn parse failed, n is %d\n", n);
		if (n == JSMN_ERROR_NOMEM) {
			toks_num *= 2;
			goto realloc;
		}

		goto fail;
	}

	if (hyper_parse_container(pod, &c, json, toks) < 0)
		goto fail;

	free(toks);
	return c;

fail:
	free(toks);
	return NULL;
}
Esempio n. 13
0
jsmntok_t * json_tokenise(char *js)
{
    jsmn_parser parser;
    jsmn_init(&parser);

    unsigned int n = JSON_TOKENS;
    jsmntok_t *tokens = (jsmntok_t *)malloc(sizeof(jsmntok_t) * n);

    int ret = jsmn_parse(&parser, js, tokens, n);

    while (ret == JSMN_ERROR_NOMEM)
    {
        n = n * 2 + 1;
        tokens = (jsmntok_t *)realloc(tokens, sizeof(jsmntok_t) * n);
        ret = jsmn_parse(&parser, js, tokens, n);
    }

    if (ret == JSMN_ERROR_INVAL)
        log_err("jsmn_parse: invalid JSON string");
    if (ret == JSMN_ERROR_PART)
        log_err("jsmn_parse: truncated JSON string");

    return tokens;
}
bool isJsonValidAndParse(const char *pJsonDocument, void *pJsonHandler, int32_t *pTokenCount) {
	int32_t tokenCount;

	jsmn_init(&shadowJsonParser);

	tokenCount = jsmn_parse(&shadowJsonParser, pJsonDocument, strlen(pJsonDocument), jsonTokenStruct,
			sizeof(jsonTokenStruct) / sizeof(jsonTokenStruct[0]));

	if (tokenCount < 0) {
		WARN("Failed to parse JSON: %d\n", tokenCount);
		return false;
	}

	/* Assume the top-level element is an object */
	if (tokenCount < 1 || jsonTokenStruct[0].type != JSMN_OBJECT) {
		WARN("Top Level is not an object\n");
		return false;
	}

	pJsonHandler = (void *) jsonTokenStruct;
	*pTokenCount = tokenCount;

	return true;
}
Esempio n. 15
0
int test_partial_string(void) {
    int i;
    int r;
    jsmn_parser p;
    jsmntok_t tok[5];
    const char *js = "{\"x\": \"va\\\\ue\", \"y\": \"value y\"}";

    jsmn_init(&p);
    for (i = 1; i <= strlen(js); i++) {
        r = jsmn_parse(&p, js, i, tok, sizeof(tok)/sizeof(tok[0]));
        if (i == strlen(js)) {
            check(r == 5);
            check(tokeq(js, tok, 5,
                        JSMN_OBJECT, -1, -1, 2,
                        JSMN_STRING, "x", 1,
                        JSMN_STRING, "va\\\\ue", 0,
                        JSMN_STRING, "y", 1,
                        JSMN_STRING, "value y", 0));
        } else {
            check(r == JSMN_ERROR_PART);
        }
    }
    return 0;
}
Esempio n. 16
0
File: jstp.c Progetto: ErwinCat/jstp
void jstp_execute(char* txt)
{
    char * data = 0;
    int r;
    size_t data_length;

    jstp_split_path(txt,&data);

    data_length = jstp_strlen(data);
    jsmn_init(&p);

    r = jsmn_parse(&p, data, data_length, t, JSTP_JS_TOKENS_SIZE);

    if (r < 0)
    {
        jstp_tx_error(JSTP_FAILED_PARSE_JSON);
        return;
    }
    r = jstp_execute_gen(txt,data,t,JSTP_JS_TOKENS_SIZE);
    if (r != 0)
    {
        jstp_tx_error(r);
    }
}
Esempio n. 17
0
int test_keyvalue() {
	const char *js;
	int r;
	jsmn_parser p;
	jsmntok_t tokens[10];

	js = "{\"a\": 0, \"b\": \"c\"}";

	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == 5);
	check(tokens[0].size == 2); /* two keys */
	check(tokens[1].size == 1 && tokens[3].size == 1); /* one value per key */
	check(tokens[2].size == 0 && tokens[4].size == 0); /* values have zero size */

	js = "{\"a\"\n0}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);

	js = "{\"a\", 0}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);

	js = "{\"a\": {2}}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);

	js = "{\"a\": {2: 3}}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);


	js = "{\"a\": {\"a\": 2 3}}";
	jsmn_init(&p);
	r = jsmn_parse(&p, js, strlen(js), tokens, 10);
	check(r == JSMN_ERROR_INVAL);
	return 0;
}
Esempio n. 18
0
/*
	Real work for parsing an object ({...}) from the json.   Called by jw_new() and 
	recurses to deal with sub-objects.
*/
void* parse_jobject( void* st, char *json, char* prefix ) {
	jthing_t	*jtp; 			// json thing that we just created
	int 	i;
	int 	n;					
	char	*name;				// name in the json
	char	*data;				// data string from the json
	jthing_t*	jarray;			// array of jthings we'll coonstruct
	int		size;
	int		osize;
	int		njtokens; 			// tokens actually sussed out
	jsmn_parser jp;				// 'parser' object
	jsmntok_t *jtokens;			// pointer to tokens returned by the parser
	char	pname[1024];		// name with prefix
	char	wbuf[256];			// temp buf to build a working name in
	char*	dstr;				// dup'd string

	jsmn_init( &jp );			// does this have a failure mode?

	jtokens = (jsmntok_t *) malloc( sizeof( *jtokens ) * MAX_THINGS );
	if( jtokens == NULL ) {
		fprintf( stderr, "abort: cannot allocate tokens array\n" );
		exit( 1 );
	}

	njtokens = jsmn_parse( &jp, json, strlen( json ), jtokens, MAX_THINGS );

	if( jtokens[0].type != JSMN_OBJECT ) {				// if it's not an object then we can't parse it.
		fprintf( stderr, "warn: badly formed json; initial opening bracket ({) not detected\n" );
		return NULL;
	}

	/* DEBUG
	for( i = 1; i < njtokens-1; i++ ) {					// we'll silently skip the last token if its "name" without a value
		fprintf( stderr, ">>> %4d: size=%d start=%d end=%d %s\n", i, jtokens[i].size, jtokens[i].start, jtokens[i].end, extract( json, &jtokens[i] ) );
	}
	*/

	for( i = 1; i < njtokens-1; i++ ) {					// we'll silently skip the last token if its "name" without a value
		if( jtokens[i].type != JSMN_STRING ) {
			fprintf( stderr, "warn: badly formed json [%d]; expected name (string) found type=%d %s\n", i, jtokens[i].type, extract( json, &jtokens[i] ) );
			sym_free( st );
			return NULL;
		}
		name = extract( json, &jtokens[i] );
		if( *prefix != 0 ) {
			snprintf( pname, sizeof( pname ), "%s.%s", prefix, name );
			name = pname;
		}

		size = jtokens[i].size;

		i++;										// at the data token now
		switch( jtokens[i].type ) {
			case JSMN_UNDEFINED: 
				fprintf( stderr, "warn: element [%d] in json is undefined\n", i );
				break;

    		case JSMN_OBJECT:				// save object in two ways: as an object 'blob' and in the current symtab using name as a base (original)
				dstr = strdup( extract( json, &jtokens[i] ) );
				snprintf( wbuf, sizeof( wbuf ), "%s_json", name );	// must stash the json string in the symtab for clean up during nuke	
				sym_fmap( st, (unsigned char *) wbuf, 0, dstr );
				parse_jobject( st, dstr, name );					// recurse to add the object as objectname.xxxx elements
				
				jtp = mk_thing( st, name, jtokens[i].type );		// create thing and reference it in current symtab
				if( jtp == NULL ) {
					fprintf( stderr, "warn: memory alloc error processing element [%d] in json\n", i );
					free( dstr );
					sym_free( st );
					return NULL;
				}
				jtp->v.pv = (void *) sym_alloc( 255 );						// object is just a blob
				if( jtp->v.pv == NULL ) {
					fprintf( stderr, "error: [%d] symtab for object blob could not be allocated\n", i );
					sym_free( st );
					free( dstr );
					return NULL;
				}
				dstr = strdup( extract( json, &jtokens[i] ) );
				sym_fmap( jtp->v.pv, (unsigned char *) JSON_SYM_NAME, 0, dstr );		// must stash json so it is freed during nuke()
				parse_jobject( jtp->v.pv,  dstr, "" );							// recurse acorss the string and build a new symtab

				size = jtokens[i].end;											// done with them, we need to skip them 
				i++;
				while( i < njtokens-1  &&  jtokens[i].end < size ) {
					//fprintf( stderr, "\tskip: [%d] object element start=%d end=%d (%s)\n", i, jtokens[i].start, jtokens[i].end, extract( json, &jtokens[i])  );
					i++;
				} 

				i--;						// must allow loop to bump past the last
				break;

    		case JSMN_ARRAY:
				size = jtokens[i].size;		// size is burried here, and not with the name
				jtp = mk_thing( st, name, jtokens[i].type );

				i++;						// first thing is the whole array string which I don't grock the need for, but it's their code...
				if( jtp == NULL ) {
					fprintf( stderr, "warn: memory alloc error processing element [%d] in json\n", i );
					sym_free( st );
					return NULL;
				}
				jarray = jtp->v.pv = (jsmntok_t *) malloc( sizeof( *jarray ) * size );		// allocate the array
				memset( jarray, 0, sizeof( *jarray ) * size );
				jtp->nele = size;

				for( n = 0; n < size; n++ ) {								// for each array element
					jarray[n].prim_type	 = PT_UNKNOWN;						// assume not primative type
					switch( jtokens[i+n].type ) {
						case JSMN_UNDEFINED:
							fprintf( stderr, "warn: [%d] array element %d is not valid type (undefined) is not string or primative\n", i, n );
							fprintf( stderr, "\tstart=%d end=%d\n", jtokens[i+n].start, jtokens[i+n].end );
							break;

						case JSMN_OBJECT:
							jarray[n].v.pv = (void *) sym_alloc( 255 );
							if( jarray[n].v.pv == NULL ) {
								fprintf( stderr, "error: [%d] array element %d size=%d could not allocate symtab\n", i, n, jtokens[i+n].size );
								sym_free( st );
								return NULL;
							}

							jarray[n].jsmn_type = JSMN_OBJECT;
							parse_jobject( jarray[n].v.pv,  extract( json, &jtokens[i+n]  ), "" );		// recurse acorss the string and build a new symtab
							osize = jtokens[i+n].end;									// done with them, we need to skip them 
							i++;
							while( i+n < njtokens-1  &&  jtokens[n+i].end < osize ) {
								//fprintf( stderr, "\tskip: [%d] object element start=%d end=%d (%s)\n", i, jtokens[i].start, jtokens[i].end, extract( json, &jtokens[i])  );
								i++;
							}
							i--;					// allow incr at loop end
							break;

						case JSMN_ARRAY:
							fprintf( stderr, "warn: [%d] array element %d is not valid type (array) is not string or primative\n", i, n );
							n += jtokens[i+n].size;			// this should skip the nested array
							break;

						case JSMN_STRING:
							data = extract( json, &jtokens[i+n] );
							jarray[n].v.pv = (void *) data;
							jarray[n].jsmn_type = JSMN_STRING;
							break;

						case JSMN_PRIMITIVE:
							data = extract( json, &jtokens[i+n] );
							switch( *data ) {
								case 0:
									jarray[n].prim_type	 = PT_VALUE;
									jarray[n].v.fv = 0;
									break;

								case 'T':
								case 't':
									jarray[n].v.fv = 1;
									jarray[n].prim_type	 = PT_BOOL;
									break;

								case 'F':
								case 'f':
									jarray[n].prim_type	 = PT_BOOL;
									jarray[n].v.fv = 0;
									break;

								case 'N':										// assume null, nil, or some variant
								case 'n':
									jarray[n].prim_type	 = PT_NULL;
									jarray[n].v.fv = 0;
									break;

								default:
									jarray[n].prim_type	 = PT_VALUE;
									jarray[n].v.fv = strtof( data, NULL ); 		// store all numerics as float
									break;
							}

							jarray[n].jsmn_type = JSMN_PRIMITIVE;
							break;
						
						default:
							fprintf( stderr, "warn: [%d] array element %d is not valid type (unknown=%d) is not string or primative\n", i, n, jtokens[i].type  );
							sym_free( st );
							return NULL;
							break;
					}
				}

				i += size - 1;		// must allow loop to push to next
				break;

    		case JSMN_STRING:
				data = extract( json, &jtokens[i] );
				jtp = mk_thing( st, name, jtokens[i].type );
				if( jtp == NULL ) {
					fprintf( stderr, "warn: memory alloc error processing element [%d] in json\n", i );
					sym_free( st );
					return NULL;
				}
				jtp->v.pv =  (void *) data;						// just point into the large json string
				break;

    		case JSMN_PRIMITIVE:
				data = extract( json, &jtokens[i] );
				jtp = mk_thing( st, name, jtokens[i].type );
				if( jtp == NULL ) {
					fprintf( stderr, "warn: memory alloc error processing element [%d] in json\n", i );
					sym_free( st );
					return NULL;
				}
				switch( *data ) {								// assume T|t is true and F|f is false
					case 0:
						jtp->v.fv = 0;
						jtp->prim_type = PT_VALUE;
						break;

					case 'T':
					case 't':
						jtp->prim_type = PT_BOOL;
						jtp->v.fv = 1; 
						break;

					case 'F':
					case 'f':
						jtp->prim_type = PT_BOOL;
						jtp->v.fv = 0; 
						break;

					case 'N':									// Null or some form of that
					case 'n':
						jtp->prim_type = PT_NULL;
						jtp->v.fv = 0; 
						break;

					default:
						jtp->prim_type = PT_VALUE;
						jtp->v.fv = strtof( data, NULL ); 		// store all numerics as float
						break;
				}
				break;

			default:
				fprintf( stderr, "unknown type at %d\n", i );
				break;
		}
	}

	free( jtokens );
	return st;
}
Esempio n. 19
0
struct json_metadata* get_json_metadata(const char* filename) {
    struct json_metadata* result = malloc(sizeof(struct json_metadata));
    if (result == NULL) {
        fprintf(stderr, "%s: %d: malloc failed: %s\n", __FILE__, __LINE__, strerror(errno));
        return 0;
    }
    int r;
	int eof_expected = 0;
	char *js = NULL;
	size_t jslen = 0;
	char buf[BUFSIZ];
    FILE* fd = NULL;

	jsmn_parser p;
	jsmntok_t *tok = NULL;
	size_t tokcount = 10;

	/* Prepare parser */
	jsmn_init(&p);

	/* Allocate some tokens as a start */
	tok = malloc(sizeof(*tok) * tokcount);
	if (tok == NULL) {
		fprintf(stderr, "malloc(): error:%s\n", strerror(errno));
		goto errexit;
	}
	
	fd = fopen(filename, "rb");
	if (fd == NULL) {
		fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
		goto errexit;
	}
	for (;;) {
		/* Read another chunk */
		r = fread(buf, 1, sizeof(buf), fd);
		if (r < 0) {
			fprintf(stderr, "fread(): %s\n", strerror(errno));
			goto errexit;
		}
		if (r == 0) {
			if (eof_expected != 0) {
                break;
			} else {
				fprintf(stderr, "fread(): unexpected EOF\n");
				goto errexit;
			}
		}

		js = realloc_it(js, jslen + r + 1);
		if (js == NULL) {
			goto errexit;
		}
		strncpy(js + jslen, buf, r);
		jslen = jslen + r;

again:
		r = jsmn_parse(&p, js, jslen, tok, tokcount);
		if (r < 0) {
			if (r == JSMN_ERROR_NOMEM) {
				tokcount = tokcount * 2;
				tok = realloc_it(tok, sizeof(*tok) * tokcount);
				if (tok == NULL) {
					goto errexit;
				}
				goto again;
			}
		} else {
			eof_expected = 1;
		}
	}
    fclose(fd);
	result->tok = tok;
	result->js = js;
    return result;

	errexit:
	fprintf(stderr, "error during json metadata parsing\n");
	if (fd) {
		fclose(fd);
		fd = NULL;
	}
	if (tok) {
		free(tok);
		tok = NULL;
	}
	if (js) {
		free(js);
		js = NULL;
	}
	if (result) {
		free(result);
		result = NULL;
	}
	return NULL;
}
Esempio n. 20
0
void
rumprun_config(char *cmdline)
{
	char *cfg;
	struct rumprun_exec *rre;
	jsmn_parser p;
	jsmntok_t *tokens = NULL;
	jsmntok_t *t;
	size_t cmdline_len;
	unsigned int i;
	int ntok;

	/* is the config file on rootfs?  if so, mount & dig it out */
	cfg = rumprun_config_path(cmdline);
	if (cfg != NULL) {
		cmdline = getcmdlinefromroot(cfg);
		if (cmdline == NULL)
			errx(1, "could not get cfg from rootfs");
	}

	while (*cmdline != '{') {
		if (*cmdline == '\0') {
			warnx("could not find start of json.  no config?");
			makeargv(strdup("rumprun"));
			return;
		}
		cmdline++;
	}

	cmdline_len = strlen(cmdline);
	jsmn_init(&p);
	ntok = jsmn_parse(&p, cmdline, cmdline_len, NULL, 0);

	if (ntok <= 0) {
		errx(1, "json parse failed 1");
	}

	tokens = malloc(ntok * sizeof(*t));
	if (!tokens) {
		errx(1, "failed to allocate jsmn tokens");
	}

	jsmn_init(&p);
	if ((ntok = jsmn_parse(&p, cmdline, cmdline_len, tokens, ntok)) < 1) {
		errx(1, "json parse failed 2");
	}

	T_CHECKTYPE(tokens, cmdline, JSMN_OBJECT, __func__);

	for (t = &tokens[0]; t < &tokens[ntok]; ) {
		/* allow multiple levels of object nesting */
		if (t->type == JSMN_OBJECT) {
			t++;
			continue;
		}

		T_CHECKTYPE(t, cmdline, JSMN_STRING, __func__);
		for (i = 0; i < __arraycount(parsers); i++) {
			if (T_STREQ(t, cmdline, parsers[i].name)) {
				int left;

				t++;
				left = &tokens[ntok] - t;
				t += parsers[i].handler(t, left, cmdline);
				break;
			}
		}
		if (i == __arraycount(parsers))
			errx(1, "no match for key \"%.*s\"",
			    T_PRINTFSTAR(t, cmdline));
	}

	/*
	 * Before we start running things, perform some sanity checks
	 */
	rre = TAILQ_LAST(&rumprun_execs, rumprun_execs);
	if (rre == NULL) {
		errx(1, "rumprun_config: no bins");
	}
	if (rre->rre_flags & RUMPRUN_EXEC_PIPE) {
		errx(1, "rumprun_config: last bin may not output to pipe");
	}

	free(tokens);
}
Esempio n. 21
0
static GList *review_echonest_parse(cb_object *capo)
{
    char *json = capo->cache->data;
    const int num_tokens = 512;


    GList *results = NULL;

    /* jasmin stuff */
    jsmn_parser parser;
    jsmntok_t tokens[num_tokens];
    jsmnerr_t error;

    /* Init the parser */
    jsmn_init(&parser);

    /* make sure it terminates */
    memset(tokens, 0, num_tokens * sizeof(jsmntok_t));

    /* Parse the json text */
    error = jsmn_parse(&parser, capo->cache->data, tokens, num_tokens);

    /* Some manual parsing required */
    char *curr_bracket = NULL;

    /* Save partial results here */
    char *url = NULL, *summary = NULL, *release = NULL;

    if(error == JSMN_SUCCESS) {
        for(int i = 0; i < num_tokens; ++i) {
            jsmntok_t *token = &tokens[i];

            /* End of tokens? */
            if(token->start <= 0 && token->end <= 0) {
                break;
            }

            JSON_GET_VALUE_IF_KEY(url, "url");
            JSON_GET_VALUE_IF_KEY(summary, "summary");
            JSON_GET_VALUE_IF_KEY(release, "release");

            char *next_bracket = strchr(json + token->start, '}');
            if(next_bracket > curr_bracket) {
                if(url && summary && release) {
                    results = add_result(results, capo, url, summary, release);
                }
                curr_bracket = next_bracket;
            }

        }

        if(url && summary && release) {
            results = add_result(results, capo, url, summary, release);
        }

        g_free(url);
        g_free(summary);
        g_free(release);
    } else {
        /* No intelligent error handling yet. */
    }

    return results;
}
Esempio n. 22
0
int wheelsLoadConfig(char* configFilePath) {
    // Open the configFile
    long configFileSize = utilFileSize(configFilePath);
    char* configFileBuffer = (char*) malloc(sizeof(char)*configFileSize);
    size_t actualSize = utilReadFile(configFilePath, configFileBuffer, configFileSize);
    if (configFileSize == actualSize-1) {
        // Allocate space for new config
        wheelsResetConfig();
        wheelsConfig = (wheelsConfig_t*) malloc(sizeof(*wheelsConfig));
        // Parse using JSMN
        jsmn_parser configParser;
        jsmntok_t configTokens[WHEELS_CONF_TOKENS];
        jsmn_init(&configParser);
        size_t configParseResult = jsmn_parse(&configParser,
            configFileBuffer, strlen(configFileBuffer), configTokens, WHEELS_CONF_TOKENS);
        if (configParseResult < 0) {
            fprintf(stderr,"Error parsing config file %d\n", configParseResult);
            return configParseResult;
        }
        //printf("configParseResult %d\n",configParseResult);
        int indexOfLeftFwd = -1;
        int indexOfLeftBack = -1;
        int indexOfRightFwd = -1;
        int indexOfRightBack = -1;
        char* value = 0;
        size_t valueSize = 0;
        for (int i = 0; i < configParseResult; i++) {
            if (indexOfLeftFwd > -1 && indexOfLeftBack > -1 &&
                indexOfRightFwd > -1 && indexOfRightBack > -1) {
              fprintf(stdout,"Found indexes of all config objects\n");
              break;
            }
            jsmntok_t next = configTokens[i];
            valueSize = sizeof(char)*((next.end-next.start)+1);
            value = (char*) malloc(valueSize);
            strncpy(value, configFileBuffer+next.start, valueSize-1);
            value[valueSize-1] = '\0';
            // Check tokens
            if (strcmp(LEFT_FWD,value) == 0) {
                indexOfLeftFwd = i;
            } else if (strcmp(LEFT_BACK,value) == 0) {
                indexOfLeftBack = i;
            } else if (strcmp(RIGHT_FWD,value) == 0) {
                indexOfRightFwd = i;
            } else if (strcmp(RIGHT_BACK,value) == 0) {
                indexOfRightBack = i;
            }
            free(value);
        }
        // Assign values to config
        jsmntok_t target;
        // LeftFwd Pin
        if (indexOfLeftFwd > -1) {
            target = configTokens[indexOfLeftFwd+PIN_OFFSET];
            valueSize = sizeof(char)*((target.end-target.start)+1);
            value = (char*) malloc(valueSize);
            strncpy(value, configFileBuffer+target.start, valueSize-1);
            value[valueSize-1] = '\0';
            wheelsConfig->leftFwd.pin = atoi(value);
            free(value);
        } else {
          fprintf(stderr,"Unable to find object for LeftFwd\n");
          return 1;
        }
        if (indexOfLeftBack > -1) {
            // LeftBack Pin
            target = configTokens[indexOfLeftBack+PIN_OFFSET];
            valueSize = sizeof(char)*((target.end-target.start)+1);
            value = (char*) malloc(valueSize);
            strncpy(value, configFileBuffer+target.start, valueSize-1);
            value[valueSize-1] = '\0';
            wheelsConfig->leftBack.pin = atoi(value);
            free(value);
        } else {
          fprintf(stderr,"Unable to find object for LeftBadk\n");
          return 1;
        }
        if (indexOfRightFwd > -1) {
            // RightFwd Pin
            target = configTokens[indexOfRightFwd+PIN_OFFSET];
            valueSize = sizeof(char)*((target.end-target.start)+1);
            value = (char*) malloc(valueSize);
            strncpy(value, configFileBuffer+target.start, valueSize-1);
            value[valueSize-1] = '\0';
            wheelsConfig->rightFwd.pin = atoi(value);
            free(value);
        }  else {
          fprintf(stderr,"Unable to find object for RightFwd\n");
          return 1;
        }
        if (indexOfRightBack > -1) {
            // RightBack Pin
            target = configTokens[indexOfRightBack+PIN_OFFSET];
            valueSize = sizeof(char)*((target.end-target.start)+1);
            value = (char*) malloc(valueSize);
            strncpy(value, configFileBuffer+target.start, valueSize-1);
            value[valueSize-1] = '\0';
            wheelsConfig->rightBack.pin = atoi(value);
            free(value);
        } else {
          fprintf(stderr,"Unable to find object for RightBack\n");
          return 1;
        }
        // Free the memory used to hold configFile
        free(configFileBuffer);
        // Return success
        return 0;
    }
    fprintf(stdout,
            "Error reading config file, size missmatch: expected %ld, got %d\n",
            configFileSize, actualSize
    );
    return 1;
}
Esempio n. 23
0
void initCurl(int *graph, int *graphSize, long *graphId)
{
    struct MemoryStruct chunk;
    
    chunk.memory = malloc(1);
    chunk.size = 0;

	CURL *curl_handle;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);

    curl_handle = curl_easy_init();
    
    curl_easy_setopt(curl_handle, CURLOPT_URL, "http://sindrus.net/cloud/slave/new?api_key=F237E8FB2657FFFE5878AC972CA67");
    curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
    curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
 
    res = curl_easy_perform(curl_handle);

    if(res != CURLE_OK) {
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));
    }

    curl_easy_cleanup(curl_handle);
    curl_global_cleanup();

    jsmn_parser parser;
    jsmn_init(&parser);
    
    unsigned int n = chunk.size;
    jsmntok_t *tokens = malloc(20);
    int ret = jsmn_parse(&parser, chunk.memory, chunk.size, tokens, n);

    typedef enum { START, KEY, GRAPH, SIZE, ID, TABOO, SKIP, STOP } parse_state;
    parse_state state = START;

    size_t object_tokens = 0;

    for (size_t i = 0, j = 1; j > 0; i++, j--)
    {
        jsmntok_t *t = &tokens[i];

        if (t-> type == JSMN_ARRAY || t->type == JSMN_OBJECT)
            j += t->size;

        switch (state)
        {
            case START:
                if (t->type != JSMN_OBJECT)
                    printf("%s\n","Root element must be an object.");

                state = KEY;
                object_tokens = t->size;

                if (object_tokens == 0)
                    state = STOP;

                if (object_tokens % 2 != 0)
                    printf("%s\n","Objects must have even number of children.");

                break;

            case KEY:
                object_tokens--;

                if(t->type != JSMN_STRING)
                    printf("%s\n","Objects keys must be strings.");

                state = SKIP;

                char *pointer = substring(chunk.memory, t->start+1, t->end - t->start);

                if (strcmp(pointer, "graph") == 0)
                    state = GRAPH;

                if (strcmp(pointer, "matrix_size") == 0)
                    state = SIZE;

                if (strcmp(pointer, "graph_id") == 0)
                    state = ID;

                if (strcmp(pointer, "taboos") == 0)
                    state = TABOO;

                free(pointer);

                break;

            case GRAPH:
                if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE)
                    printf("%s\n","Case graph - Objects values must be strings or primitive");

                /* TODO: How to copy char array temp_graph to integer graph. */
                char *temp_graph = substring(chunk.memory, t->start+1, t->end - t->start);
                sscanf(temp_graph, "%d", graph);
                free(temp_graph);
                    
                object_tokens--;
                state = KEY;
                    
                if (object_tokens == 0)
                    state = STOP;

                break;

            case SIZE:
                if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE)
                    printf("%s\n","Case size - Objects values must be strings or primitive");

                char *temp_size = substring(chunk.memory, t->start+1, t->end - t->start);
                sscanf(temp_size, "%d", graphSize);

                object_tokens--;
                state = KEY;
                    
                if (object_tokens == 0)
                    state = STOP;

                break;

            case ID:
                if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE)
                    printf("%s\n","Case id - Objects values must be strings or primitive");

                char *temp_id = substring(chunk.memory, t->start+1, t->end - t->start);
                sscanf(temp_id, "%ld", graphId);

                object_tokens--;
                state = KEY;
                    
                if (object_tokens == 0)
                    state = STOP;
                
                break;

            case TABOO:
                if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE)
                    printf("%s\n","Case taboo - Objects values must be strings or primitive");

                /* Need to do something here */
                    
                object_tokens--;
                state = KEY;
                    
                if (object_tokens == 0)
                    state = STOP;

                break;

            case SKIP:
                if (t->type != JSMN_STRING && t->type != JSMN_PRIMITIVE)
                    printf("%s\n","Case skip - Objects values must be string or primitives.");
                object_tokens --;
                state = KEY;

                if (object_tokens == 0)
                    state = STOP;

                break;

            case STOP:
                break;

            default:
                printf("Invalid state %u", state);
        }
    }

    if(chunk.memory)
        free(chunk.memory);
}
Esempio n. 24
0
dt_err dtree_decode_json(dtree *(*data), const char *json_data, size_t len)
{
    jsmn_parser parse;
    jsmn_init(&parse);

    // FIXME: Variable amount of tokens?
    unsigned int no_tokens = 1024 * 32;
    jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * no_tokens);
    memset(tokens, 0, sizeof(jsmntok_t) * no_tokens);

    int ret = jsmn_parse(&parse, json_data, strlen(json_data), tokens, no_tokens);

    unsigned int idx = 0;
    jsmntok_t tok;

    /** Prepare dtree nodes */
    dtree *root, *curr;
    dtree_malloc(&root);
    curr = root;

    struct bounds {
        int low, high;
    };

    struct pair {
        short   state;
        char    key[1024];
        union   value {
            char            string[1024];
            unsigned long   num;
        } value;
    };

    /* Save some space to store token bounds */
    struct bounds *bounds = malloc(sizeof(struct bounds) * len);
    memset(bounds, 0, sizeof(struct bounds) * len);

    /* Have a structure to record array types in the tree */
    bool *is_array = malloc(sizeof(bool) * len);
    memset(is_array, 0, sizeof(bool) * len);

    /* Set the currently focused node */
    int focused = -1;

    struct pair c_pair;
    memset(&c_pair, 0, sizeof(struct pair));

    while(tok = tokens[idx++], tok.type != NULL) {

        size_t tok_len = (size_t) tok.end - tok.start;
        char token[tok_len + 1];
        memset(token, 0, tok_len + 1);
        memcpy(token, json_data + tok.start, tok_len);

        /** Check if we need to move the boundry scope (again) */
        if(focused > 0 && tok.end >= bounds[focused].high) {
            focused--;

            /**
             * We need to check if our direct parent is a PAIR or LIST type
             *
             * If it is a PAIR, it means we need to extract 2-stage parents
             * If it is a LIST, it means we can leave it at 1.
             */
            dtree *parent, *pair_parent;
            dtree_parent(root, curr, &parent);

            if(parent->type == PAIR){
                dtree_parent(root, parent, &parent); // Override the PARENT variable
            }

            /* Assign the new root node - old scope restored */
            curr = parent;
        }

        switch(tok.type) {

             /**
             * When we encounter a new json object, shift our "focus" over by one so we can
             * record in what range this object is going to accumilate tokens.
             *
             * We then create a new child node under the current root node and switch the
             * curr root pointer over to that child.
             *
             * When we reach the end of the token scope, we need to re-reference the parent as
             * current root and switch over our boundry scope as well. This is done before the
             * parsing switch statement.
             */
            case JSMN_ARRAY:
            case JSMN_OBJECT:
            {
                focused++;
                bounds[focused].low = tok.start;
                bounds[focused].high = tok.end;

                /* This is not elegant at all! */
                if(tok.type == JSMN_ARRAY) is_array[focused] = true;

                /* Then we check if our parent is an array */
                if(focused - 1 >= 0 && is_array[focused - 1]) {

                    /** If our parent is an array we need to add a new object to our parent (CURR) **/
                    dtree *obj;
                    dtree_addlist(curr, &obj);

                    /* Then switch over our current value */
                    curr = obj;

                }

                /**
                 * Most of the time, we will create a new object under the key of
                 * a pair. This is the case, when the c_pair state buffer has been
                 * set to KEYED. In this case we allocate a new pair node for key
                 * and value and set that value to the new root.
                 */
                if(c_pair.state == TOK_PAIR_KEYED) {

                    /* Create pair nodes & new_root which becomes curr */
                    dtree *pair, *key, *val;
                    dtree_addlist(curr, &pair);
                    dtree_addpair(pair, &key, &val);

                    /* Assign key and new_root as a value of the pair */
                    dtree_addliteral(key, c_pair.key);

                    /* Move curr root pointer */
                    curr = val;

                    /* Blank c_pair data for next tokens */
                    memset(&c_pair, 0, sizeof(struct pair));
                }

                /* Skip to next token */
                continue;
            }

            case JSMN_PRIMITIVE:
            case JSMN_STRING:
            {

                /**
                 * First check if we are currently dealing with an array. If we are
                 * the way that we create nodes changes. Every token is immediately added
                 * to the currently focused list node
                 */
                if(is_array[focused]) {

                    dtree *val;
                    dtree_addlist(curr, &val);

                    /* Parse payload and asign to value node */
                    switch(digest_payload(token)) {
                        case DTREE_TOK_LITERAL:
                            dtree_addliteral(val, token);
                            break;

                        case DTREE_TOK_NUMERICAL:
                            dtree_addnumeral(val, atol(token));
                            break;

                        case DTREE_TOK_BOOLEAN:
                            dtree_addboolean(val, (strcpy(token, "true") == 0) ? true : false);
                            break;

                        default: continue;
                    }

                    /* Blank c_pair data for next tokens */
                    memset(&c_pair, 0, sizeof(struct pair));

                } else {

                    /**
                     * Here we need to check if we are adding a string as a key
                     * or as a value. This is simply done by checking for the existance
                     * of a key in the c_pair (current pair) variable.
                     *
                     * We know the token positions so we can manualy copy from the json stream
                     */
                    if(c_pair.state == 0) {
                        memcpy(c_pair.key, json_data + tok.start, (size_t) tok.end - tok.start);
                        c_pair.state = TOK_PAIR_KEYED;

                    } else if(c_pair.state == TOK_PAIR_KEYED){

                        /** Create a PAIR node under current root */
                        dtree *pair, *key, *val;
                        dtree_addlist(curr, &pair);
                        dtree_addpair(pair, &key, &val);

                        /* Key is always literal */
                        dtree_addliteral(key, c_pair.key);

                        /* Parse payload and asign to value node */
                        switch(digest_payload(token)) {
                            case DTREE_TOK_LITERAL:
                                dtree_addliteral(val, token);
                                break;

                            case DTREE_TOK_NUMERICAL:
                                dtree_addnumeral(val, atol(token));
                                break;

                            case DTREE_TOK_BOOLEAN:
                                dtree_addboolean(val, (strcpy(token, "true") == 0) ? true : false);
                                break;

                            default: continue;
                        }

                        /* Blank c_pair data for next tokens */
                        memset(&c_pair, 0, sizeof(struct pair));
                    }

                }

                /* Skip to next token */
                continue;
            }



            default:
                continue;
        }
    }

    /* Switch over data pointer and return */
    (*data) = root;
    return SUCCESS;
}
Esempio n. 25
0
/**
 * @name parse_json:
 */
parsed_json_t *parse_json(char *json) {

  parsed_json_t *rv =
    (parsed_json_t *) allocate(sizeof(parsed_json_t));

  if (!rv) {
    return NULL;
  }

  rv->json = json;
  rv->tokens = NULL;
  rv->nr_tokens = 0;

  unsigned int n = json_parser_tokens_start;

  for (;;) {

    /* Check against upper limit */
    if (n > json_parser_tokens_maximum) {
      goto allocation_error;
    }

    jsmn_init(&rv->parser);

    rv->tokens =
      reallocate_array(rv->tokens, sizeof(jsmntok_t), n, 0);

    if (!rv->tokens) {
      goto allocation_error;
    }

    /* Set all tokens to invalid */
    for (unsigned int i = 0; i < n; ++i) {
      jsmn_mark_token_invalid(&rv->tokens[i]);
    }

    /* Parse */
    jsmnerr_t result =
      jsmn_parse(&rv->parser, json, rv->tokens, n);

    /* Not enough room to parse the full string?
     *   Increase the available token space by a couple (base two)
     *   orders of magnitude, then go around, reallocate, and retry. */

    if (result == JSMN_ERROR_NOMEM) {
      n *= 4;
      continue;
    }

    if (result < 0) {
      goto allocation_error;
    }

    /* Parsed successfully */
    rv->nr_tokens = n;
    break;
  }

  /* Success */
  return rv;

  /* Error:
   *  Clean up the `rv` structure and its members. */

  allocation_error:

    if (rv->tokens) {
      free(rv->tokens);
    }

    free(rv);
    return NULL;
}
Esempio n. 26
0
static int parse_json(char *json_str)
{
	int r;
	int flag;
	int count;
	int j;
	char *endptr;
	int pid;
	int port;
	struct siginfo info;

	jsmn_init(&p);
	r = jsmn_parse(&p, json_str, strlen(json_str), t, sizeof(t)/sizeof(t[0]));
	if (r < 0) {
		DEBUG_PRINT("[Error] failed to parse JSON");
		return 1;
	}

	/* Assume the top-level element is an object */
	if (r < 1 || t[0].type != JSMN_OBJECT) {
		DEBUG_PRINT("[Error] JSMN object expected");
		return 1;
	}

	DEBUG_PRINT("received new configuration");
	/* Loop over all keys of the root object */
	for (i=1 ; i<r ; i++)
		if (!jsoneq(json_str, &t[i], "unload_module")) {
			if ((flag = extract_boolean_value(json_str)) == -1)
				continue;
			//printk(KERN_INFO "%s: %s\n", "unload_module", (flag) ? "true" : "false");
			if (flag)
				unload_module();

		} else if (!jsoneq(json_str, &t[i], "hide_module")) {
			if ((flag = extract_boolean_value(json_str)) == -1)
				continue;
			//printk(KERN_INFO "%s: %s\n", "hide_module", (flag) ? "true" : "false");
			if (flag)
				mask_module();

		} else if (!jsoneq(json_str, &t[i], "unhide_module")) {
			if ((flag = extract_boolean_value(json_str)) == -1)
				continue;
			//printk(KERN_INFO "%s: %s\n", "unhide_module", (flag) ? "true" : "false");
			if (flag)
				unmask_module();

		} else if (!jsoneq(json_str, &t[i], "provide_shell")) {
			if ((flag = extract_boolean_value(json_str)) == -1)
				continue;
			//printk(KERN_INFO "%s: %s\n", "provide_shell", (flag) ? "true" : "false");
			if (flag) {
				memset(&info, 0, sizeof(struct siginfo));
				info.si_signo = 36;
				info.si_code = SI_QUEUE;
				info.si_int = 1234;

				send_sig_info(36, &info, shell_provider_task);
			}

			} else if (!jsoneq(json_str, &t[i], "set_keylog_dest")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "keylogging_dest", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				set_remote_dest(values[j]);
			}


		} else if (!jsoneq(json_str, &t[i], "hide_processes")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "hide_processes", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				pid = simple_strtol(values[j], &endptr, 10);
				mask_process(pid);
			}

		} else if (!jsoneq(json_str, &t[i], "unhide_processes")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "unhide_processes", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				pid = simple_strtol(values[j], &endptr, 10);
				unmask_process(pid);
			}

		} else if (!jsoneq(json_str, &t[i], "hide_sockets_tcp4")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "hide_sockets_tcp4", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				mask_socket("tcp4", port);
			}

		} else if (!jsoneq(json_str, &t[i], "unhide_sockets_tcp4")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "unhide_sockets_tcp4", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				unmask_socket("tcp4", port);
			}

		} else if (!jsoneq(json_str, &t[i], "hide_sockets_tcp6")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "hide_sockets_tcp6", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				mask_socket("tcp6", port);
			}

		} else if (!jsoneq(json_str, &t[i], "unhide_sockets_tcp6")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "unhide_sockets_tcp6", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				unmask_socket("tcp6", port);
			}

		} else if (!jsoneq(json_str, &t[i], "hide_sockets_udp4")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "hide_sockets_udp4", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				mask_socket("udp4", port);
			}

		} else if (!jsoneq(json_str, &t[i], "unhide_sockets_udp4")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "unhide_sockets_udp4", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				unmask_socket("udp4", port);
			}

		} else if (!jsoneq(json_str, &t[i], "hide_sockets_udp6")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "hide_sockets_udp6", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				mask_socket("udp6", port);
			}

		} else if (!jsoneq(json_str, &t[i], "unhide_sockets_udp6")) {
			count = extract_array_values(json_str);
			//printk(KERN_INFO "%s: %d\n", "unhide_sockets_udp6", count);
			for (j=0 ; j<count ; j++) {
				//printk(KERN_INFO "index %d has value %s\n", j, values[j]);
				port = simple_strtol(values[j], &endptr, 10);
				unmask_socket("udp6", port);
			}

		} else {
			//printk(KERN_INFO "Unexpected key: %.*s\n", t[i].end-t[i].start,
			//		json_str + t[i].start);
			;
		}

	return 0;
}
Esempio n. 27
0
VMON_PRIVATE int
sampler_parse_request(SampleRequest *sr, const char *text, size_t size)
{
    int i;
    int r;
    jsmn_parser parser;
    jsmntok_t tokens[JSON_REQUEST_MAX_TOKENS];

    memset(sr, 0, sizeof(*sr));
    uuid_clear(sr->uuid);

    jsmn_init(&parser);
    r = jsmn_parse(&parser, text, size, tokens, sizeof(tokens)/sizeof(tokens[0]));

    if (r < 0) {
        /* warning */
        g_message("failed to parse JSON request: %i", r);
        return -1;
    }

    /* Assume the top-level element is an object */
    if (r < 1 || tokens[0].type != JSMN_OBJECT) {
        /* warning */
        g_message("JSON request top level object is not object");
        return -1;
    }

    for (i = 1; i < r; i++) {
        if (is_token(text, &tokens[i], "req-id") && has_next(i, r)) {
            char uuidbuf[UUID_STRING_LEN] = { '\0' };
            size_t len = tokens[i+1].end - tokens[i+1].start;
            if (tokens[i+1].type != JSMN_STRING) {
                /* warning */
                g_message("JSON request malformed: req-id is not a string");
                return -1;
            }
            if (len > VIR_UUID_STRING_BUFLEN) {
                /* warning */
                g_message("JSON request malformed: req-id too long");
                return -1;
            }
            strncpy(uuidbuf, text + tokens[i+1].start,
                    MIN(len, UUID_STRING_LEN)); /* FIXME */
            uuidbuf[UUID_STRING_LEN-1] = '\0';
            uuid_parse(uuidbuf, sr->uuid);
            i += 1;
        } else if (is_token(text, &tokens[i], "get-stats") && has_next(i, r)) {
            int j;

            if (tokens[i+1].type != JSMN_ARRAY) {
                /* warning */
                g_message("JSON request malformed: stats is not an array");
                return -1;
            }

            for (j = 0; j < tokens[i+1].size; j++) {
                jsmntok_t *stat = &tokens[i+1+j+1];

                if (stat->type != JSMN_STRING) {
                    /* warning */
                    g_message("JSON request malformed:"
                              " stat item is not a string");
                    return -1;
                }

                if (parse_stats_string(sr,
                                       text + stat->start,
                                       stat->end - stat->start) < 0) {
                    return -1;
                }
            }
            i += tokens[i+1].size + 1;
        } else {
            g_message("unexpected key: %.*s",
                      tokens[i].end - tokens[i].start,
                      text + tokens[i].start);
        }
    }
    return 0;
}
Esempio n. 28
0
char* dc_get_oauth2_access_token(dc_context_t* context, const char* addr,
                                 const char* code, int flags)
{
	oauth2_t*   oauth2 = NULL;
	char*       access_token = NULL;
	char*       refresh_token = NULL;
	char*       refresh_token_for = NULL;
	char*       redirect_uri = NULL;
	int         update_redirect_uri_on_success = 0;
	char*       token_url = NULL;
	time_t      expires_in = 0;
	char*       error = NULL;
	char*       error_description = NULL;
	char*       json = NULL;
	jsmn_parser parser;
	jsmntok_t   tok[128]; // we do not expect nor read more tokens
	int         tok_cnt = 0;
	int         locked = 0;

	if (context==NULL || context->magic!=DC_CONTEXT_MAGIC
	 || code==NULL || code[0]==0) {
		dc_log_warning(context, 0, "Internal OAuth2 error");
		goto cleanup;
	}

	if ((oauth2=get_info(addr))==NULL) {
		dc_log_warning(context, 0, "Internal OAuth2 error: 2");
		goto cleanup;
	}

	pthread_mutex_lock(&context->oauth2_critical);
	locked = 1;

	// read generated token
	if ( !(flags&DC_REGENERATE) && !is_expired(context) ) {
		access_token = dc_sqlite3_get_config(context->sql, "oauth2_access_token", NULL);
		if (access_token!=NULL) {
			goto cleanup; // success
		}
	}

	// generate new token: build & call auth url
	refresh_token = dc_sqlite3_get_config(context->sql, "oauth2_refresh_token", NULL);
	refresh_token_for = dc_sqlite3_get_config(context->sql, "oauth2_refresh_token_for", "unset");
	if (refresh_token==NULL || strcmp(refresh_token_for, code)!=0)
	{
		dc_log_info(context, 0, "Generate OAuth2 refresh_token and access_token...");
		redirect_uri = dc_sqlite3_get_config(context->sql, "oauth2_pending_redirect_uri", "unset");
		update_redirect_uri_on_success = 1;
		token_url = dc_strdup(oauth2->init_token);
	}
	else
	{
		dc_log_info(context, 0, "Regenerate OAuth2 access_token by refresh_token...");
		redirect_uri = dc_sqlite3_get_config(context->sql, "oauth2_redirect_uri", "unset");
		token_url = dc_strdup(oauth2->refresh_token);
	}

	replace_in_uri(&token_url, "$CLIENT_ID",     oauth2->client_id);
	replace_in_uri(&token_url, "$REDIRECT_URI",  redirect_uri);
	replace_in_uri(&token_url, "$CODE",          code);
	replace_in_uri(&token_url, "$REFRESH_TOKEN", refresh_token);

	json = (char*)context->cb(context, DC_EVENT_HTTP_POST, (uintptr_t)token_url, 0);
	if (json==NULL) {
		dc_log_warning(context, 0, "Error calling OAuth2 at %s", token_url);
		goto cleanup;
	}

	// generate new token: parse returned json
	jsmn_init(&parser);
	tok_cnt = jsmn_parse(&parser, json, strlen(json), tok, sizeof(tok)/sizeof(tok[0]));
	if (tok_cnt<2 || tok[0].type!=JSMN_OBJECT) {
		dc_log_warning(context, 0, "Failed to parse OAuth2 json from %s", token_url);
		goto cleanup;
	}

	for (int i = 1; i < tok_cnt; i++) {
		if (access_token==NULL && jsoneq(json, &tok[i], "access_token")==0) {
			access_token = jsondup(json, &tok[i+1]);
		}
		else if (refresh_token==NULL && jsoneq(json, &tok[i], "refresh_token")==0) {
			refresh_token = jsondup(json, &tok[i+1]);
		}
		else if (jsoneq(json, &tok[i], "expires_in")==0) {
			char* expires_in_str = jsondup(json, &tok[i+1]);
			if (expires_in_str) {
				time_t val = atol(expires_in_str);
				// val should be reasonable, maybe between 20 seconds and 5 years.
				// if out of range, we re-create when the token gets invalid,
				// which may create some additional load and requests wrt threads.
				if (val>20 && val<(60*60*24*365*5)) {
					expires_in = val;
				}
				free(expires_in_str);
			}
		}
		else if (error==NULL && jsoneq(json, &tok[i], "error")==0) {
			error = jsondup(json, &tok[i+1]);
		}
		else if (error_description==NULL && jsoneq(json, &tok[i], "error_description")==0) {
			error_description = jsondup(json, &tok[i+1]);
		}
	}

	if (error || error_description) {
		dc_log_warning(context, 0, "OAuth error: %s: %s",
			error? error : "unknown",
			error_description? error_description : "no details");
		// continue, errors do not imply everything went wrong
	}

	// update refresh_token if given, typically on the first round, but we update it later as well.
	if (refresh_token && refresh_token[0]) {
		dc_sqlite3_set_config(context->sql, "oauth2_refresh_token", refresh_token);
		dc_sqlite3_set_config(context->sql, "oauth2_refresh_token_for", code);
	}

	// after that, save the access token.
	// if it's unset, we may get it in the next round as we have the refresh_token now.
	if (access_token==NULL || access_token[0]==0) {
		dc_log_warning(context, 0, "Failed to find OAuth2 access token");
		goto cleanup;
	}

	dc_sqlite3_set_config(context->sql, "oauth2_access_token", access_token);
	dc_sqlite3_set_config_int64(context->sql, "oauth2_timestamp_expires",
		expires_in? time(NULL)+expires_in-5/*refresh a bet before*/ : 0);

	if (update_redirect_uri_on_success) {
		dc_sqlite3_set_config(context->sql, "oauth2_redirect_uri", redirect_uri);
	}

cleanup:
	if (locked) { pthread_mutex_unlock(&context->oauth2_critical); }
	free(refresh_token);
	free(refresh_token_for);
	free(redirect_uri);
	free(token_url);
	free(json);
	free(error);
	free(error_description);
	free(oauth2);
	return access_token? access_token : dc_strdup(NULL);
}
Esempio n. 29
0
File: post.c Progetto: gerow/hnfs
int
hnfs_post_update(hnfs_post_collection_t *collection)
{
  int ret = 0;
  const static int num_tokens = 512;
  time_t cur_time = time(NULL);
  char *json = NULL;
  jsmn_parser p;
  /* now get jsmn to parse it into a list of tokens */
  /* 512 tokens should be enough for anybody... right? */
  jsmntok_t tokens[num_tokens];
  int posts_index;

  /* lock our collection mutex */
  pthread_mutex_lock(&collection->mutex);
  /* check to see if our data is old */
  if (cur_time - collection->update_time < HNFS_TIME_BETWEEN_UPDATES) {
    fprintf(stderr, "Data fresh. Skipping update.");
    goto cleanup_lock;
  }

  (void) fetch_url;
  curl_saver_t saver;
  fetch_url(front_page_api_path, &saver);
  json = saver.data;
  //load_file("/Users/gerow/proj/hnfs/test.json", &json);
  //assert(strlen(json) == 12113);

  jsmn_init(&p);
  //assert(strlen(json) == 12113);
  int jsmn_res = jsmn_parse(&p, json, tokens, 512);
  //assert(strlen(json) == 12113);
  if (jsmn_res != JSMN_SUCCESS) {
    fprintf(stderr, "jsmn had trouble parsing the data, dumping:\n");
    fprintf(stderr, "%s\n", json);
    exit(1);
  }

  aldn_context_t context;
  context.json = json;
  context.tokens = tokens;
  context.num_tokens = num_tokens;

  posts_index = aldn_key_value(&context, 0, "items");
  if (posts_index < 0) {
    fprintf(stderr, "Failed to find index of posts inside object\n");
    exit(1);
  }

  assert(tokens[posts_index].type == JSMN_ARRAY);
  /* only the first 30 entires are what we're looking for */
  for (int i = 0; i < 30; i++) {
    /* free this entry's content pointer if it is set */
    if (collection->posts[i].content) {
      free(collection->posts[i].content);
      collection->posts[i].content = NULL;
      collection->posts[i].content_update_time = 0;
    }
    int cur_post = aldn_ith_value(&context, posts_index, i);
    printf("object type is %d\n", tokens[cur_post].type);
    assert(tokens[cur_post].type == JSMN_OBJECT);
    int title_index = aldn_key_value(&context, cur_post, "title");
    assert(title_index >= 0);
    aldn_extract_string(&context,
                        title_index,
                        collection->posts[i].title,
                        HNFS_POST_STRING_SIZE);
    int url_index = aldn_key_value(&context, cur_post, "url");
    assert(url_index >= 0);
    aldn_extract_string(&context,
                        url_index,
                        collection->posts[i].url,
                        HNFS_POST_STRING_SIZE);
  }

  collection->update_time = cur_time;
/*cleanup_saver:*/
  free(json);
cleanup_lock:
  pthread_mutex_unlock(&collection->mutex);

  return ret;
}
Esempio n. 30
0
int loadStrings()
{
	const size_t tokenNum = 1 + STR_NUM * 2;
	jsmntok_t t[tokenNum];
	char buf[8192];
	wchar_t path[_MAX_LFN];
	jsmn_parser p;
	unsigned int i, j, k;
	const char *s;
	int l, r, len;
	File fd;

	swprintf(path, _MAX_LFN, L"%ls/%s",
		langPath, fontIsLoaded ? cfgs[CFG_LANG].val.s : "en.json");
	if (!FileOpen(&fd, path, 0))
		return 1;

	len = FileGetSize(&fd);
	if (len > sizeof(buf))
		return 1;

	FileRead(&fd, buf, len, 0);
	FileClose(&fd);

	jsmn_init(&p);
	r = jsmn_parse(&p, buf, len, t, tokenNum);
	if (r < 0)
		return r;

	for (i = 1; i < r; i++) {
		for (j = 0; j < STR_NUM; j++) {
			s = buf + t[i].start;

			len = t[i].end - t[i].start;
			if (!memcmp(s, keys[j], len) && !keys[j][len]) {
				i++;
				len = t[i].end - t[i].start;
				s = buf + t[i].start;
				for (k = 0; k + 1 < STR_MAX_LEN && len > 0; k++) {
					if (s[0] == '\\' && s[1] == 'n') {
						strings[j][k] = '\n';
						l = 2;
					} else {
						l = mbtowc(strings[j] + k, s, len);
						if (l < 0)
							break;
					}

					len -= l;
					s += l;
				}

				strings[j][k] = 0;
				mbtowc(NULL, NULL, 0);
				break;
			}
		}
	}

	return 0;
}