VirtualBox

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

Last change on this file since 78052 was 77700, checked in by vboxsync, 6 years ago

Runtime/json: Plug small memory leak when parsing a JSON file fails

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 62.0 KB
Line 
1/* $Id: json.cpp 77700 2019-03-14 00:41:50Z 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 * Cleans up any resources still in control of the given token.
937 *
938 * @returns nothing.
939 * @param pToken The toke nto clean up.
940 */
941static void rtJsonTokenizerTokenCleanup(PRTJSONTOKEN pToken)
942{
943 if ( pToken->enmClass == RTJSONTOKENCLASS_STRING
944 && pToken->Class.String.pszStr)
945 RTStrFree(pToken->Class.String.pszStr);
946}
947
948/**
949 * Destroys a given tokenizer state.
950 *
951 * @returns nothing.
952 * @param pTokenizer The tokenizer to destroy.
953 */
954static void rtJsonTokenizerDestroy(PRTJSONTOKENIZER pTokenizer)
955{
956 rtJsonTokenizerTokenCleanup(pTokenizer->pTokenCurr);
957 rtJsonTokenizerTokenCleanup(pTokenizer->pTokenNext);
958}
959
960/**
961 * Get the current token in the input stream.
962 *
963 * @returns Pointer to the next token in the stream.
964 * @param pTokenizer The tokenizer state.
965 * @param ppToken Where to store the pointer to the current token on success.
966 */
967DECLINLINE(int) rtJsonTokenizerGetToken(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN *ppToken)
968{
969 if (RT_SUCCESS(pTokenizer->rcTok))
970 {
971 *ppToken = pTokenizer->pTokenCurr;
972 return VINF_SUCCESS;
973 }
974
975 return pTokenizer->rcTok;
976}
977
978/**
979 * Consume the current token advancing to the next in the stream.
980 *
981 * @returns nothing.
982 * @param pTokenizer The tokenizer state.
983 */
984static void rtJsonTokenizerConsume(PRTJSONTOKENIZER pTokenizer)
985{
986 PRTJSONTOKEN pTokenTmp = pTokenizer->pTokenCurr;
987
988 /* Switch next token to current token and read in the next token. */
989 pTokenizer->pTokenCurr = pTokenizer->pTokenNext;
990 pTokenizer->pTokenNext = pTokenTmp;
991 rtJsonTokenizerReadNextToken(pTokenizer, pTokenizer->pTokenNext);
992}
993
994/**
995 * Consumes the current token if it matches the given class returning an indicator.
996 *
997 * @returns true if the class matched and the token was consumed.
998 * @retval false otherwise.
999 * @param pTokenizer The tokenizer state.
1000 * @param enmClass The token class to match against.
1001 */
1002static bool rtJsonTokenizerConsumeIfMatched(PRTJSONTOKENIZER pTokenizer, RTJSONTOKENCLASS enmClass)
1003{
1004 PRTJSONTOKEN pToken = NULL;
1005 int rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1006 if (RT_SUCCESS(rc))
1007 {
1008 if (pToken->enmClass == enmClass)
1009 {
1010 rtJsonTokenizerConsume(pTokenizer);
1011 return true;
1012 }
1013 }
1014
1015 return false;
1016}
1017
1018/**
1019 * Destroys a given JSON value releasing the reference to all child values.
1020 *
1021 * @returns nothing.
1022 * @param pThis The JSON value to destroy.
1023 */
1024static void rtJsonValDestroy(PRTJSONVALINT pThis)
1025{
1026 switch (pThis->enmType)
1027 {
1028 case RTJSONVALTYPE_OBJECT:
1029 for (unsigned i = 0; i < pThis->Type.Object.cMembers; i++)
1030 {
1031 RTStrFree(pThis->Type.Object.papszNames[i]);
1032 RTJsonValueRelease(pThis->Type.Object.papValues[i]);
1033 }
1034 RTMemFree(pThis->Type.Object.papszNames);
1035 RTMemFree(pThis->Type.Object.papValues);
1036 break;
1037 case RTJSONVALTYPE_ARRAY:
1038 for (unsigned i = 0; i < pThis->Type.Array.cItems; i++)
1039 RTJsonValueRelease(pThis->Type.Array.papItems[i]);
1040 RTMemFree(pThis->Type.Array.papItems);
1041 break;
1042 case RTJSONVALTYPE_STRING:
1043 RTStrFree(pThis->Type.String.pszStr);
1044 break;
1045 case RTJSONVALTYPE_INTEGER:
1046 case RTJSONVALTYPE_NUMBER:
1047 case RTJSONVALTYPE_NULL:
1048 case RTJSONVALTYPE_TRUE:
1049 case RTJSONVALTYPE_FALSE:
1050 /* nothing to do. */
1051 break;
1052 default:
1053 AssertMsgFailed(("Invalid JSON value type: %u\n", pThis->enmType));
1054 }
1055 RTMemFree(pThis);
1056}
1057
1058/**
1059 * Creates a new JSON value with the given type.
1060 *
1061 * @returns Pointer to JSON value on success, NULL if out of memory.
1062 * @param enmType The JSON value type.
1063 */
1064static PRTJSONVALINT rtJsonValueCreate(RTJSONVALTYPE enmType)
1065{
1066 PRTJSONVALINT pThis = (PRTJSONVALINT)RTMemAllocZ(sizeof(RTJSONVALINT));
1067 if (RT_LIKELY(pThis))
1068 {
1069 pThis->enmType = enmType;
1070 pThis->cRefs = 1;
1071 }
1072
1073 return pThis;
1074}
1075
1076/**
1077 * Parses an JSON array.
1078 *
1079 * @returns IPRT status code.
1080 * @param pTokenizer The tokenizer to use.
1081 * @param pJsonVal The JSON array value to fill in.
1082 */
1083static int rtJsonParseArray(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal)
1084{
1085 int rc = VINF_SUCCESS;
1086 PRTJSONTOKEN pToken = NULL;
1087 uint32_t cItems = 0;
1088 uint32_t cItemsMax = 0;
1089 PRTJSONVALINT *papItems = NULL;
1090
1091 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1092 while ( RT_SUCCESS(rc)
1093 && pToken->enmClass != RTJSONTOKENCLASS_END_ARRAY
1094 && pToken->enmClass != RTJSONTOKENCLASS_EOS)
1095 {
1096 PRTJSONVALINT pVal = NULL;
1097 rc = rtJsonParseValue(pTokenizer, pToken, &pVal);
1098 if (RT_SUCCESS(rc))
1099 {
1100 if (cItems == cItemsMax)
1101 {
1102 cItemsMax += 10;
1103 PRTJSONVALINT *papItemsNew = (PRTJSONVALINT *)RTMemRealloc(papItems, cItemsMax * sizeof(PRTJSONVALINT));
1104 if (RT_UNLIKELY(!papItemsNew))
1105 {
1106 rc = VERR_NO_MEMORY;
1107 break;
1108 }
1109 papItems = papItemsNew;
1110 }
1111
1112 Assert(cItems < cItemsMax);
1113 papItems[cItems] = pVal;
1114 cItems++;
1115 }
1116
1117 /* Skip value separator and continue with next token. */
1118 bool fSkippedSep = rtJsonTokenizerConsumeIfMatched(pTokenizer, RTJSONTOKENCLASS_VALUE_SEPARATOR);
1119 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1120
1121 if ( RT_SUCCESS(rc)
1122 && !fSkippedSep
1123 && pToken->enmClass != RTJSONTOKENCLASS_END_ARRAY)
1124 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of array (#1) (line %zu col %zu)",
1125 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1126 }
1127
1128 if (RT_SUCCESS(rc))
1129 {
1130 if (pToken->enmClass == RTJSONTOKENCLASS_END_ARRAY)
1131 {
1132 rtJsonTokenizerConsume(pTokenizer);
1133 pJsonVal->Type.Array.cItems = cItems;
1134 pJsonVal->Type.Array.papItems = papItems;
1135 }
1136 else
1137 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of array (#2) (line %zu col %zu)",
1138 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1139 }
1140
1141 if (RT_FAILURE(rc))
1142 {
1143 for (uint32_t i = 0; i < cItems; i++)
1144 RTJsonValueRelease(papItems[i]);
1145 RTMemFree(papItems);
1146 }
1147
1148 return rc;
1149}
1150
1151/**
1152 * Parses an JSON object.
1153 *
1154 * @returns IPRT status code.
1155 * @param pTokenizer The tokenizer to use.
1156 * @param pJsonVal The JSON object value to fill in.
1157 */
1158static int rtJsonParseObject(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT pJsonVal)
1159{
1160 int rc = VINF_SUCCESS;
1161 PRTJSONTOKEN pToken = NULL;
1162 uint32_t cMembers = 0;
1163 uint32_t cMembersMax = 0;
1164 PRTJSONVALINT *papValues = NULL;
1165 char **papszNames = NULL;
1166
1167 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1168 while ( RT_SUCCESS(rc)
1169 && pToken->enmClass == RTJSONTOKENCLASS_STRING)
1170 {
1171 char *pszName = pToken->Class.String.pszStr; /* We can consume this string as it was allocated. */
1172 pToken->Class.String.pszStr = NULL;
1173
1174 rtJsonTokenizerConsume(pTokenizer);
1175 if (rtJsonTokenizerConsumeIfMatched(pTokenizer, RTJSONTOKENCLASS_NAME_SEPARATOR))
1176 {
1177 PRTJSONVALINT pVal = NULL;
1178 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1179 if (RT_SUCCESS(rc))
1180 rc = rtJsonParseValue(pTokenizer, pToken, &pVal);
1181 if (RT_SUCCESS(rc))
1182 {
1183 if (cMembers == cMembersMax)
1184 {
1185 cMembersMax += 10;
1186 PRTJSONVALINT *papValuesNew = (PRTJSONVALINT *)RTMemRealloc(papValues, cMembersMax * sizeof(PRTJSONVALINT));
1187 char **papszNamesNew = (char **)RTMemRealloc(papszNames, cMembersMax * sizeof(char *));
1188 if (RT_UNLIKELY(!papValuesNew || !papszNamesNew))
1189 {
1190 if (papValuesNew)
1191 RTMemFree(papValuesNew);
1192 if (papszNamesNew)
1193 RTMemFree(papszNamesNew);
1194 RTStrFree(pszName);
1195 rc = VERR_NO_MEMORY;
1196 break;
1197 }
1198
1199 papValues = papValuesNew;
1200 papszNames = papszNamesNew;
1201 }
1202
1203 Assert(cMembers < cMembersMax);
1204 papszNames[cMembers] = pszName;
1205 papValues[cMembers] = pVal;
1206 cMembers++;
1207
1208 /* Skip value separator and continue with next token. */
1209 bool fSkippedSep = rtJsonTokenizerConsumeIfMatched(pTokenizer, RTJSONTOKENCLASS_VALUE_SEPARATOR);
1210 rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1211
1212 if ( RT_SUCCESS(rc)
1213 && !fSkippedSep
1214 && pToken->enmClass != RTJSONTOKENCLASS_END_OBJECT)
1215 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#1) (line %zu col %zu)",
1216 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1217 }
1218 else
1219 RTStrFree(pszName);
1220 }
1221 else
1222 {
1223 RTStrFree(pszName);
1224 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected name separator (line %zu col %zu)",
1225 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1226 }
1227 }
1228
1229 if (RT_SUCCESS(rc))
1230 {
1231 if (pToken->enmClass == RTJSONTOKENCLASS_END_OBJECT)
1232 {
1233 rtJsonTokenizerConsume(pTokenizer);
1234 pJsonVal->Type.Object.cMembers = cMembers;
1235 pJsonVal->Type.Object.papValues = papValues;
1236 pJsonVal->Type.Object.papszNames = papszNames;
1237 }
1238 else
1239 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#2) (line %zu col %zu)",
1240 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1241 }
1242
1243 if (RT_FAILURE(rc))
1244 {
1245 for (uint32_t i = 0; i < cMembers; i++)
1246 {
1247 RTJsonValueRelease(papValues[i]);
1248 RTStrFree(papszNames[i]);
1249 }
1250 RTMemFree(papValues);
1251 RTMemFree(papszNames);
1252 }
1253
1254 return rc;
1255}
1256
1257/**
1258 * Parses a single JSON value and returns it on success.
1259 *
1260 * @returns IPRT status code.
1261 * @param pTokenizer The tokenizer to use.
1262 * @param pToken The token to parse.
1263 * @param ppJsonVal Where to store the pointer to the JSON value on success.
1264 */
1265static int rtJsonParseValue(PRTJSONTOKENIZER pTokenizer, PRTJSONTOKEN pToken, PRTJSONVALINT *ppJsonVal)
1266{
1267 int rc = VINF_SUCCESS;
1268 PRTJSONVALINT pVal = NULL;
1269
1270 switch (pToken->enmClass)
1271 {
1272 case RTJSONTOKENCLASS_BEGIN_ARRAY:
1273 rtJsonTokenizerConsume(pTokenizer);
1274 pVal = rtJsonValueCreate(RTJSONVALTYPE_ARRAY);
1275 if (RT_LIKELY(pVal))
1276 rc = rtJsonParseArray(pTokenizer, pVal);
1277 break;
1278 case RTJSONTOKENCLASS_BEGIN_OBJECT:
1279 rtJsonTokenizerConsume(pTokenizer);
1280 pVal = rtJsonValueCreate(RTJSONVALTYPE_OBJECT);
1281 if (RT_LIKELY(pVal))
1282 rc = rtJsonParseObject(pTokenizer, pVal);
1283 break;
1284 case RTJSONTOKENCLASS_STRING:
1285 pVal = rtJsonValueCreate(RTJSONVALTYPE_STRING);
1286 if (RT_LIKELY(pVal))
1287 pVal->Type.String.pszStr = pToken->Class.String.pszStr;
1288 rtJsonTokenizerConsume(pTokenizer);
1289 break;
1290 case RTJSONTOKENCLASS_INTEGER:
1291 pVal = rtJsonValueCreate(RTJSONVALTYPE_INTEGER);
1292 if (RT_LIKELY(pVal))
1293 pVal->Type.Integer.i64Num = pToken->Class.Integer.i64Num;
1294 rtJsonTokenizerConsume(pTokenizer);
1295 break;
1296 case RTJSONTOKENCLASS_NUMBER:
1297 pVal = rtJsonValueCreate(RTJSONVALTYPE_NUMBER);
1298 if (RT_LIKELY(pVal))
1299 pVal->Type.rdNum = pToken->Class.rdNum;
1300 rtJsonTokenizerConsume(pTokenizer);
1301 break;
1302 case RTJSONTOKENCLASS_NULL:
1303 rtJsonTokenizerConsume(pTokenizer);
1304 pVal = rtJsonValueCreate(RTJSONVALTYPE_NULL);
1305 break;
1306 case RTJSONTOKENCLASS_FALSE:
1307 rtJsonTokenizerConsume(pTokenizer);
1308 pVal = rtJsonValueCreate(RTJSONVALTYPE_FALSE);
1309 break;
1310 case RTJSONTOKENCLASS_TRUE:
1311 rtJsonTokenizerConsume(pTokenizer);
1312 pVal = rtJsonValueCreate(RTJSONVALTYPE_TRUE);
1313 break;
1314
1315 case RTJSONTOKENCLASS_INVALID:
1316 Assert(!pTokenizer->pErrInfo || RTErrInfoIsSet(pTokenizer->pErrInfo));
1317 rc = VERR_JSON_MALFORMED;
1318 break;
1319 case RTJSONTOKENCLASS_END_ARRAY:
1320 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected '}' (line %zu col %zu)",
1321 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1322 break;
1323 case RTJSONTOKENCLASS_END_OBJECT:
1324 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ']' (line %zu col %zu)",
1325 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1326 break;
1327 case RTJSONTOKENCLASS_NAME_SEPARATOR:
1328 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ':' (line %zu col %zu)",
1329 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1330 break;
1331 case RTJSONTOKENCLASS_VALUE_SEPARATOR:
1332 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "unexpected ',' (line %zu col %zu)",
1333 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1334 break;
1335 case RTJSONTOKENCLASS_EOS:
1336 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "expected end of object (#1) (line %zu col %zu)",
1337 pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1338 break;
1339 default:
1340 rc = RTErrInfoSetF(pTokenizer->pErrInfo, VERR_JSON_MALFORMED, "Unexpected token class %d (line %zu col %zu)",
1341 pToken->enmClass, pTokenizer->Pos.iLine, pTokenizer->Pos.iChStart);
1342 break;
1343 }
1344
1345 if (RT_SUCCESS(rc))
1346 {
1347 if (pVal)
1348 *ppJsonVal = pVal;
1349 else
1350 rc = VERR_NO_MEMORY;
1351 }
1352 else if (pVal)
1353 rtJsonValDestroy(pVal);
1354
1355 return rc;
1356}
1357
1358/**
1359 * Entry point to parse a JSON document.
1360 *
1361 * @returns IPRT status code.
1362 * @param pTokenizer The tokenizer state.
1363 * @param ppJsonVal Where to store the root JSON value on success.
1364 */
1365static int rtJsonParse(PRTJSONTOKENIZER pTokenizer, PRTJSONVALINT *ppJsonVal)
1366{
1367 PRTJSONTOKEN pToken = NULL;
1368 int rc = rtJsonTokenizerGetToken(pTokenizer, &pToken);
1369 if (RT_SUCCESS(rc))
1370 rc = rtJsonParseValue(pTokenizer, pToken, ppJsonVal);
1371
1372 return rc;
1373}
1374
1375/**
1376 * Read callback for RTJsonParseFromBuf().
1377 */
1378static DECLCALLBACK(int) rtJsonTokenizerParseFromBuf(void *pvUser, size_t offInput,
1379 void *pvBuf, size_t cbBuf,
1380 size_t *pcbRead)
1381{
1382 PRTJSONREADERARGS pArgs = (PRTJSONREADERARGS)pvUser;
1383 size_t cbLeft = offInput < pArgs->cbData ? pArgs->cbData - offInput : 0;
1384
1385 if (cbLeft)
1386 memcpy(pvBuf, &pArgs->u.pbBuf[offInput], RT_MIN(cbLeft, cbBuf));
1387
1388 *pcbRead = RT_MIN(cbLeft, cbBuf);
1389
1390 return VINF_SUCCESS;
1391}
1392
1393/**
1394 * Read callback for RTJsonParseFromString().
1395 */
1396static DECLCALLBACK(int) rtJsonTokenizerParseFromString(void *pvUser, size_t offInput,
1397 void *pvBuf, size_t cbBuf,
1398 size_t *pcbRead)
1399{
1400 const char *pszStr = (const char *)pvUser;
1401 size_t cchStr = strlen(pszStr) + 1; /* Include zero terminator. */
1402 size_t cbLeft = offInput < cchStr ? cchStr - offInput : 0;
1403
1404 if (cbLeft)
1405 memcpy(pvBuf, &pszStr[offInput], RT_MIN(cbLeft, cbBuf));
1406
1407 *pcbRead = RT_MIN(cbLeft, cbBuf);
1408
1409 return VINF_SUCCESS;
1410}
1411
1412/**
1413 * Read callback for RTJsonParseFromFile().
1414 */
1415static DECLCALLBACK(int) rtJsonTokenizerParseFromFile(void *pvUser, size_t offInput,
1416 void *pvBuf, size_t cbBuf,
1417 size_t *pcbRead)
1418{
1419 PRTJSONREADERARGS pArgs = (PRTJSONREADERARGS)pvUser;
1420
1421 RT_NOREF_PV(offInput);
1422
1423 size_t cbRead = 0;
1424 int rc = RTStrmReadEx(pArgs->u.hStream, pvBuf, cbBuf, &cbRead);
1425 if (RT_SUCCESS(rc))
1426 *pcbRead = cbRead;
1427
1428 return rc;
1429}
1430
1431RTDECL(int) RTJsonParseFromBuf(PRTJSONVAL phJsonVal, const uint8_t *pbBuf, size_t cbBuf, PRTERRINFO pErrInfo)
1432{
1433 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1434 AssertPtrReturn(pbBuf, VERR_INVALID_POINTER);
1435 AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
1436
1437 RTJSONTOKENIZER Tokenizer;
1438 RTJSONREADERARGS Args;
1439 Args.cbData = cbBuf;
1440 Args.u.pbBuf = pbBuf;
1441
1442 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromBuf, &Args, pErrInfo);
1443 if (RT_SUCCESS(rc))
1444 {
1445 rc = rtJsonParse(&Tokenizer, phJsonVal);
1446 rtJsonTokenizerDestroy(&Tokenizer);
1447 }
1448
1449 return rc;
1450}
1451
1452RTDECL(int) RTJsonParseFromString(PRTJSONVAL phJsonVal, const char *pszStr, PRTERRINFO pErrInfo)
1453{
1454 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1455 AssertPtrReturn(pszStr, VERR_INVALID_POINTER);
1456
1457 /** @todo r=bird: The rtJsonTokenizerParseFromString function does
1458 * strlen() on the whole pszStr for each read. For larger strings (
1459 * longer than sizeof(Tokenizer.achBuf)) it would be good to join
1460 * forces with RTJsonParseFromBuf. */
1461 RTJSONTOKENIZER Tokenizer;
1462 int rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromString, (void *)pszStr, pErrInfo);
1463 if (RT_SUCCESS(rc))
1464 {
1465 rc = rtJsonParse(&Tokenizer, phJsonVal);
1466 rtJsonTokenizerDestroy(&Tokenizer);
1467 }
1468
1469 return rc;
1470}
1471
1472RTDECL(int) RTJsonParseFromFile(PRTJSONVAL phJsonVal, const char *pszFilename, PRTERRINFO pErrInfo)
1473{
1474 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1475 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1476
1477 int rc = VINF_SUCCESS;
1478 RTJSONREADERARGS Args;
1479
1480 Args.cbData = 0;
1481 rc = RTStrmOpen(pszFilename, "r", &Args.u.hStream);
1482 if (RT_SUCCESS(rc))
1483 {
1484 RTJSONTOKENIZER Tokenizer;
1485
1486 rc = rtJsonTokenizerInit(&Tokenizer, rtJsonTokenizerParseFromFile, &Args, pErrInfo);
1487 if (RT_SUCCESS(rc))
1488 {
1489 rc = rtJsonParse(&Tokenizer, phJsonVal);
1490 rtJsonTokenizerDestroy(&Tokenizer);
1491 }
1492 RTStrmClose(Args.u.hStream);
1493 }
1494
1495 return rc;
1496}
1497
1498RTDECL(uint32_t) RTJsonValueRetain(RTJSONVAL hJsonVal)
1499{
1500 PRTJSONVALINT pThis = hJsonVal;
1501 AssertPtrReturn(pThis, UINT32_MAX);
1502
1503 uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
1504 AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
1505 return cRefs;
1506}
1507
1508RTDECL(uint32_t) RTJsonValueRelease(RTJSONVAL hJsonVal)
1509{
1510 PRTJSONVALINT pThis = hJsonVal;
1511 if (pThis == NIL_RTJSONVAL)
1512 return 0;
1513 AssertPtrReturn(pThis, UINT32_MAX);
1514
1515 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
1516 AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
1517 if (cRefs == 0)
1518 rtJsonValDestroy(pThis);
1519 return cRefs;
1520}
1521
1522RTDECL(RTJSONVALTYPE) RTJsonValueGetType(RTJSONVAL hJsonVal)
1523{
1524 PRTJSONVALINT pThis = hJsonVal;
1525 AssertPtrReturn(pThis, RTJSONVALTYPE_INVALID);
1526
1527 if (pThis == NIL_RTJSONVAL)
1528 return RTJSONVALTYPE_INVALID;
1529
1530 return pThis->enmType;
1531}
1532
1533
1534RTDECL(const char *) RTJsonValueTypeName(RTJSONVALTYPE enmType)
1535{
1536 switch (enmType)
1537 {
1538 case RTJSONVALTYPE_INVALID: return "invalid";
1539 case RTJSONVALTYPE_OBJECT: return "object";
1540 case RTJSONVALTYPE_ARRAY: return "array";
1541 case RTJSONVALTYPE_STRING: return "string";
1542 case RTJSONVALTYPE_INTEGER: return "integer";
1543 case RTJSONVALTYPE_NUMBER: return "number";
1544 case RTJSONVALTYPE_NULL: return "null";
1545 case RTJSONVALTYPE_TRUE: return "true";
1546 case RTJSONVALTYPE_FALSE: return "false";
1547 default: return "???";
1548 }
1549}
1550
1551
1552#define RTJSON_ASSERT_VALID_HANDLE_AND_TYPE_RETURN(pJson, enmExpectedType, ret) do { \
1553 AssertPtrReturn((pJson), (ret)); \
1554 AssertReturn((pJson) != NIL_RTJSONVAL, (ret)); \
1555 AssertReturn((pJson)->enmType == (enmExpectedType), (ret)); \
1556 } while (0)
1557
1558#define RTJSON_TYPECHECK_RETURN(pJson, enmExpectedType) do {\
1559 if ((pJson)->enmType == (enmExpectedType)) { /*likely*/ } \
1560 else { /*AssertFailed();*/ return VERR_JSON_VALUE_INVALID_TYPE; } \
1561 } while (0)
1562
1563
1564#define RTJSON_TYPECHECK_CONTAINER_RETURN(pJson) do { \
1565 if ( (pJson)->enmType == RTJSONVALTYPE_ARRAY \
1566 || (pJson)->enmType == RTJSONVALTYPE_OBJECT) \
1567 { /* likely */ } \
1568 else { /*AssertFailed();*/ return VERR_JSON_VALUE_INVALID_TYPE;} \
1569 } while (0)
1570
1571
1572RTDECL(const char *) RTJsonValueGetString(RTJSONVAL hJsonVal)
1573{
1574 PRTJSONVALINT pThis = hJsonVal;
1575 RTJSON_ASSERT_VALID_HANDLE_AND_TYPE_RETURN(pThis, RTJSONVALTYPE_STRING, NULL);
1576
1577 return pThis->Type.String.pszStr;
1578}
1579
1580
1581RTDECL(int) RTJsonValueQueryString(RTJSONVAL hJsonVal, const char **ppszStr)
1582{
1583 PRTJSONVALINT pThis = hJsonVal;
1584 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1585 AssertPtrReturn(ppszStr, VERR_INVALID_POINTER);
1586
1587 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_STRING);
1588 *ppszStr = pThis->Type.String.pszStr;
1589 return VINF_SUCCESS;
1590}
1591
1592RTDECL(int) RTJsonValueQueryInteger(RTJSONVAL hJsonVal, int64_t *pi64Num)
1593{
1594 PRTJSONVALINT pThis = hJsonVal;
1595 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1596 AssertPtrReturn(pi64Num, VERR_INVALID_POINTER);
1597
1598 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_INTEGER);
1599 *pi64Num = pThis->Type.Integer.i64Num;
1600 return VINF_SUCCESS;
1601}
1602
1603RTDECL(int) RTJsonValueQueryNumber(RTJSONVAL hJsonVal, double *prdNum)
1604{
1605 PRTJSONVALINT pThis = hJsonVal;
1606 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1607 AssertPtrReturn(prdNum, VERR_INVALID_POINTER);
1608
1609 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_NUMBER);
1610 *prdNum = pThis->Type.rdNum;
1611 return VINF_SUCCESS;
1612}
1613
1614RTDECL(int) RTJsonValueQueryByName(RTJSONVAL hJsonVal, const char *pszName, PRTJSONVAL phJsonVal)
1615{
1616 PRTJSONVALINT pThis = hJsonVal;
1617 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1618 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
1619 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1620
1621 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_OBJECT);
1622
1623 int rc = VERR_NOT_FOUND;
1624 for (unsigned i = 0; i < pThis->Type.Object.cMembers; i++)
1625 {
1626 if (!RTStrCmp(pThis->Type.Object.papszNames[i], pszName))
1627 {
1628 RTJsonValueRetain(pThis->Type.Object.papValues[i]);
1629 *phJsonVal = pThis->Type.Object.papValues[i];
1630 rc = VINF_SUCCESS;
1631 break;
1632 }
1633 }
1634
1635 return rc;
1636}
1637
1638RTDECL(int) RTJsonValueQueryIntegerByName(RTJSONVAL hJsonVal, const char *pszName, int64_t *pi64Num)
1639{
1640 RTJSONVAL hJsonValNum = NIL_RTJSONVAL;
1641 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValNum);
1642 if (RT_SUCCESS(rc))
1643 {
1644 rc = RTJsonValueQueryInteger(hJsonValNum, pi64Num);
1645 RTJsonValueRelease(hJsonValNum);
1646 }
1647
1648 return rc;
1649}
1650
1651RTDECL(int) RTJsonValueQueryNumberByName(RTJSONVAL hJsonVal, const char *pszName, double *prdNum)
1652{
1653 RTJSONVAL hJsonValNum = NIL_RTJSONVAL;
1654 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValNum);
1655 if (RT_SUCCESS(rc))
1656 {
1657 rc = RTJsonValueQueryNumber(hJsonValNum, prdNum);
1658 RTJsonValueRelease(hJsonValNum);
1659 }
1660
1661 return rc;
1662}
1663
1664RTDECL(int) RTJsonValueQueryStringByName(RTJSONVAL hJsonVal, const char *pszName, char **ppszStr)
1665{
1666 RTJSONVAL hJsonValStr = NIL_RTJSONVAL;
1667 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValStr);
1668 if (RT_SUCCESS(rc))
1669 {
1670 const char *pszStr = NULL;
1671 rc = RTJsonValueQueryString(hJsonValStr, &pszStr);
1672 if (RT_SUCCESS(rc))
1673 {
1674 *ppszStr = RTStrDup(pszStr);
1675 if (!*ppszStr)
1676 rc = VERR_NO_STR_MEMORY;
1677 }
1678 RTJsonValueRelease(hJsonValStr);
1679 }
1680
1681 return rc;
1682}
1683
1684RTDECL(int) RTJsonValueQueryBooleanByName(RTJSONVAL hJsonVal, const char *pszName, bool *pfBoolean)
1685{
1686 AssertPtrReturn(pfBoolean, VERR_INVALID_POINTER);
1687
1688 RTJSONVAL hJsonValBool = NIL_RTJSONVAL;
1689 int rc = RTJsonValueQueryByName(hJsonVal, pszName, &hJsonValBool);
1690 if (RT_SUCCESS(rc))
1691 {
1692 RTJSONVALTYPE enmType = RTJsonValueGetType(hJsonValBool);
1693 if (enmType == RTJSONVALTYPE_TRUE)
1694 *pfBoolean = true;
1695 else if (enmType == RTJSONVALTYPE_FALSE)
1696 *pfBoolean = false;
1697 else
1698 rc = VERR_JSON_VALUE_INVALID_TYPE;
1699 RTJsonValueRelease(hJsonValBool);
1700 }
1701
1702 return rc;
1703}
1704
1705RTDECL(unsigned) RTJsonValueGetArraySize(RTJSONVAL hJsonVal)
1706{
1707 PRTJSONVALINT pThis = hJsonVal;
1708 RTJSON_ASSERT_VALID_HANDLE_AND_TYPE_RETURN(pThis, RTJSONVALTYPE_ARRAY, 0);
1709
1710 return pThis->Type.Array.cItems;
1711}
1712
1713RTDECL(int) RTJsonValueQueryArraySize(RTJSONVAL hJsonVal, unsigned *pcItems)
1714{
1715 PRTJSONVALINT pThis = hJsonVal;
1716 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1717 AssertPtrReturn(pcItems, VERR_INVALID_POINTER);
1718
1719 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
1720 *pcItems = pThis->Type.Array.cItems;
1721 return VINF_SUCCESS;
1722}
1723
1724RTDECL(int) RTJsonValueQueryByIndex(RTJSONVAL hJsonVal, unsigned idx, PRTJSONVAL phJsonVal)
1725{
1726 PRTJSONVALINT pThis = hJsonVal;
1727 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1728 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1729
1730 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
1731 if (RT_UNLIKELY(idx >= pThis->Type.Array.cItems))
1732 return VERR_OUT_OF_RANGE;
1733
1734 RTJsonValueRetain(pThis->Type.Array.papItems[idx]);
1735 *phJsonVal = pThis->Type.Array.papItems[idx];
1736 return VINF_SUCCESS;
1737}
1738
1739static int rtJsonIteratorBeginWorker(PRTJSONVALINT pThis, PRTJSONIT phJsonIt)
1740{
1741 PRTJSONITINT pIt = (PRTJSONITINT)RTMemTmpAllocZ(sizeof(RTJSONITINT));
1742 if (pIt)
1743 {
1744 RTJsonValueRetain(pThis);
1745 pIt->pJsonVal = pThis;
1746 pIt->idxCur = 0;
1747
1748 *phJsonIt = pIt;
1749 return VINF_SUCCESS;
1750 }
1751 return VERR_NO_MEMORY;
1752}
1753
1754RTDECL(int) RTJsonIteratorBegin(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt)
1755{
1756 PRTJSONVALINT pThis = hJsonVal;
1757 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1758 AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
1759 RTJSON_TYPECHECK_CONTAINER_RETURN(pThis);
1760
1761 return rtJsonIteratorBeginWorker(pThis, phJsonIt);
1762}
1763
1764RTDECL(int) RTJsonIteratorBeginArray(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt)
1765{
1766 PRTJSONVALINT pThis = hJsonVal;
1767 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1768 AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
1769 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_ARRAY);
1770
1771 if (pThis->Type.Array.cItems > 0)
1772 return rtJsonIteratorBeginWorker(pThis, phJsonIt);
1773 return VERR_JSON_IS_EMPTY;
1774}
1775
1776RTDECL(int) RTJsonIteratorBeginObject(RTJSONVAL hJsonVal, PRTJSONIT phJsonIt)
1777{
1778 PRTJSONVALINT pThis = hJsonVal;
1779 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
1780 AssertPtrReturn(phJsonIt, VERR_INVALID_POINTER);
1781 RTJSON_TYPECHECK_RETURN(pThis, RTJSONVALTYPE_OBJECT);
1782
1783 if (pThis->Type.Object.cMembers > 0)
1784 return rtJsonIteratorBeginWorker(pThis, phJsonIt);
1785 return VERR_JSON_IS_EMPTY;
1786}
1787
1788RTDECL(int) RTJsonIteratorQueryValue(RTJSONIT hJsonIt, PRTJSONVAL phJsonVal, const char **ppszName)
1789{
1790 PRTJSONITINT pIt = hJsonIt;
1791 AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
1792 AssertReturn(pIt != NIL_RTJSONIT, VERR_INVALID_HANDLE);
1793 AssertPtrReturn(phJsonVal, VERR_INVALID_POINTER);
1794
1795 int rc = VINF_SUCCESS;
1796 PRTJSONVALINT pThis = pIt->pJsonVal;
1797 if (pThis->enmType == RTJSONVALTYPE_ARRAY)
1798 {
1799 if (pIt->idxCur < pThis->Type.Array.cItems)
1800 {
1801 if (ppszName)
1802 *ppszName = NULL;
1803
1804 RTJsonValueRetain(pThis->Type.Array.papItems[pIt->idxCur]);
1805 *phJsonVal = pThis->Type.Array.papItems[pIt->idxCur];
1806 }
1807 else
1808 rc = VERR_JSON_ITERATOR_END;
1809 }
1810 else
1811 {
1812 Assert(pThis->enmType == RTJSONVALTYPE_OBJECT);
1813
1814 if (pIt->idxCur < pThis->Type.Object.cMembers)
1815 {
1816 if (ppszName)
1817 *ppszName = pThis->Type.Object.papszNames[pIt->idxCur];
1818
1819 RTJsonValueRetain(pThis->Type.Object.papValues[pIt->idxCur]);
1820 *phJsonVal = pThis->Type.Object.papValues[pIt->idxCur];
1821 }
1822 else
1823 rc = VERR_JSON_ITERATOR_END;
1824 }
1825
1826 return rc;
1827}
1828
1829RTDECL(int) RTJsonIteratorNext(RTJSONIT hJsonIt)
1830{
1831 PRTJSONITINT pIt = hJsonIt;
1832 AssertPtrReturn(pIt, VERR_INVALID_HANDLE);
1833 AssertReturn(pIt != NIL_RTJSONIT, VERR_INVALID_HANDLE);
1834
1835 int rc = VINF_SUCCESS;
1836 PRTJSONVALINT pThis = pIt->pJsonVal;
1837 if (pThis->enmType == RTJSONVALTYPE_ARRAY)
1838 {
1839 if (pIt->idxCur < pThis->Type.Array.cItems)
1840 pIt->idxCur++;
1841
1842 if (pIt->idxCur == pThis->Type.Object.cMembers)
1843 rc = VERR_JSON_ITERATOR_END;
1844 }
1845 else
1846 {
1847 Assert(pThis->enmType == RTJSONVALTYPE_OBJECT);
1848
1849 if (pIt->idxCur < pThis->Type.Object.cMembers)
1850 pIt->idxCur++;
1851
1852 if (pIt->idxCur == pThis->Type.Object.cMembers)
1853 rc = VERR_JSON_ITERATOR_END;
1854 }
1855
1856 return rc;
1857}
1858
1859RTDECL(void) RTJsonIteratorFree(RTJSONIT hJsonIt)
1860{
1861 PRTJSONITINT pThis = hJsonIt;
1862 AssertPtrReturnVoid(pThis);
1863
1864 if (pThis == NIL_RTJSONIT)
1865 return;
1866
1867 RTJsonValueRelease(pThis->pJsonVal);
1868 RTMemTmpFree(pThis);
1869}
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