Esempio n. 1
0
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);
}
Esempio n. 2
0
static void js_parser_assert(lua_State* L,
                             yajl_status status,
                             yajl_handle* handle,
                             const unsigned char* json_text,
                             size_t json_text_len,
                             const char* file,
                             int line)
{
    int verbose = 1;
    unsigned char* msg;

    switch ( status ) {
    case yajl_status_ok:
        return;
    case yajl_status_client_canceled:
        lua_pushfstring(L, "Unreachable: yajl_status_client_canceled should never be returned since all callbacks return true at %s line %d",
                        file, line);
        break;
    case yajl_status_error:
        msg = yajl_get_error(*handle, verbose, json_text, json_text_len);
        lua_pushfstring(L, "InvalidJSONInput: %s at %s line %d", msg, file, line);
        yajl_free_error(*handle, msg);
        break;
    }
    lua_error(L);
}
Esempio n. 3
0
unsigned char *json_get_error(const unsigned char *data_utf8, size_t len)
{
	if (*my_error != '\0') 
		return (unsigned char *)my_error;

	return yajl_get_error(parser, 1, data_utf8, len);
}
Esempio n. 4
0
static size_t cj_curl_callback (void *buf, /* {{{ */
    size_t size, size_t nmemb, void *user_data)
{
  cj_t *db;
  size_t len;
  yajl_status status;

  len = size * nmemb;

  if (len <= 0)
    return (len);

  db = user_data;
  if (db == NULL)
    return (0);

  status = yajl_parse(db->yajl, (unsigned char *)buf, len);
  if (status == yajl_status_ok)
    return (len);
#if !HAVE_YAJL_V2
  else if (status == yajl_status_insufficient_data)
    return (len);
#endif

  unsigned char *msg = yajl_get_error(db->yajl, /* verbose = */ 1,
        /* jsonText = */ (unsigned char *) buf, (unsigned int) len);
  ERROR ("curl_json plugin: yajl_parse failed: %s", msg);
  yajl_free_error(db->yajl, msg);
  return (0); /* abort write callback */
} /* }}} size_t cj_curl_callback */
Esempio n. 5
0
static void
_j4status_i3bar_ouput_action(J4statusPluginContext *context, gchar *line)
{
    gsize length = strlen(line);
    yajl_status json_state;

    json_state = yajl_parse(context->json_handle, (const unsigned char *) line, length);

    if ( json_state == yajl_status_ok )
        return;
    g_free(context->parse_context.name);
    g_free(context->parse_context.instance);

    if ( json_state == yajl_status_error )
    {
        unsigned char *str_error;
        str_error = yajl_get_error(context->json_handle, 0, (const unsigned char *) line, length);
        g_warning("Couldn't parse section from i3bar: %s", str_error);
        yajl_free_error(context->json_handle, str_error);
    }
    else if ( json_state == yajl_status_client_canceled )
    {
        g_warning("i3bar JSON protocol error: %s", context->parse_context.error);
        g_free(context->parse_context.error);
    }
}
Esempio n. 6
0
// Yajl Helpers ==============================================================
void yajl_helper_check_status(yajl_handle handle, yajl_status status, int verbose, const unsigned char * jsonText, size_t jsonTextLength) {
    if(status != yajl_status_ok) {
        unsigned char * str = yajl_get_error(handle, verbose, jsonText, jsonTextLength);
        yajl_free_error(handle, str);
        rb_raise(rb_const_get(rb_const_get(rb_cObject, rb_intern("Wankel")), rb_intern("ParseError")), "%s", (const char*) str);
    }
}
static const char *parse_json(state_t *st, unsigned char *buf, size_t len)
{
  yajl_parser_config cfg = {
    1, /* allow comments */
    0  /* don't check UTF-8 */
  };
  yajl_handle yh;
  yajl_status ys;
  const char *err=NULL;

  alloc_funcs.ctx = st->env; /* will be passed to alloc funcs */
  yh = yajl_alloc(&callbacks, &cfg, &alloc_funcs, st);
  ys = yajl_parse(yh, buf, len);
  if (ys == yajl_status_insufficient_data) {
    ys = yajl_parse_complete(yh);
  }
  if (ys == yajl_status_insufficient_data) {
    err = "unexpected end of document";
  } else if (ys != yajl_status_ok) {
    unsigned char *msg = yajl_get_error(yh, 0, NULL, 0);
    strncpy(st->errmsg, (char *)msg, sizeof(st->errmsg)-1);
    yajl_free_error(yh, msg);
    st->errmsg[sizeof(st->errmsg)] = 0;
    err = st->errmsg;
  }
  yajl_free(yh);
  return err;
}
Esempio n. 8
0
inline bool __JSONParseWithString(__JSONRef json, CFStringRef string, CFErrorRef *error) {
  bool success = 1;
  json->yajlParser = yajl_alloc(&json->yajlParserCallbacks, &json->yajlAllocFuncs, (void *)json);
  if (json->yajlParser) {
//  yajl_config(json->yajlParser, yajl_allow_comments, kJSONReadOptionAllowComments | options ? 1 : 0);
//  yajl_config(json->yajlParser, yajl_dont_validate_strings, kJSONReadOptionCheckUTF8 | options ? 1 : 0);
  
    CFDataRef data = CFStringCreateExternalRepresentation(json->allocator, string, kCFStringEncodingUTF8, 0);
    if (data) {
      if ((json->yajlParserStatus = yajl_parse(json->yajlParser, CFDataGetBytePtr(data), CFDataGetLength(data))) != yajl_status_ok) {
        if (error) {
          success = 0;
          
          unsigned char * str = yajl_get_error(json->yajlParser, 1, CFDataGetBytePtr(data), CFDataGetLength(data));
          fprintf(stderr, "%s", (const char *) str);
          yajl_free_error(json->yajlParser, str);
          
          *error = CFErrorCreateWithUserInfoKeysAndValues(json->allocator, CFSTR("com.github.mirek.CoreJSON"), (CFIndex)json->yajlParserStatus, (const void *) { kCFErrorDescriptionKey }, (const void *) { CFSTR("Test") }, 1);
        }
        // TODO: Error stuff
        //printf("ERROR: %s\n", yajl_get_error(json->yajlParser, 1, __JSONUTF8StringGetBuffer(utf8), __JSONUTF8StringGetMaximumSize(utf8)));
      }
    
      json->yajlParserStatus = yajl_complete_parse(json->yajlParser);
      CFRelease(data);
    } else {
Esempio n. 9
0
std::vector<char> LKStreamTranslator::process(const std::vector<char> &chunk)
{
	std::vector<char> retval;
	
	// We work with C strings here, because YAJL
	const char *str = chunk.data();
	size_t len = chunk.size();
	
	// Abort if the string is empty anyways
	if(len == 0)
		return retval;
	
	// Skip any BOM if there is one
	const unsigned char bom[] = {0xEF, 0xBB, 0xBF};
	if(len >= 3 && memcmp(bom, str, 3) == 0)
	{
		str += 3;
		len -= 3;
	}
	
	// Abort if it had nothing but a BOM
	if(len == 0)
		return retval;
	
	// KanColle specific: most datablobs are prefixed with "svdata=", that's
	// needed for the game to work, but we can't parse with it left in
	const char *svdata = "svdata=";
	if(memcmp(svdata, str, (size_t)std::min(strlen(svdata), len)) == 0)
	{
		str += strlen(svdata);
		len -= strlen(svdata);
		
		retval.insert(retval.end(), svdata, svdata + strlen(svdata));
	}
	
	if(len > 0)
	{
		if(yajl_parse(ctx.parser, (const unsigned char *)str, len) != yajl_status_ok)
		{
			unsigned char *error = yajl_get_error(ctx.parser, true, (const unsigned char *)str, len);
			std::string error_string((const char *)error);
			yajl_free_error(ctx.parser, error);
			
			throw std::runtime_error("JSON Parsing Error: " + error_string);
		}
		
		const unsigned char *buf;
		size_t buf_len;
		yajl_gen_get_buf(ctx.gen, &buf, &buf_len);
		
		if(buf_len > 0)
		{
			retval.insert(retval.end(), buf, buf + buf_len);
			yajl_gen_clear(ctx.gen);
		}
	}
	
	return retval;
}
Esempio n. 10
0
static int cj_curl_perform (cj_t *db, CURL *curl) /* {{{ */
{
  int status;
  long rc;
  char *url;
  yajl_handle yprev = db->yajl;

  db->yajl = yajl_alloc (&ycallbacks, NULL, NULL, (void *)db);
  if (db->yajl == NULL)
  {
    ERROR ("curl_json plugin: yajl_alloc failed.");
    db->yajl = yprev;
    return (-1);
  }

  url = NULL;
  curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);

  status = curl_easy_perform (curl);
  if (status != 0)
  {
    ERROR ("curl_json plugin: curl_easy_perform failed with status %i: %s (%s)",
           status, db->curl_errbuf, (url != NULL) ? url : "<null>");
    yajl_free (db->yajl);
    db->yajl = yprev;
    return (-1);
  }

  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);

  /* The response code is zero if a non-HTTP transport was used. */
  if ((rc != 0) && (rc != 200))
  {
    ERROR ("curl_json plugin: curl_easy_perform failed with "
        "response code %ld (%s)", rc, url);
    yajl_free (db->yajl);
    db->yajl = yprev;
    return (-1);
  }

  status = yajl_parse_complete (db->yajl);
  if (status != yajl_status_ok)
  {
    unsigned char *errmsg;

    errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
        /* jsonText = */ NULL, /* jsonTextLen = */ 0);
    ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
        (char *) errmsg);
    yajl_free_error (db->yajl, errmsg);
    yajl_free (db->yajl);
    db->yajl = yprev;
    return (-1);
  }

  yajl_free (db->yajl);
  db->yajl = yprev;
  return (0);
} /* }}} int cj_curl_perform */
Esempio n. 11
0
static void do_yajl_error(yajl_handle yajl, struct asfd *asfd)
{
	unsigned char *str;
	str=yajl_get_error(yajl, 1,
		(const unsigned char *)asfd->rbuf->buf, asfd->rbuf->len);
	logp("yajl error: %s\n", (const char *)str);
	yajl_free_error(yajl, str);
}
Esempio n. 12
0
    /* this method can only be called once */
    void GetError(const std::string& json, std::string* errout) {
        assert(errstring == nullptr);
        errstring = yajl_get_error(hand, 1,
                       (unsigned char*)json.c_str(),
                       json.length());
         *errout = std::string((char*)errstring);

    }
Esempio n. 13
0
/* 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;
}
Esempio n. 14
0
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);
}
Esempio n. 15
0
static struct rest_json_payload *
rest_get_json_upload(mtev_http_rest_closure_t *restc,
                    int *mask, int *complete) {
  struct rest_json_payload *rxc;
  mtev_http_request *req = mtev_http_session_request(restc->http_ctx);
  httptrap_closure_t *ccl = NULL;
  int content_length;
  char buffer[32768];

  content_length = mtev_http_request_content_length(req);
  rxc = restc->call_closure;
  rxc->check = noit_poller_lookup(rxc->check_id);
  if (!rxc->check) {
    *complete = 1;
    return NULL;
  }

  if(!strcmp(rxc->check->module, "httptrap")) ccl = rxc->check->closure;
  rxc->immediate = noit_httptrap_check_asynch(ccl ? ccl->self : NULL, rxc->check);
  while(!rxc->complete) {
    int len;
    len = mtev_http_session_req_consume(
            restc->http_ctx, buffer,
            MIN(content_length - rxc->len, sizeof(buffer)),
            sizeof(buffer),
            mask);
    if(len > 0) {
      yajl_status status;
      _YD("inbound payload chunk (%d bytes) continuing YAJL parse\n", len);
      status = yajl_parse(rxc->parser, (unsigned char *)buffer, len);
      if(status != yajl_status_ok) {
        unsigned char *err;
        *complete = 1;
        err = yajl_get_error(rxc->parser, 0, (unsigned char *)buffer, len);
        rxc->error = strdup((char *)err);
        yajl_free_error(rxc->parser, err);
        return rxc;
      }
      rxc->len += len;
    }
    if(len < 0 && errno == EAGAIN) return NULL;
    else if(len < 0) {
      *complete = 1;
      return NULL;
    }
    content_length = mtev_http_request_content_length(req);
    if((mtev_http_request_payload_chunked(req) && len == 0) ||
       (rxc->len == content_length)) {
      rxc->complete = 1;
      _YD("no more data, finishing YAJL parse\n");
      yajl_complete_parse(rxc->parser);
    }
  }

  *complete = 1;
  return rxc;
}
Esempio n. 16
0
bool hkvJsonStreamReader::Parse(const char* pszFileName, unsigned int uiChunkSize)
{
  yajl_status stat;
  void* pFile;
  size_t rd;

  // Allocate read buffer
  unsigned char* pFileData = (unsigned char*)(YA_MALLOC(&(m_pHandle->alloc), uiChunkSize));
  assert(pFileData);

  // Open file
  pFile = m_pFileHandler->Open(pszFileName, hkvJsonFileHandler::JFM_READ);
  if (pFile == NULL)
    return false;

  for (;;)
  {
    rd = m_pFileHandler->Read((void*)pFileData, uiChunkSize - 1, pFile);

    if (rd == 0)
    {
      if (!m_pFileHandler->IsEOF(pFile))
        fprintf(stderr, "Error encountered on file read.\n");
      break;
    }
    pFileData[rd] = 0;

    // Pass to parser
    stat = yajl_parse(m_pHandle, pFileData, rd);

    if (stat != yajl_status_ok)
      break;
  }

  // Parse any remaining buffered data
  stat = yajl_complete_parse(m_pHandle);

  if (stat != yajl_status_ok)
  {
    unsigned char * str = yajl_get_error(m_pHandle, 1, pFileData, rd);
    fprintf(stderr, "%s", (const char *) str);
    yajl_free_error(m_pHandle, str);

    assert(false);
    return false;
  }

  // Close the file
  m_pFileHandler->Close(pFile);

  // Free read buffer
  YA_FREE(&(m_pHandle->alloc), pFileData);

  return true;
}
Esempio n. 17
0
void yajl_parse_chunk(const unsigned char * chunk, unsigned int len, yajl_handle parser) {
    yajl_status stat;

    stat = yajl_parse(parser, chunk, len);

    if (stat != yajl_status_ok && stat != yajl_status_insufficient_data) {
        unsigned char * str = yajl_get_error(parser, 1, chunk, len);
        rb_raise(cParseError, "%s", (const char *) str);
        yajl_free_error(parser, str);
    }
}
Esempio n. 18
0
unsigned char *json_streamer_get_last_error(struct json_streamer *json,
		unsigned char *data, size_t size)
{
	if (!json || !json->yajl || !data || !size)
		return false;

	return yajl_get_error(json->yajl,
			1,
			data,
			size);
}
Esempio n. 19
0
static int cj_perform (cj_t *db) /* {{{ */
{
    int status;
    yajl_handle yprev = db->yajl;

    db->yajl = yajl_alloc (&ycallbacks,
#if HAVE_YAJL_V2
                           /* alloc funcs = */ NULL,
#else
                           /* alloc funcs = */ NULL, NULL,
#endif
                           /* context = */ (void *)db);
    if (db->yajl == NULL)
    {
        ERROR ("curl_json plugin: yajl_alloc failed.");
        db->yajl = yprev;
        return (-1);
    }

    if (db->url)
        status = cj_curl_perform (db);
    else
        status = cj_sock_perform (db);
    if (status < 0)
    {
        yajl_free (db->yajl);
        db->yajl = yprev;
        return (-1);
    }

#if HAVE_YAJL_V2
    status = yajl_complete_parse(db->yajl);
#else
    status = yajl_parse_complete(db->yajl);
#endif
    if (status != yajl_status_ok)
    {
        unsigned char *errmsg;

        errmsg = yajl_get_error (db->yajl, /* verbose = */ 0,
                                 /* jsonText = */ NULL, /* jsonTextLen = */ 0);
        ERROR ("curl_json plugin: yajl_parse_complete failed: %s",
               (char *) errmsg);
        yajl_free_error (db->yajl, errmsg);
        yajl_free (db->yajl);
        db->yajl = yprev;
        return (-1);
    }

    yajl_free (db->yajl);
    db->yajl = yprev;
    return (0);
} /* }}} int cj_perform */
int parseSampleFromFile(Sample *sample, FILE *inputFile) {
    yajl_handle hand;
    static unsigned char fileData[READ_BUFFER_SIZE];
    ParseContext g;
    g.sample = sample;

    yajl_status stat;
    size_t rd;
    /* allow comments */
    yajl_parser_config cfg = { 1, 1 };
    int retval = 1, done = 0;

    /* ok.  open file.  let's read and parse */
    hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) &g);
        
    while (!done) {
        rd = fread((void *) fileData, 1, sizeof(fileData) - 1, inputFile);
        
        if (rd == 0) {
            if (!feof(inputFile)) {
                fprintf(stderr, "error on file read.\n");
                retval = 0;
                break;
            }
            done = 1;
        }
        fileData[rd] = 0;
        
        if (done)
            /* parse any remaining buffered data */
            stat = yajl_parse_complete(hand);
        else
            /* read file data, pass to parser */
            stat = yajl_parse(hand, fileData, rd);

        if (stat != yajl_status_ok &&
            stat != yajl_status_insufficient_data)
        {
            unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
            fprintf(stderr, "%s", (const char *) str);
            yajl_free_error(hand, str);
            retval = 0;
            if( stat == yajl_status_client_canceled) {
                fprintf(stderr, "%s\n", g.lastError);
            }
            break;
        }
    }

    yajl_free(hand);
    
    return retval;
}
Esempio n. 21
0
void reformat(ErlDrvPort port, char* buf, int len)
{
    yajl_handle hand;
    /* generator config */
    yajl_gen_config conf = { 1, "  " };
	yajl_gen g;
    yajl_status stat;
    /* allow comments */
    yajl_parser_config cfg = { 1, 1 };

    g = yajl_gen_alloc(&conf, NULL);

    /* ok.  open file.  let's read and parse */
    hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g);
    
    /* read file data, pass to parser */
    stat = yajl_parse(hand, (unsigned char*) buf, len);

    if (stat != yajl_status_ok &&
        stat != yajl_status_insufficient_data)
    {
        char* err = (char*) yajl_get_error(hand, 1, (unsigned char*) buf, len);
        int len = strlen(err);

        ErlDrvTermData msg[] = {
            ERL_DRV_ATOM, driver_mk_atom("error"), 
            ERL_DRV_BUF2BINARY, (ErlDrvTermData) err, (ErlDrvUInt) len,
            ERL_DRV_TUPLE, 2
        };

        driver_send_term(port, driver_caller(port), msg, sizeof(msg) / sizeof(msg[0]));
    } else {
        const unsigned char* json;
        unsigned int len;
        yajl_gen_get_buf(g, &json, &len);

        ErlDrvTermData msg[] = {
            ERL_DRV_ATOM, driver_mk_atom("ok"),
            ERL_DRV_BUF2BINARY, (ErlDrvTermData) json, (ErlDrvUInt) len,
            ERL_DRV_TUPLE, 2
        };

        driver_send_term(port, driver_caller(port), msg, sizeof(msg) / sizeof(msg[0]));

        yajl_gen_clear(g);
    }

    yajl_gen_free(g);
    yajl_free(hand);
}
Esempio n. 22
0
/**
 * Feed one chunk of data to the JSON parser.
 */
int json_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char **error_msg) {
    if (error_msg == NULL) return -1;
    *error_msg = NULL;

    /* Feed our parser and catch any errors */
    msr->json->status = yajl_parse(msr->json->handle, buf, size);
    if (msr->json->status != yajl_status_ok) {
        /* We need to free the yajl error message later, how to do this? */
        *error_msg = yajl_get_error(msr->json->handle, 0, buf, size);
        return -1;
    }

    return 1;
}
Esempio n. 23
0
TEST_F(Yajl, yajl_parse_nullcallbacks) {
	for (size_t i = 0; i < kTrialCount; i++) {
		yajl_handle hand = yajl_alloc(&nullcallbacks, NULL, NULL);
		yajl_status stat = yajl_parse(hand, (unsigned char*)json_, length_ - 1);
		//ASSERT_EQ(yajl_status_ok, stat);
		if (stat != yajl_status_ok) {
			unsigned char * str = yajl_get_error(hand, 1, (unsigned char*)json_, length_ + 1);
			fprintf(stderr, "%s", (const char *) str);
		}
		stat = yajl_complete_parse(hand);
		ASSERT_EQ(yajl_status_ok, stat);
		yajl_free(hand);
	}	
}
Esempio n. 24
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);
}
Esempio n. 25
0
int main(int argc, char ** argv)
{
    yajl_handle hand;
    yajl_status stat;
    yajl_parser_config cfg = { 1, 1 };

    int done = 0;

    hand = yajl_alloc(&callbacks, &cfg, NULL, (void *) g);
        
    while (!done) {
        rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
        
        if (rd == 0) {
            if (!feof(stdin)) {
                fprintf(stderr, "error on file read.\n");
                break;
            }
            done = 1;
        }
        fileData[rd] = 0;
        
        if (done)
            /* parse any remaining buffered data */
            stat = yajl_parse_complete(hand);
        else
            /* read file data, pass to parser */
            stat = yajl_parse(hand, fileData, rd);

        if (stat != yajl_status_ok &&
            stat != yajl_status_insufficient_data)
        {
            unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
            fprintf(stderr, (const char *) str);
            yajl_free_error(hand, str);
        } else {
            const unsigned char * buf;
            unsigned int len;
            yajl_gen_get_buf(g, &buf, &len);
            fwrite(buf, 1, len, stdout);
            yajl_gen_clear(g);
        }
    }

    yajl_gen_free(g);
    yajl_free(hand);
    
    return 0;
}
Esempio n. 26
0
static struct rest_json_payload *
rest_get_json_upload(mtev_http_rest_closure_t *restc,
                    int *mask, int *complete) {
  struct rest_json_payload *rxc;
  mtev_http_request *req = mtev_http_session_request(restc->http_ctx);
  httptrap_closure_t *ccl;
  int content_length;
  char buffer[32768];

  content_length = mtev_http_request_content_length(req);
  rxc = restc->call_closure;
  ccl = rxc->check->closure;
  rxc->immediate = mtev_httptrap_check_aynsch(ccl->self, rxc->check);
  while(!rxc->complete) {
    int len;
    len = mtev_http_session_req_consume(
            restc->http_ctx, buffer,
            MIN(content_length - rxc->len, sizeof(buffer)),
            sizeof(buffer),
            mask);
    if(len > 0) {
      yajl_status status;
      status = yajl_parse(rxc->parser, (unsigned char *)buffer, len);
      if(status != yajl_status_ok) {
        unsigned char *err;
        *complete = 1;
        err = yajl_get_error(rxc->parser, 0, (unsigned char *)buffer, len);
        rxc->error = strdup((char *)err);
        yajl_free_error(rxc->parser, err);
        return rxc;
      }
      rxc->len += len;
    }
    if(len < 0 && errno == EAGAIN) return NULL;
    else if(len < 0) {
      *complete = 1;
      return NULL;
    }
    content_length = mtev_http_request_content_length(req);
    if((mtev_http_request_payload_chunked(req) && len == 0) ||
       (rxc->len == content_length)) {
      rxc->complete = 1;
      yajl_complete_parse(rxc->parser);
    }
  }

  *complete = 1;
  return rxc;
}
Esempio n. 27
0
/**
 * Finalise JSON parsing.
 */
