xref: /spdk/lib/json/json_util.c (revision c6480ea767bb81c01eda34cc804ef45b4a748b82)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/json.h"
7 
8 #include "spdk_internal/utf.h"
9 #include "spdk/log.h"
10 
11 #define SPDK_JSON_DEBUG(...) SPDK_DEBUGLOG(json_util, __VA_ARGS__)
12 
13 size_t
spdk_json_val_len(const struct spdk_json_val * val)14 spdk_json_val_len(const struct spdk_json_val *val)
15 {
16 	if (val == NULL) {
17 		return 0;
18 	}
19 
20 	if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN || val->type == SPDK_JSON_VAL_OBJECT_BEGIN) {
21 		return val->len + 2;
22 	}
23 
24 	return 1;
25 }
26 
27 bool
spdk_json_strequal(const struct spdk_json_val * val,const char * str)28 spdk_json_strequal(const struct spdk_json_val *val, const char *str)
29 {
30 	size_t len;
31 
32 	if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) {
33 		return false;
34 	}
35 
36 	len = strlen(str);
37 	if (val->len != len) {
38 		return false;
39 	}
40 
41 	return memcmp(val->start, str, len) == 0;
42 }
43 
44 char *
spdk_json_strdup(const struct spdk_json_val * val)45 spdk_json_strdup(const struct spdk_json_val *val)
46 {
47 	size_t len;
48 	char *s;
49 
50 	if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NAME) {
51 		return NULL;
52 	}
53 
54 	len = val->len;
55 
56 	if (memchr(val->start, '\0', len)) {
57 		/* String contains embedded NUL, so it is not a valid C string. */
58 		return NULL;
59 	}
60 
61 	s = malloc(len + 1);
62 	if (s == NULL) {
63 		return s;
64 	}
65 
66 	memcpy(s, val->start, len);
67 	s[len] = '\0';
68 
69 	return s;
70 }
71 
72 struct spdk_json_num {
73 	bool negative;
74 	uint64_t significand;
75 	int64_t exponent;
76 };
77 
78 static int
json_number_split(const struct spdk_json_val * val,struct spdk_json_num * num)79 json_number_split(const struct spdk_json_val *val, struct spdk_json_num *num)
80 {
81 	const char *iter;
82 	size_t remaining;
83 	uint64_t *pval;
84 	uint64_t frac_digits = 0;
85 	uint64_t exponent_u64 = 0;
86 	bool exponent_negative = false;
87 	enum {
88 		NUM_STATE_INT,
89 		NUM_STATE_FRAC,
90 		NUM_STATE_EXP,
91 	} state;
92 
93 	memset(num, 0, sizeof(*num));
94 
95 	if (val->type != SPDK_JSON_VAL_NUMBER) {
96 		return -EINVAL;
97 	}
98 
99 	remaining = val->len;
100 	if (remaining == 0) {
101 		return -EINVAL;
102 	}
103 
104 	iter = val->start;
105 	if (*iter == '-') {
106 		num->negative = true;
107 		iter++;
108 		remaining--;
109 	}
110 
111 	state = NUM_STATE_INT;
112 	pval = &num->significand;
113 	while (remaining--) {
114 		char c = *iter++;
115 
116 		if (c == '.') {
117 			state = NUM_STATE_FRAC;
118 		} else if (c == 'e' || c == 'E') {
119 			state = NUM_STATE_EXP;
120 			pval = &exponent_u64;
121 		} else if (c == '-') {
122 			assert(state == NUM_STATE_EXP);
123 			exponent_negative = true;
124 		} else if (c == '+') {
125 			assert(state == NUM_STATE_EXP);
126 			/* exp_negative = false; */ /* already false by default */
127 		} else {
128 			uint64_t new_val;
129 
130 			assert(c >= '0' && c <= '9');
131 			new_val = *pval * 10 + c - '0';
132 			if (new_val < *pval) {
133 				return -ERANGE;
134 			}
135 
136 			if (state == NUM_STATE_FRAC) {
137 				frac_digits++;
138 			}
139 
140 			*pval = new_val;
141 		}
142 	}
143 
144 	if (exponent_negative) {
145 		if (exponent_u64 > 9223372036854775808ULL) { /* abs(INT64_MIN) */
146 			return -ERANGE;
147 		}
148 		num->exponent = (int64_t) - exponent_u64;
149 	} else {
150 		if (exponent_u64 > INT64_MAX) {
151 			return -ERANGE;
152 		}
153 		num->exponent = exponent_u64;
154 	}
155 	num->exponent -= frac_digits;
156 
157 	/* Apply as much of the exponent as possible without overflow or truncation */
158 	if (num->exponent < 0) {
159 		while (num->exponent && num->significand >= 10 && num->significand % 10 == 0) {
160 			num->significand /= 10;
161 			num->exponent++;
162 		}
163 	} else { /* positive exponent */
164 		while (num->exponent) {
165 			uint64_t new_val = num->significand * 10;
166 
167 			if (new_val < num->significand) {
168 				break;
169 			}
170 
171 			num->significand = new_val;
172 			num->exponent--;
173 		}
174 	}
175 
176 	return 0;
177 }
178 
179 int
spdk_json_number_to_uint8(const struct spdk_json_val * val,uint8_t * num)180 spdk_json_number_to_uint8(const struct spdk_json_val *val, uint8_t *num)
181 {
182 	struct spdk_json_num split_num;
183 	int rc;
184 
185 	rc = json_number_split(val, &split_num);
186 	if (rc) {
187 		return rc;
188 	}
189 
190 	if (split_num.exponent || split_num.negative) {
191 		return -ERANGE;
192 	}
193 
194 	if (split_num.significand > UINT8_MAX) {
195 		return -ERANGE;
196 	}
197 	*num = (uint8_t)split_num.significand;
198 	return 0;
199 }
200 
201 int
spdk_json_number_to_uint16(const struct spdk_json_val * val,uint16_t * num)202 spdk_json_number_to_uint16(const struct spdk_json_val *val, uint16_t *num)
203 {
204 	struct spdk_json_num split_num;
205 	int rc;
206 
207 	rc = json_number_split(val, &split_num);
208 	if (rc) {
209 		return rc;
210 	}
211 
212 	if (split_num.exponent || split_num.negative) {
213 		return -ERANGE;
214 	}
215 
216 	if (split_num.significand > UINT16_MAX) {
217 		return -ERANGE;
218 	}
219 	*num = (uint16_t)split_num.significand;
220 	return 0;
221 }
222 
223 int
spdk_json_number_to_int32(const struct spdk_json_val * val,int32_t * num)224 spdk_json_number_to_int32(const struct spdk_json_val *val, int32_t *num)
225 {
226 	struct spdk_json_num split_num;
227 	int rc;
228 
229 	rc = json_number_split(val, &split_num);
230 	if (rc) {
231 		return rc;
232 	}
233 
234 	if (split_num.exponent) {
235 		return -ERANGE;
236 	}
237 
238 	if (split_num.negative) {
239 		if (split_num.significand > 2147483648) { /* abs(INT32_MIN) */
240 			return -ERANGE;
241 		}
242 		*num = (int32_t) - (int64_t)split_num.significand;
243 		return 0;
244 	}
245 
246 	/* positive */
247 	if (split_num.significand > INT32_MAX) {
248 		return -ERANGE;
249 	}
250 	*num = (int32_t)split_num.significand;
251 	return 0;
252 }
253 
254 int
spdk_json_number_to_uint32(const struct spdk_json_val * val,uint32_t * num)255 spdk_json_number_to_uint32(const struct spdk_json_val *val, uint32_t *num)
256 {
257 	struct spdk_json_num split_num;
258 	int rc;
259 
260 	rc = json_number_split(val, &split_num);
261 	if (rc) {
262 		return rc;
263 	}
264 
265 	if (split_num.exponent || split_num.negative) {
266 		return -ERANGE;
267 	}
268 
269 	if (split_num.significand > UINT32_MAX) {
270 		return -ERANGE;
271 	}
272 	*num = (uint32_t)split_num.significand;
273 	return 0;
274 }
275 
276 int
spdk_json_number_to_uint64(const struct spdk_json_val * val,uint64_t * num)277 spdk_json_number_to_uint64(const struct spdk_json_val *val, uint64_t *num)
278 {
279 	struct spdk_json_num split_num;
280 	int rc;
281 
282 	rc = json_number_split(val, &split_num);
283 	if (rc) {
284 		return rc;
285 	}
286 
287 	if (split_num.exponent || split_num.negative) {
288 		return -ERANGE;
289 	}
290 
291 	*num = split_num.significand;
292 	return 0;
293 }
294 
295 static int
_json_decode_object(const struct spdk_json_val * values,const struct spdk_json_object_decoder * decoders,size_t num_decoders,void * out,bool relaxed)296 _json_decode_object(const struct spdk_json_val *values,
297 		    const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out, bool relaxed)
298 {
299 	uint32_t i;
300 	bool invalid = false;
301 	size_t decidx;
302 	bool *seen;
303 
304 	if (values == NULL || values->type != SPDK_JSON_VAL_OBJECT_BEGIN) {
305 		return -1;
306 	}
307 
308 	seen = calloc(sizeof(bool), num_decoders);
309 	if (seen == NULL) {
310 		return -1;
311 	}
312 
313 	for (i = 0; i < values->len;) {
314 		const struct spdk_json_val *name = &values[i + 1];
315 		const struct spdk_json_val *v = &values[i + 2];
316 		bool found = false;
317 
318 		for (decidx = 0; decidx < num_decoders; decidx++) {
319 			const struct spdk_json_object_decoder *dec = &decoders[decidx];
320 			if (spdk_json_strequal(name, dec->name)) {
321 				void *field = (void *)((uintptr_t)out + dec->offset);
322 
323 				found = true;
324 
325 				if (seen[decidx]) {
326 					/* duplicate field name */
327 					invalid = true;
328 					SPDK_JSON_DEBUG("Duplicate key '%s'\n", dec->name);
329 				} else {
330 					seen[decidx] = true;
331 					if (dec->decode_func(v, field)) {
332 						invalid = true;
333 						SPDK_JSON_DEBUG("Decoder failed to decode key '%s'\n", dec->name);
334 						/* keep going to fill out any other valid keys */
335 					}
336 				}
337 				break;
338 			}
339 		}
340 
341 		if (!relaxed && !found) {
342 			invalid = true;
343 			SPDK_JSON_DEBUG("Decoder not found for key '%.*s'\n", name->len, (char *)name->start);
344 		}
345 
346 		i += 1 + spdk_json_val_len(v);
347 	}
348 
349 	for (decidx = 0; decidx < num_decoders; decidx++) {
350 		if (!decoders[decidx].optional && !seen[decidx]) {
351 			/* required field is missing */
352 			invalid = true;
353 			break;
354 		}
355 	}
356 
357 	free(seen);
358 	return invalid ? -1 : 0;
359 }
360 
361 void
spdk_json_free_object(const struct spdk_json_object_decoder * decoders,size_t num_decoders,void * obj)362 spdk_json_free_object(const struct spdk_json_object_decoder *decoders, size_t num_decoders,
363 		      void *obj)
364 {
365 	struct spdk_json_val invalid_val = {
366 		.start = "",
367 		.len = 0,
368 		.type = SPDK_JSON_VAL_INVALID
369 	};
370 	size_t decidx;
371 
372 	for (decidx = 0; decidx < num_decoders; decidx++) {
373 		const struct spdk_json_object_decoder *dec = &decoders[decidx];
374 		void *field = (void *)((uintptr_t)obj + dec->offset);
375 
376 		/* decoding an invalid value will free the
377 		 * previous memory without allocating it again.
378 		 */
379 		dec->decode_func(&invalid_val, field);
380 	}
381 }
382 
383 
384 int
spdk_json_decode_object(const struct spdk_json_val * values,const struct spdk_json_object_decoder * decoders,size_t num_decoders,void * out)385 spdk_json_decode_object(const struct spdk_json_val *values,
386 			const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out)
387 {
388 	return _json_decode_object(values, decoders, num_decoders, out, false);
389 }
390 
391 int
spdk_json_decode_object_relaxed(const struct spdk_json_val * values,const struct spdk_json_object_decoder * decoders,size_t num_decoders,void * out)392 spdk_json_decode_object_relaxed(const struct spdk_json_val *values,
393 				const struct spdk_json_object_decoder *decoders, size_t num_decoders, void *out)
394 {
395 	return _json_decode_object(values, decoders, num_decoders, out, true);
396 }
397 
398 int
spdk_json_decode_array(const struct spdk_json_val * values,spdk_json_decode_fn decode_func,void * out,size_t max_size,size_t * out_size,size_t stride)399 spdk_json_decode_array(const struct spdk_json_val *values, spdk_json_decode_fn decode_func,
400 		       void *out, size_t max_size, size_t *out_size, size_t stride)
401 {
402 	uint32_t i;
403 	char *field;
404 
405 	if (values == NULL || values->type != SPDK_JSON_VAL_ARRAY_BEGIN) {
406 		return -1;
407 	}
408 
409 	*out_size = 0;
410 	field = out;
411 	for (i = 0; i < values->len;) {
412 		const struct spdk_json_val *v = &values[i + 1];
413 
414 		if (*out_size == max_size) {
415 			return -1;
416 		}
417 
418 		if (decode_func(v, field)) {
419 			return -1;
420 		}
421 
422 		i += spdk_json_val_len(v);
423 		field += stride;
424 		(*out_size)++;
425 	}
426 
427 	return 0;
428 }
429 
430 int
spdk_json_decode_bool(const struct spdk_json_val * val,void * out)431 spdk_json_decode_bool(const struct spdk_json_val *val, void *out)
432 {
433 	bool *f = out;
434 
435 	if (val->type != SPDK_JSON_VAL_TRUE && val->type != SPDK_JSON_VAL_FALSE) {
436 		return -1;
437 	}
438 
439 	*f = val->type == SPDK_JSON_VAL_TRUE;
440 	return 0;
441 }
442 
443 int
spdk_json_decode_uint8(const struct spdk_json_val * val,void * out)444 spdk_json_decode_uint8(const struct spdk_json_val *val, void *out)
445 {
446 	uint8_t *i = out;
447 
448 	return spdk_json_number_to_uint8(val, i);
449 }
450 
451 int
spdk_json_decode_uint16(const struct spdk_json_val * val,void * out)452 spdk_json_decode_uint16(const struct spdk_json_val *val, void *out)
453 {
454 	uint16_t *i = out;
455 
456 	return spdk_json_number_to_uint16(val, i);
457 }
458 
459 int
spdk_json_decode_int32(const struct spdk_json_val * val,void * out)460 spdk_json_decode_int32(const struct spdk_json_val *val, void *out)
461 {
462 	int32_t *i = out;
463 
464 	return spdk_json_number_to_int32(val, i);
465 }
466 
467 int
spdk_json_decode_uint32(const struct spdk_json_val * val,void * out)468 spdk_json_decode_uint32(const struct spdk_json_val *val, void *out)
469 {
470 	uint32_t *i = out;
471 
472 	return spdk_json_number_to_uint32(val, i);
473 }
474 
475 int
spdk_json_decode_uint64(const struct spdk_json_val * val,void * out)476 spdk_json_decode_uint64(const struct spdk_json_val *val, void *out)
477 {
478 	uint64_t *i = out;
479 
480 	return spdk_json_number_to_uint64(val, i);
481 }
482 
483 int
spdk_json_decode_string(const struct spdk_json_val * val,void * out)484 spdk_json_decode_string(const struct spdk_json_val *val, void *out)
485 {
486 	char **s = out;
487 
488 	free(*s);
489 
490 	*s = spdk_json_strdup(val);
491 
492 	if (*s) {
493 		return 0;
494 	} else {
495 		return -1;
496 	}
497 }
498 
499 int
spdk_json_decode_uuid(const struct spdk_json_val * val,void * out)500 spdk_json_decode_uuid(const struct spdk_json_val *val, void *out)
501 {
502 	struct spdk_uuid *uuid = out;
503 	char *str = NULL;
504 	int rc;
505 
506 	rc = spdk_json_decode_string(val, &str);
507 	if (rc != 0) {
508 		return rc;
509 	}
510 
511 	rc = spdk_uuid_parse(uuid, str);
512 	free(str);
513 
514 	return rc == 0 ? 0 : -1;
515 }
516 
517 static struct spdk_json_val *
json_first(struct spdk_json_val * object,enum spdk_json_val_type type)518 json_first(struct spdk_json_val *object, enum spdk_json_val_type type)
519 {
520 	/* 'object' must be JSON object or array. 'type' might be combination of these two. */
521 	assert((type & (SPDK_JSON_VAL_ARRAY_BEGIN | SPDK_JSON_VAL_OBJECT_BEGIN)) != 0);
522 
523 	assert(object != NULL);
524 
525 	if ((object->type & type) == 0) {
526 		return NULL;
527 	}
528 
529 	object++;
530 	if (object->len == 0) {
531 		return NULL;
532 	}
533 
534 	return object;
535 }
536 
537 static struct spdk_json_val *
json_value(struct spdk_json_val * key)538 json_value(struct spdk_json_val *key)
539 {
540 	return key->type == SPDK_JSON_VAL_NAME ? key + 1 : NULL;
541 }
542 
543 int
spdk_json_find(struct spdk_json_val * object,const char * key_name,struct spdk_json_val ** key,struct spdk_json_val ** val,enum spdk_json_val_type type)544 spdk_json_find(struct spdk_json_val *object, const char *key_name, struct spdk_json_val **key,
545 	       struct spdk_json_val **val, enum spdk_json_val_type type)
546 {
547 	struct spdk_json_val *_key = NULL;
548 	struct spdk_json_val *_val = NULL;
549 	struct spdk_json_val *it_first, *it;
550 
551 	assert(object != NULL);
552 
553 	it_first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN);
554 	if (!it_first) {
555 		SPDK_JSON_DEBUG("Not enclosed in {}\n");
556 		return -EPROTOTYPE;
557 	}
558 
559 	for (it = it_first;
560 	     it != NULL;
561 	     it = spdk_json_next(it)) {
562 		if (it->type != SPDK_JSON_VAL_NAME) {
563 			continue;
564 		}
565 
566 		if (spdk_json_strequal(it, key_name) != true) {
567 			continue;
568 		}
569 
570 		if (_key) {
571 			SPDK_JSON_DEBUG("Duplicate key '%s'", key_name);
572 			return -EINVAL;
573 		}
574 
575 		_key = it;
576 		_val = json_value(_key);
577 
578 		if (type != SPDK_JSON_VAL_ANY && (_val->type & type) == 0) {
579 			SPDK_JSON_DEBUG("key '%s' type is %#x but expected one of %#x\n", key_name, _val->type, type);
580 			return -EDOM;
581 		}
582 	}
583 
584 	if (key) {
585 		*key = _key;
586 	}
587 
588 	if (val) {
589 		*val = _val;
590 	}
591 
592 	return _val ? 0 : -ENOENT;
593 }
594 
595 int
spdk_json_find_string(struct spdk_json_val * object,const char * key_name,struct spdk_json_val ** key,struct spdk_json_val ** val)596 spdk_json_find_string(struct spdk_json_val *object, const char *key_name,
597 		      struct spdk_json_val **key, struct spdk_json_val **val)
598 {
599 	return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_STRING);
600 }
601 
602 int
spdk_json_find_array(struct spdk_json_val * object,const char * key_name,struct spdk_json_val ** key,struct spdk_json_val ** val)603 spdk_json_find_array(struct spdk_json_val *object, const char *key_name,
604 		     struct spdk_json_val **key, struct spdk_json_val **val)
605 {
606 	return spdk_json_find(object, key_name, key, val, SPDK_JSON_VAL_ARRAY_BEGIN);
607 }
608 
609 struct spdk_json_val *
spdk_json_object_first(struct spdk_json_val * object)610 spdk_json_object_first(struct spdk_json_val *object)
611 {
612 	struct spdk_json_val *first = json_first(object, SPDK_JSON_VAL_OBJECT_BEGIN);
613 
614 	/* Empty object? */
615 	return first && first->type != SPDK_JSON_VAL_OBJECT_END ? first : NULL;
616 }
617 
618 struct spdk_json_val *
spdk_json_array_first(struct spdk_json_val * array_begin)619 spdk_json_array_first(struct spdk_json_val *array_begin)
620 {
621 	struct spdk_json_val *first = json_first(array_begin, SPDK_JSON_VAL_ARRAY_BEGIN);
622 
623 	/* Empty array? */
624 	return first && first->type != SPDK_JSON_VAL_ARRAY_END ? first : NULL;
625 }
626 
627 static struct spdk_json_val *
json_skip_object_or_array(struct spdk_json_val * val)628 json_skip_object_or_array(struct spdk_json_val *val)
629 {
630 	unsigned lvl;
631 	enum spdk_json_val_type end_type;
632 	struct spdk_json_val *it;
633 
634 	if (val->type == SPDK_JSON_VAL_OBJECT_BEGIN) {
635 		end_type = SPDK_JSON_VAL_OBJECT_END;
636 	} else if (val->type == SPDK_JSON_VAL_ARRAY_BEGIN) {
637 		end_type = SPDK_JSON_VAL_ARRAY_END;
638 	} else {
639 		SPDK_JSON_DEBUG("Expected JSON object (%#x) or array (%#x) but got %#x\n",
640 				SPDK_JSON_VAL_OBJECT_BEGIN, SPDK_JSON_VAL_ARRAY_BEGIN, val->type);
641 		return NULL;
642 	}
643 
644 	lvl = 1;
645 	for (it = val + 1; it->type != SPDK_JSON_VAL_INVALID && lvl != 0; it++) {
646 		if (it->type == val->type) {
647 			lvl++;
648 		} else if (it->type == end_type) {
649 			lvl--;
650 		}
651 	}
652 
653 	/* if lvl != 0 we have invalid JSON object */
654 	if (lvl != 0) {
655 		SPDK_JSON_DEBUG("Can't find end of object (type: %#x): lvl (%u) != 0)\n", val->type, lvl);
656 		it = NULL;
657 	}
658 
659 	return it;
660 }
661 
662 struct spdk_json_val *
spdk_json_next(struct spdk_json_val * it)663 spdk_json_next(struct spdk_json_val *it)
664 {
665 	struct spdk_json_val *val, *next;
666 
667 	switch (it->type) {
668 	case SPDK_JSON_VAL_NAME:
669 		val = json_value(it);
670 		next = spdk_json_next(val);
671 		break;
672 
673 	/* We are in the middle of an array - get to next entry */
674 	case SPDK_JSON_VAL_NULL:
675 	case SPDK_JSON_VAL_TRUE:
676 	case SPDK_JSON_VAL_FALSE:
677 	case SPDK_JSON_VAL_NUMBER:
678 	case SPDK_JSON_VAL_STRING:
679 		val = it + 1;
680 		return val;
681 
682 	case SPDK_JSON_VAL_ARRAY_BEGIN:
683 	case SPDK_JSON_VAL_OBJECT_BEGIN:
684 		next = json_skip_object_or_array(it);
685 		break;
686 
687 	/* Can't go to the next object if started from the end of array or object */
688 	case SPDK_JSON_VAL_ARRAY_END:
689 	case SPDK_JSON_VAL_OBJECT_END:
690 	case SPDK_JSON_VAL_INVALID:
691 		return NULL;
692 	default:
693 		assert(false);
694 		return NULL;
695 
696 	}
697 
698 	/* EOF ? */
699 	if (next == NULL) {
700 		return NULL;
701 	}
702 
703 	switch (next->type) {
704 	case SPDK_JSON_VAL_ARRAY_END:
705 	case SPDK_JSON_VAL_OBJECT_END:
706 	case SPDK_JSON_VAL_INVALID:
707 		return NULL;
708 	default:
709 		/* Next value */
710 		return next;
711 	}
712 }
713 
714 SPDK_LOG_REGISTER_COMPONENT(json_util)
715