static int js_parser(lua_State *L) { yajl_handle* handle; luaL_checktype(L, 1, LUA_TTABLE); handle = (yajl_handle*)lua_newuserdata(L, sizeof(yajl_handle)); *handle = yajl_alloc(&js_parser_callbacks, NULL, (void*)L); luaL_getmetatable(L, "yajl.parser.meta"); lua_setmetatable(L, -2); lua_getfield(L, 1, "allow_comments"); if ( ! lua_isnil(L, -1) ) { yajl_config(*handle, yajl_allow_comments, lua_toboolean(L, -1)); } lua_pop(L, 1); lua_getfield(L, 1, "check_utf8"); if ( ! lua_isnil(L, -1) ) { yajl_config(*handle, yajl_dont_validate_strings, !lua_toboolean(L, -1)); } lua_pop(L, 1); lua_getfield(L, 1, "events"); /* Create callback function that calls yajl_parse[_complete]() upvalue(1) = yajl_handle* upvalue(2) = events table */ lua_pushcclosure(L, &js_parser_parse, 2); return 1; }
// Configure void yajl_configure(yajl_handle handle, VALUE options) { if(rb_hash_aref(options, sym_allow_comments) == Qtrue) { yajl_config(handle, yajl_allow_comments, 1); } else { yajl_config(handle, yajl_allow_comments, 0); } if(rb_hash_aref(options, sym_validate_utf8) == Qtrue) { yajl_config(handle, yajl_dont_validate_strings, 0); } else { yajl_config(handle, yajl_dont_validate_strings, 1); } if(rb_hash_aref(options, sym_allow_trailing_garbage) == Qtrue) { yajl_config(handle, yajl_allow_trailing_garbage, 1); } else { yajl_config(handle, yajl_allow_trailing_garbage, 0); } if(rb_hash_aref(options, sym_multiple_values) == Qtrue) { yajl_config(handle, yajl_allow_multiple_values, 1); } else { yajl_config(handle, yajl_allow_multiple_values, 0); } if(rb_hash_aref(options, sym_allow_partial_values) == Qtrue) { yajl_config(handle, yajl_allow_partial_values, 1); } else { yajl_config(handle, yajl_allow_partial_values, 0); } }
void hkvJsonStreamReader::InitConfig(int iParserFlags) { assert(m_pHandle); yajl_config(m_pHandle, yajl_allow_comments, (iParserFlags & ALLOW_COMMENTS)); yajl_config(m_pHandle, yajl_dont_validate_strings, (iParserFlags & DISABLE_UTF8_VALIDATION)); yajl_config(m_pHandle, yajl_allow_trailing_garbage, (iParserFlags & ALLOW_TRAILING_GARBAGE)); yajl_config(m_pHandle, yajl_allow_multiple_values, (iParserFlags & ALLOW_MULTIPLE_VALUES)); }
/* XXX add an incremental streaming parser - yajl trivially supports it */ virJSONValuePtr virJSONValueFromString(const char *jsonstring) { yajl_handle hand; virJSONParser parser = { NULL, NULL, 0 }; virJSONValuePtr ret = NULL; # ifndef WITH_YAJL2 yajl_parser_config cfg = { 1, 1 }; # endif VIR_DEBUG("string=%s", jsonstring); # ifdef WITH_YAJL2 hand = yajl_alloc(&parserCallbacks, NULL, &parser); if (hand) { yajl_config(hand, yajl_allow_comments, 1); yajl_config(hand, yajl_dont_validate_strings, 0); } # else hand = yajl_alloc(&parserCallbacks, &cfg, NULL, &parser); # endif if (!hand) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Unable to create JSON parser")); goto cleanup; } if (yajl_parse(hand, (const unsigned char *)jsonstring, strlen(jsonstring)) != yajl_status_ok) { unsigned char *errstr = yajl_get_error(hand, 1, (const unsigned char*)jsonstring, strlen(jsonstring)); virReportError(VIR_ERR_INTERNAL_ERROR, _("cannot parse json %s: %s"), jsonstring, (const char*) errstr); VIR_FREE(errstr); virJSONValueFree(parser.head); goto cleanup; } ret = parser.head; cleanup: yajl_free(hand); if (parser.nstate) { size_t i; for (i = 0; i < parser.nstate; i++) { VIR_FREE(parser.state[i].key); } VIR_FREE(parser.state); } VIR_DEBUG("result=%p", parser.head); return ret; }
yajl_val yajl_tree_parse_options (const char *input, char *error_buffer, size_t error_buffer_size, yajl_tree_option options) { static const yajl_callbacks callbacks = { /* null = */ handle_null, /* boolean = */ handle_boolean, /* integer = */ NULL, /* double = */ NULL, /* number = */ handle_number, /* string = */ handle_string, /* start map = */ handle_start_map, /* map key = */ handle_string, /* end map = */ handle_end_map, /* start array = */ handle_start_array, /* end array = */ handle_end_array }; yajl_handle handle; yajl_status status; char * internal_err_str; context_t ctx = { NULL, NULL, NULL, 0 }; ctx.errbuf = error_buffer; ctx.errbuf_size = error_buffer_size; if (error_buffer != NULL) memset (error_buffer, 0, error_buffer_size); handle = yajl_alloc (&callbacks, NULL, &ctx); yajl_config(handle, yajl_allow_comments, (options & yajl_tree_option_dont_allow_comments) ? 0 : 1); yajl_config(handle, yajl_allow_trailing_separator, (options & yajl_tree_option_allow_trailing_separator) ? 1 : 0); status = yajl_parse(handle, (unsigned char *) input, strlen (input)); status = yajl_complete_parse (handle); if (status != yajl_status_ok) { if (error_buffer != NULL && error_buffer_size > 0) { internal_err_str = (char *) yajl_get_error(handle, 1, (const unsigned char *) input, strlen(input)); snprintf(error_buffer, error_buffer_size, "%s", internal_err_str); YA_FREE(&(handle->alloc), internal_err_str); } while (ctx.stack) { yajl_tree_free(context_pop(&ctx)); } yajl_free (handle); return NULL; } yajl_free (handle); return (ctx.root); }
static VALUE mParser_do_yajl_parse(VALUE self, VALUE str, VALUE yajl_opts) { yajl_handle hand; yajl_status stat; unsigned char *err; volatile CTX ctx; rb_ivar_set(self, rb_intern("key"), Qnil); rb_ivar_set(self, rb_intern("stack"), rb_ary_new()); rb_ivar_set(self, rb_intern("key_stack"), rb_ary_new()); ctx.self = self; ctx.symbolizeKeys = get_opts_key(self, "symbolize_keys"); ctx.uniqueKeyChecking = get_opts_key(self, "unique_key_checking"); hand = yajl_alloc(&callbacks, NULL, (void *)&ctx); if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_comments"))) == Qtrue) { yajl_config(hand, yajl_allow_comments, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_dont_validate_strings"))) == Qtrue) { yajl_config(hand, yajl_dont_validate_strings, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_trailing_garbage"))) == Qtrue) { yajl_config(hand, yajl_allow_trailing_garbage, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_multiple_values"))) == Qtrue) { yajl_config(hand, yajl_allow_multiple_values, 1); } if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_partial_values"))) == Qtrue) { yajl_config(hand, yajl_allow_partial_values, 1); } if ((stat = yajl_parse(hand, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str))) != yajl_status_ok) { err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); goto raise; } if ((stat = yajl_complete_parse(hand)) != yajl_status_ok) { err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); goto raise; } yajl_free(hand); return rb_ary_pop(rb_ivar_get(self, rb_intern("stack"))); raise: if (hand) { yajl_free(hand); } rb_raise(cParseError, "%s", err); }
CJSONVariantParser::CJSONVariantParser(IParseCallback *callback) { m_callback = callback; #if YAJL_MAJOR == 2 m_handler = yajl_alloc(&callbacks, NULL, this); yajl_config(m_handler, yajl_allow_comments, 1); yajl_config(m_handler, yajl_dont_validate_strings, 0); #else yajl_parser_config cfg = { 1, 1 }; m_handler = yajl_alloc(&callbacks, &cfg, NULL, this); #endif m_status = ParseVariable; }
/* STRATEGY: * * Each of the js_to_value_callbacks perform these actions: * * [1] Push a new value onto the top of the Lua stack. * * [2] Call the function that was at the top of the Lua stack before * step [1] occurred. * * The purpose of the function call in [2] is to take the value at the * top of the stack and store it in the appropriate location. * Initially, the function is the noop (no operation) function which * does nothing. Therefore we know that the final result is on the * top of the Lua stack. * * The to_value_start_map and to_value_start_array callbacks are * different since they need to use a bit of the Lua stack to store * some state information. When these callbacks are ran, they perform * these actions: * * [a] Push a new table which will represent the final "array" or * "object" onto the top of the Lua stack. * * [b] Allocate space for the "key" (in the case of arrays, this is * the index into the array to use as part of the next insertion) * * [c] Push the got_array_value or got_map_key function. * * The got_array_value function will take the value at the top of the * stack and insert it into the table created in step [a]. It will * then increment the index created in step [b]. As a final step, it * removes the value at the top of the stack. * * The got_map_key function simply takes the value at the top of the * stack and stores it in the space allocated by step [b] above. It * then replaces the function pushed onto the stack by step [c] with * the got_map_value function. As a final step, it removes the value * at the top of the stack. * * The got_map_value function takes the value at the top of the stack * and inserts it into the table created in step [a] with the key * whose space was allocated in step [b]. The function pushed onto * the stack by step [c] is then restored back to the got_map_key * function. As a final step, it removes the value at the top of the * stack. */ static int js_to_value(lua_State *L) { yajl_handle handle; size_t len; const unsigned char* buff = (const unsigned char*) luaL_checklstring(L, 1, &len); if ( NULL == buff ) return 0; handle = yajl_alloc(&js_to_value_callbacks, NULL, (void*)L); lua_pushcfunction(L, noop); if ( lua_istable(L, 2) ) { lua_getfield(L, 2, "allow_comments"); if ( ! lua_isnil(L, -1) ) { yajl_config(handle, yajl_allow_comments, lua_toboolean(L, -1)); } lua_pop(L, 1); lua_getfield(L, 2, "check_utf8"); if ( ! lua_isnil(L, -1) ) { yajl_config(handle, yajl_dont_validate_strings, !lua_toboolean(L, -1)); } lua_pop(L, 1); } js_parser_assert(L, yajl_parse(handle, buff, len), &handle, buff, len, __FILE__, __LINE__); js_parser_assert(L, yajl_complete_parse(handle), &handle, buff, len, __FILE__, __LINE__); yajl_free(handle); return 1; }
static int lyajl_config (lua_State *L) { const char* option; luvit_parser_t *parser = parser_get(L, 1); option = luaL_checkstring(L, 2); if (strcmp(option, "allow_comments") == 0) { yajl_config(parser->handle, yajl_allow_comments, lua_toboolean(L, 3)); } else if (strcmp(option, "dont_validate_strings") == 0) { yajl_config(parser->handle, yajl_dont_validate_strings, lua_toboolean(L, 3)); } else if (strcmp(option, "allow_trailing_garbage") == 0) { yajl_config(parser->handle, yajl_allow_trailing_garbage, lua_toboolean(L, 3)); } else if (strcmp(option, "allow_multiple_values") == 0) { yajl_config(parser->handle, yajl_allow_multiple_values, lua_toboolean(L, 3)); } else if (strcmp(option, "allow_partial_values") == 0) { yajl_config(parser->handle, yajl_allow_partial_values, lua_toboolean(L, 3)); } else { luaL_error(L, "Invalid config option %s", option); } return 0; }
/* * Public functions */ yajl_val yajl_tree_parse (const char *input, char *error_buffer, size_t error_buffer_size) { static const yajl_callbacks callbacks = { /* null = */ handle_null, /* boolean = */ handle_boolean, /* integer = */ NULL, /* double = */ NULL, /* number = */ handle_number, /* string = */ handle_string, /* start map = */ handle_start_map, /* map key = */ handle_string, /* end map = */ handle_end_map, /* start array = */ handle_start_array, /* end array = */ handle_end_array }; yajl_handle handle; yajl_status status; context_t ctx = { NULL, NULL, NULL, 0 }; ctx.errbuf = error_buffer; ctx.errbuf_size = error_buffer_size; if (error_buffer != NULL) memset (error_buffer, 0, error_buffer_size); handle = yajl_alloc (&callbacks, NULL, &ctx); yajl_config(handle, yajl_allow_comments, 1); status = yajl_parse(handle, (unsigned char *) input, strlen (input)); status = yajl_complete_parse (handle); if (status != yajl_status_ok) { if (error_buffer != NULL && error_buffer_size > 0) { snprintf( error_buffer, error_buffer_size, "%s", (char *) yajl_get_error(handle, 1, (const unsigned char *) input, strlen(input))); } yajl_free (handle); return NULL; } yajl_free (handle); return (ctx.root); }
/** * Initialise JSON parser. */ int json_init(modsec_rec *msr, char **error_msg) { /** * yajl configuration and callbacks */ static yajl_callbacks callbacks = { yajl_null, yajl_boolean, NULL /* yajl_integer */, NULL /* yajl_double */, yajl_number, yajl_string, yajl_start_map, yajl_map_key, yajl_end_map, NULL /* yajl_start_array */, NULL /* yajl_end_array */ }; if (error_msg == NULL) return -1; *error_msg = NULL; msr_log(msr, 4, "JSON parser initialization"); msr->json = apr_pcalloc(msr->mp, sizeof(json_data)); if (msr->json == NULL) return -1; /** * Prefix and current key are initially empty */ msr->json->prefix = (unsigned char *) NULL; msr->json->current_key = (unsigned char *) NULL; /** * yajl initialization * * yajl_parser_config definition: * http://lloyd.github.io/yajl/yajl-2.0.1/yajl__parse_8h.html#aec816c5518264d2ac41c05469a0f986c * * TODO: make UTF8 validation optional, as it depends on Content-Encoding */ if (msr->txcfg->debuglog_level >= 9) { msr_log(msr, 9, "yajl JSON parsing callback initialization"); } msr->json->handle = yajl_alloc(&callbacks, NULL, msr); yajl_config(msr->json->handle, yajl_allow_partial_values, 0); return 1; }
stream_get_key_handl *init_json_stream_get_key(const char **path) { context_yajl *ctx = calloc(1, sizeof(context_yajl)); stream_get_key_handl *s_handler = calloc(1, sizeof(stream_get_key_handl)); int count_max_depth = 0; ctx->path = path; for (count_max_depth = 0; path[count_max_depth]; count_max_depth++) ; ctx->current_depth = 0; ctx->max_depth = count_max_depth; ctx->find_status = fs_find; ctx->return_str = NULL; s_handler->ctx = ctx; s_handler->yajl_handle = yajl_alloc(&callbacks, NULL, (void *) s_handler->ctx); yajl_config(s_handler->yajl_handle, yajl_allow_comments, 1); return s_handler; }
// Client records will be coming through in alphabetical order. // FIX THIS: If a client is deleted on the server, it is not deleted from // clist. int json_input(struct asfd *asfd, struct sel *sel) { static yajl_handle yajl=NULL; cslist=&sel->clist; sselbu=&sel->backup; sllines=&sel->llines; if(!yajl) { if(!(yajl=yajl_alloc(&callbacks, NULL, NULL))) goto error; yajl_config(yajl, yajl_dont_validate_strings, 1); } if(yajl_parse(yajl, (const unsigned char *)asfd->rbuf->buf, asfd->rbuf->len)!=yajl_status_ok) { do_yajl_error(yajl, asfd); goto error; } if(!map_depth) { // Got to the end of the JSON object. if(!sel->gotfirstresponse) sel->gotfirstresponse=1; if(yajl_complete_parse(yajl)!=yajl_status_ok) { do_yajl_error(yajl, asfd); goto error; } yajl_free(yajl); yajl=NULL; } return 0; error: yajl_free(yajl); yajl=NULL; return -1; }
void nx_json_parse(nx_json_parser_ctx_t *ctx, const char *json, size_t len) { yajl_handle hand; yajl_gen g; g = yajl_gen_alloc(NULL); //yajl_gen_config(g, yajl_gen_validate_utf8, 1); hand = yajl_alloc(&callbacks, NULL, (void *) ctx); yajl_config(hand, yajl_allow_comments, 1); if ( (yajl_parse(hand, (const unsigned char *) json, len) != yajl_status_ok) || (yajl_complete_parse(hand) != yajl_status_ok) ) { unsigned char *errstr = yajl_get_error(hand, 1, (const unsigned char *) json, len); log_error("failed to parse json string, %s [%s]", errstr, json); yajl_free_error(hand, errstr); } yajl_gen_free(g); yajl_free(hand); }
void json_init() { parser = yajl_alloc(&callbacks, NULL, NULL); yajl_config(parser, yajl_allow_comments, 1); *my_error = 0; }
CCObject* CCJSONParser::load(const char* json, size_t length) { // use memory input stream CCMemoryInputStream* mis = CCMemoryInputStream::create((char*)json, length); // status of yajl yajl_status stat; // get gen instance yajl_gen g = yajl_gen_alloc(NULL); // register callback ccJSONContext ctx; ctx.g = g; ctx.root = NULL; ctx.objStack = new vector<CCObject*>(); ctx.flagStack = new vector<bool>(); yajl_handle hand = yajl_alloc(&callbacks, NULL, (void*)&ctx); // config yajl yajl_gen_config(g, yajl_gen_beautify, 1); yajl_gen_config(g, yajl_gen_validate_utf8, 1); yajl_config(hand, yajl_allow_comments, 1); // parse char buf[4096]; while(true) { // read data int rd = mis->read(buf, 4096); if (rd == 0) break; // parese data stat = yajl_parse(hand, (const unsigned char*)buf, rd); // if parse error, break if (stat != yajl_status_ok) break; } // complete parse stat = yajl_complete_parse(hand); // check error if (stat != yajl_status_ok) { unsigned char* str = yajl_get_error(hand, 1, (const unsigned char*)json, length); CCLOGWARN("parse json error: %s", str); yajl_free_error(hand, str); // when error, doesn't return anything ctx.root = NULL; } // free yajl_gen_free(g); yajl_free(hand); delete ctx.objStack; delete ctx.flagStack; // return return ctx.root; }
int main(int argc, char ** argv) { yajl_status stat; size_t rd; yajl_handle hand; static unsigned char fileData[65536]; int quiet = 0; int retval = 0; int a = 1; /* allocate a parser */ hand = yajl_alloc(NULL, NULL, NULL); /* check arguments.*/ while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) { unsigned int i; for ( i=1; i < strlen(argv[a]); i++) { switch (argv[a][i]) { case 'q': quiet = 1; break; case 'c': yajl_config(hand, yajl_allow_comments, 1); break; case 'u': yajl_config(hand, yajl_dont_validate_strings, 1); break; default: fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]); usage(argv[0]); } } ++a; } if (a < argc) { usage(argv[0]); } for (;;) { rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin); retval = 0; if (rd == 0) { if (!feof(stdin)) { if (!quiet) { fprintf(stderr, "error encountered on file read\n"); } retval = 1; } break; } fileData[rd] = 0; /* read file data, pass to parser */ stat = yajl_parse(hand, fileData, rd); if (stat != yajl_status_ok) break; } /* parse any remaining buffered data */ stat = yajl_complete_parse(hand); if (stat != yajl_status_ok) { if (!quiet) { unsigned char * str = yajl_get_error(hand, 1, fileData, rd); fprintf(stderr, "%s", (const char *) str); yajl_free_error(hand, str); } retval = 1; } yajl_free(hand); if (!quiet) { printf("JSON is %s\n", retval ? "invalid" : "valid"); } return retval; }
static int rest_httptrap_handler(mtev_http_rest_closure_t *restc, int npats, char **pats) { int mask, complete = 0, cnt; struct rest_json_payload *rxc = NULL; const char *error = "internal error", *secret = NULL; mtev_http_session_ctx *ctx = restc->http_ctx; char json_out[128]; noit_check_t *check; uuid_t check_id; if(npats != 2) { error = "bad uri"; goto error; } if(uuid_parse(pats[0], check_id)) { error = "uuid parse error"; goto error; } if(restc->call_closure == NULL) { httptrap_closure_t *ccl; const char *delimiter = NULL; rxc = restc->call_closure = calloc(1, sizeof(*rxc)); rxc->delimiter = DEFAULT_HTTPTRAP_DELIMITER; check = noit_poller_lookup(check_id); if(!check || strcmp(check->module, "httptrap")) { error = "no such httptrap check"; goto error; } (void)mtev_hash_retr_str(check->config, "secret", strlen("secret"), &secret); if(!secret) secret = ""; if(strcmp(pats[1], secret)) { error = "secret mismatch"; goto error; } (void)mtev_hash_retr_str(check->config, "delimiter", strlen("delimiter"), &delimiter); if(delimiter && *delimiter) rxc->delimiter = *delimiter; rxc->check = check; ccl = check->closure; if(!ccl) { error = "noitd is booting, try again in a bit"; goto error; } rxc->parser = yajl_alloc(&httptrap_yajl_callbacks, NULL, rxc); rxc->depth = -1; yajl_config(rxc->parser, yajl_allow_comments, 1); yajl_config(rxc->parser, yajl_dont_validate_strings, 1); yajl_config(rxc->parser, yajl_allow_trailing_garbage, 1); yajl_config(rxc->parser, yajl_allow_partial_values, 1); restc->call_closure_free = rest_json_payload_free; } else rxc = restc->call_closure; /* flip threads */ { mtev_http_connection *conn = mtev_http_session_connection(ctx); eventer_t e = mtev_http_connection_event(conn); if(e) { pthread_t tgt = CHOOSE_EVENTER_THREAD_FOR_CHECK(rxc->check); if(!pthread_equal(e->thr_owner, tgt)) { e->thr_owner = tgt; return EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION; } } } rxc = rest_get_json_upload(restc, &mask, &complete); if(rxc == NULL && !complete) return mask; if(!rxc) goto error; if(rxc->error) goto error; cnt = push_payload_at_check(rxc); mtev_http_response_ok(ctx, "application/json"); snprintf(json_out, sizeof(json_out), "{ \"stats\": %d }", cnt); mtev_http_response_append(ctx, json_out, strlen(json_out)); mtev_http_response_end(ctx); return 0; error: mtev_http_response_server_error(ctx, "application/json"); mtev_http_response_append(ctx, "{ error: \"", 10); if(rxc && rxc->error) error = rxc->error; mtev_http_response_append(ctx, error, strlen(error)); mtev_http_response_append(ctx, "\" }", 3); mtev_http_response_end(ctx); return 0; }
int rp_config_load(rp_pool_t *pool, const char *path, struct rp_config *cfg) { struct rp_json_ctx ctx; rp_pool_t *tmp_pool; tmp_pool = rp_create_pool(RP_DEFAULT_POOL_SIZE); ctx.pool = pool; ctx.tmp_pool = tmp_pool; ctx.cfg = cfg; ctx.state = START; ctx.server = NULL; yajl_alloc_funcs alloc_funcs = { rpcfg_alloc, rpcfg_realloc, rpcfg_free, &ctx, }; yajl_handle hand = yajl_alloc(&callbacks, &alloc_funcs, &ctx); yajl_config(hand, yajl_allow_comments, 1); FILE *f = fopen(path, "r"); if (!f) { fprintf(stderr, "%s not found\n", path); return -1; } unsigned char *buf = rp_palloc(tmp_pool, READ_BUF_SZ); for (;;) { size_t rd = fread((void *)buf, 1, READ_BUF_SZ, f); if (rd == 0) { if (!feof(f)) { fprintf(stderr, "error on file read\n"); } break; } int stat = yajl_parse(hand, buf, rd); if (stat != yajl_status_ok) { unsigned char *str = yajl_get_error(hand, 1, buf, rd); fprintf(stderr, "%s", (const char *)str); yajl_free_error(hand, str); return -1; } } fclose(f); yajl_free(hand); rp_destroy_pool(ctx.tmp_pool); // TODO: Verify the configuration is valid return 0; }
bool JSonDB::readDBInternal(const u8* source, u32 sourceLength) { bool res = false; m_baseRecordHeader.fields = &m_field[0]; m_baseRecordHeader.pNextRecord = NULL; m_arrayCnt = 0; m_record = 0; m_recordEntry = 0; m_stringAlloc = 0; m_startRecord = NULL; m_currRecIdx = 0; m_mapCnt = 0; m_res = MOVE_UNFETCH; mStringRecordBuffer = KLBNEWA(char, sourceLength); mStringRecordCount = 0; m_StringRecordBufferSize = sourceLength; if (mStringRecordBuffer) { static yajl_callbacks callbacks = { JSonDB::read_null, JSonDB::read_boolean, JSonDB::read_int, JSonDB::read_double, NULL, JSonDB::read_string, JSonDB::read_start_map, JSonDB::read_map_key, JSonDB::read_end_map, JSonDB::read_start_array, JSonDB::read_end_array }; memcpy(mStringRecordBuffer, source, sourceLength); yajl_handle hand; /* generator config */ // yajl_gen g; // My Context. 2012.12.06 使用していなかったのでコメントアウト yajl_status stat; // g = yajl_gen_alloc(NULL); // 2012.12.06 使用していなかったのでコメントアウト /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, NULL, this); if (hand) { this->parserCtx = hand; /* and let's allow comments by default */ yajl_config(hand, yajl_allow_comments, 1); stat = yajl_parse(hand, (const unsigned char*)mStringRecordBuffer, sourceLength); if (stat == yajl_status_ok) { stat = yajl_complete_parse(hand); if (stat == yajl_status_ok) { res = true; } } } } if (!res) { clean(); } m_recordPtr = m_startRecord; return res; }
bool jsaxparser_init(jsaxparser_ref parser, JSchemaInfoRef schemaInfo, PJSAXCallbacks *callback, void *callback_ctxt) { memset(parser, 0, sizeof(struct jsaxparser) - sizeof(mem_pool_t)); parser->validator = NOTHING_VALIDATOR; parser->uri_resolver = NULL; parser->schemaInfo = schemaInfo; if (schemaInfo && schemaInfo->m_schema) { parser->validator = schemaInfo->m_schema->validator; parser->uri_resolver = schemaInfo->m_schema->uri_resolver; } if (callback == NULL) { parser->yajl_cb = no_callbacks; } else { parser->yajl_cb.yajl_null = callback->m_null ? (pj_yajl_null)callback->m_null : no_callbacks.yajl_null; parser->yajl_cb.yajl_boolean = callback->m_boolean ? (pj_yajl_boolean)callback->m_boolean : no_callbacks.yajl_boolean; parser->yajl_cb.yajl_integer = NULL; parser->yajl_cb.yajl_double = NULL; parser->yajl_cb.yajl_number = callback->m_number ? (pj_yajl_number)callback->m_number : no_callbacks.yajl_number; parser->yajl_cb.yajl_string = callback->m_string ? (pj_yajl_string)callback->m_string : no_callbacks.yajl_string; parser->yajl_cb.yajl_start_map = callback->m_objStart ? (pj_yajl_start_map)callback->m_objStart : no_callbacks.yajl_start_map; parser->yajl_cb.yajl_map_key = callback->m_objKey ? (pj_yajl_map_key)callback->m_objKey : no_callbacks.yajl_map_key; parser->yajl_cb.yajl_end_map = callback->m_objEnd ? (pj_yajl_end_map)callback->m_objEnd : no_callbacks.yajl_end_map; parser->yajl_cb.yajl_start_array = callback->m_arrStart ? (pj_yajl_start_array)callback->m_arrStart : no_callbacks.yajl_start_array; parser->yajl_cb.yajl_end_array = callback->m_arrEnd ? (pj_yajl_end_array)callback->m_arrEnd : no_callbacks.yajl_end_array; } parser->errorHandler.m_parser = err_parser; parser->errorHandler.m_schema = err_schema; parser->errorHandler.m_unknown = err_unknown; parser->errorHandler.m_ctxt = parser; validation_state_init(&(parser->validation_state), parser->validator, parser->uri_resolver, &jparse_notification); PJSAXContext __internalCtxt = { .ctxt = (callback_ctxt != NULL ? callback_ctxt : NULL), .m_handlers = &parser->yajl_cb, .m_errors = &parser->errorHandler, .m_error_code = 0, .errorDescription = NULL, .validation_state = &parser->validation_state, }; parser->internalCtxt = __internalCtxt; mempool_init(&parser->memory_pool); yajl_alloc_funcs allocFuncs = { mempool_malloc, mempool_realloc, mempool_free, &parser->memory_pool }; const bool allow_comments = true; #if YAJL_VERSION < 20000 yajl_parser_config yajl_opts = { allow_comments, 0, // currently only UTF-8 will be supported for input. }; parser->handle = yajl_alloc(&my_bounce, &yajl_opts, &allocFuncs, &parser->internalCtxt); #else parser->handle = yajl_alloc(&my_bounce, &allocFuncs, &parser->internalCtxt); yajl_config(parser->handle, yajl_allow_comments, allow_comments ? 1 : 0); // currently only UTF-8 will be supported for input. yajl_config(parser->handle, yajl_dont_validate_strings, 1); #endif // YAJL_VERSION return true; } static bool jsaxparser_process_error(jsaxparser_ref parser, const char *buf, int buf_len, bool final_stage) { if ( #if YAJL_VERSION < 20000 (final_stage || yajl_status_insufficient_data != parser->status) && #endif !handle_yajl_error(parser->status, parser->handle, buf, buf_len, parser->schemaInfo, &parser->internalCtxt) ) { if (parser->yajlError) { yajl_free_error(parser->handle, (unsigned char*)parser->yajlError); parser->yajlError = NULL; } parser->yajlError = (char*)yajl_get_error(parser->handle, 1, (unsigned char*)buf, buf_len); return false; } return true; } const char *jsaxparser_get_error(jsaxparser_ref parser) { SANITY_CHECK_POINTER(parser); if (parser->schemaError) return parser->schemaError; if (parser->yajlError) return parser->yajlError; return NULL; }
int main(int argc, char ** argv) { yajl_handle hand; const char * fileName = NULL; static unsigned char * fileData = NULL; FILE *file; size_t bufSize = BUF_SIZE; yajl_status stat; size_t rd; int i, j; /* memory allocation debugging: allocate a structure which collects * statistics */ yajlTestMemoryContext memCtx = { 0,0 }; /* memory allocation debugging: allocate a structure which holds * allocation routines */ yajl_alloc_funcs allocFuncs = { yajlTestMalloc, yajlTestRealloc, yajlTestFree, (void *) NULL }; allocFuncs.ctx = (void *) &memCtx; /* allocate the parser */ hand = yajl_alloc(&callbacks, &allocFuncs, NULL); /* check arguments. We expect exactly one! */ for (i=1;i<argc;i++) { if (!strcmp("-c", argv[i])) { yajl_config(hand, yajl_allow_comments, 1); } else if (!strcmp("-b", argv[i])) { if (++i >= argc) usage(argv[0]); /* validate integer */ for (j=0;j<(int)strlen(argv[i]);j++) { if (argv[i][j] <= '9' && argv[i][j] >= '0') continue; fprintf(stderr, "-b requires an integer argument. '%s' " "is invalid\n", argv[i]); usage(argv[0]); } bufSize = atoi(argv[i]); if (!bufSize) { fprintf(stderr, "%zu is an invalid buffer size\n", bufSize); } } else if (!strcmp("-g", argv[i])) { yajl_config(hand, yajl_allow_trailing_garbage, 1); } else if (!strcmp("-m", argv[i])) { yajl_config(hand, yajl_allow_multiple_values, 1); } else if (!strcmp("-p", argv[i])) { yajl_config(hand, yajl_allow_partial_values, 1); } else { fileName = argv[i]; break; } } fileData = (unsigned char *) malloc(bufSize); if (fileData == NULL) { fprintf(stderr, "failed to allocate read buffer of %zu bytes, exiting.", bufSize); yajl_free(hand); exit(2); } if (fileName) { file = fopen(fileName, "r"); } else { file = stdin; } for (;;) { rd = fread((void *) fileData, 1, bufSize, file); if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error reading from '%s'\n", fileName); } break; } /* read file data, now pass to parser */ stat = yajl_parse(hand, fileData, rd); if (stat != yajl_status_ok) break; } stat = yajl_complete_parse(hand); if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(hand, 0, fileData, rd); fflush(stdout); fprintf(stderr, "%s", (char *) str); yajl_free_error(hand, str); } yajl_free(hand); free(fileData); if (fileName) { fclose(file); } /* finally, print out some memory statistics */ /* (lth) only print leaks here, as allocations and frees may vary depending * on read buffer size, causing false failures. * * printf("allocations:\t%u\n", memCtx.numMallocs); * printf("frees:\t\t%u\n", memCtx.numFrees); */ fflush(stderr); fflush(stdout); printf("memory leaks:\t%u\n", memCtx.numMallocs - memCtx.numFrees); return 0; }
//todo: Test layouts with missing or misspelled keys. //print error message if key with non registered name is encountered. int read_layout(char* filename, int layoutTable[NUM_CANONICAL] ) { //("testing_read array size %d \n", layoutTable[0] ); //we'll ignore filename for now. FILE* fd; //selectedLayoutName=layoutname; //I've got an array //i've got its length in NUM_CANONICAL. //It's an int array. //fill it with -1 //in read int //look up the name in canonical names. //really? //no hash. if(filename != NULL) { //("about to open %s to load %s \n", filename); fd = fopen(filename, "r"); if (fd == NULL) { //("Could not open %s, exiting...\n", filename); exit(-7); } else { //("Could open but something else went wrong \n"); } } else { fd = stdin; } //("testing_read 1 \n"); yajl_handle hand; static unsigned char fileData[65536]; /* generator config */ yajl_status stat; size_t rd; int retval = 0; int a = 1; /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, NULL, layoutTable); //("testing_read 2 \n"); /* and let's allow comments by default */ yajl_config(hand, yajl_allow_multiple_values, 1); //("testing_read 3 \n"); size_t bytesConsumed; for (;;) { //("testing_read 4a \n"); rd = fread((void *) fileData, 1, sizeof(fileData) - 1, fd); //("rd %ld\n",rd); if (rd == 0) { if (!feof(fd)) { //(stderr, "error on file read.\n"); retval = 1; } break; } fileData[rd] = 0; stat = yajl_parse(hand, fileData, rd); //("testing_read 4b \n"); bytesConsumed = yajl_get_bytes_consumed(hand); //("loop read: %ld consumed: %ld \n", rd, bytesConsumed); if (stat != yajl_status_ok) { //("not ok in \n"); break; } { //("okay\n"); } } //("testing_read 5 \n"); //("completed parse\n"); stat = yajl_complete_parse(hand); if (stat != yajl_status_ok) { //("not ok out\n"); unsigned char * str = yajl_get_error(hand, 1, fileData, rd); //(stderr, "%s", (const char *) str); yajl_free_error(hand, str); retval = 1; } yajl_free(hand); return retval; }
static int run(int validate_utf8) { long long times = 0; double starttime; unsigned long long sumsize = 0; starttime = mygettime(); /* allocate a parser */ for (;;) { int i; { double now = mygettime(); if (now - starttime >= PARSE_TIME_SECS) break; } for (i = 0; i < 100; i++) { yajl_handle hand = yajl_alloc(NULL, NULL, NULL); yajl_status stat; const char ** d; yajl_config(hand, yajl_dont_validate_strings, validate_utf8 ? 0 : 1); for (d = get_doc(times % num_docs()); *d; d++) { size_t size = strlen(*d); sumsize += size; stat = yajl_parse(hand, (unsigned char *) *d, size); if (stat != yajl_status_ok) break; } stat = yajl_complete_parse(hand); if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(hand, 1, (unsigned char *) *d, (*d ? strlen(*d) : 0)); fprintf(stderr, "%s", (const char *) str); yajl_free_error(hand, str); return 1; } yajl_free(hand); times++; } } /* parsed doc 'times' times */ { double throughput; double now; const char * all_units[] = { "B/s", "KB/s", "MB/s", (char *) 0 }; const char ** units = all_units; now = mygettime(); throughput = sumsize / (now - starttime); while (*(units + 1) && throughput > 1024) { throughput /= 1024; units++; } printf("Parsing speed: %g %s\n", throughput, *units); } return 0; }
int enron_messages_init_fread(FILE *stream, size_t maxrecip, double **time, size_t **from, size_t ***to, size_t **nto, intptr_t **attr, size_t *nmsg) { unsigned char fileData[65536]; size_t rd; yajl_status stat; int parse_ok = 1; struct message_parse parse; message_parse_init(&parse, maxrecip); yajl_handle hand = yajl_alloc(&parse_callbacks, NULL, (void *) &parse); yajl_config(hand, yajl_allow_comments, 1); yajl_config(hand, yajl_dont_validate_strings, 1); for (;;) { rd = fread((void *)fileData, 1, sizeof(fileData) - 1, stream); if (rd == 0) { if (!feof(stream)) { fprintf(stderr, "error on file read.\n"); parse_ok = 0; } break; } fileData[rd] = 0; stat = yajl_parse(hand, fileData, rd); if (stat != yajl_status_ok) break; } stat = yajl_complete_parse(hand); if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(hand, 1, fileData, rd); fprintf(stderr, "%s", (const char *) str); yajl_free_error(hand, str); parse_ok = 0; } yajl_free(hand); message_parse_deinit(&parse); if (!parse_ok) { size_t i, n = parse.nmsg; for (i = 0; i < n; i++) { free(parse.to[i]); } free(parse.attr); free(parse.nto); free(parse.to); free(parse.from); free(parse.time); } *time = parse.time; *from = parse.from; *to = parse.to; *nto = parse.nto; *attr = parse.attr; *nmsg = parse.nmsg; return parse_ok ? 0 : -1; }
Validator* parse_schema_n(char const *str, size_t len, UriResolver *uri_resolver, char const *root_scope, JschemaErrorFunc error_func, void *error_ctxt) { //JsonSchemaParserTrace(stdout, ">>> "); void *parser = JsonSchemaParserAlloc(malloc); YajlContext yajl_context = { .parser = parser, .parser_ctxt = { .validator = NULL, .error = SEC_OK, }, }; const bool allow_comments = true; #if YAJL_VERSION < 20000 yajl_parser_config yajl_opts = { allow_comments, 0, }; yajl_handle yh = yajl_alloc(&callbacks, &yajl_opts, NULL, &yajl_context); #else yajl_handle yh = yajl_alloc(&callbacks, NULL, &yajl_context); yajl_config(yh, yajl_allow_comments, allow_comments ? 1 : 0); yajl_config(yh, yajl_dont_validate_strings, 1); #endif // YAJL_VERSION if (!yh) { JsonSchemaParserFree(parser, free); return NULL; } if (yajl_status_ok != yajl_parse(yh, (const unsigned char *)str, len)) { if (yajl_context.parser_ctxt.error == SEC_OK) { unsigned char *err = yajl_get_error(yh, 0/*verbose*/, (const unsigned char *)str, len); if (error_func) error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt); yajl_free_error(yh, err); } else { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); } yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } #if YAJL_VERSION < 20000 if (yajl_status_ok != yajl_parse_complete(yh)) #else if (yajl_status_ok != yajl_complete_parse(yh)) #endif { if (yajl_context.parser_ctxt.error == SEC_OK) { unsigned char *err = yajl_get_error(yh, 0, (const unsigned char *)str, len); if (error_func) error_func(yajl_get_bytes_consumed(yh), SEC_SYNTAX, (const char *) err, error_ctxt); yajl_free_error(yh, err); } else { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); } yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } // Let the parser finish its job. static TokenParam token_param; JsonSchemaParser(parser, 0, token_param, &yajl_context.parser_ctxt); // Even if parsing was completed there can be an error if (!yajl_context.parser_ctxt.error == SEC_OK) { if (error_func) error_func(yajl_get_bytes_consumed(yh), yajl_context.parser_ctxt.error, SchemaGetErrorMessage(yajl_context.parser_ctxt.error), error_ctxt); validator_unref(yajl_context.parser_ctxt.validator); yajl_free(yh); JsonSchemaParserFree(parser, free); return NULL; } yajl_free(yh); JsonSchemaParserFree(parser, free); // Post-parse processing Validator *v = yajl_context.parser_ctxt.validator; // Move parsed features to the validators validator_apply_features(v); // Combine type validator and other validators contaiters (allOf, anyOf, etc.) validator_combine(v); if (uri_resolver) validator_collect_uri(v, root_scope, uri_resolver); // Substitute every SchemaParsing by its type validator for every node // in the AST. Validator *result = validator_finalize_parse(v); // Forget about SchemaParsing then. validator_unref(v); return result; }
int SLNSubmissionParseMetaFile(SLNSubmissionRef const sub, uint64_t const fileID, DB_txn *const txn, uint64_t *const out) { assert(out); if(!sub) return DB_EINVAL; if(!fileID) return DB_EINVAL; if(!txn) return DB_EINVAL; strarg_t const type = SLNSubmissionGetType(sub); if(!type) return 0; if(0 != strcasecmp(SLN_META_TYPE, type) && 0 != strcasecmp("text/x-sln-meta+json; charset=utf-8", type) && 0 != strcasecmp("text/efs-meta+json; charset=utf-8", type)) return 0; // TODO: Get rid of these obsolete types. uv_file const fd = SLNSubmissionGetFile(sub); if(fd < 0) return DB_EINVAL; int rc = 0; uv_buf_t buf[1] = {}; DB_txn *subtxn = NULL; parser_t ctx[1] = {}; yajl_handle parser = NULL; buf->base = malloc(BUF_LEN); if(!buf->base) { rc = DB_ENOMEM; goto cleanup; } buf->len = URI_MAX; int64_t pos = 0; ssize_t len = async_fs_read(fd, buf, 1, pos); if(len < 0) { fprintf(stderr, "Submission meta-file read error (%s)\n", sln_strerror(len)); rc = DB_EIO; goto cleanup; } size_t i; for(i = 0; i < len; i++) { char const c = buf->base[i]; if('\r' == c || '\n' == c) break; } if(i >= len) { fprintf(stderr, "Submission meta-file parse error (invalid target URI)\n"); rc = DB_EIO; goto cleanup; } assert(i < len); str_t targetURI[URI_MAX]; memcpy(targetURI, buf->base, i); targetURI[i] = '\0'; pos += i; // TODO: Support sub-transactions in LevelDB backend. // TODO: db_txn_begin should support NULL env. // rc = db_txn_begin(NULL, txn, DB_RDWR, &subtxn); // if(rc < 0) goto cleanup; subtxn = txn; uint64_t const metaFileID = add_metafile(subtxn, fileID, targetURI); if(!metaFileID) goto cleanup; // Duplicate meta-file, not an error. // TODO: Unless the previous version wasn't actually a meta-file. ctx->txn = subtxn; ctx->metaFileID = metaFileID; ctx->targetURI = targetURI; ctx->depth = -1; parser = yajl_alloc(&callbacks, NULL, ctx); if(!parser) rc = DB_ENOMEM; if(rc < 0) goto cleanup; yajl_config(parser, yajl_allow_partial_values, (int)true); yajl_status status = yajl_status_ok; for(;;) { buf->len = MIN(BUF_LEN, PARSE_MAX-pos); if(!buf->len) break; len = async_fs_read(fd, buf, 1, pos); if(len < 0) { fprintf(stderr, "Submission meta-file read error (%s)\n", sln_strerror(len)); rc = DB_EIO; goto cleanup; } if(0 == len) break; status = yajl_parse(parser, (byte_t const *)buf->base, len); if(yajl_status_ok != status) break; pos += len; } status = yajl_complete_parse(parser); if(yajl_status_ok != status) { unsigned char *msg = yajl_get_error(parser, true, (byte_t const *)buf->base, len); fprintf(stderr, "%s", msg); yajl_free_error(parser, msg); msg = NULL; rc = DB_EIO; goto cleanup; } assert(-1 == ctx->depth); // rc = db_txn_commit(subtxn); subtxn = NULL; if(rc < 0) goto cleanup; *out = metaFileID; cleanup: // db_txn_abort(subtxn); subtxn = NULL; FREE(&buf->base); if(parser) yajl_free(parser); parser = NULL; assert_zeroed(ctx->fields, DEPTH_MAX); return rc; }
SCLError Siren_ComputeHash( HASH_Algorithm hash, const char* sirenData, uint8_t* hashOut, bool sorted) { SCLError err = kSCLError_NoErr; yajl_gen_status stat = yajl_gen_status_ok; yajl_handle pHand = NULL; SirenJSONcontext *jctx = NULL; static yajl_callbacks callbacks = { NULL, sParse_bool, NULL, NULL, sParse_number, sParse_string, sParse_start_map, sParse_map_key, sParse_end_map, NULL, NULL }; yajl_alloc_funcs allocFuncs = { yajlMalloc, yajlRealloc, yajlFree, (void *) NULL }; jctx = XMALLOC(sizeof (SirenJSONcontext)); CKNULL(jctx); ZERO(jctx, sizeof(SirenJSONcontext)); err = HASH_Init(hash, &jctx->hash); CKERR; // use yajl to fill the hashableItems with tags and values we can hash pHand = yajl_alloc(&callbacks, &allocFuncs, (void *) jctx); yajl_config(pHand, yajl_allow_comments, 1); stat = (yajl_gen_status) yajl_parse(pHand, (uint8_t*)sirenData, strlen(sirenData)); CKSTAT; stat = (yajl_gen_status) yajl_complete_parse(pHand); CKSTAT; // hash the items found in hashableItems using sHashable_tags_list order int items2Hash = jctx->hashableItemsCount; #if DEBUG_HASH DPRINTF(XCODE_COLORS_RED_TXT,"\nSiren_ComputeHash %s\n", sorted?"sorted":"" ); #endif if(sorted) { for(int j = 0; sHashable_tags_list[j] && items2Hash > 0; j++) { for(int i = 0; i < jctx->hashableItemsCount; i++) { char* tag = sHashable_tags_list[j]; HashableItem *item = &jctx->hashableItems[i]; if(item->tag && strncmp(tag, item->tag, strlen(tag)) == 0 ) { err = shashItem(jctx->hash, item); items2Hash--; break; } } } } else { for(int i = 0; i < items2Hash; i++) { HashableItem *item = &jctx->hashableItems[i]; if(item->tag) { err = shashItem(jctx->hash, item); } } } err = HASH_Final(jctx->hash, hashOut); CKERR; #if DEBUG_HASH DPRINTF(XCODE_COLORS_RED_TXT,"\n"); dumpHex(hashOut, 32, 0); DPRINTF(XCODE_COLORS_RED_TXT,"\n"); #endif done: if(IsntNull(jctx)) { ZERO(jctx, sizeof(SirenJSONcontext)); XFREE(jctx); } if(IsntNull(pHand)) yajl_free(pHand); return err; }
int main(int argc, char ** argv) { yajl_handle hand; static unsigned char fileData[65536]; /* generator config */ yajl_gen g; yajl_status stat; size_t rd; int retval = 0; int a = 1; g = yajl_gen_alloc(NULL); yajl_gen_config(g, yajl_gen_beautify, 1); yajl_gen_config(g, yajl_gen_validate_utf8, 1); /* ok. open file. let's read and parse */ hand = yajl_alloc(&callbacks, NULL, (void *) g); /* and let's allow comments by default */ yajl_config(hand, yajl_allow_comments, 1); /* check arguments.*/ while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) { unsigned int i; for ( i=1; i < strlen(argv[a]); i++) { switch (argv[a][i]) { case 'm': yajl_gen_config(g, yajl_gen_beautify, 0); break; case 'u': yajl_config(hand, yajl_dont_validate_strings, 1); break; default: fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]); usage(argv[0]); } } ++a; } if (a < argc) { usage(argv[0]); } for (;;) { rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin); if (rd == 0) { if (!feof(stdin)) { fprintf(stderr, "error on file read.\n"); retval = 1; } break; } fileData[rd] = 0; stat = yajl_parse(hand, fileData, rd); if (stat != yajl_status_ok) break; { const unsigned char * buf; size_t len; yajl_gen_get_buf(g, &buf, &len); //fwrite(buf, 1, len, stdout); std::cout << "Buf => " << buf << std::endl; yajl_gen_clear(g); } } stat = yajl_complete_parse(hand); if (stat != yajl_status_ok) { unsigned char * str = yajl_get_error(hand, 1, fileData, rd); fprintf(stderr, "%s", (const char *) str); yajl_free_error(hand, str); retval = 1; } yajl_gen_free(g); yajl_free(hand); return retval; }
/* * Parse the JSON protocol header to determine protocol version and features. * In case the buffer does not contain a valid header (invalid JSON, or no * version field found), the 'correct' field of the returned header is set to * false. The amount of bytes consumed by parsing the header is returned in * *consumed (if non-NULL). * */ void parse_json_header(i3bar_child *child, const unsigned char *buffer, int length, unsigned int *consumed) { static yajl_callbacks version_callbacks = { .yajl_boolean = header_boolean, .yajl_integer = header_integer, .yajl_map_key = &header_map_key, }; child_init(child); current_key = NO_KEY; yajl_handle handle = yajl_alloc(&version_callbacks, NULL, child); /* Allow trailing garbage. yajl 1 always behaves that way anyways, but for * yajl 2, we need to be explicit. */ yajl_config(handle, yajl_allow_trailing_garbage, 1); yajl_status state = yajl_parse(handle, buffer, length); if (state != yajl_status_ok) { child_init(child); if (consumed != NULL) *consumed = 0; } else { if (consumed != NULL) *consumed = yajl_get_bytes_consumed(handle); } yajl_free(handle); }