json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
/* json_dumper.c
|
2018-11-20 02:47:36 +01:00
|
|
|
* Routines for serializing data as JSON.
|
|
|
|
*
|
|
|
|
* Copyright 2018, Peter Wu <peter@lekensteyn.nl>
|
2018-12-14 00:21:51 +01:00
|
|
|
* Copyright (C) 2016 Jakub Zawadzki
|
2018-11-20 02:47:36 +01:00
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
2024-04-18 09:54:46 -07:00
|
|
|
#include "config.h"
|
|
|
|
#define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
#include <glib.h>
|
|
|
|
|
2018-11-20 02:47:36 +01:00
|
|
|
#include "json_dumper.h"
|
2019-02-09 19:20:28 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2024-05-20 20:08:45 +10:00
|
|
|
#include <wsutil/array.h>
|
2021-06-15 00:06:02 +01:00
|
|
|
#include <wsutil/wslog.h>
|
|
|
|
|
2018-11-20 02:47:36 +01:00
|
|
|
/*
|
|
|
|
* json_dumper.state[current_depth] describes a nested element:
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
* - type: none/object/array/non-base64 value/base64 value
|
2018-11-20 02:47:36 +01:00
|
|
|
* - has_name: Whether the object member name was set.
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
*
|
|
|
|
* (A base64 value isn't really a nested element, but that's a
|
|
|
|
* convenient way of handling them, with a begin call that opens
|
|
|
|
* the string with a double-quote, one or more calls to convert
|
|
|
|
* raw bytes to base64 and add them to the value, and an end call
|
|
|
|
* that finishes the base64 encoding, adds any remaining raw bytes
|
|
|
|
* in base64 encoding, and closes the string with a double-quote.)
|
2018-11-20 02:47:36 +01:00
|
|
|
*/
|
|
|
|
enum json_dumper_element_type {
|
|
|
|
JSON_DUMPER_TYPE_NONE = 0,
|
|
|
|
JSON_DUMPER_TYPE_VALUE = 1,
|
|
|
|
JSON_DUMPER_TYPE_OBJECT = 2,
|
|
|
|
JSON_DUMPER_TYPE_ARRAY = 3,
|
2018-12-13 15:39:21 +01:00
|
|
|
JSON_DUMPER_TYPE_BASE64 = 4,
|
2018-11-20 02:47:36 +01:00
|
|
|
};
|
2018-12-13 15:39:21 +01:00
|
|
|
#define JSON_DUMPER_TYPE(state) ((enum json_dumper_element_type)((state) & 7))
|
|
|
|
#define JSON_DUMPER_HAS_NAME (1 << 3)
|
2018-11-20 02:47:36 +01:00
|
|
|
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
static const char *json_dumper_element_type_names[] = {
|
|
|
|
[JSON_DUMPER_TYPE_NONE] = "none",
|
|
|
|
[JSON_DUMPER_TYPE_VALUE] = "value",
|
|
|
|
[JSON_DUMPER_TYPE_OBJECT] = "object",
|
|
|
|
[JSON_DUMPER_TYPE_ARRAY] = "array",
|
|
|
|
[JSON_DUMPER_TYPE_BASE64] = "base64"
|
|
|
|
};
|
2024-05-20 20:08:45 +10:00
|
|
|
#define NUM_JSON_DUMPER_ELEMENT_TYPE_NAMES array_length(json_dumper_element_type_names)
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
|
2018-11-20 02:47:36 +01:00
|
|
|
#define JSON_DUMPER_FLAGS_ERROR (1 << 16) /* Output flag: an error occurred. */
|
|
|
|
|
|
|
|
enum json_dumper_change {
|
|
|
|
JSON_DUMPER_BEGIN,
|
|
|
|
JSON_DUMPER_END,
|
|
|
|
JSON_DUMPER_SET_NAME,
|
|
|
|
JSON_DUMPER_SET_VALUE,
|
2018-12-13 15:39:21 +01:00
|
|
|
JSON_DUMPER_WRITE_BASE64,
|
2018-11-20 02:47:36 +01:00
|
|
|
JSON_DUMPER_FINISH,
|
|
|
|
};
|
|
|
|
|
2022-01-23 11:48:24 +08:00
|
|
|
/* JSON Dumper putc */
|
2018-11-20 02:47:36 +01:00
|
|
|
static void
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(const json_dumper *dumper, char c)
|
|
|
|
{
|
|
|
|
if (dumper->output_file) {
|
|
|
|
fputc(c, dumper->output_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dumper->output_string) {
|
|
|
|
g_string_append_c(dumper->output_string, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* JSON Dumper puts */
|
|
|
|
static void
|
|
|
|
jd_puts(const json_dumper *dumper, const char *s)
|
|
|
|
{
|
|
|
|
if (dumper->output_file) {
|
|
|
|
fputs(s, dumper->output_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dumper->output_string) {
|
|
|
|
g_string_append(dumper->output_string, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-09-22 13:42:49 -07:00
|
|
|
jd_puts_len(const json_dumper *dumper, const char *s, size_t len)
|
2022-01-23 11:48:24 +08:00
|
|
|
{
|
|
|
|
if (dumper->output_file) {
|
|
|
|
fwrite(s, 1, len, dumper->output_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dumper->output_string) {
|
|
|
|
g_string_append_len(dumper->output_string, s, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
jd_vprintf(const json_dumper *dumper, const char *format, va_list args)
|
|
|
|
{
|
|
|
|
if (dumper->output_file) {
|
|
|
|
vfprintf(dumper->output_file, format, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dumper->output_string) {
|
|
|
|
g_string_append_vprintf(dumper->output_string, format, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-09-22 13:42:49 -07:00
|
|
|
json_puts_string(const json_dumper *dumper, const char *str, bool dot_to_underscore)
|
2018-11-20 02:47:36 +01:00
|
|
|
{
|
2018-12-08 23:54:57 +01:00
|
|
|
if (!str) {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_puts(dumper, "null");
|
2018-12-08 23:54:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:47:36 +01:00
|
|
|
static const char json_cntrl[0x20][6] = {
|
|
|
|
"u0000", "u0001", "u0002", "u0003", "u0004", "u0005", "u0006", "u0007", "b", "t", "n", "u000b", "f", "r", "u000e", "u000f",
|
|
|
|
"u0010", "u0011", "u0012", "u0013", "u0014", "u0015", "u0016", "u0017", "u0018", "u0019", "u001a", "u001b", "u001c", "u001d", "u001e", "u001f"
|
|
|
|
};
|
|
|
|
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, '"');
|
2018-11-20 02:47:36 +01:00
|
|
|
for (int i = 0; str[i]; i++) {
|
2023-09-22 13:42:49 -07:00
|
|
|
if ((unsigned)str[i] < 0x20) {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, '\\');
|
2023-09-22 13:42:49 -07:00
|
|
|
jd_puts(dumper, json_cntrl[(unsigned)str[i]]);
|
2019-02-10 00:34:32 +01:00
|
|
|
} else if (i > 0 && str[i - 1] == '<' && str[i] == '/') {
|
|
|
|
// Convert </script> to <\/script> to avoid breaking web pages.
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_puts(dumper, "\\/");
|
2018-11-20 02:47:36 +01:00
|
|
|
} else {
|
|
|
|
if (str[i] == '\\' || str[i] == '"') {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, '\\');
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
2018-12-10 13:42:17 +01:00
|
|
|
if (dot_to_underscore && str[i] == '.')
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, '_');
|
2018-12-10 13:42:17 +01:00
|
|
|
else
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, str[i]);
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, '"');
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
static inline uint8_t
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_get_prev_state(json_dumper *dumper)
|
|
|
|
{
|
2023-09-22 13:42:49 -07:00
|
|
|
unsigned depth = dumper->current_depth;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
return depth != 0 ? dumper->state[depth - 1] : 0;
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
static inline uint8_t
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_get_curr_state(json_dumper *dumper)
|
|
|
|
{
|
2023-09-22 13:42:49 -07:00
|
|
|
unsigned depth = dumper->current_depth;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
return dumper->state[depth];
|
|
|
|
}
|
|
|
|
|
2018-12-14 00:21:51 +01:00
|
|
|
/**
|
|
|
|
* Called when a programming error is encountered where the JSON manipulation
|
|
|
|
* state got corrupted. This could happen when pairing the wrong begin/end
|
|
|
|
* calls, when writing multiple values for the same object, etc.
|
|
|
|
*/
|
|
|
|
static void
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_bad(json_dumper *dumper, const char *what)
|
2018-12-14 00:21:51 +01:00
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
dumper->flags |= JSON_DUMPER_FLAGS_ERROR;
|
2018-12-14 00:21:51 +01:00
|
|
|
if ((dumper->flags & JSON_DUMPER_FLAGS_NO_DEBUG)) {
|
|
|
|
/* Console output can be slow, disable log calls to speed up fuzzing. */
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
/*
|
|
|
|
* XXX - should this call abort()? If that flag isn't set,
|
|
|
|
* ws_error() wou;d call it; is there any point in continuing
|
|
|
|
* to do anything if we get here when fuzzing?
|
|
|
|
*/
|
2018-12-14 00:21:51 +01:00
|
|
|
return;
|
|
|
|
}
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
|
2022-01-23 11:48:24 +08:00
|
|
|
if (dumper->output_file) {
|
|
|
|
fflush(dumper->output_file);
|
|
|
|
}
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
char unknown_curr_type_name[10+1];
|
|
|
|
char unknown_prev_type_name[10+1];
|
|
|
|
const char *curr_type_name, *prev_type_name;
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t curr_state = json_dumper_get_curr_state(dumper);
|
|
|
|
uint8_t curr_type = JSON_DUMPER_TYPE(curr_state);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (curr_type < NUM_JSON_DUMPER_ELEMENT_TYPE_NAMES) {
|
|
|
|
curr_type_name = json_dumper_element_type_names[curr_type];
|
|
|
|
} else {
|
|
|
|
snprintf(unknown_curr_type_name, sizeof unknown_curr_type_name, "%u", curr_type);
|
|
|
|
curr_type_name = unknown_curr_type_name;
|
|
|
|
}
|
|
|
|
if (dumper->current_depth != 0) {
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t prev_state = json_dumper_get_prev_state(dumper);
|
|
|
|
uint8_t prev_type = JSON_DUMPER_TYPE(prev_state);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (prev_type < NUM_JSON_DUMPER_ELEMENT_TYPE_NAMES) {
|
|
|
|
prev_type_name = json_dumper_element_type_names[prev_type];
|
|
|
|
} else {
|
|
|
|
snprintf(unknown_prev_type_name, sizeof unknown_prev_type_name, "%u", prev_type);
|
|
|
|
prev_type_name = unknown_prev_type_name;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prev_type_name = "(none)";
|
|
|
|
}
|
|
|
|
ws_error("json_dumper error: %s: current stack depth %u, current type %s, previous_type %s",
|
|
|
|
what, dumper->current_depth, curr_type_name, prev_type_name);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
static inline bool
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_stack_would_overflow(json_dumper *dumper)
|
|
|
|
{
|
|
|
|
if (dumper->current_depth + 1 >= JSON_DUMPER_MAX_DEPTH) {
|
|
|
|
json_dumper_bad(dumper, "JSON dumper stack overflow");
|
2023-09-22 13:42:49 -07:00
|
|
|
return true;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
static inline bool
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_stack_would_underflow(json_dumper *dumper)
|
|
|
|
{
|
|
|
|
if (dumper->current_depth == 0) {
|
|
|
|
json_dumper_bad(dumper, "JSON dumper stack underflow");
|
2023-09-22 13:42:49 -07:00
|
|
|
return true;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
2018-12-14 00:21:51 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 02:47:36 +01:00
|
|
|
/**
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
* Checks that the dumper has not already had an error. Fail, and
|
2023-09-22 13:42:49 -07:00
|
|
|
* return false, to tell our caller not to do any more work, if it
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
* has.
|
2018-11-20 02:47:36 +01:00
|
|
|
*/
|
2023-09-22 13:42:49 -07:00
|
|
|
static bool
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_check_previous_error(json_dumper *dumper)
|
2018-11-20 02:47:36 +01:00
|
|
|
{
|
|
|
|
if ((dumper->flags & JSON_DUMPER_FLAGS_ERROR)) {
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_bad(dumper, "previous corruption detected");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
2023-09-22 13:42:49 -07:00
|
|
|
return true;
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-09-22 13:42:49 -07:00
|
|
|
print_newline_indent(const json_dumper *dumper, unsigned depth)
|
2018-11-20 02:47:36 +01:00
|
|
|
{
|
|
|
|
if ((dumper->flags & JSON_DUMPER_FLAGS_PRETTY_PRINT)) {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, '\n');
|
2023-09-22 13:42:49 -07:00
|
|
|
for (unsigned i = 0; i < depth; i++) {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_puts(dumper, " ");
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints commas, newlines and indentation (if necessary). Used for array
|
|
|
|
* values, object names and normal values (strings, etc.).
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
prepare_token(json_dumper *dumper)
|
|
|
|
{
|
|
|
|
if (dumper->current_depth == 0) {
|
|
|
|
// not part of an array or object.
|
|
|
|
return;
|
|
|
|
}
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t prev_state = dumper->state[dumper->current_depth - 1];
|
2018-11-20 02:47:36 +01:00
|
|
|
|
|
|
|
// While processing the object value, reset the key state as it is consumed.
|
|
|
|
dumper->state[dumper->current_depth - 1] &= ~JSON_DUMPER_HAS_NAME;
|
|
|
|
|
|
|
|
switch (JSON_DUMPER_TYPE(prev_state)) {
|
|
|
|
case JSON_DUMPER_TYPE_OBJECT:
|
|
|
|
if ((prev_state & JSON_DUMPER_HAS_NAME)) {
|
|
|
|
// Object key already set, value follows. No indentation needed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_ARRAY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Initial values do not need indentation.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t curr_state = json_dumper_get_curr_state(dumper);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (curr_state != JSON_DUMPER_TYPE_NONE) {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, ',');
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
print_newline_indent(dumper, dumper->current_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
* Common code to open an object/array/base64 value, printing
|
|
|
|
* an opening character.
|
|
|
|
*
|
|
|
|
* It also makes various correctness checks.
|
2018-11-20 02:47:36 +01:00
|
|
|
*/
|
2023-09-22 13:42:49 -07:00
|
|
|
static bool
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_begin_nested_element(json_dumper *dumper, enum json_dumper_element_type type)
|
2018-11-20 02:47:36 +01:00
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
/* Make sure we won't overflow the dumper stack */
|
|
|
|
if (json_dumper_stack_would_overflow(dumper)) {
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prepare_token(dumper);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
switch (type) {
|
|
|
|
case JSON_DUMPER_TYPE_OBJECT:
|
|
|
|
jd_putc(dumper, '{');
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_ARRAY:
|
|
|
|
jd_putc(dumper, '[');
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_BASE64:
|
|
|
|
dumper->base64_state = 0;
|
|
|
|
dumper->base64_save = 0;
|
|
|
|
|
|
|
|
jd_putc(dumper, '"');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
json_dumper_bad(dumper, "beginning unknown nested element type");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
2018-11-20 02:47:36 +01:00
|
|
|
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
dumper->state[dumper->current_depth] = type;
|
|
|
|
/*
|
|
|
|
* Guaranteed not to overflow, as json_dumper_stack_would_overflow()
|
2023-09-22 13:42:49 -07:00
|
|
|
* returned false.
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
*/
|
2018-11-20 02:47:36 +01:00
|
|
|
++dumper->current_depth;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
dumper->state[dumper->current_depth] = JSON_DUMPER_TYPE_NONE;
|
2023-09-22 13:42:49 -07:00
|
|
|
return true;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common code to close an object/array/base64 value, printing a
|
|
|
|
* closing character (and if necessary, it is preceded by newline
|
|
|
|
* and indentation).
|
|
|
|
*
|
|
|
|
* It also makes various correctness checks.
|
|
|
|
*/
|
2023-09-22 13:42:49 -07:00
|
|
|
static bool
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_end_nested_element(json_dumper *dumper, enum json_dumper_element_type type)
|
|
|
|
{
|
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t prev_state = json_dumper_get_prev_state(dumper);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case JSON_DUMPER_TYPE_OBJECT:
|
|
|
|
if (JSON_DUMPER_TYPE(prev_state) != JSON_DUMPER_TYPE_OBJECT) {
|
|
|
|
json_dumper_bad(dumper, "ending non-object nested item type as object");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_ARRAY:
|
|
|
|
if (JSON_DUMPER_TYPE(prev_state) != JSON_DUMPER_TYPE_ARRAY) {
|
|
|
|
json_dumper_bad(dumper, "ending non-array nested item type as array");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_BASE64:
|
|
|
|
if (JSON_DUMPER_TYPE(prev_state) != JSON_DUMPER_TYPE_BASE64) {
|
|
|
|
json_dumper_bad(dumper, "ending non-base64 nested item type as base64");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
json_dumper_bad(dumper, "ending unknown nested element type");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prev_state & JSON_DUMPER_HAS_NAME) {
|
|
|
|
json_dumper_bad(dumper, "finishing object with last item having name but no value");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we won't underflow the dumper stack */
|
|
|
|
if (json_dumper_stack_would_underflow(dumper)) {
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
2023-08-25 22:00:01 +01:00
|
|
|
// if the object/array was non-empty, add a newline and indentation.
|
|
|
|
if (dumper->state[dumper->current_depth]) {
|
|
|
|
print_newline_indent(dumper, dumper->current_depth - 1);
|
|
|
|
}
|
|
|
|
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
switch (type) {
|
|
|
|
case JSON_DUMPER_TYPE_OBJECT:
|
|
|
|
jd_putc(dumper, '}');
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_ARRAY:
|
|
|
|
jd_putc(dumper, ']');
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_BASE64:
|
|
|
|
{
|
2023-09-22 13:42:49 -07:00
|
|
|
char buf[4];
|
|
|
|
size_t wrote;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
wrote = g_base64_encode_close(false, buf, &dumper->base64_state, &dumper->base64_save);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
jd_puts_len(dumper, buf, wrote);
|
|
|
|
|
|
|
|
jd_putc(dumper, '"');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2024-06-06 12:04:10 +00:00
|
|
|
json_dumper_bad(dumper, "ending unknown nested element type");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Guaranteed not to underflow, as json_dumper_stack_would_underflow()
|
2023-09-22 13:42:49 -07:00
|
|
|
* returned false.
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
*/
|
|
|
|
--dumper->current_depth;
|
2023-09-22 13:42:49 -07:00
|
|
|
return true;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_begin_object(json_dumper *dumper)
|
|
|
|
{
|
|
|
|
json_dumper_begin_nested_element(dumper, JSON_DUMPER_TYPE_OBJECT);
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_set_member_name(json_dumper *dumper, const char *name)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t prev_state = json_dumper_get_prev_state(dumper);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
|
|
|
|
/* Only object members, not array members, have names. */
|
|
|
|
if (JSON_DUMPER_TYPE(prev_state) != JSON_DUMPER_TYPE_OBJECT) {
|
|
|
|
json_dumper_bad(dumper, "setting name on non-object nested item type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* An object member name can only be set once before its value is set. */
|
|
|
|
if (prev_state & JSON_DUMPER_HAS_NAME) {
|
|
|
|
json_dumper_bad(dumper, "setting name twice on an object member");
|
2018-11-20 02:47:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_token(dumper);
|
2022-01-23 11:48:24 +08:00
|
|
|
json_puts_string(dumper, name, dumper->flags & JSON_DUMPER_DOT_TO_UNDERSCORE);
|
|
|
|
jd_putc(dumper, ':');
|
2018-11-20 02:47:36 +01:00
|
|
|
if ((dumper->flags & JSON_DUMPER_FLAGS_PRETTY_PRINT)) {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, ' ');
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dumper->state[dumper->current_depth - 1] |= JSON_DUMPER_HAS_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_end_object(json_dumper *dumper)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_end_nested_element(dumper, JSON_DUMPER_TYPE_OBJECT);
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_begin_array(json_dumper *dumper)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_begin_nested_element(dumper, JSON_DUMPER_TYPE_ARRAY);
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_end_array(json_dumper *dumper)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_end_nested_element(dumper, JSON_DUMPER_TYPE_ARRAY);
|
|
|
|
}
|
2018-11-20 02:47:36 +01:00
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
static bool
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_setting_value_ok(json_dumper *dumper)
|
|
|
|
{
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t prev_state = json_dumper_get_prev_state(dumper);
|
2018-11-20 02:47:36 +01:00
|
|
|
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
switch (JSON_DUMPER_TYPE(prev_state)) {
|
|
|
|
case JSON_DUMPER_TYPE_OBJECT:
|
|
|
|
/*
|
|
|
|
* This value is part of an object. As such, it must
|
|
|
|
* have a name.
|
|
|
|
*/
|
|
|
|
if (!(prev_state & JSON_DUMPER_HAS_NAME)) {
|
|
|
|
json_dumper_bad(dumper, "setting value of object member without a name");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_ARRAY:
|
|
|
|
/*
|
|
|
|
* This value is part of an array. As such, it's not
|
|
|
|
* required to have a name (and shouldn't have a name;
|
|
|
|
* that's already been checked in json_dumper_set_member_name()).
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_BASE64:
|
|
|
|
/*
|
|
|
|
* We're in the middle of constructing a base64-encoded
|
|
|
|
* value. Only json_dumper_write_base64() can be used
|
|
|
|
* for that; we can't add individual values to it.
|
|
|
|
*/
|
|
|
|
json_dumper_bad(dumper, "attempt to set value of base64 item to something not base64-encoded");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
case JSON_DUMPER_TYPE_NONE:
|
|
|
|
case JSON_DUMPER_TYPE_VALUE:
|
|
|
|
{
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t curr_state = json_dumper_get_curr_state(dumper);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
switch (JSON_DUMPER_TYPE(curr_state)) {
|
|
|
|
case JSON_DUMPER_TYPE_NONE:
|
|
|
|
/*
|
|
|
|
* We haven't put a value yet, so we can put one now.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case JSON_DUMPER_TYPE_VALUE:
|
|
|
|
/*
|
|
|
|
* This value isn't part of an object or array,
|
|
|
|
* and we've already put one value.
|
|
|
|
*/
|
|
|
|
json_dumper_bad(dumper, "value not in object or array immediately follows another value");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
case JSON_DUMPER_TYPE_OBJECT:
|
|
|
|
case JSON_DUMPER_TYPE_ARRAY:
|
|
|
|
case JSON_DUMPER_TYPE_BASE64:
|
|
|
|
/*
|
|
|
|
* This should never be the case, no matter what
|
|
|
|
* our callers do:
|
|
|
|
*
|
|
|
|
* JSON_DUMPER_TYPE_OBJECT can be the previous
|
|
|
|
* type, meaning we're in the process of adding
|
|
|
|
* elements to an object, but it should never be
|
|
|
|
* the current type;
|
|
|
|
*
|
|
|
|
* JSON_DUMPER_TYPE_ARRAY can be the previous
|
|
|
|
* type, meaning we're in the process of adding
|
|
|
|
* elements to an array, but it should never be
|
|
|
|
* the current type;
|
|
|
|
*
|
|
|
|
* JSON_DUMPER_TYPE_BASE64 should only be the
|
|
|
|
* current type if we're in the middle of
|
|
|
|
* building a base64 value, in which case the
|
|
|
|
* previous type should also be JSON_DUMPER_TYPE_BASE64,
|
|
|
|
* but that's not the previous type.
|
|
|
|
*/
|
|
|
|
json_dumper_bad(dumper, "internal error setting value - should not happen");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
default:
|
|
|
|
json_dumper_bad(dumper, "internal error setting value, bad current state - should not happen");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
json_dumper_bad(dumper, "internal error setting value, bad previous state - should not happen");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
2023-09-22 13:42:49 -07:00
|
|
|
return true;
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_value_string(json_dumper *dumper, const char *value)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json_dumper_setting_value_ok(dumper)) {
|
2018-11-20 02:47:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_token(dumper);
|
2023-09-22 13:42:49 -07:00
|
|
|
json_puts_string(dumper, value, false);
|
2018-11-20 02:47:36 +01:00
|
|
|
|
|
|
|
dumper->state[dumper->current_depth] = JSON_DUMPER_TYPE_VALUE;
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:20:28 +01:00
|
|
|
void
|
|
|
|
json_dumper_value_double(json_dumper *dumper, double value)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_dumper_setting_value_ok(dumper)) {
|
2019-02-09 19:20:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_token(dumper);
|
2023-09-22 13:42:49 -07:00
|
|
|
char buffer[G_ASCII_DTOSTR_BUF_SIZE] = { 0 };
|
2019-02-09 19:20:28 +01:00
|
|
|
if (isfinite(value) && g_ascii_dtostr(buffer, G_ASCII_DTOSTR_BUF_SIZE, value) && buffer[0]) {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_puts(dumper, buffer);
|
2019-02-09 19:20:28 +01:00
|
|
|
} else {
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_puts(dumper, "null");
|
2019-02-09 19:20:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dumper->state[dumper->current_depth] = JSON_DUMPER_TYPE_VALUE;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:47:36 +01:00
|
|
|
void
|
2018-12-25 23:19:46 +01:00
|
|
|
json_dumper_value_va_list(json_dumper *dumper, const char *format, va_list ap)
|
2018-11-20 02:47:36 +01:00
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_dumper_setting_value_ok(dumper)) {
|
2018-11-20 02:47:36 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_token(dumper);
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_vprintf(dumper, format, ap);
|
2018-11-20 02:47:36 +01:00
|
|
|
|
|
|
|
dumper->state[dumper->current_depth] = JSON_DUMPER_TYPE_VALUE;
|
|
|
|
}
|
|
|
|
|
2018-12-25 23:19:46 +01:00
|
|
|
void
|
|
|
|
json_dumper_value_anyf(json_dumper *dumper, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
json_dumper_value_va_list(dumper, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
bool
|
2018-11-20 02:47:36 +01:00
|
|
|
json_dumper_finish(json_dumper *dumper)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dumper->current_depth != 0) {
|
|
|
|
json_dumper_bad(dumper, "JSON dumper stack not empty at finish");
|
2023-09-22 13:42:49 -07:00
|
|
|
return false;
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_putc(dumper, '\n');
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
dumper->state[0] = JSON_DUMPER_TYPE_NONE;
|
2023-09-22 13:42:49 -07:00
|
|
|
return true;
|
2018-11-20 02:47:36 +01:00
|
|
|
}
|
2018-12-13 15:39:21 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_begin_base64(json_dumper *dumper)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_begin_nested_element(dumper, JSON_DUMPER_TYPE_BASE64);
|
2018-12-13 15:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-09-22 13:42:49 -07:00
|
|
|
json_dumper_write_base64(json_dumper* dumper, const unsigned char *data, size_t len)
|
2018-12-13 15:39:21 +01:00
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
if (!json_dumper_check_previous_error(dumper)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-22 13:42:49 -07:00
|
|
|
uint8_t prev_state = json_dumper_get_prev_state(dumper);
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
|
|
|
|
if (JSON_DUMPER_TYPE(prev_state) != JSON_DUMPER_TYPE_BASE64) {
|
|
|
|
json_dumper_bad(dumper, "writing base64 data to a non-base64 value");
|
2018-12-13 15:39:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CHUNK_SIZE 1024
|
2023-09-22 13:42:49 -07:00
|
|
|
char buf[(CHUNK_SIZE / 3 + 1) * 4 + 4];
|
2018-12-13 15:39:21 +01:00
|
|
|
|
|
|
|
while (len > 0) {
|
2023-09-22 13:42:49 -07:00
|
|
|
size_t chunk_size = len < CHUNK_SIZE ? len : CHUNK_SIZE;
|
|
|
|
size_t output_size = g_base64_encode_step(data, chunk_size, false, buf, &dumper->base64_state, &dumper->base64_save);
|
2022-01-23 11:48:24 +08:00
|
|
|
jd_puts_len(dumper, buf, output_size);
|
2018-12-13 15:39:21 +01:00
|
|
|
data += chunk_size;
|
|
|
|
len -= chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
dumper->state[dumper->current_depth] = JSON_DUMPER_TYPE_BASE64;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
json_dumper_end_base64(json_dumper *dumper)
|
|
|
|
{
|
json_dumper: rework the error checking to improve error messages.
Fix the file name in the introductory comment.
Update a comment to note that a base64 value is handled, in some ways,
like a nested element, even though it's not nested in the way that an
object or array is.
Have json_dumper_bad() write current stack depth and the current and
previous types in, if possible, symbolic or numeric form; don't dump
other information. Also have it set JSON_DUMPER_FLAGS_ERROR, so no
other routine needs to do so.
Add routines to check for dumper stack overflow *and* underflow and
report them with appropriate messages, and use them in routines that
push onto or pop off of that stack, respectively.
This means that the stack depth won't overflow or underflow, so we can
make it unsigned (as it will never underflow below 0) and don't need to
check for negative or bigger-than-the-stack values.
Pull check out of json_dumper_check_state() into various existing or new
routines (for common code to call in those existing routines), and have
the error messages passed to json_dumper_bad() give a more detailed
explanation of the particular problem detected.
2023-03-08 03:00:51 -08:00
|
|
|
json_dumper_end_nested_element(dumper, JSON_DUMPER_TYPE_BASE64);
|
2018-12-13 15:39:21 +01:00
|
|
|
}
|