int json_complete(modsec_rec *msr, char **error_msg) {
    char *json_data = (char *) NULL;

    if (error_msg == NULL) return -1;
    *error_msg = NULL;

    /* Wrap up the parsing process */
    msr->json->status = yajl_complete_parse(msr->json->handle);
    if (msr->json->status != yajl_status_ok) {
        /* We need to free the yajl error message later, how to do this? */
        *error_msg = yajl_get_error(msr->json->handle, 0, NULL, 0);
        return -1;
    }

    return 1;
}
Esempio n. 28
-1
/**
 * Read JSON data and parse it using the given YAJL parser.
 * @param url URL of the JSON data.
 * @param hand YAJL parser handle.
 * @return -1 on error, 0 on success.
 */
static int
soundcloud_parse_json(const char *url, yajl_handle hand, GMutex* mutex, GCond* cond)
{
	struct input_stream *input_stream;
	GError *error = NULL;
	char buffer[4096];
	unsigned char *ubuffer = (unsigned char *)buffer;
	size_t nbytes;

	input_stream = input_stream_open(url, mutex, cond, &error);
	if (input_stream == NULL) {
		if (error != NULL) {
			g_warning("%s", error->message);
			g_error_free(error);
		}
		return -1;
	}

	g_mutex_lock(mutex);
	input_stream_wait_ready(input_stream);

	yajl_status stat;
	int done = 0;

	while (!done) {
		nbytes = input_stream_read(input_stream, buffer, sizeof(buffer), &error);
		if (nbytes == 0) {
			if (error != NULL) {
				g_warning("%s", error->message);
				g_error_free(error);
			}
			if (input_stream_eof(input_stream)) {
				done = true;
			} else {
				g_mutex_unlock(mutex);
				input_stream_close(input_stream);
				return -1;
			}
		}

		if (done)
			stat = yajl_parse_complete(hand);
		else
			stat = yajl_parse(hand, ubuffer, nbytes);

		if (stat != yajl_status_ok &&
			stat != yajl_status_insufficient_data)
		{
			unsigned char *str = yajl_get_error(hand, 1, ubuffer, nbytes);
			g_warning("%s", str);
			yajl_free_error(hand, str);
			break;
		}
	}

	g_mutex_unlock(mutex);
	input_stream_close(input_stream);

	return 0;
}
struct aws_dynamo_create_table_response
*aws_dynamo_parse_create_table_response(const char *response, int response_len)
{
	yajl_handle hand;
	yajl_status stat;
	struct ctx _ctx = { 0 };

	_ctx.r = calloc(sizeof(*(_ctx.r)), 1);
	if (_ctx.r == NULL) {
		Warnx("aws_dynamo_parse_create_table_response: response alloc failed.");
		return NULL;
	}

#if YAJL_MAJOR == 2
	hand = yajl_alloc(&handle_callbacks, NULL, &_ctx);
	yajl_parse(hand, response, response_len);
	stat = yajl_complete_parse(hand);
#else
	hand = yajl_alloc(&handle_callbacks, NULL, NULL, &_ctx);
	yajl_parse(hand, response, response_len);
	stat = yajl_parse_complete(hand);
#endif

	if (stat != yajl_status_ok) {
		unsigned char *str = yajl_get_error(hand, 1, response, response_len);
		Warnx("aws_dynamo_parse_create_table_response: json parse failed, '%s'", (const char *)str);
		yajl_free_error(hand, str);
		yajl_free(hand);
		aws_dynamo_free_create_table_response(_ctx.r);
		return NULL;
	}

	yajl_free(hand);
	return _ctx.r;
}
Esempio n. 30
-4
void json_parse_to_ei(ErlDrvData session, const unsigned char* s, int len, int opts) {
  int parseValue = opts & EEP0018_PARSE_VALUE;
  
  unsigned char* yajl_msg = 0;
  unsigned char* msg = 0;
  ErlDrvPort port = (ErlDrvPort) session;

  /*
   * initialize yajl parser
   */
  State state;
  init(&state, opts);
  
  yajl_parser_config conf = { YAJL_ALLOW_COMMENTS }; // , YAJL_CHECK_UTF8 };
  yajl_handle handle = yajl_alloc(&callbacks, &conf, &state);

  /* start parser */
  yajl_status stat;
  if(parseValue) stat = yajl_parse(handle, (const unsigned char*) "[ ", 2);
  stat = yajl_parse(handle, s, len);
  if(parseValue) stat = yajl_parse(handle, (const unsigned char*) " ]", 2);

  /*
   * sometimes the parser is still stuck inside a JSON token. This finishs
   * the token no matter what.
   */
  if(stat == yajl_status_insufficient_data)
    stat = yajl_parse(handle, (const unsigned char*) " ", 1);

  /* 
   * get an error message on errors
   */
  switch(stat) {
    case yajl_status_ok: break;
    case yajl_status_insufficient_data: 
      msg = (unsigned char*)"Insufficient data"; 
      break;
    default:
      msg = yajl_msg = yajl_get_error(handle, 0, s, len);
      break;
  }

  /* 
   * if result is not ok: we write {error, "reason"} instead. This is 
   * something that will never be encoded from any JSON data.
   */
  if(msg) {
    ei_x_free(&state.ei_buf);
    ei_x_new_with_version(&state.ei_buf);

    ei_x_encode_tuple_header(&state.ei_buf, 2);
    ei_x_encode_atom_len(&state.ei_buf, "error", 5);
    ei_x_encode_string_len(&state.ei_buf, (const char*) msg, strlen((const char*) msg));

    if(yajl_msg) yajl_free_error(yajl_msg);
  } 

  send_data(port, EEP0018_EI, state.ei_buf.buff, state.ei_buf.index);
  deinit(&state);
}