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