/*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
*
* Jansson is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
#define _GNU_SOURCE
#include <ctype.h>
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
-#include <unistd.h>
#include <assert.h>
#include <jansson.h>
int line, column;
union {
char *string;
- int integer;
+ json_int_t integer;
double real;
} value;
} lex_t;
/*** error reporting ***/
-static void error_init(json_error_t *error)
-{
- if(error)
- {
- error->text[0] = '\0';
- error->line = -1;
- }
-}
-
static void error_set(json_error_t *error, const lex_t *lex,
const char *msg, ...)
{
va_list ap;
- char text[JSON_ERROR_TEXT_LENGTH];
+ char msg_text[JSON_ERROR_TEXT_LENGTH];
+
+ int line = -1, col = -1;
+ const char *result = msg_text;
- if(!error || error->text[0] != '\0') {
- /* error already set */
+ if(!error)
return;
- }
va_start(ap, msg);
- vsnprintf(text, JSON_ERROR_TEXT_LENGTH, msg, ap);
+ vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap);
va_end(ap);
if(lex)
{
const char *saved_text = strbuffer_value(&lex->saved_text);
- error->line = lex->line;
+ char msg_with_context[JSON_ERROR_TEXT_LENGTH];
+
+ line = lex->line;
+
if(saved_text && saved_text[0])
{
if(lex->saved_text.length <= 20) {
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
- "%s near '%s'", text, saved_text);
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
+ "%s near '%s'", msg_text, saved_text);
+ result = msg_with_context;
}
- else
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
}
else
{
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH,
- "%s near end of file", text);
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,
+ "%s near end of file", msg_text);
+ result = msg_with_context;
}
}
- else
- {
- error->line = -1;
- snprintf(error->text, JSON_ERROR_TEXT_LENGTH, "%s", text);
- }
+
+ jsonp_error_set(error, line, col, "%s", result);
}
/*** lexical analyzer ***/
-void stream_init(stream_t *stream, get_func get, eof_func eof, void *data)
+static void
+stream_init(stream_t *stream, get_func get, eof_func eof, void *data)
{
stream->get = get;
stream->eof = eof;
c = stream->buffer[0];
- if(c == EOF && stream->eof(stream->data))
- return EOF;
-
- if(c < 0)
+ if((unsigned char)c >= 0x80 && c != (char)EOF)
{
/* multi-byte UTF-8 sequence */
int i, count;
for(i = 1; i < count; i++)
stream->buffer[i] = stream->get(stream->data);
- if(!utf8_check_full(stream->buffer, count))
+ if(!utf8_check_full(stream->buffer, count, NULL))
goto out;
stream->stream_pos += count;
}
/* assumes that str points to 'u' plus at least 4 valid hex digits */
-static int decode_unicode_escape(const char *str)
+static int32_t decode_unicode_escape(const char *str)
{
int i;
- int value = 0;
+ int32_t value = 0;
assert(str[0] == 'u');
lex->value.string = NULL;
lex->token = TOKEN_INVALID;
- /* skip the " */
c = lex_get_save(lex, error);
while(c != '"') {
- if(c == EOF) {
+ if(c == (char)EOF) {
+ lex_unget_unsave(lex, c);
if(lex_eof(lex))
error_set(error, lex, "premature end of input");
goto out;
}
- else if(0 <= c && c <= 0x1F) {
+ else if((unsigned char)c <= 0x1F) {
/* control character */
lex_unget_unsave(lex, c);
if(c == '\n')
- two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair
are converted to 4 bytes
*/
- lex->value.string = malloc(lex->saved_text.length + 1);
+ lex->value.string = jsonp_malloc(lex->saved_text.length + 1);
if(!lex->value.string) {
/* this is not very nice, since TOKEN_INVALID is returned */
goto out;
if(*p == 'u') {
char buffer[4];
int length;
- int value;
+ int32_t value;
value = decode_unicode_escape(p);
p += 5;
if(0xD800 <= value && value <= 0xDBFF) {
/* surrogate pair */
if(*p == '\\' && *(p + 1) == 'u') {
- int value2 = decode_unicode_escape(++p);
+ int32_t value2 = decode_unicode_escape(++p);
p += 5;
if(0xDC00 <= value2 && value2 <= 0xDFFF) {
/* valid second surrogate */
- value = ((value - 0xD800) << 10) +
- (value2 - 0xDC00) +
- 0x10000;
+ value =
+ ((value - 0xD800) << 10) +
+ (value2 - 0xDC00) +
+ 0x10000;
}
else {
/* invalid second surrogate */
return;
out:
- free(lex->value.string);
+ jsonp_free(lex->value.string);
}
-static void lex_scan_number(lex_t *lex, char c, json_error_t *error)
+#if JSON_INTEGER_IS_LONG_LONG
+#define json_strtoint strtoll
+#else
+#define json_strtoint strtol
+#endif
+
+static int lex_scan_number(lex_t *lex, char c, json_error_t *error)
{
const char *saved_text;
char *end;
+ double value;
lex->token = TOKEN_INVALID;
goto out;
}
}
- else /* c != '0' */ {
+ else if(isdigit(c)) {
c = lex_get_save(lex, error);
while(isdigit(c))
c = lex_get_save(lex, error);
}
+ else {
+ lex_unget_unsave(lex, c);
+ goto out;
+ }
if(c != '.' && c != 'E' && c != 'e') {
+ json_int_t value;
+
lex_unget_unsave(lex, c);
- lex->token = TOKEN_INTEGER;
saved_text = strbuffer_value(&lex->saved_text);
- lex->value.integer = strtol(saved_text, &end, 10);
+
+ errno = 0;
+ value = json_strtoint(saved_text, &end, 10);
+ if(errno == ERANGE) {
+ if(value < 0)
+ error_set(error, lex, "too big negative integer");
+ else
+ error_set(error, lex, "too big integer");
+ goto out;
+ }
+
assert(end == saved_text + lex->saved_text.length);
- return;
+ lex->token = TOKEN_INTEGER;
+ lex->value.integer = value;
+ return 0;
}
if(c == '.') {
}
lex_unget_unsave(lex, c);
- lex->token = TOKEN_REAL;
saved_text = strbuffer_value(&lex->saved_text);
- lex->value.real = strtod(saved_text, &end);
+ value = strtod(saved_text, &end);
assert(end == saved_text + lex->saved_text.length);
+ if(errno == ERANGE && value != 0) {
+ error_set(error, lex, "real number overflow");
+ goto out;
+ }
+
+ lex->token = TOKEN_REAL;
+ lex->value.real = value;
+ return 0;
+
out:
- return;
+ return -1;
}
static int lex_scan(lex_t *lex, json_error_t *error)
strbuffer_clear(&lex->saved_text);
if(lex->token == TOKEN_STRING) {
- free(lex->value.string);
- lex->value.string = NULL;
+ jsonp_free(lex->value.string);
+ lex->value.string = NULL;
}
c = lex_get(lex, error);
c = lex_get(lex, error);
}
- if(c == EOF) {
+ if(c == (char)EOF) {
if(lex_eof(lex))
lex->token = TOKEN_EOF;
else
else if(c == '"')
lex_scan_string(lex, error);
- else if(isdigit(c) || c == '-')
- lex_scan_number(lex, c, error);
+ else if(isdigit(c) || c == '-') {
+ if(lex_scan_number(lex, c, error))
+ goto out;
+ }
else if(isupper(c) || islower(c)) {
/* eat up the whole identifier for clearer error messages */
return lex->token;
}
+static char *lex_steal_string(lex_t *lex)
+{
+ char *result = NULL;
+ if(lex->token == TOKEN_STRING)
+ {
+ result = lex->value.string;
+ lex->value.string = NULL;
+ }
+ return result;
+}
+
static int lex_init(lex_t *lex, get_func get, eof_func eof, void *data)
{
stream_init(&lex->stream, get, eof, data);
static void lex_close(lex_t *lex)
{
if(lex->token == TOKEN_STRING)
- free(lex->value.string);
+ jsonp_free(lex->value.string);
strbuffer_close(&lex->saved_text);
}
goto error;
}
- key = strdup(lex->value.string);
+ key = lex_steal_string(lex);
if(!key)
return NULL;
lex_scan(lex, error);
if(lex->token != ':') {
- free(key);
+ jsonp_free(key);
error_set(error, lex, "':' expected");
goto error;
}
lex_scan(lex, error);
value = parse_value(lex, error);
if(!value) {
- free(key);
+ jsonp_free(key);
goto error;
}
if(json_object_set_nocheck(object, key, value)) {
- free(key);
+ jsonp_free(key);
json_decref(value);
goto error;
}
json_decref(value);
- free(key);
+ jsonp_free(key);
lex_scan(lex, error);
if(lex->token != ',')
break;
case '{':
- json = parse_object(lex, error);
+ json = parse_object(lex, error);
break;
case '[':
return json;
}
-json_t *parse_json(lex_t *lex, json_error_t *error)
+static json_t *parse_json(lex_t *lex, json_error_t *error)
{
- error_init(error);
-
lex_scan(lex, error);
if(lex->token != '[' && lex->token != '{') {
error_set(error, lex, "'[' or '{' expected");
return (stream->data[stream->pos] == '\0');
}
-json_t *json_loads(const char *string, json_error_t *error)
+json_t *json_loads(const char *string, size_t flags, json_error_t *error)
{
lex_t lex;
json_t *result;
+ string_data_t stream_data = {string, 0};
- string_data_t stream_data = {
- .data = string,
- .pos = 0
- };
+ (void)flags; /* unused */
if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
return NULL;
+ jsonp_error_init(error, "<string>");
+
result = parse_json(&lex, error);
if(!result)
goto out;
return result;
}
-json_t *json_loadf(FILE *input, json_error_t *error)
+json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{
lex_t lex;
+ const char *source;
json_t *result;
+ (void)flags; /* unused */
if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
return NULL;
+ if(input == stdin)
+ source = "<stdin>";
+ else
+ source = "<stream>";
+
+ jsonp_error_init(error, source);
+
result = parse_json(&lex, error);
+ if(!result)
+ goto out;
+ lex_scan(&lex, error);
+ if(lex.token != TOKEN_EOF) {
+ error_set(error, &lex, "end of file expected");
+ json_decref(result);
+ result = NULL;
+ }
+
+out:
lex_close(&lex);
return result;
}
-json_t *json_load_file(const char *path, json_error_t *error)
+json_t *json_load_file(const char *path, size_t flags, json_error_t *error)
{
json_t *result;
FILE *fp;
+ jsonp_error_init(error, path);
+
fp = fopen(path, "r");
if(!fp)
{
return NULL;
}
- result = json_loadf(fp, error);
+ result = json_loadf(fp, flags, error);
fclose(fp);
return result;