2021-11-26 03:31:05 +00:00
|
|
|
/** @file
|
2018-11-20 01:47:36 +00:00
|
|
|
* Routines for serializing data as JSON.
|
|
|
|
*
|
|
|
|
* Copyright 2018, Peter Wu <peter@lekensteyn.nl>
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __JSON_DUMPER_H__
|
|
|
|
#define __JSON_DUMPER_H__
|
|
|
|
|
|
|
|
#include "ws_symbol_export.h"
|
|
|
|
#include <glib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* json_dumper dumper = {
|
2022-01-23 03:48:24 +00:00
|
|
|
* .output_file = stdout, // or .output_string = g_string_new(NULL)
|
2018-11-20 01:47:36 +00:00
|
|
|
* .flags = JSON_DUMPER_FLAGS_PRETTY_PRINT,
|
|
|
|
* };
|
|
|
|
* json_dumper_begin_object(&dumper);
|
|
|
|
* json_dumper_set_member_name(&dumper, "key");
|
|
|
|
* json_dumper_value_string(&dumper, "value");
|
|
|
|
* json_dumper_set_member_name(&dumper, "array");
|
|
|
|
* json_dumper_begin_array(&dumper);
|
|
|
|
* json_dumper_value_anyf(&dumper, "true");
|
2019-02-09 18:20:28 +00:00
|
|
|
* json_dumper_value_double(&dumper, 1.0);
|
2018-12-13 14:39:21 +00:00
|
|
|
* json_dumper_begin_base64(&dumper);
|
|
|
|
* json_dumper_write_base64(&dumper, (const guchar *)"abcd", 4);
|
|
|
|
* json_dumper_write_base64(&dumper, (const guchar *)"1234", 4);
|
|
|
|
* json_dumper_end_base64(&dumper);
|
2018-11-20 01:47:36 +00:00
|
|
|
* json_dumper_begin_object(&dumper);
|
|
|
|
* json_dumper_end_object(&dumper);
|
|
|
|
* json_dumper_begin_array(&dumper);
|
|
|
|
* json_dumper_end_array(&dumper);
|
|
|
|
* json_dumper_end_array(&dumper);
|
|
|
|
* json_dumper_end_object(&dumper);
|
|
|
|
* json_dumper_finish(&dumper);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Maximum object/array nesting depth. */
|
2019-01-19 22:36:53 +00:00
|
|
|
#define JSON_DUMPER_MAX_DEPTH 1100
|
2018-11-20 01:47:36 +00:00
|
|
|
typedef struct json_dumper {
|
2022-01-23 03:48:24 +00:00
|
|
|
FILE *output_file; /**< Output file. If it is not NULL, JSON will be dumped in the file. */
|
|
|
|
GString *output_string; /**< Output GLib strings. If it is not NULL, JSON will be dumped in the string. */
|
2018-11-20 01:47:36 +00:00
|
|
|
#define JSON_DUMPER_FLAGS_PRETTY_PRINT (1 << 0) /* Enable pretty printing. */
|
2018-12-10 12:42:17 +00:00
|
|
|
#define JSON_DUMPER_DOT_TO_UNDERSCORE (1 << 1) /* Convert dots to underscores in keys */
|
2022-12-16 01:36:17 +00:00
|
|
|
#define JSON_DUMPER_FLAGS_NO_DEBUG (1 << 17) /* Disable fatal ws_error messsges on error(intended for speeding up fuzzing). */
|
2018-11-20 01:47:36 +00:00
|
|
|
int flags;
|
|
|
|
/* for internal use, initialize with zeroes. */
|
|
|
|
int current_depth;
|
2018-12-13 14:39:21 +00:00
|
|
|
gint base64_state;
|
|
|
|
gint base64_save;
|
2018-11-20 01:47:36 +00:00
|
|
|
guint8 state[JSON_DUMPER_MAX_DEPTH];
|
|
|
|
} json_dumper;
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_begin_object(json_dumper *dumper);
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_set_member_name(json_dumper *dumper, const char *name);
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_end_object(json_dumper *dumper);
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_begin_array(json_dumper *dumper);
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_end_array(json_dumper *dumper);
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_value_string(json_dumper *dumper, const char *value);
|
|
|
|
|
2019-02-09 18:20:28 +00:00
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_value_double(json_dumper *dumper, double value);
|
|
|
|
|
2018-11-20 01:47:36 +00:00
|
|
|
/**
|
|
|
|
* Dump number, "true", "false" or "null" values.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_value_anyf(json_dumper *dumper, const char *format, ...)
|
|
|
|
G_GNUC_PRINTF(2, 3);
|
|
|
|
|
2018-12-25 22:19:46 +00:00
|
|
|
/**
|
|
|
|
* Dump literal values (like json_dumper_value_anyf), but taking a va_list
|
|
|
|
* as parameter. String values MUST be properly quoted by the caller, no
|
|
|
|
* escaping occurs. Do not use with untrusted data.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_value_va_list(json_dumper *dumper, const char *format, va_list ap);
|
|
|
|
|
2018-12-13 14:39:21 +00:00
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_begin_base64(json_dumper *dumper);
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_end_base64(json_dumper *dumper);
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC void
|
|
|
|
json_dumper_write_base64(json_dumper *dumper, const guchar *data, size_t len);
|
|
|
|
|
2018-11-20 01:47:36 +00:00
|
|
|
/**
|
|
|
|
* Finishes dumping data. Returns TRUE if everything is okay and FALSE if
|
|
|
|
* something went wrong (open/close mismatch, missing values, etc.).
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC gboolean
|
|
|
|
json_dumper_finish(json_dumper *dumper);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* __JSON_DUMPER_H__ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|