2 * JavaScript Object Notation (JSON) parser (RFC7159)
3 * Copyright (c) 2017, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
15 #define JSON_MAX_DEPTH 10
16 #define JSON_MAX_TOKENS 500
19 void json_escape_string(char *txt, size_t maxlen, const char *data, size_t len)
21 char *end = txt + maxlen;
24 for (i = 0; i < len; i++) {
50 if (data[i] >= 32 && data[i] <= 126) {
53 txt += os_snprintf(txt, end - txt, "\\u%04x",
64 static char * json_parse_string(const char **json_pos, const char *end)
66 const char *pos = *json_pos;
67 char *str, *spos, *s_end;
68 size_t max_len, buf_len;
71 pos++; /* skip starting quote */
73 max_len = end - pos + 1;
74 buf_len = max_len > 10 ? 10 : max_len;
75 str = os_malloc(buf_len);
79 s_end = str + buf_len;
81 for (; pos < end; pos++) {
82 if (buf_len < max_len && s_end - spos < 3) {
88 if (buf_len > max_len)
90 tmp = os_realloc(str, buf_len);
95 s_end = str + buf_len;
99 case '\"': /* end string */
101 /* caller will move to the next position */
123 hexstr2bin(pos + 1, bin, 2) < 0 ||
125 wpa_printf(MSG_DEBUG,
126 "JSON: Invalid \\u escape");
129 if (bin[0] == 0x00) {
138 wpa_printf(MSG_DEBUG,
139 "JSON: Unknown escape '%c'", *pos);
155 static int json_parse_number(const char **json_pos, const char *end,
158 const char *pos = *json_pos;
162 for (; pos < end; pos++) {
163 if (*pos != '-' && (*pos < '0' || *pos > '9')) {
170 len = pos - *json_pos + 1;
171 str = os_malloc(len + 1);
174 os_memcpy(str, *json_pos, len);
177 *ret_val = atoi(str);
184 static int json_check_tree_state(struct json_token *token)
188 if (json_check_tree_state(token->child) < 0 ||
189 json_check_tree_state(token->sibling) < 0)
191 if (token->state != JSON_COMPLETED) {
192 wpa_printf(MSG_DEBUG,
193 "JSON: Unexpected token state %d (name=%s type=%d)",
194 token->state, token->name ? token->name : "N/A",
202 static struct json_token * json_alloc_token(unsigned int *tokens)
205 if (*tokens > JSON_MAX_TOKENS) {
206 wpa_printf(MSG_DEBUG, "JSON: Maximum token limit exceeded");
209 return os_zalloc(sizeof(struct json_token));
213 struct json_token * json_parse(const char *data, size_t data_len)
215 struct json_token *root = NULL, *curr_token = NULL, *token = NULL;
216 const char *pos, *end;
219 unsigned int depth = 0;
220 unsigned int tokens = 0;
223 end = data + data_len;
225 for (; pos < end; pos++) {
227 case '[': /* start array */
228 case '{': /* start object */
230 token = json_alloc_token(&tokens);
235 } else if (curr_token->state == JSON_WAITING_VALUE) {
237 } else if (curr_token->parent &&
238 curr_token->parent->type == JSON_ARRAY &&
239 curr_token->parent->state == JSON_STARTED &&
240 curr_token->state == JSON_EMPTY) {
243 wpa_printf(MSG_DEBUG,
244 "JSON: Invalid state for start array/object");
248 if (depth > JSON_MAX_DEPTH) {
249 wpa_printf(MSG_DEBUG,
250 "JSON: Max depth exceeded");
253 token->type = *pos == '[' ? JSON_ARRAY : JSON_OBJECT;
254 token->state = JSON_STARTED;
255 token->child = json_alloc_token(&tokens);
258 curr_token = token->child;
259 curr_token->parent = token;
260 curr_token->state = JSON_EMPTY;
262 case ']': /* end array */
263 case '}': /* end object */
264 if (!curr_token || !curr_token->parent ||
265 curr_token->parent->state != JSON_STARTED) {
266 wpa_printf(MSG_DEBUG,
267 "JSON: Invalid state for end array/object");
271 curr_token = curr_token->parent;
273 curr_token->type != JSON_ARRAY) ||
275 curr_token->type != JSON_OBJECT)) {
276 wpa_printf(MSG_DEBUG,
277 "JSON: Array/Object mismatch");
280 if (curr_token->child->state == JSON_EMPTY &&
281 !curr_token->child->child &&
282 !curr_token->child->sibling) {
283 /* Remove pending child token since the
284 * array/object was empty. */
285 json_free(curr_token->child);
286 curr_token->child = NULL;
288 curr_token->state = JSON_COMPLETED;
290 case '\"': /* string */
291 str = json_parse_string(&pos, end);
295 token = json_alloc_token(&tokens);
298 token->type = JSON_STRING;
300 token->state = JSON_COMPLETED;
301 } else if (curr_token->parent &&
302 curr_token->parent->type == JSON_ARRAY &&
303 curr_token->parent->state == JSON_STARTED &&
304 curr_token->state == JSON_EMPTY) {
305 curr_token->string = str;
306 curr_token->state = JSON_COMPLETED;
307 curr_token->type = JSON_STRING;
308 wpa_printf(MSG_MSGDUMP,
309 "JSON: String value: '%s'",
311 } else if (curr_token->state == JSON_EMPTY) {
312 curr_token->type = JSON_VALUE;
313 curr_token->name = str;
314 curr_token->state = JSON_STARTED;
315 } else if (curr_token->state == JSON_WAITING_VALUE) {
316 curr_token->string = str;
317 curr_token->state = JSON_COMPLETED;
318 curr_token->type = JSON_STRING;
319 wpa_printf(MSG_MSGDUMP,
320 "JSON: String value: '%s' = '%s'",
324 wpa_printf(MSG_DEBUG,
325 "JSON: Invalid state for a string");
334 /* ignore whitespace */
336 case ':': /* name/value separator */
337 if (!curr_token || curr_token->state != JSON_STARTED)
339 curr_token->state = JSON_WAITING_VALUE;
341 case ',': /* member separator */
344 curr_token->sibling = json_alloc_token(&tokens);
345 if (!curr_token->sibling)
347 curr_token->sibling->parent = curr_token->parent;
348 curr_token = curr_token->sibling;
349 curr_token->state = JSON_EMPTY;
352 case 'f': /* false */
354 if (!((end - pos >= 4 &&
355 os_strncmp(pos, "true", 4) == 0) ||
357 os_strncmp(pos, "false", 5) == 0) ||
359 os_strncmp(pos, "null", 4) == 0))) {
360 wpa_printf(MSG_DEBUG,
361 "JSON: Invalid literal name");
365 token = json_alloc_token(&tokens);
369 } else if (curr_token->state == JSON_WAITING_VALUE) {
370 wpa_printf(MSG_MSGDUMP,
371 "JSON: Literal name: '%s' = %c",
372 curr_token->name, *pos);
373 } else if (curr_token->parent &&
374 curr_token->parent->type == JSON_ARRAY &&
375 curr_token->parent->state == JSON_STARTED &&
376 curr_token->state == JSON_EMPTY) {
377 wpa_printf(MSG_MSGDUMP,
378 "JSON: Literal name: %c", *pos);
380 wpa_printf(MSG_DEBUG,
381 "JSON: Invalid state for a literal name");
386 curr_token->type = JSON_BOOLEAN;
387 curr_token->number = 1;
391 curr_token->type = JSON_BOOLEAN;
392 curr_token->number = 0;
396 curr_token->type = JSON_NULL;
400 curr_token->state = JSON_COMPLETED;
414 if (json_parse_number(&pos, end, &num) < 0)
417 token = json_alloc_token(&tokens);
420 token->type = JSON_NUMBER;
422 token->state = JSON_COMPLETED;
423 } else if (curr_token->state == JSON_WAITING_VALUE) {
424 curr_token->number = num;
425 curr_token->state = JSON_COMPLETED;
426 curr_token->type = JSON_NUMBER;
427 wpa_printf(MSG_MSGDUMP,
428 "JSON: Number value: '%s' = '%d'",
431 } else if (curr_token->parent &&
432 curr_token->parent->type == JSON_ARRAY &&
433 curr_token->parent->state == JSON_STARTED &&
434 curr_token->state == JSON_EMPTY) {
435 curr_token->number = num;
436 curr_token->state = JSON_COMPLETED;
437 curr_token->type = JSON_NUMBER;
438 wpa_printf(MSG_MSGDUMP,
439 "JSON: Number value: %d",
442 wpa_printf(MSG_DEBUG,
443 "JSON: Invalid state for a number");
448 wpa_printf(MSG_DEBUG,
449 "JSON: Unexpected JSON character: %c", *pos);
459 if (json_check_tree_state(root) < 0) {
460 wpa_printf(MSG_DEBUG, "JSON: Incomplete token in the tree");
466 wpa_printf(MSG_DEBUG, "JSON: Parsing failed");
472 void json_free(struct json_token *json)
476 json_free(json->child);
477 json_free(json->sibling);
479 os_free(json->string);
484 struct json_token * json_get_member(struct json_token *json, const char *name)
486 struct json_token *token, *ret = NULL;
488 if (!json || json->type != JSON_OBJECT)
490 /* Return last matching entry */
491 for (token = json->child; token; token = token->sibling) {
492 if (token->name && os_strcmp(token->name, name) == 0)
499 struct wpabuf * json_get_member_base64url(struct json_token *json,
502 struct json_token *token;
507 token = json_get_member(json, name);
508 if (!token || token->type != JSON_STRING)
510 buf = base64_url_decode((const unsigned char *) token->string,
511 os_strlen(token->string), &buflen);
514 ret = wpabuf_alloc_ext_data(buf, buflen);
522 static const char * json_type_str(enum json_type type)
544 static void json_print_token(struct json_token *token, int depth,
545 char *buf, size_t buflen)
552 len = os_strlen(buf);
553 ret = os_snprintf(buf + len, buflen - len, "[%d:%s:%s]",
554 depth, json_type_str(token->type),
555 token->name ? token->name : "");
556 if (os_snprintf_error(buflen - len, ret)) {
560 json_print_token(token->child, depth + 1, buf, buflen);
561 json_print_token(token->sibling, depth, buf, buflen);
565 void json_print_tree(struct json_token *root, char *buf, size_t buflen)
568 json_print_token(root, 1, buf, buflen);