VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/json.cpp@ 76781

Last change on this file since 76781 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.4 KB
Line 
1/* $Id: json.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT JSON parser API (JSON).
4 */
5
6/*
7 * Copyright (C) 2016-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/json.h>
32#include "internal/iprt.h"
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/mem.h>
39#include <iprt/stream.h>
40#include <iprt/string.h>
41#include <iprt/utf16.h>
42
43#include <stdlib.h> /* strtod() */
44#include <errno.h> /* errno */
45
46
47/*********************************************************************************************************************************
48* Structures and Typedefs *
49*********************************************************************************************************************************/
50
51/**
52 * JSON parser position information.
53 */
54typedef struct RTJSONPOS
55{
56 /** Line in the source. */
57 size_t iLine;
58 /** Current start character .*/
59 size_t iChStart;
60 /** Current end character. */
61 size_t iChEnd;
62} RTJSONPOS;
63/** Pointer to a position. */
64typedef RTJSONPOS *PRTJSONPOS;
65
66/**
67 * JSON token class.
68 */
69typedef enum RTJSONTOKENCLASS
70{
71 /** Invalid. */
72 RTJSONTOKENCLASS_INVALID = 0,
73 /** Array begin. */
74 RTJSONTOKENCLASS_BEGIN_ARRAY,
75 /** Object begin. */
76 RTJSONTOKENCLASS_BEGIN_OBJECT,
77 /** Array end. */
78 RTJSONTOKENCLASS_END_ARRAY,
79 /** Object end. */
80 RTJSONTOKENCLASS_END_OBJECT,
81 /** Separator for name/value pairs. */
82 RTJSONTOKENCLASS_NAME_SEPARATOR,
83 /** Value separator. */
84 RTJSONTOKENCLASS_VALUE_SEPARATOR,
85 /** String */
86 RTJSONTOKENCLASS_STRING,
87 /** Integer number. */
88 RTJSONTOKENCLASS_INTEGER,
89 /** Floating point number. */
90 RTJSONTOKENCLASS_NUMBER,
91 /** null keyword. */
92 RTJSONTOKENCLASS_NULL,
93 /** false keyword. */
94 RTJSONTOKENCLASS_FALSE,
95 /** true keyword. */
96 RTJSONTOKENCLASS_TRUE,
97 /** End of stream */
98 RTJSONTOKENCLASS_EOS,
99 /** 32bit hack. */
100 RTJSONTOKENCLASS_32BIT_HACK = 0x7fffffff
101} RTJSONTOKENCLASS;
102/** Pointer to a token class. */
103typedef RTJSONTOKENCLASS *PRTJSONTOKENCLASS;
104
105/**
106 * JSON token.
107 */
108typedef struct RTJSONTOKEN
109{
110 /** Token class. */
111 RTJSONTOKENCLASS enmClass;
112 /** Token position in the source buffer. */
113 RTJSONPOS Pos;
114 /** Data based on the token class. */
115 union
116 {
117 /** String. */
118 struct
119 {
120 /** Pointer to the start of the string. */
121 char *pszStr;
122 } String;
123 /** Number. */
124 struct
125 {
126 int64_t i64Num;
127 } Integer;
128 /** Floating point number. */
129 double rdNum;
130 } Class;
131} RTJSONTOKEN;
132/** Pointer to a JSON token. */
133typedef RTJSONTOKEN *PRTJSONTOKEN;
134/** Pointer to a const script token. */
135typedef const RTJSONTOKEN *PCRTJSONTOKEN;
136
137/**
138 * Tokenizer read input callback.
139 *
140 * @returns IPRT status code.
141 * @param pvUser Opaque user data for the callee.
142 * @param offInput Start offset from the start of the input stream to read from.
143 * @param pvBuf Where to store the read data.
144 * @param cbBuf How much to read.
145 * @param pcbRead Where to store the amount of data read on succcess.
146 */
147typedef DECLCALLBACK(int) FNRTJSONTOKENIZERREAD(void *pvUser, size_t offInput, void *pvBuf, size_t cbBuf,
148 size_t *pcbRead);
149/** Pointer to a tokenizer read buffer callback. */
150typedef FNRTJSONTOKENIZERREAD *PFNRTJSONTOKENIZERREAD;
151
152/**
153 * Tokenizer state.
154 */
155typedef struct RTJSONTOKENIZER
156{
157 /** Read callback. */
158 PFNRTJSONTOKENIZERREAD pfnRead;
159 /** Opaque user data. */
160 void *pvUser;
161 /** Current offset into the input stream. */
162 size_t offInput;
163 /** Number of valid bytes in the input buffer. */
164 size_t cbBuf;
165 /** Current offset into the input buffer. */
166 size_t offBuf;
167 /** Input cache buffer. */
168 char achBuf[512];
169 /** Current position into the input stream. */
170 RTJSONPOS Pos;
171 /** Token 1. */
172 RTJSONTOKEN Token1;
173 /** Token 2. */
174 RTJSONTOKEN Token2;
175 /** Pointer to the current active token. */
176 PRTJSONTOKEN pTokenCurr;
177 /** The next token in the input stream (used for peeking). */
178 PRTJSONTOKEN pTokenNext;
179 /** The tokenizer error state. */
180 int rcTok;
181 /** Where to return extended error information.*/
182 PRTERRINFO pErrInfo;
183} RTJSONTOKENIZER;
184/** Pointer to a JSON tokenizer. */
185typedef RTJSONTOKENIZER *PRTJSONTOKENIZER;
186
187/** Pointer to the internal JSON value instance. */
188typedef struct RTJSONVALINT *PRTJSONVALINT;
189
190/**
191 * A JSON value.
192 */
193typedef struct RTJSONVALINT
194{
195 /** Type of the JSON value. */
196 RTJSONVALTYPE enmType;
197 /** Reference count for this JSON value. */
198 volatile uint32_t cRefs;
199 /** Type dependent data. */
200 union
201 {
202 /** String type*/
203 struct
204 {
205 /** Pointer to the string. */
206 char *pszStr;
207 } String;
208 /** Number type. */
209 struct
210 {
211 /** Signed 64-bit integer. */
212 int64_t i64Num;
213 } Integer;
214 /** Floating point number . */
215 double rdNum;
216 /** Array type. */
217 struct
218 {
219 /** Number of elements in the array. */
220 unsigned cItems;
221 /** Pointer to the array of items. */
222 PRTJSONVALINT *papItems;
223 } Array;
224 /** Object type. */
225 struct
226 {
227 /** Number of members. */
228 unsigned cMembers;
229 /** Pointer to the array holding the member names. */
230 char **papszNames;
231 /** Pointer to the array holding the values. */
232 PRTJSONVALINT *papValues;
233 } Object;
234 } Type;
235} RTJSONVALINT;
236
237/**
238 * A JSON iterator.
239 */
240typedef struct RTJSONITINT
241{
242 /** Referenced JSON value. */
243 PRTJSONVALINT pJsonVal;
244 /** Current index. */
245 unsigned idxCur;
246} RTJSONITINT;
247/** Pointer to the internal JSON iterator instance. */
248typedef RTJSONITINT *PRTJSONITINT;
249
250/**
251 * Passing arguments for the read callbacks.
252 */
253typedef struct RTJSONREADERARGS
254{
255 /** Buffer/File size */
256 size_t cbData;
257 /** Data specific for one callback. */
258 union
259 {
260 PRTSTREAM hStream;
261 const uint8_t *pbBuf;
262 } u;
263} RTJSONREADERARGS;
264/** Pointer to a readers argument. */
265typedef RTJSONREADERARGS *PRTJSONREADERARGS;
266
267
268/*********************************************************************************************************************************
269* Internal Functions *
270*********************************************************************************************************************************/
271static int rtJsonParseValue(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken, PRTJSONVALINT *ppJsonVal);
272
273
274/**
275 * Fill the input buffer from the input stream.
276 *
277 * @returns IPRT status code.
278 * @param pTokenizer The tokenizer state.
279 */
280static int rtJsonTokenizerRead(PRTJSONTOKENIZER pTokenizer)
281{
282 size_t cbRead = 0;
283 int rc = pTokenizer->pfnRead(pTokenizer->pvUser, pTokenizer->offInput, &pTokenizer->achBuf[0],
284 sizeof(pTokenizer->achBuf), &cbRead);
285 if (RT_SUCCESS(rc))
286 {
287 pTokenizer->cbBuf = cbRead;
288 pTokenizer->offInput += cbRead;
289 pTokenizer->offBuf = 0;
290 /* Validate UTF-8 encoding. */
291 rc = RTStrValidateEncodingEx(&pTokenizer->achBuf[0], cbRead, 0 /* fFlags */);
292 /* If we read less than requested we reached the end and fill the remainder with terminators. */
293 if (cbRead < sizeof(pTokenizer->achBuf))
294 memset(&pTokenizer->achBuf[cbRead], 0, sizeof(pTokenizer->achBuf) - cbRead);
295 }
296
297 return rc;
298}
299
300/**
301 * Skips the given amount of characters in the input stream.
302 *
303 * @returns IPRT status code.
304 * @param pTokenizer The tokenizer state.
305 * @param cchSkip The amount of characters to skip.
306 */
307static int rtJsonTokenizerSkip(PRTJSONTOKENIZER pTokenizer, size_t cchSkip)
308{
309 int rc = VINF_SUCCESS;
310
311 /*
312 * In case we reached the end of the stream don't even attempt to read new data.
313 * Safety precaution for possible bugs in the parser causing out of bounds reads
314 */
315 if (pTokenizer->achBuf[pTokenizer->offBuf] == '\0')
316 return rc;
317
318 while ( cchSkip > 0
319 && pTokenizer->offBuf < pTokenizer->cbBuf
320 && RT_SUCCESS(rc))
321 {
322 size_t cchThisSkip = RT_MIN(cchSkip, pTokenizer->cbBuf - pTokenizer->offBuf);
323
324 pTokenizer->offBuf += cchThisSkip;
325 /* Read new data if required and we didn't reach the end yet. */
326 if ( pTokenizer->offBuf == pTokenizer->cbBuf
327 && pTokenizer->cbBuf == sizeof(pTokenizer->achBuf))
328 rc = rtJsonTokenizerRead(pTokenizer);
329
330 cchSkip -= cchThisSkip;
331 }
332
333 return rc;
334}
335
336
337/**
338 * Returns whether the tokenizer reached the end of the stream.
339 *
340 * @returns true if the tokenizer reached the end of stream marker
341 * false otherwise.
342 * @param pTokenizer The tokenizer state.
343 */
344DECLINLINE(bool) rtJsonTokenizerIsEos(PRTJSONTOKENIZER pTokenizer)
345{
346 return pTokenizer->achBuf[pTokenizer->offBuf] == '\0';
347}
348
349/**
350 * Skip one character in the input stream.
351 *
352 * @returns nothing.
353 * @param pTokenizer The tokenizer state.
354 */
355DECLINLINE(void) rtJsonTokenizerSkipCh(PRTJSONTOKENIZER pTokenizer)
356{
357 rtJsonTokenizerSkip(pTokenizer, 1);
358 pTokenizer->Pos.iChStart++;
359 pTokenizer->Pos.iChEnd++;
360}
361
362/**
363 * Returns the next char in the input buffer without advancing it.
364 *
365 * @returns Next character in the input buffer.
366 * @param pTokenizer The tokenizer state.
367 */
368DECLINLINE(char) rtJsonTokenizerPeekCh(PRTJSONTOKENIZER pTokenizer)
369{
370 return !rtJsonTokenizerIsEos(pTokenizer)
371 ? pTokenizer->achBuf[pTokenizer->offBuf + 1] /** @todo Read out of bounds */
372 : '\0';
373}
374
375/**
376 * Returns the next character in the input buffer advancing the internal
377 * position.
378 *
379 * @returns Next character in the stream.
380 * @param pTokenizer The tokenizer state.
381 */
382DECLINLINE(char) rtJsonTokenizerGetCh(PRTJSONTOKENIZER pTokenizer)
383{
384 char ch;
385
386 if (!rtJsonTokenizerIsEos(pTokenizer))
387 ch = pTokenizer->achBuf[pTokenizer->offBuf];
388 else
389 ch = '\0';
390
391 return ch;
392}
393
394/**
395 * Sets a new line for the tokenizer.
396 *
397 * @returns nothing.
398 * @param pTokenizer The tokenizer state.
399 * @param cSkip Amount of characters to skip making up the new line.
400 */
401DECLINLINE(void) rtJsonTokenizerNewLine(PRTJSONTOKENIZER pTokenizer, unsigned cSkip)
402{
403 rtJsonTokenizerSkip(pTokenizer, cSkip);
404 pTokenizer->Pos.iLine++;
405 pTokenizer->Pos.iChStart = 1;
406 pTokenizer->Pos.iChEnd = 1;
407}
408
409/**
410 * Checks whether the current position in the input stream is a new line
411 * and skips it.
412 *
413 * @returns Flag whether there was a new line at the current position
414 * in the input buffer.
415 * @param pTokenizer The tokenizer state.
416 */
417DECLINLINE(bool) rtJsonTokenizerIsSkipNewLine(PRTJSONTOKENIZER pTokenizer)
418{
419 bool fNewline = true;
420
421 if ( rtJsonTokenizerGetCh(pTokenizer) == '\r'
422 && rtJsonTokenizerPeekCh(pTokenizer) == '\n')
423 rtJsonTokenizerNewLine(pTokenizer, 2);
424 else if (rtJsonTokenizerGetCh(pTokenizer) == '\n')
425 rtJsonTokenizerNewLine(pTokenizer, 1);
426 else
427 fNewline = false;
428
429 return fNewline;
430}
431
432/**
433 * Skip all whitespace starting from the current input buffer position.
434 * Skips all present comments too.
435 *
436 * @returns nothing.
437 * @param pTokenizer The tokenizer state.
438 */
439DECLINLINE(void) rtJsonTokenizerSkipWhitespace(PRTJSONTOKENIZER pTokenizer)
440{
441 while (!rtJsonTokenizerIsEos(pTokenizer))
442 {
443 while ( rtJsonTokenizerGetCh(pTokenizer) == ' '
444 || rtJsonTokenizerGetCh(pTokenizer) == '\t')
445 rtJsonTokenizerSkipCh(pTokenizer);
446
447 if ( !rtJsonTokenizerIsEos(pTokenizer)
448 && !rtJsonTokenizerIsSkipNewLine(pTokenizer))
449 break; /* Skipped everything, next is some real content. */
450 }
451}
452
453/**
454 * Get an literal token from the tokenizer.
455 *
456 * @returns IPRT status code.
457 * @param pTokenizer The tokenizer state.
458 * @param pToken The uninitialized token.
459 */
460static int rtJsonTokenizerGetLiteral(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken)
461{
462 int rc = VINF_SUCCESS;
463 char ch = rtJsonTokenizerGetCh(pTokenizer);
464 size_t cchLiteral = 0;
465 char szLiteral[6]; /* false + 0 terminator as the lingest possible literal. */
466 RT_ZERO(szLiteral);
467
468 pToken->Pos = pTokenizer->Pos;
469
470 Assert(RT_C_IS_ALPHA(ch));
471
472 while ( RT_C_IS_ALPHA(ch)
473 && cchLiteral < RT_ELEMENTS(szLiteral) - 1)
474 {
475 szLiteral[cchLiteral] = ch;
476 cchLiteral++;
477 rtJsonTokenizerSkipCh(pTokenizer);
478 ch = rtJsonTokenizerGetCh(pTokenizer);
479 }
480
481 if (!RTStrNCmp(&szLiteral[0], "false", RT_ELEMENTS(szLiteral)))
482 pToken->enmClass = RTJSONTOKENCLASS_FALSE;
483 else if (!RTStrNCmp(&szLiteral[0], "true", RT_ELEMENTS(szLiteral)))
484 pToken->enmClass = RTJSONTOKENCLASS_TRUE;
485 else if (!RTStrNCmp(&szLiteral[0], "null", RT_ELEMENTS(szLiteral)))
486 pToken->enmClass = RTJSONTOKENCLASS_NULL;
487 else
488 {
489 pToken->enmClass = RTJSONTOKENCLASS_INVALID;
490 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "malformed literal '%.6s' (line %zu col %zu)",
491 &szLiteral[0], pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
492 }
493
494 pToken->Pos.iChEnd += cchLiteral;
495 return rc;
496}
497
498/**
499 * Get a numerical constant from the tokenizer.
500 *
501 * @returns IPRT status code.
502 * @param pTokenizer The tokenizer state.
503 * @param pToken The uninitialized token.
504 */
505static int rtJsonTokenizerGetNumber(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken)
506{
507 size_t cchNum = 0;
508 char szTmp[128]; /* Everything larger is not possible to display in signed 64bit. */
509
510 pToken->enmClass = RTJSONTOKENCLASS_INTEGER;
511
512 char ch = rtJsonTokenizerGetCh(pTokenizer);
513 if (ch == '-')
514 {
515 szTmp[cchNum++] = '-';
516 rtJsonTokenizerSkipCh(pTokenizer);
517 ch = rtJsonTokenizerGetCh(pTokenizer);
518 }
519
520 while ( RT_C_IS_DIGIT(ch)
521 && cchNum < sizeof(szTmp) - 1)
522 {
523 szTmp[cchNum] = ch;
524 cchNum++;
525 rtJsonTokenizerSkipCh(pTokenizer);
526 ch = rtJsonTokenizerGetCh(pTokenizer);
527 }
528
529 int rc = VINF_SUCCESS;
530 if (RT_C_IS_DIGIT(ch) && cchNum >= sizeof(szTmp) - 1)
531 rc = VERR_NUMBER_TOO_BIG;
532 else if (ch != '.')
533 {
534 szTmp[cchNum] = '\0';
535 rc = RTStrToInt64Ex(&szTmp[0], NULL, 10, &pToken->Class.Integer.i64Num);
536 Assert(RT_SUCCESS(rc) || rc == VWRN_NUMBER_TOO_BIG);
537 if (rc == VWRN_NUMBER_TOO_BIG)
538 rc = VERR_NUMBER_TOO_BIG;
539 }
540 else
541 {
542 /*
543 * A floating point value.
544 */
545 pToken->enmClass = RTJSONTOKENCLASS_NUMBER;
546 rtJsonTokenizerSkipCh(pTokenizer);
547 szTmp[cchNum++] = '.';
548
549 ch = rtJsonTokenizerGetCh(pTokenizer);
550 while ( RT_C_IS_DIGIT(ch)
551 && cchNum < sizeof(szTmp) - 1)
552 {
553 szTmp[cchNum++] = ch;
554 rtJsonTokenizerSkipCh(pTokenizer);
555 ch = rtJsonTokenizerGetCh(pTokenizer);
556 }
557 if ( (ch == 'e' || ch == 'E')
558 && cchNum < sizeof(szTmp) - 2)
559 {
560 szTmp[cchNum++] = 'e';
561 rtJsonTokenizerSkipCh(pTokenizer);
562 ch = rtJsonTokenizerGetCh(pTokenizer);
563 if (ch == '+' || ch == '-')
564 {
565 szTmp[cchNum++] = ch;
566 rtJsonTokenizerSkipCh(pTokenizer);
567 ch = rtJsonTokenizerGetCh(pTokenizer);
568 }
569 while ( RT_C_IS_DIGIT(ch)
570 && cchNum < sizeof(szTmp) - 1)
571 {
572 szTmp[cchNum++] = ch;
573 rtJsonTokenizerSkipCh(pTokenizer);
574 ch = rtJsonTokenizerGetCh(pTokenizer);
575 }
576 }
577 if (cchNum < sizeof(szTmp) - 1)
578 {
579 szTmp[cchNum] = '\0';
580
581 /** @todo Not sure if strtod does the 100% right thing here... */
582 errno = 0;
583 char *pszNext = NULL;
584 pToken->Class.rdNum = strtod(szTmp, &pszNext);
585 if (errno == 0)
586 {
587 rc = VINF_SUCCESS;
588 Assert(!pszNext || *pszNext == '\0');
589 }
590 else
591 rc = RTErrConvertFromErrno(errno);
592 }
593 }
594
595 return rc;
596}
597
598/**
599 * Parses a string constant.
600 *
601 * @returns IPRT status code.
602 * @param pTokenizer The tokenizer state.
603 * @param pToken The uninitialized token.
604 */
605static int rtJsonTokenizerGetString(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken)
606{
607 size_t cchStrMax = 64;
608 char *pszDecoded = (char *)RTStrAlloc(cchStrMax);
609 AssertReturn(pszDecoded, VERR_NO_STR_MEMORY);
610
611 Assert(rtJsonTokenizerGetCh(pTokenizer) == '\"');
612 rtJsonTokenizerSkipCh(pTokenizer); /* Skip " */
613
614 pToken->enmClass = RTJSONTOKENCLASS_STRING;
615 pToken->Pos = pTokenizer->Pos;
616
617 size_t cchStr = 0;
618 char ch = rtJsonTokenizerGetCh(pTokenizer);
619 while ( ch != '\"'
620 && ch != '\0')
621 {
622 if (ch != '\\')
623 {
624 pszDecoded[cchStr++] = ch;
625 rtJsonTokenizerSkipCh(pTokenizer);
626 }
627 else
628 {
629 /* Escape sequence, check the next character */
630 rtJsonTokenizerSkipCh(pTokenizer);
631 char chNext = rtJsonTokenizerGetCh(pTokenizer);
632 switch (chNext)
633 {
634 case '\"':
635 pszDecoded[cchStr++] = '\"';
636 rtJsonTokenizerSkipCh(pTokenizer);
637 break;
638 case '\\':
639 pszDecoded[cchStr++] = '\\';
640 rtJsonTokenizerSkipCh(pTokenizer);
641 break;
642 case '/':
643 pszDecoded[cchStr++] = '/';
644 rtJsonTokenizerSkipCh(pTokenizer);
645 break;
646 case 'b':
647 pszDecoded[cchStr++] = '\b';
648 rtJsonTokenizerSkipCh(pTokenizer);
649 break;
650 case 'n':
651 pszDecoded[cchStr++] = '\n';
652 rtJsonTokenizerSkipCh(pTokenizer);
653 break;
654 case 'f':
655 pszDecoded[cchStr++] = '\f';
656 rtJsonTokenizerSkipCh(pTokenizer);
657 break;
658 case 'r':
659 pszDecoded[cchStr++] = '\r';
660 rtJsonTokenizerSkipCh(pTokenizer);
661 break;
662 case 't':
663 pszDecoded[cchStr++] = '\t';
664 rtJsonTokenizerSkipCh(pTokenizer);
665 break;
666 case 'u':
667 {
668 /* \uXXXX */
669 int rc = VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE;
670 rtJsonTokenizerSkipCh(pTokenizer);
671 char chX1 = rtJsonTokenizerGetCh(pTokenizer);
672 if (RT_C_IS_XDIGIT(chX1))
673 {
674 rtJsonTokenizerSkipCh(pTokenizer);
675 char chX2 = rtJsonTokenizerGetCh(pTokenizer);
676 if (RT_C_IS_XDIGIT(chX2))
677 {
678 rtJsonTokenizerSkipCh(pTokenizer);
679 char chX3 = rtJsonTokenizerGetCh(pTokenizer);
680 if (RT_C_IS_XDIGIT(chX3))
681 {
682 rtJsonTokenizerSkipCh(pTokenizer);
683 char chX4 = rtJsonTokenizerGetCh(pTokenizer);
684 if (RT_C_IS_XDIGIT(chX4))
685 {
686 rtJsonTokenizerSkipCh(pTokenizer);
687
688 RTUNICP uc = ((RTUTF16)(chX1 <= '9' ? chX1 - '0' : (chX1 & 7) + 9) << 12)
689 | ((RTUTF16)(chX2 <= '9' ? chX2 - '0' : (chX2 & 7) + 9) << 8)
690 | ((RTUTF16)(chX3 <= '9' ? chX3 - '0' : (chX3 & 7) + 9) << 4)
691 | ((RTUTF16)(chX4 <= '9' ? chX4 - '0' : (chX4 & 7) + 9));
692 if ( !RTUtf16IsHighSurrogate((RTUTF16)uc)
693 && !RTUtf16IsLowSurrogate((RTUTF16)uc))
694 rc = VINF_SUCCESS;
695 else if (RTUtf16IsHighSurrogate((RTUTF16)uc))
696 {
697 /* The must be a low surrogate pair following the high one: */
698 rc = VINF_SUCCESS;
699 ch = rtJsonTokenizerGetCh(pTokenizer);
700 if (ch == '\\')
701 rtJsonTokenizerSkipCh(pTokenizer);
702 else
703 rc = VERR_JSON_MISSING_SURROGATE_PAIR;
704 ch = rtJsonTokenizerGetCh(pTokenizer);
705 if (ch == 'u')
706 rtJsonTokenizerSkipCh(pTokenizer);
707 else
708 rc = VERR_JSON_MISSING_SURROGATE_PAIR;
709 chX1 = rtJsonTokenizerGetCh(pTokenizer);
710 if (RT_C_IS_XDIGIT(chX1))
711 rtJsonTokenizerSkipCh(pTokenizer);
712 else if (RT_SUCCESS_NP(rc))
713 rc = VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE;
714 chX2 = rtJsonTokenizerGetCh(pTokenizer);
715 if (RT_C_IS_XDIGIT(chX2))
716 rtJsonTokenizerSkipCh(pTokenizer);
717 else if (RT_SUCCESS_NP(rc))
718 rc = VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE;
719 chX3 = rtJsonTokenizerGetCh(pTokenizer);
720 if (RT_C_IS_XDIGIT(chX3))
721 rtJsonTokenizerSkipCh(pTokenizer);
722 else if (RT_SUCCESS_NP(rc))
723 rc = VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE;
724 chX4 = rtJsonTokenizerGetCh(pTokenizer);
725 if (RT_C_IS_XDIGIT(chX4))
726 rtJsonTokenizerSkipCh(pTokenizer);
727 else if (RT_SUCCESS_NP(rc))
728 rc = VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE;
729 if (RT_SUCCESS(rc))
730 {
731 RTUTF16 wc2 = ((RTUTF16)(chX1 <= '9' ? chX1 - '0' : (chX1 & 7) + 9) << 12)
732 | ((RTUTF16)(chX2 <= '9' ? chX2 - '0' : (chX2 & 7) + 9) << 8)
733 | ((RTUTF16)(chX3 <= '9' ? chX3 - '0' : (chX3 & 7) + 9) << 4)
734 | ((RTUTF16)(chX4 <= '9' ? chX4 - '0' : (chX4 & 7) + 9));
735 if (RTUtf16IsLowSurrogate(wc2))
736 uc = 0x10000 + (((uc & 0x3ff) << 10) | (wc2 & 0x3ff));
737 else
738 rc = VERR_JSON_BAD_SURROGATE_PAIR_SEQUENCE;
739 }
740 }
741 else
742 rc = VERR_JSON_BAD_SURROGATE_PAIR_SEQUENCE;
743 if (RT_SUCCESS(rc))
744 {
745 if ( uc != 0
746 && uc != 0xfffe
747 && uc != 0xffff)
748 {
749 Assert(cchStr + RTStrCpSize(uc) < cchStrMax);
750 char *pszNext = RTStrPutCp(&pszDecoded[cchStr], uc);
751 Assert((size_t)(pszNext - &pszDecoded[cchStr]) == RTStrCpSize(uc));
752 cchStr += pszNext - &pszDecoded[cchStr];
753 break;
754 }
755 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_INVALID_CODEPOINT,
756 "Invalid \\u code point: %#x (line %zu col %zu)",
757 uc, pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
758 }
759 }
760 }
761 }
762 }
763 RTStrFree(pszDecoded);
764 if (rc == VERR_JSON_INVALID_UTF16_ESCAPE_SEQUENCE)
765 rc = RTErrInfoSetF(pTokenizer->pErrInfo, rc, "Invalid \\u escape sequence (line %zu col %zu)",
766 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
767 else if (rc == VERR_JSON_MISSING_SURROGATE_PAIR)
768 rc = RTErrInfoSetF(pTokenizer->pErrInfo, rc, "Missing UTF-16 surrogate pair (line %zu col %zu)",
769 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
770 else if (rc == VERR_JSON_BAD_SURROGATE_PAIR_SEQUENCE)
771 rc = RTErrInfoSetF(pTokenizer->pErrInfo, rc, "Invalid UTF-16 surrogate pair (line %zu col %zu)",
772 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
773 return rc;
774 }
775
776 default:
777 RTStrFree(pszDecoded);
778 return RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "bad escape sequence (line %zu col %zu)",
779 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
780 }
781 }
782
783
784 if (cchStr < cchStrMax - 4)
785 { /* likely */ }
786 else
787 {
788 /* Increase string space. */
789 size_t cchStrMaxNew = cchStrMax < _4K ? cchStrMax * 2 : cchStrMax + _4K;
790 int rc = RTStrRealloc(&pszDecoded, cchStrMaxNew);
791 if (RT_SUCCESS(rc))
792 cchStrMax = cchStrMaxNew;
793 else
794 {
795 RTStrFree(pszDecoded);
796 return rc;
797 }
798 }
799 ch = rtJsonTokenizerGetCh(pTokenizer);
800 }
801
802 if (ch == '\"')
803 rtJsonTokenizerSkipCh(pTokenizer); /* Skip closing " */
804
805 Assert(cchStr < cchStrMax);
806 pszDecoded[cchStr] = '\0';
807 if (cchStrMax - cchStr >= cchStrMax / 2)
808 RTStrRealloc(&pszDecoded, cchStr + 1);
809 pToken->Class.String.pszStr = pszDecoded;
810
811 pToken->Pos.iChEnd = pTokenizer->Pos.iChEnd;
812 return VINF_SUCCESS;
813}
814
815/**
816 * Get the end of stream token.
817 *
818 * @returns IPRT status code.
819 * @param pTokenizer The tokenizer state.
820 * @param pToken The uninitialized token.
821 */
822static int rtJsonTokenizerGetEos(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken)
823{
824 Assert(rtJsonTokenizerGetCh(pTokenizer) == '\0');
825
826 pToken->enmClass = RTJSONTOKENCLASS_EOS;
827 pToken->Pos = pTokenizer->Pos;
828 return VINF_SUCCESS;
829}
830
831/**
832 * Read the next token from the tokenizer stream.
833 *
834 * @returns IPRT status code.
835 * @param pTokenizer The tokenizer to read from.
836 * @param pToken Uninitialized token to fill the token data into.
837 */
838static int rtJsonTokenizerReadNextToken(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken)
839{
840 int rc = VINF_SUCCESS;
841
842 /* Skip all eventually existing whitespace and newlines first. */
843 rtJsonTokenizerSkipWhitespace(pTokenizer);
844
845 char ch = rtJsonTokenizerGetCh(pTokenizer);
846 if (RT_C_IS_ALPHA(ch))
847 rc = rtJsonTokenizerGetLiteral(pTokenizer, pToken);
848 else if (RT_C_IS_DIGIT(ch) || ch == '-')
849 rc = rtJsonTokenizerGetNumber(pTokenizer, pToken);
850 else if (ch == '\"')
851 rc = rtJsonTokenizerGetString(pTokenizer, pToken);
852 else if (ch == '\0')
853 rc = rtJsonTokenizerGetEos(pTokenizer, pToken);
854 else if (ch == '{')
855 {
856 pToken->enmClass = RTJSONTOKENCLASS_BEGIN_OBJECT;
857 rtJsonTokenizerSkipCh(pTokenizer);
858 }
859 else if (ch == '}')
860 {
861 pToken->enmClass = RTJSONTOKENCLASS_END_OBJECT;
862 rtJsonTokenizerSkipCh(pTokenizer);
863 }
864 else if (ch == '[')
865 {
866 pToken->enmClass = RTJSONTOKENCLASS_BEGIN_ARRAY;
867 rtJsonTokenizerSkipCh(pTokenizer);
868 }
869 else if (ch == ']')
870 {
871 pToken->enmClass = RTJSONTOKENCLASS_END_ARRAY;
872 rtJsonTokenizerSkipCh(pTokenizer);
873 }
874 else if (ch == ':')
875 {
876 pToken->enmClass = RTJSONTOKENCLASS_NAME_SEPARATOR;
877 rtJsonTokenizerSkipCh(pTokenizer);
878 }
879 else if (ch == ',')
880 {
881 pToken->enmClass = RTJSONTOKENCLASS_VALUE_SEPARATOR;
882 rtJsonTokenizerSkipCh(pTokenizer);
883 }
884 else
885 {
886 pToken->enmClass = RTJSONTOKENCLASS_INVALID;
887 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "bad token '%c' (line %zu col %zu)",
888 ch, pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
889 }
890
891 if (RT_FAILURE(rc))
892 pTokenizer->rcTok = rc;
893
894 return rc;
895}
896
897/**
898 * Create a new tokenizer.
899 *
900 * @returns IPRT status code.
901 * @param pTokenizer The tokenizer state to initialize.
902 * @param pfnRead Read callback for the input stream.
903 * @param pvUser Opaque user data to pass to the callback.
904 * @param pErrInfo Where to return extended error info.
905 */
906static int rtJsonTokenizerInit(PRTJSONTOKENIZER pTokenizer, PFNRTJSONTOKENIZERREAD pfnRead, void *pvUser, PRTERRINFO pErrInfo)
907{
908 pTokenizer->pfnRead = pfnRead;
909 pTokenizer->pvUser = pvUser;
910 pTokenizer->offInput = 0;
911 pTokenizer->cbBuf = 0;
912 pTokenizer->offBuf = 0;
913 pTokenizer->Pos.iLine = 1;
914 pTokenizer->Pos.iChStart = 1;
915 pTokenizer->Pos.iChEnd = 1;
916 pTokenizer->pTokenCurr = &pTokenizer->Token1;
917 pTokenizer->pTokenNext = &pTokenizer->Token2;
918 pTokenizer->rcTok = VINF_SUCCESS;
919 pTokenizer->pErrInfo = pErrInfo;
920
921 RT_ZERO(pTokenizer->achBuf);
922
923 /* Fill the input buffer. */
924 int rc = rtJsonTokenizerRead(pTokenizer);
925
926 /* Fill the tokenizer with two first tokens. */
927 if (RT_SUCCESS(rc))
928 rc = rtJsonTokenizerReadNextToken(pTokenizer, pTokenizer->pTokenCurr);
929 if (RT_SUCCESS(rc))
930 rc = rtJsonTokenizerReadNextToken(pTokenizer, pTokenizer->pTokenNext);
931
932 return rc;
933}
934
935/**
936 * Destroys a given tokenizer state.
937 *
938 * @returns nothing.
939 * @param pTokenizer The tokenizer to destroy.
940 */
941static void rtJsonTokenizerDestroy(PRTJSONTOKENIZER pTokenizer)
942{
943 RT_NOREF_PV(pTokenizer);
944}
945
946/**
947 * Get the current token in the input stream.
948 *
949 * @returns Pointer to the next token in the stream.
950 * @param pTokenizer The tokenizer state.
951 * @param ppToken Where to store the pointer to the current token on success.
952 */
953DECLINLINE(int) rtJsonTokenizerGetToken(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN *ppToken)
954{
955 if (RT_SUCCESS(pTokenizer->rcTok))
956 {
957 *ppToken = pTokenizer->pTokenCurr;
958 return VINF_SUCCESS;
959 }
960
961 return pTokenizer->rcTok;
962}
963
964/**
965 * Consume the current token advancing to the next in the stream.
966 *
967 * @returns nothing.
968 * @param pTokenizer The tokenizer state.
969 */
970static void rtJsonTokenizerConsume(PRTJSONTOKENIZER pTokenizer)
971{
972 PRTJSONTOKEN pTokenTmp = pTokenizer->pTokenCurr;
973
974 /* Switch next token to current token and read in the next token. */
975 pTokenizer->pTokenCurr = pTokenizer->pTokenNext;
976 pTokenizer->pTokenNext = pTokenTmp;
977 rtJsonTokenizerReadNextToken(pTokenizer, pTokenizer->pTokenNext);
978}
979
980/**
981 * Consumes the current token if it matches the given class returning an indicator.
982 *
983 * @returns true if the class matched and the token was consumed.
984 * @retval false otherwise.
985 * @param pTokenizer The tokenizer state.
986 * @param enmClass The token class to match against.
987 */
988static bool rtJsonTokenizerConsumeIfMatched(PRTJSONTOKENIZER pTokenizer, RTJSONTOKENCLASS enmClass)
989{
990 PRTJSONTOKEN pToken = NULL;
991 int rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
992 if (RT_SUCCESS(rc))
993 {
994 if (pToken->enmClass == enmClass)
995 {
996 rtJsonTokenizerConsume(pTokenizer);
997 return true;
998 }
999 }
1000
1001 return false;
1002}
1003
1004/**
1005 * Destroys a given JSON value releasing the reference to all child values.
1006 *
1007 * @returns nothing.
1008 * @param pThis The JSON value to destroy.
1009 */
1010static void rtJsonValDestroy(PRTJSONVALINT pThis)
1011{
1012 switch (pThis->enmType)
1013 {
1014 case RTJSONVALTYPE_OBJECT:
1015 for (unsigned i = 0; i < pThis->Type.Object.cMembers; i++)
1016 {
1017 RTStrFree(pThis->Type.Object.papszNames[i]);
1018 RTJsonValueRelease(pThis->Type.Object.papValues[i]);
1019 }
1020 RTMemFree(pThis->Type.Object.papszNames);
1021 RTMemFree(pThis->Type.Object.papValues);
1022 break;
1023 case RTJSONVALTYPE_ARRAY:
1024 for (unsigned i = 0; i < pThis->Type.Array.cItems; i++)
1025 RTJsonValueRelease(pThis->Type.Array.papItems[i]);
1026 RTMemFree(pThis->Type.Array.papItems);
1027 break;
1028 case RTJSONVALTYPE_STRING:
1029 RTStrFree(pThis->Type.String.pszStr);
1030 break;
1031 case RTJSONVALTYPE_INTEGER:
1032 case RTJSONVALTYPE_NUMBER:
1033 case RTJSONVALTYPE_NULL:
1034 case RTJSONVALTYPE_TRUE:
1035 case RTJSONVALTYPE_FALSE:
1036 /* nothing to do. */
1037 break;
1038 default:
1039 AssertMsgFailed(("Invalid JSON value type: %u\n", pThis->enmType));
1040 }
1041 RTMemFree(pThis);
1042}
1043
1044/**
1045 * Creates a new JSON value with the given type.
1046 *
1047 * @returns Pointer to JSON value on success, NULL if out of memory.
1048 * @param enmType The JSON value type.
1049 */
1050static PRTJSONVALINT rtJsonValueCreate(RTJSONVALTYPE enmType)
1051{
1052 PRTJSONVALINT pThis = (PRTJSONVALINT)RTMemAllocZ(sizeof(RTJSONVALINT));
1053 if (RT_LIKELY(pThis))
1054 {
1055 pThis->enmType = enmType;
1056 pThis->cRefs = 1;
1057 }
1058
1059 return pThis;
1060}
1061
1062/**
1063 * Parses an JSON array.
1064 *
1065 * @returns IPRT status code.
1066 * @param pTokenizer The tokenizer to use.
1067 * @param pJsonVal The JSON array value to fill in.
1068 */
1069static int rtJsonParseArray(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal)
1070{
1071 int rc = VINF_SUCCESS;
1072 PRTJSONTOKEN pToken = NULL;
1073 uint32_t cItems = 0;
1074 uint32_t cItemsMax = 0;
1075 PRTJSONVALINT *papItems = NULL;
1076
1077 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1078 while ( RT_SUCCESS(rc)
1079 && pToken->enmClass != RTJSONTOKENCLASS_END_ARRAY
1080 && pToken->enmClass != RTJSONTOKENCLASS_EOS)
1081 {
1082 PRTJSONVALINT pVal = NULL;
1083 rc = rtJsonParseValue(pTokenizer, pToken, &pVal);
1084 if (RT_SUCCESS(rc))
1085 {
1086 if (cItems == cItemsMax)
1087 {
1088 cItemsMax += 10;
1089 PRTJSONVALINT *papItemsNew = (PRTJSONVALINT *)RTMemRealloc(papItems, cItemsMax * sizeof(PRTJSONVALINT));
1090 if (RT_UNLIKELY(!papItemsNew))
1091 {
1092 rc = VERR_NO_MEMORY;
1093 break;
1094 }
1095 papItems = papItemsNew;
1096 }
1097
1098 Assert(cItems < cItemsMax);
1099 papItems[cItems] = pVal;
1100 cItems++;
1101 }
1102
1103 /* Skip value separator and continue with next token. */
1104 bool fSkippedSep = rtJsonTokenizerConsumeIfMatched(pTokenizer, RTJSONTOKENCLASS_VALUE_SEPARATOR);
1105 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1106
1107 if ( RT_SUCCESS(rc)
1108 && !fSkippedSep
1109 && pToken->enmClass != RTJSONTOKENCLASS_END_ARRAY)
1110 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of array (#1) (line %zu col %zu)",
1111 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1112 }
1113
1114 if (RT_SUCCESS(rc))
1115 {
1116 if (pToken->enmClass == RTJSONTOKENCLASS_END_ARRAY)
1117 {
1118 rtJsonTokenizerConsume(pTokenizer);
1119 pJsonVal->Type.Array.cItems = cItems;
1120 pJsonVal->Type.Array.papItems = papItems;
1121 }
1122 else
1123 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of array (#2) (line %zu col %zu)",
1124 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1125 }
1126
1127 if (RT_FAILURE(rc))
1128 {
1129 for (uint32_t i = 0; i < cItems; i++)
1130 RTJsonValueRelease(papItems[i]);
1131 RTMemFree(papItems);
1132 }
1133
1134 return rc;
1135}
1136
1137/**
1138 * Parses an JSON object.
1139 *
1140 * @returns IPRT status code.
1141 * @param pTokenizer The tokenizer to use.
1142 * @param pJsonVal The JSON object value to fill in.
1143 */
1144static int rtJsonParseObject(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal)
1145{
1146 int rc = VINF_SUCCESS;
1147 PRTJSONTOKEN pToken = NULL;
1148 uint32_t cMembers = 0;
1149 uint32_t cMembersMax = 0;
1150 PRTJSONVALINT *papValues = NULL;
1151 char **papszNames = NULL;
1152
1153 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1154 while ( RT_SUCCESS(rc)
1155 && pToken->enmClass == RTJSONTOKENCLASS_STRING)
1156 {
1157 char *pszName = pToken->Class.String.pszStr; /* We can consume this string as it was allocated. */
1158
1159 rtJsonTokenizerConsume(pTokenizer);
1160 if (rtJsonTokenizerConsumeIfMatched(pTokenizer, RTJSONTOKENCLASS_NAME_SEPARATOR))
1161 {
1162 PRTJSONVALINT pVal = NULL;
1163 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1164 if (RT_SUCCESS(rc))
1165 rc = rtJsonParseValue(pTokenizer, pToken, &pVal);
1166 if (RT_SUCCESS(rc))
1167 {
1168 if (cMembers == cMembersMax)
1169 {
1170 cMembersMax += 10;
1171 PRTJSONVALINT *papValuesNew = (PRTJSONVALINT *)RTMemRealloc(papValues, cMembersMax * sizeof(PRTJSONVALINT));
1172 char **papszNamesNew = (char **)RTMemRealloc(papszNames, cMembersMax * sizeof(char *));
1173 if (RT_UNLIKELY(!papValuesNew || !papszNamesNew))
1174 {
1175 if (papValuesNew)
1176 RTMemFree(papValuesNew);
1177 if (papszNamesNew)
1178 RTMemFree(papszNamesNew);
1179 rc = VERR_NO_MEMORY;
1180 break;
1181 }
1182
1183 papValues = papValuesNew;
1184 papszNames = papszNamesNew;
1185 }
1186
1187 Assert(cMembers < cMembersMax);
1188 papszNames[cMembers] = pszName;
1189 papValues[cMembers] = pVal;
1190 cMembers++;
1191
1192 /* Skip value separator and continue with next token. */
1193 bool fSkippedSep = rtJsonTokenizerConsumeIfMatched(pTokenizer, RTJSONTOKENCLASS_VALUE_SEPARATOR);
1194 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1195
1196 if ( RT_SUCCESS(rc)
1197 && !fSkippedSep
1198 && pToken->enmClass != RTJSONTOKENCLASS_END_OBJECT)
1199 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#1) (line %zu col %zu)",
1200 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1201 }
1202 }
1203 else
1204 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected name separator (line %zu col %zu)",
1205 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1206 }
1207
1208 if (RT_SUCCESS(rc))
1209 {
1210 if (pToken->enmClass == RTJSONTOKENCLASS_END_OBJECT)
1211 {
1212 rtJsonTokenizerConsume(pTokenizer);
1213 pJsonVal->Type.Object.cMembers = cMembers;
1214 pJsonVal->Type.Object.papValues = papValues;
1215 pJsonVal->Type.Object.papszNames = papszNames;
1216 }
1217 else
1218 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#2) (line %zu col %zu)",
1219 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1220 }
1221
1222 if (RT_FAILURE(rc))
1223 {
1224 for (uint32_t i = 0; i < cMembers; i++)
1225 {
1226 RTJsonValueRelease(papValues[i]);
1227 RTStrFree(papszNames[i]);
1228 }
1229 RTMemFree(papValues);
1230 RTMemFree(papszNames);
1231 }
1232
1233 return rc;
1234}
1235
1236/**
1237 * Parses a single JSON value and returns it on success.
1238 *
1239 * @returns IPRT status code.
1240 * @param pTokenizer The tokenizer to use.
1241 * @param pToken The token to parse.
1242 * @param ppJsonVal Where to store the pointer to the JSON value on success.
1243 */
1244static int rtJsonParseValue(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken, PRTJSONVALINT *ppJsonVal)
1245{
1246 int rc = VINF_SUCCESS;
1247 PRTJSONVALINT pVal = NULL;
1248
1249 switch (pToken->enmClass)
1250 {
1251 case RTJSONTOKENCLASS_BEGIN_ARRAY:
1252 rtJsonTokenizerConsume(pTokenizer);
1253 pVal = rtJsonValueCreate(RTJSONVALTYPE_ARRAY);
1254 if (RT_LIKELY(pVal))
1255 rc = rtJsonParseArray(pTokenizer, pVal);
1256 break;
1257 case RTJSONTOKENCLASS_BEGIN_OBJECT:
1258 rtJsonTokenizerConsume(pTokenizer);
1259 pVal = rtJsonValueCreate(RTJSONVALTYPE_OBJECT);
1260 if (RT_LIKELY(pVal))
1261 rc = rtJsonParseObject(pTokenizer, pVal);
1262 break;
1263 case RTJSONTOKENCLASS_STRING:
1264 pVal = rtJsonValueCreate(RTJSONVALTYPE_STRING);
1265 if (RT_LIKELY(pVal))
1266 pVal->Type.String.pszStr = pToken->Class.String.pszStr;
1267 rtJsonTokenizerConsume(pTokenizer);
1268 break;
1269 case RTJSONTOKENCLASS_INTEGER:
1270 pVal = rtJsonValueCreate(RTJSONVALTYPE_INTEGER);
1271 if (RT_LIKELY(pVal))
1272 pVal->Type.Integer.i64Num = pToken->Class.Integer.i64Num;
1273 rtJsonTokenizerConsume(pTokenizer);
1274 break;
1275 case RTJSONTOKENCLASS_NUMBER:
1276 pVal = rtJsonValueCreate(RTJSONVALTYPE_NUMBER);
1277 if (RT_LIKELY(pVal))
1278 pVal->Type.rdNum = pToken->Class.rdNum;
1279 rtJsonTokenizerConsume(pTokenizer);
1280 break;
1281 case RTJSONTOKENCLASS_NULL:
1282 rtJsonTokenizerConsume(pTokenizer);
1283 pVal = rtJsonValueCreate(RTJSONVALTYPE_NULL);
1284 break;
1285 case RTJSONTOKENCLASS_FALSE:
1286 rtJsonTokenizerConsume(pTokenizer);
1287 pVal = rtJsonValueCreate(RTJSONVALTYPE_FALSE);
1288 break;
1289 case RTJSONTOKENCLASS_TRUE:
1290 rtJsonTokenizerConsume(pTokenizer);
1291 pVal = rtJsonValueCreate(RTJSONVALTYPE_TRUE);
1292 break;
1293
1294 case RTJSONTOKENCLASS_INVALID:
1295 Assert(!pTokenizer->pErrInfo || RTErrInfoIsSet(pTokenizer->pErrInfo));
1296 rc = VERR_JSON_MALFORMED;
1297 break;
1298 case RTJSONTOKENCLASS_END_ARRAY:
1299 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected '}' (line %zu col %zu)",
1300 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1301 break;
1302 case RTJSONTOKENCLASS_END_OBJECT:
1303 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ']' (line %zu col %zu)",
1304 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1305 break;
1306 case RTJSONTOKENCLASS_NAME_SEPARATOR:
1307 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ':' (line %zu col %zu)",
1308 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1309 break;
1310 case RTJSONTOKENCLASS_VALUE_SEPARATOR:
1311 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ',' (line %zu col %zu)",
1312 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1313 break;
1314 case RTJSONTOKENCLASS_EOS:
1315 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#1) (line %zu col %zu)",
1316 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1317 break;
1318 default:
1319 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "Unexpected token class %d (line %zu col %zu)",
1320 pToken->enmClass, pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1321 break;
1322 }
1323
1324 if (RT_SUCCESS(rc))
1325 {
1326 if (pVal)
1327 *ppJsonVal = pVal;
1328 else
1329 rc = VERR_NO_MEMORY;
1330 }
1331 else if (pVal)
1332 rtJsonValDestroy(pVal);
1333
1334 return rc;
1335}
1336
1337/**
1338 * Entry point to parse a JSON document.
1339 *
1340 * @returns IPRT status code.
1341 * @param pTokenizer The tokenizer state.
1342 * @param ppJsonVal Where to store the root JSON value on success.
1343 */
1344static int rtJsonParse(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT *ppJsonVal)
1345{
1346 PRTJSONTOKEN pToken = NULL;
1347 int rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1348 if (RT_SUCCESS(rc))
1349 rc = rtJsonParseValue(pTokenizer, pToken, ppJsonVal);
1350
1351 return rc;
1352}
1353
1354/**
1355 * Read callback for RTJsonParseFromBuf().
1356 */
1357static DECLCALLBACK(int) rtJsonTokenizerParseFromBuf(void *pvUser, size_t offInput,
1358 void *pvBuf, size_t cbBuf,
1359 size_t *pcbRead)
1360{
1361 PRTJSONREADERARGS pArgs = (PRTJSONREADERARGS)pvUser;
1362 size_t cbLeft = offInput < pArgs->cbData ? pArgs->cbData - offInput : 0;
1363
1364 if (cbLeft)
1365 memcpy(pvBuf, &pArgs->u.pbBuf[offInput], RT_MIN(cbLeft, cbBuf));
1366
1367 *pcbRead = RT_MIN(cbLeft, cbBuf);
1368
1369 return VINF_SUCCESS;
1370}
1371
1372/**
1373 * Read callback for RTJsonParseFromString().
1374 */
1375static DECLCALLBACK(int) rtJsonTokenizerParseFromString(void *pvUser, size_t offInput,
1376 void *pvBuf, size_t cbBuf,
1377 size_t *pcbRead)
1378{
1379 const char *pszStr = (const char *)pvUser;
1380 size_t cchStr = strlen(pszStr) + 1; /* Include zero terminator. */
1381 size_t cbLeft = offInput < cchStr ? cchStr - offInput : 0;
1382
1383 if (cbLeft)
1384 memcpy(pvBuf, &pszStr[offInput], RT_MIN(cbLeft, cbBuf));
1385
1386 *pcbRead = RT_MIN(cbLeft, cbBuf);
1387
1388 return VINF_SUCCESS;
1389}
1390
1391/**
1392 * Read callback for RTJsonParseFromFile().
1393 */
1394static DECLCALLBACK(int) rtJsonTokenizerParseFromFile(void *pvUser, size_t offInput,
1395 void *pvBuf, size_t cbBuf,
1396 size_t *pcbRead)
1397{
1398 PRTJSONREADERARGS pArgs = (PRTJSONREADERARGS)pvUser;
1399
1400 RT_NOREF_PV(offInput);
1401
1402 size_t cbRead = 0;
1403 int rc = RTStrmReadEx(pArgs->u.hStream, pvBuf, cbBuf, &cbRead);
1404 if (RT_SUCCESS(rc))
1405 *pcbRead = cbRead;
1406
1407 return rc;
1408}
1409
1410RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf, PRTERRINFO pErrInfo)
1411{
1412 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1413 AssertPtrReturn(pbBuf, VERR_INVALID_POINTER);
1414 AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
1415
1416 RTJSONTOKENIZER Tokenizer;
1417 RTJSONREADERARGS Args;
1418 Args.cbData = cbBuf;
1419 Args.u.pbBuf = pbBuf;
1420
1421 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromBuf, &Args, pErrInfo);
1422 if (RT_SUCCESS(rc))
1423 {
1424 rc = rtJsonParse(&Tokenizer, phJsonVal);
1425 rtJsonTokenizerDestroy(&Tokenizer);
1426 }
1427
1428 return rc;
1429}
1430
1431RTDECL(int) RTJsonParseFromString(PRTJSONVAL phJsonVal, const char *pszStr, PRTERRINFO pErrInfo)
1432{
1433 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1434 AssertPtrReturn(pszStr, VERR_INVALID_POINTER);
1435
1436 /** @todo r=bird: The rtJsonTokenizerParseFromString function does
1437 * strlen() on the whole pszStr for each read. For larger strings (
1438 * longer than sizeof(Tokenizer.achBuf)) it would be good to join
1439 * forces with RTJsonParseFromBuf. */
1440 RTJSONTOKENIZER Tokenizer;
1441 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromString, (void *)pszStr, pErrInfo);
1442 if (RT_SUCCESS(rc))
1443 {
1444 rc = rtJsonParse(&Tokenizer, phJsonVal);
1445 rtJsonTokenizerDestroy(&Tokenizer);
1446 }
1447
1448 return rc;
1449}
1450
1451RTDECL(int) RTJsonParseFromFile(PRTJSONVAL phJsonVal, const char *pszFilename, PRTERRINFO pErrInfo)
1452{
1453 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1454 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1455
1456 int rc = VINF_SUCCESS;
1457 RTJSONREADERARGS Args;
1458
1459 Args.cbData = 0;
1460 rc = RTStrmOpen(pszFilename, "r", &Args.u.hStream);
1461 if (RT_SUCCESS(rc))
1462 {
1463 RTJSONTOKENIZER Tokenizer;
1464
1465 rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromFile, &Args, pErrInfo);
1466 if (RT_SUCCESS(rc))
1467 {
1468 rc = rtJsonParse(&Tokenizer, phJsonVal);
1469 rtJsonTokenizerDestroy(&Tokenizer);
1470 }
1471 RTStrmClose(Args.u.hStream);
1472 }
1473
1474 return rc;
1475}
1476
1477RTDECL(uint32_t) RTJsonValueRetain(RTJSONVAL hJsonVal)
1478{
1479 PRTJSONVALINT pThis = hJsonVal;
1480 AssertPtrReturn(pThis, UINT32_MAX);
1481
1482 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
1483 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
1484 return cRefs;
1485}
1486
1487RTDECL(uint32_t) RTJsonValueRelease(RTJSONVAL hJsonVal)
1488{
1489 PRTJSONVALINT pThis = hJsonVal;
1490 if (pThis == NIL_RTJSONVAL)
1491 return 0;
1492 AssertPtrReturn(pThis, UINT32_MAX);
1493
1494 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
1495 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
1496 if (cRefs == 0)
1497 rtJsonValDestroy(pThis);
1498 return cRefs;
1499}
1500
1501RTDECL(RTJSONVALTYPE) RTJsonValueGetType(RTJSONVAL hJsonVal)
1502{
1503 PRTJSONVALINT pThis = hJsonVal;
1504 AssertPtrReturn(pThis, RTJSONVALTYPE_INVALID);
1505
1506 if (pThis == NIL_RTJSONVAL)
1507 return RTJSONVALTYPE_INVALID;
1508
1509 return pThis->enmType;
1510}
1511
1512
1513RTDECL(const char *) RTJsonValueTypeName(RTJSONVALTYPE enmType)
1514{
1515 switch (enmType)
1516 {
1517 case RTJSONVALTYPE_INVALID: return "invalid";
1518 case RTJSONVALTYPE_OBJECT: return "object";
1519 case RTJSONVALTYPE_ARRAY: return "array";
1520 case RTJSONVALTYPE_STRING: return "string";
1521 case RTJSONVALTYPE_INTEGER: return "integer";
1522 case RTJSONVALTYPE_NUMBER: return "number";
1523 case RTJSONVALTYPE_NULL: return "null";
1524 case RTJSONVALTYPE_TRUE: return "true";
1525 case RTJSONVALTYPE_FALSE: return "false";
1526 default: return "???";
1527 }
1528}
1529
1530
1531#define RTJSON_ASSERT_VALID_HANDLE_AND_TYPE_RETURN(pJson, enmExpectedType, ret) do { \
1532 AssertPtrReturn((pJson), (ret)); \
1533 AssertReturn((pJson) != NIL_RTJSONVAL, (ret)); \
1534 AssertReturn((pJson)->enmType == (enmExpectedType), (ret)); \
1535 } while (0)
1536
1537#define RTJSON_TYPECHECK_RETURN(pJson, enmExpectedType) do {\
1538 if ((pJson)->enmType == (enmExpectedType)) { /*likely*/ } \
1539 else { /*AssertFailed();*/ return VERR_JSON_VALUE_INVALID_TYPE; } \
1540 } while (0)
1541
1542
1543#define RTJSON_TYPECHECK_CONTAINER_RETURN(pJson) do { \
1544 if ( (pJson)->enmType == RTJSONVALTYPE_ARRAY \
1545 || (pJson)->enmType == RTJSONVALTYPE_OBJECT) \
1546 { /* likely */ } \
1547 else { /*AssertFailed();*/ return VERR_JSON_VALUE_INVALID_TYPE;} \
1548 } while (0)
1549
1550
1551RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal)
1552{
1553 PRTJSONVALINT pThis = hJsonVal;
1554 RTJSON_ASSERT_VALID_HANDLE_AND_TYPE_RETURN(pThis, RTJSONVALTYPE_STRING, NULL);
1555
1556 return pThis->Type.String.pszStr;
1557}
1558
1559
1560RTDECL(int) RTJsonValueQueryString(RTJSONVAL hJsonVal, const char **ppszStr)
1561{
1562 PRTJSONVALINT pThis = hJsonVal;
1563 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1564 AssertPtrReturn(ppszStr, VERR_INVALID_POINTER);
1565
1566 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_STRING);
1567 *ppszStr = pThis->Type.String.pszStr;
1568 return VINF_SUCCESS;
1569}
1570
1571RTDECL(int) RTJsonValueQueryInteger(RTJSONVAL hJsonVal, int64_t *pi64Num)
1572{
1573 PRTJSONVALINT pThis = hJsonVal;
1574 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1575 AssertPtrReturn(pi64Num, VERR_INVALID_POINTER);
1576
1577 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_INTEGER);
1578 *pi64Num = pThis->Type.Integer.i64Num;
1579 return VINF_SUCCESS;
1580}
1581
1582RTDECL(int) RTJsonValueQueryNumber(RTJSONVAL hJsonVal, double *prdNum)
1583{
1584 PRTJSONVALINT pThis = hJsonVal;
1585 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1586 AssertPtrReturn(prdNum, VERR_INVALID_POINTER);
1587
1588 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_NUMBER);
1589 *prdNum = pThis->Type.rdNum;
1590 return VINF_SUCCESS;
1591}
1592
1593RTDECL(int) RTJsonValueQueryByName(RTJSONVAL hJsonVal, const char *pszName, PRTJSONVAL phJsonVal)
1594{
1595 PRTJSONVALINT pThis = hJsonVal;
1596 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1597 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1598 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1599
1600 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_OBJECT);
1601
1602 int rc = VERR_NOT_FOUND;
1603 for (unsigned i = 0; i < pThis->Type.Object.cMembers; i++)
1604 {
1605 if (!RTStrCmp(pThis->Type.Object.papszNames[i], pszName))
1606 {
1607 RTJsonValueRetain(pThis->Type.Object.papValues[i]);
1608 *phJsonVal = pThis->Type.Object.papValues[i];
1609 rc = VINF_SUCCESS;
1610 break;
1611 }
1612 }
1613
1614 return rc;
1615}
1616
1617RTDECL(int) RTJsonValueQueryIntegerByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num)
1618{
1619 RTJSONVAL hJsonValNum = NIL_RTJSONVAL;
1620 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValNum);
1621 if (RT_SUCCESS(rc))
1622 {
1623 rc = RTJsonValueQueryInteger(hJsonValNum, pi64Num);
1624 RTJsonValueRelease(hJsonValNum);
1625 }
1626
1627 return rc;
1628}
1629
1630RTDECL(int) RTJsonValueQueryNumberByName(RTJSONVAL hJsonVal, const char *pszName, double *prdNum)
1631{
1632 RTJSONVAL hJsonValNum = NIL_RTJSONVAL;
1633 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValNum);
1634 if (RT_SUCCESS(rc))
1635 {
1636 rc = RTJsonValueQueryNumber(hJsonValNum, prdNum);
1637 RTJsonValueRelease(hJsonValNum);
1638 }
1639
1640 return rc;
1641}
1642
1643RTDECL(int) RTJsonValueQueryStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr)
1644{
1645 RTJSONVAL hJsonValStr = NIL_RTJSONVAL;
1646 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValStr);
1647 if (RT_SUCCESS(rc))
1648 {
1649 const char *pszStr = NULL;
1650 rc = RTJsonValueQueryString(hJsonValStr, &pszStr);
1651 if (RT_SUCCESS(rc))
1652 {
1653 *ppszStr = RTStrDup(pszStr);
1654 if (!*ppszStr)
1655 rc = VERR_NO_STR_MEMORY;
1656 }
1657 RTJsonValueRelease(hJsonValStr);
1658 }
1659
1660 return rc;
1661}
1662
1663RTDECL(int) RTJsonValueQueryBooleanByName(RTJSONVAL hJsonVal, const char *pszName, bool *pfBoolean)
1664{
1665 AssertPtrReturn(pfBoolean, VERR_INVALID_POINTER);
1666
1667 RTJSONVAL hJsonValBool = NIL_RTJSONVAL;
1668 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValBool);
1669 if (RT_SUCCESS(rc))
1670 {
1671 RTJSONVALTYPE enmType = RTJsonValueGetType(hJsonValBool);
1672 if (enmType == RTJSONVALTYPE_TRUE)
1673 *pfBoolean = true;
1674 else if (enmType == RTJSONVALTYPE_FALSE)
1675 *pfBoolean = false;
1676 else
1677 rc = VERR_JSON_VALUE_INVALID_TYPE;
1678 RTJsonValueRelease(hJsonValBool);
1679 }
1680
1681 return rc;
1682}
1683
1684RTDECL(unsigned) RTJsonValueGetArraySize(RTJSONVAL hJsonVal)
1685{
1686 PRTJSONVALINT pThis = hJsonVal;
1687 RTJSON_ASSERT_VALID_HANDLE_AND_TYPE_RETURN(pThis, RTJSONVALTYPE_ARRAY, 0);
1688
1689 return pThis->Type.Array.cItems;
1690}
1691
1692RTDECL(int) RTJsonValueQueryArraySize(RTJSONVAL hJsonVal, unsigned *pcItems)
1693{
1694 PRTJSONVALINT pThis = hJsonVal;
1695 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1696 AssertPtrReturn(pcItems, VERR_INVALID_POINTER);
1697
1698 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
1699 *pcItems = pThis->Type.Array.cItems;
1700 return VINF_SUCCESS;
1701}
1702
1703RTDECL(int) RTJsonValueQueryByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal)
1704{
1705 PRTJSONVALINT pThis = hJsonVal;
1706 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1707 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1708
1709 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
1710 if (RT_UNLIKELY(idx >= pThis->Type.Array.cItems))
1711 return VERR_OUT_OF_RANGE;
1712
1713 RTJsonValueRetain(pThis->Type.Array.papItems[idx]);
1714 *phJsonVal = pThis->Type.Array.papItems[idx];
1715 return VINF_SUCCESS;
1716}
1717
1718static int rtJsonIteratorBeginWorker(PRTJSONVALINT pThis, PRTJSONIT phJsonIt)
1719{
1720 PRTJSONITINT pIt = (PRTJSONITINT)RTMemTmpAllocZ(sizeof(RTJSONITINT));
1721 if (pIt)
1722 {
1723 RTJsonValueRetain(pThis);
1724 pIt->pJsonVal = pThis;
1725 pIt->idxCur = 0;
1726
1727 *phJsonIt = pIt;
1728 return VINF_SUCCESS;
1729 }
1730 return VERR_NO_MEMORY;
1731}
1732
1733RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt)
1734{
1735 PRTJSONVALINT pThis = hJsonVal;
1736 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1737 AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
1738 RTJSON_TYPECHECK_CONTAINER_RETURN(pThis);
1739
1740 return rtJsonIteratorBeginWorker(pThis, phJsonIt);
1741}
1742
1743RTDECL(int) RTJsonIteratorBeginArray(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt)
1744{
1745 PRTJSONVALINT pThis = hJsonVal;
1746 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1747 AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
1748 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
1749
1750 if (pThis->Type.Array.cItems > 0)
1751 return rtJsonIteratorBeginWorker(pThis, phJsonIt);
1752 return VERR_JSON_IS_EMPTY;
1753}
1754
1755RTDECL(int) RTJsonIteratorBeginObject(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt)
1756{
1757 PRTJSONVALINT pThis = hJsonVal;
1758 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1759 AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
1760 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_OBJECT);
1761
1762 if (pThis->Type.Object.cMembers > 0)
1763 return rtJsonIteratorBeginWorker(pThis, phJsonIt);
1764 return VERR_JSON_IS_EMPTY;
1765}
1766
1767RTDECL(int) RTJsonIteratorQueryValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName)
1768{
1769 PRTJSONITINT pIt = hJsonIt;
1770 AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
1771 AssertReturn(pIt != NIL_RTJSONIT, VERR_INVALID_HANDLE);
1772 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1773
1774 int rc = VINF_SUCCESS;
1775 PRTJSONVALINT pThis = pIt->pJsonVal;
1776 if (pThis->enmType == RTJSONVALTYPE_ARRAY)
1777 {
1778 if (pIt->idxCur < pThis->Type.Array.cItems)
1779 {
1780 if (ppszName)
1781 *ppszName = NULL;
1782
1783 RTJsonValueRetain(pThis->Type.Array.papItems[pIt->idxCur]);
1784 *phJsonVal = pThis->Type.Array.papItems[pIt->idxCur];
1785 }
1786 else
1787 rc = VERR_JSON_ITERATOR_END;
1788 }
1789 else
1790 {
1791 Assert(pThis->enmType == RTJSONVALTYPE_OBJECT);
1792
1793 if (pIt->idxCur < pThis->Type.Object.cMembers)
1794 {
1795 if (ppszName)
1796 *ppszName = pThis->Type.Object.papszNames[pIt->idxCur];
1797
1798 RTJsonValueRetain(pThis->Type.Object.papValues[pIt->idxCur]);
1799 *phJsonVal = pThis->Type.Object.papValues[pIt->idxCur];
1800 }
1801 else
1802 rc = VERR_JSON_ITERATOR_END;
1803 }
1804
1805 return rc;
1806}
1807
1808RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt)
1809{
1810 PRTJSONITINT pIt = hJsonIt;
1811 AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
1812 AssertReturn(pIt != NIL_RTJSONIT, VERR_INVALID_HANDLE);
1813
1814 int rc = VINF_SUCCESS;
1815 PRTJSONVALINT pThis = pIt->pJsonVal;
1816 if (pThis->enmType == RTJSONVALTYPE_ARRAY)
1817 {
1818 if (pIt->idxCur < pThis->Type.Array.cItems)
1819 pIt->idxCur++;
1820
1821 if (pIt->idxCur == pThis->Type.Object.cMembers)
1822 rc = VERR_JSON_ITERATOR_END;
1823 }
1824 else
1825 {
1826 Assert(pThis->enmType == RTJSONVALTYPE_OBJECT);
1827
1828 if (pIt->idxCur < pThis->Type.Object.cMembers)
1829 pIt->idxCur++;
1830
1831 if (pIt->idxCur == pThis->Type.Object.cMembers)
1832 rc = VERR_JSON_ITERATOR_END;
1833 }
1834
1835 return rc;
1836}
1837
1838RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt)
1839{
1840 PRTJSONITINT pThis = hJsonIt;
1841 AssertPtrReturnVoid(pThis);
1842
1843 if (pThis == NIL_RTJSONIT)
1844 return;
1845
1846 RTJsonValueRelease(pThis->pJsonVal);
1847 RTMemTmpFree(pThis);
1848}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette