summaryrefslogtreecommitdiff
path: root/json.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2014-09-28 08:33:41 -0400
committerEric S. Raymond <esr@thyrsus.com>2014-09-28 08:33:41 -0400
commit1dc4b36b9d2026ae2eb9f856b0c77734d6768c5b (patch)
tree5d2f1c05c172ab87ade75e1c282ad076652a7665 /json.c
parent91a2a0a5f182ddf02b6d77ffda369906e5def4d3 (diff)
downloadgpsd-1dc4b36b9d2026ae2eb9f856b0c77734d6768c5b.tar.gz
More microjson resync. All regression tests pass.
Diffstat (limited to 'json.c')
-rw-r--r--json.c78
1 files changed, 14 insertions, 64 deletions
diff --git a/json.c b/json.c
index 7cea23a3..bad17507 100644
--- a/json.c
+++ b/json.c
@@ -258,7 +258,8 @@ static int json_internal_read_object(const char *cp,
state = in_attr;
pattr = attrbuf;
#ifndef JSON_MINIMAL
- *end = cp;
+ if (end != NULL)
+ *end = cp;
#endif /* JSON_MINIMAL */
} else if (*cp == '}')
break;
@@ -272,12 +273,9 @@ static int json_internal_read_object(const char *cp,
}
break;
case in_attr:
- if (pattr == NULL) {
-#ifndef JSON_MINIMAL
+ if (pattr == NULL)
/* don't update end here, leave at attribute start */
-#endif /* JSON_MINIMAL */
return JSON_ERR_NULLPTR;
- }
if (*cp == '"') {
*pattr++ = '\0';
json_debug_trace((1, "Collected attribute name %s\n",
@@ -292,9 +290,7 @@ static int json_internal_read_object(const char *cp,
json_debug_trace((1,
"Unknown attribute name '%s' (attributes begin with '%s').\n",
attrbuf, attrs->attribute));
-#ifndef JSON_MINIMAL
/* don't update end here, leave at attribute start */
-#endif /* JSON_MINIMAL */
return JSON_ERR_BADATTR;
}
state = await_value;
@@ -309,9 +305,7 @@ static int json_internal_read_object(const char *cp,
pval = valbuf;
} else if (pattr >= attrbuf + JSON_ATTR_MAX - 1) {
json_debug_trace((1, "Attribute name too long.\n"));
-#ifndef JSON_MINIMAL
/* don't update end here, leave at attribute start */
-#endif /* JSON_MINIMAL */
return JSON_ERR_ATTRLEN;
} else
*pattr++ = *cp;
@@ -353,12 +347,9 @@ static int json_internal_read_object(const char *cp,
}
break;
case in_val_string:
- if (pval == NULL) {
-#ifndef JSON_MINIMAL
+ if (pval == NULL)
/* don't update end here, leave at value start */
-#endif /* JSON_MINIMAL */
return JSON_ERR_NULLPTR;
- }
if (*cp == '\\')
state = in_escape;
else if (*cp == '"') {
@@ -368,21 +359,15 @@ static int json_internal_read_object(const char *cp,
} else if (pval > valbuf + JSON_VAL_MAX - 1
|| pval > valbuf + maxlen) {
json_debug_trace((1, "String value too long.\n"));
-#ifndef JSON_MINIMAL
/* don't update end here, leave at value start */
-#endif /* JSON_MINIMAL */
return JSON_ERR_STRLONG; /* */
} else
*pval++ = *cp;
break;
case in_escape:
- if (pval == NULL) {
-#ifndef JSON_MINIMAL
- if (end != NULL)
- *end = cp;
-#endif /* JSON_MINIMAL */
+ if (pval == NULL)
+ /* don't update end here, leave at value start */
return JSON_ERR_NULLPTR;
- }
switch (*cp) {
case 'b':
*pval++ = '\b';
@@ -413,13 +398,9 @@ static int json_internal_read_object(const char *cp,
state = in_val_string;
break;
case in_val_token:
- if (pval == NULL) {
-#ifndef JSON_MINIMAL
- if (end != NULL)
- *end = cp;
-#endif /* JSON_MINIMAL */
+ if (pval == NULL)
+ /* don't update end here, leave at value start */
return JSON_ERR_NULLPTR;
- }
if (isspace((unsigned char) *cp) || *cp == ',' || *cp == '}') {
*pval = '\0';
json_debug_trace((1, "Collected token value %s.\n", valbuf));
@@ -428,9 +409,7 @@ static int json_internal_read_object(const char *cp,
--cp;
} else if (pval > valbuf + JSON_VAL_MAX - 1) {
json_debug_trace((1, "Token value too long.\n"));
-#ifndef JSON_MINIMAL
/* don't update end here, leave at start of value token */
-#endif /* JSON_MINIMAL */
return JSON_ERR_TOKLONG;
} else
*pval++ = *cp;
@@ -486,10 +465,6 @@ static int json_internal_read_object(const char *cp,
}
json_debug_trace((1, "Invalid enumerated value string %s.\n",
valbuf));
-#ifndef JSON_MINIMAL
- if (end != NULL)
- *end = cp;
-#endif /* JSON_MINIMAL */
return JSON_ERR_BADENUM;
foundit:
(void)snprintf(valbuf, sizeof(valbuf), "%d", mp->value);
@@ -535,12 +510,9 @@ static int json_internal_read_object(const char *cp,
}
break;
case t_character:
- if (strlen(valbuf) > 1) {
-#ifndef JSON_MINIMAL
- /* don't update end here, leave at start of value token */
-#endif /* JSON_MINIMAL */
+ if (strlen(valbuf) > 1)
+ /* don't update end here, leave at value start */
return JSON_ERR_STRLONG;
- }
else
lptr[0] = valbuf[0];
break;
@@ -554,9 +526,7 @@ static int json_internal_read_object(const char *cp,
json_debug_trace((1,
"Required attribute value %s not present.\n",
cursor->dflt.check));
-#ifndef JSON_MINIMAL
/* don't update end here, leave at start of attribute */
-#endif /* JSON_MINIMAL */
return JSON_ERR_CHECKFAIL;
}
break;
@@ -631,13 +601,8 @@ int json_read_array(const char *cp, const struct json_array_t *arr,
case t_string:
if (isspace((unsigned char) *cp))
cp++;
- if (*cp != '"') {
-#ifndef JSON_MINIMAL
- if (end != NULL)
- *end = cp;
-#endif /* JSON_MINIMAL */
+ if (*cp != '"')
return JSON_ERR_BADSTRING;
- }
else
++cp;
arr->arr.strings.ptrs[offset] = tp;
@@ -650,10 +615,6 @@ int json_read_array(const char *cp, const struct json_array_t *arr,
} else if (*cp == '\0') {
json_debug_trace((1,
"Bad string syntax in string list.\n"));
-#ifndef JSON_MINIMAL
- if (end != NULL)
- *end = cp;
-#endif /* JSON_MINIMAL */
return JSON_ERR_BADSTRING;
} else {
*tp = *cp++;
@@ -687,9 +648,8 @@ int json_read_array(const char *cp, const struct json_array_t *arr,
case t_uinteger:
#ifndef JSON_MINIMAL
arr->arr.uintegers.store[offset] = (unsigned int)strtoul(cp, &ep, 0);
- if (ep == cp) {
+ if (ep == cp)
return JSON_ERR_BADNUM;
- }
else
cp = ep;
break;
@@ -709,14 +669,13 @@ int json_read_array(const char *cp, const struct json_array_t *arr,
return JSON_ERR_BADSTRING;
else
++cp;
-#endif /* JSON_MINIMAL */
break;
+#endif /* JSON_MINIMAL */
case t_real:
#ifndef JSON_MINIMAL
arr->arr.reals.store[offset] = strtod(cp, &ep);
- if (ep == cp) {
+ if (ep == cp)
return JSON_ERR_BADNUM;
- }
else
cp = ep;
break;
@@ -733,16 +692,11 @@ int json_read_array(const char *cp, const struct json_array_t *arr,
}
break;
#endif /* JSON_MINIMAL */
- case t_time:
case t_character:
case t_array:
case t_check:
case t_ignore:
json_debug_trace((1, "Invalid array subtype.\n"));
-#ifndef JSON_MINIMAL
- if (end != NULL)
- *end = cp;
-#endif /* JSON_MINIMAL */
return JSON_ERR_SUBTYPE;
}
arrcount++;
@@ -755,10 +709,6 @@ int json_read_array(const char *cp, const struct json_array_t *arr,
cp++;
else {
json_debug_trace((1, "Bad trailing syntax on array.\n"));
-#ifndef JSON_MINIMAL
- if (end != NULL)
- *end = cp;
-#endif /* JSON_MINIMAL */
return JSON_ERR_BADSUBTRAIL;
}
}