10#define GVM_JSON_CHAR_EOF -1
11#define GVM_JSON_CHAR_ERROR -2
12#define GVM_JSON_CHAR_UNDEFINED -3
28 new_elem->
depth = depth;
63 cJSON_Delete (event->
value);
78 FILE *input_stream,
size_t parse_buffer_limit,
79 size_t read_buffer_size)
82 assert (input_stream);
85 if (parse_buffer_limit <= 0)
88 if (read_buffer_size <= 0)
92 parser->
path = g_queue_new ();
122 g_queue_free_full (parser->
path,
137 return g_strdup_printf (
"error reading JSON stream: %s", strerror (errno));
156 event->error_message =
157 g_strdup_printf (
"%s exceeds size limit of %zu bytes", value_type,
212 const char *value_name,
213 cJSON_bool (*validate_func) (
const cJSON *
const),
216 cJSON *parsed_value = cJSON_Parse (parser->
parse_buffer->str);
218 if (validate_func (parsed_value) == 0)
221 event->error_message = g_strdup_printf (
"error parsing %s", value_name);
222 cJSON_free (parsed_value);
225 *cjson_value = parsed_value;
249 event->error_message = g_strdup (
"unexpected EOF");
295 gboolean escape_next_char = FALSE;
303 if (escape_next_char)
304 escape_next_char = FALSE;
306 escape_next_char = TRUE;
380 for (
size_t i = 0; i < strlen (keyword); i++)
390 event->error_message =
391 g_strdup_printf (
"misspelled keyword '%s'", keyword);
407 if (parser->
path->length)
431 cJSON *key_cjson = NULL;
440 key_str = g_strdup (key_cjson->valuestring);
441 cJSON_Delete (key_cjson);
452 event->error_message = g_strdup_printf (
"expected colon");
458 path_elem = g_queue_peek_tail (parser->
path);
459 g_free (path_elem->
key);
460 path_elem->
key = key_str;
473 event->error_message = g_strdup (
"unexpected closing square bracket");
477 event->error_message = g_strdup (
"unexpected character");
506 path_elem = g_queue_peek_tail (parser->
path);
515 path_elem = g_queue_peek_tail (parser->
path);
516 if (path_elem == NULL
520 event->error_message = g_strdup (
"unexpected closing square bracket");
530 path_elem = g_queue_peek_tail (parser->
path);
531 if (path_elem == NULL
535 event->error_message = g_strdup (
"unexpected closing curly brace");
545 event->error_message = g_strdup (
"expected comma or end of container");
570 cJSON *cjson_value = NULL;
579 event->value = cjson_value;
586 event->value = cJSON_CreateNull ();
593 event->value = cJSON_CreateFalse ();
600 event->value = cJSON_CreateTrue ();
612 path_elem = g_queue_peek_tail (parser->
path);
613 if (path_elem == NULL
617 event->error_message = g_strdup (
"unexpected closing square bracket");
636 event->error_message = g_strdup (
"unexpected closing curly brace");
646 event->value = cjson_value;
652 event->error_message = g_strdup (
"unexpected character");
685 event->path = parser->
path;
711 event->error_message = g_strdup_printf (
712 "unexpected character at end of file (%d)", parser->
last_read_char);
746 gchar **error_message)
751 gboolean in_string, escape_next_char, in_expanded_container;
757 *error_message = NULL;
763 g_queue_push_tail (parser->
path, path_tail);
776 g_strdup (
"can only expand after array or object start");
780 start_depth = path_tail->
depth;
781 in_string = escape_next_char = FALSE;
782 in_expanded_container = TRUE;
790 g_strdup_printf (
"container exceeds size limit of %zu bytes",
797 if (escape_next_char)
799 escape_next_char = FALSE;
816 g_queue_push_tail (parser->
path, path_tail);
821 g_queue_push_tail (parser->
path, path_tail);
824 path_tail = g_queue_pop_tail (parser->
path);
829 g_strdup (
"unexpected closing square bracket");
833 if (path_tail->
depth == start_depth)
834 in_expanded_container = FALSE;
839 path_tail = g_queue_pop_tail (parser->
path);
844 g_strdup (
"unexpected closing curly brace");
848 if (path_tail->
depth == start_depth)
849 in_expanded_container = FALSE;
867 *error_message = g_strdup (
"unexpected EOF");
875 if (expanded == NULL && error_message)
876 *error_message = g_strdup (
"could not parse expanded container");
889 GString *path_string)
894 g_string_append_printf (path_string,
"['%s']", escaped_key);
895 g_free (escaped_key);
898 g_string_append_printf (path_string,
"[%d]", path_elem->
index);
911 GString *path_string = g_string_new (
"$");
913 return g_string_free (path_string, FALSE);
gchar * gvm_json_string_escape(const char *string, gboolean single_quote)
Escapes a string according to the JSON or JSONPath standard.
Definition json.c:17
static int gvm_json_pull_parse_number(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, cJSON **cjson_value)
Parses a number in a JSON pull parser.
Definition jsonpull.c:336
void gvm_json_pull_parser_cleanup(gvm_json_pull_parser_t *parser)
Frees the data of a JSON pull parser.
Definition jsonpull.c:119
void gvm_json_pull_parser_init_full(gvm_json_pull_parser_t *parser, FILE *input_stream, size_t parse_buffer_limit, size_t read_buffer_size)
Initializes a JSON pull parser.
Definition jsonpull.c:77
static int gvm_json_pull_parse_string(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, cJSON **cjson_value)
Parses a string in a JSON pull parser.
Definition jsonpull.c:292
static int gvm_json_pull_skip_space(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, gboolean allow_eof)
Skips whitespaces in the input stream of a JSON pull parser.
Definition jsonpull.c:266
static int gvm_json_pull_parse_comma(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Handles the case that a comma is expected in a JSON pull parser.
Definition jsonpull.c:496
static int gvm_json_pull_parser_next_char(gvm_json_pull_parser_t *parser)
Reads the next character in a pull parser input stream.
Definition jsonpull.c:173
void gvm_json_pull_event_cleanup(gvm_json_pull_event_t *event)
Frees all data of JSON pull event data structure.
Definition jsonpull.c:61
static int gvm_json_pull_parse_value(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Handles the case that a value is expected in a JSON pull parser.
Definition jsonpull.c:564
void gvm_json_pull_parser_next(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Get the next event from a JSON pull parser.
Definition jsonpull.c:668
#define GVM_JSON_CHAR_UNDEFINED
Undefined state.
Definition jsonpull.c:12
void gvm_json_pull_parser_init(gvm_json_pull_parser_t *parser, FILE *input_stream)
Initializes a JSON pull parser with default buffer sizes.
Definition jsonpull.c:108
static int gvm_json_pull_parse_key(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Handles the case that an object key is expected in a JSON pull parser.
Definition jsonpull.c:425
static void gvm_json_pull_handle_read_end(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, gboolean allow_eof)
Handles error or EOF after reading a character in JSON pull parser.
Definition jsonpull.c:237
gchar * gvm_json_path_to_string(GQueue *path)
Converts a path as used by a JSON pull parser to a JSONPath string.
Definition jsonpull.c:909
#define GVM_JSON_CHAR_ERROR
Error reading file.
Definition jsonpull.c:11
cJSON * gvm_json_pull_expand_container(gvm_json_pull_parser_t *parser, gchar **error_message)
Expands the current array or object of a JSON pull parser.
Definition jsonpull.c:745
static int gvm_json_pull_parse_keyword(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, const char *keyword)
Parses a keyword value in a JSON pull parser.
Definition jsonpull.c:377
static void gvm_json_path_string_add_elem(gvm_json_path_elem_t *path_elem, GString *path_string)
Appends a string path element to a JSONPath string.
Definition jsonpull.c:888
static int gvm_json_pull_parse_buffered(gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event, const char *value_name, cJSON_bool(*validate_func)(const cJSON *const), cJSON **cjson_value)
Tries to parse the buffer content of a JSON pull parser.
Definition jsonpull.c:210
static void parse_value_next_expect(gvm_json_pull_parser_t *parser)
Updates the expectation for a JSON pull parser according to the path.
Definition jsonpull.c:405
#define GVM_JSON_CHAR_EOF
End of file.
Definition jsonpull.c:10
static int gvm_json_pull_check_parse_buffer_size(const char *value_type, gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
Checks if the parse buffer limit of a JSON pull parser is reached.
Definition jsonpull.c:150
void gvm_json_pull_path_elem_free(gvm_json_path_elem_t *elem)
Frees a JSON path element.
Definition jsonpull.c:38
gvm_json_path_elem_t * gvm_json_pull_path_elem_new(gvm_json_pull_container_type_t parent_type, int depth)
Creates a new JSON path element.
Definition jsonpull.c:23
void gvm_json_pull_event_init(gvm_json_pull_event_t *event)
Initializes a JSON pull event data structure.
Definition jsonpull.c:50
static gchar * gvm_json_read_stream_error_str()
Generates message for an error that occurred reading the JSON stream.
Definition jsonpull.c:135
#define GVM_JSON_PULL_READ_BUFFER_SIZE
Definition jsonpull.h:80
#define GVM_JSON_PULL_PARSE_BUFFER_LIMIT
Definition jsonpull.h:78
gvm_json_pull_container_type_t
Type of container the parser is currently in.
Definition jsonpull.h:20
@ GVM_JSON_PULL_CONTAINER_OBJECT
Object.
Definition jsonpull.h:23
@ GVM_JSON_PULL_CONTAINER_ARRAY
Array.
Definition jsonpull.h:22
@ GVM_JSON_PULL_EXPECT_VALUE
Expect start of a value.
Definition jsonpull.h:72
@ GVM_JSON_PULL_EXPECT_KEY
Expect start of a key.
Definition jsonpull.h:73
@ GVM_JSON_PULL_EXPECT_EOF
Expect end of file.
Definition jsonpull.h:75
@ GVM_JSON_PULL_EXPECT_COMMA
Expect comma or container end brace.
Definition jsonpull.h:74
@ GVM_JSON_PULL_EVENT_STRING
Definition jsonpull.h:47
@ GVM_JSON_PULL_EVENT_OBJECT_START
Definition jsonpull.h:45
@ GVM_JSON_PULL_EVENT_ERROR
Definition jsonpull.h:52
@ GVM_JSON_PULL_EVENT_NULL
Definition jsonpull.h:50
@ GVM_JSON_PULL_EVENT_EOF
Definition jsonpull.h:51
@ GVM_JSON_PULL_EVENT_NUMBER
Definition jsonpull.h:48
@ GVM_JSON_PULL_EVENT_ARRAY_END
Definition jsonpull.h:44
@ GVM_JSON_PULL_EVENT_OBJECT_END
Definition jsonpull.h:46
@ GVM_JSON_PULL_EVENT_ARRAY_START
Definition jsonpull.h:43
@ GVM_JSON_PULL_EVENT_BOOLEAN
Definition jsonpull.h:49
struct gvm_json_path_elem gvm_json_path_elem_t
Path element types for the JSON pull parser.
int depth
Number of ancestor elements.
Definition jsonpull.h:34
int index
Index of the element within the parent.
Definition jsonpull.h:32
char * key
Key if element is in an object.
Definition jsonpull.h:33
gvm_json_pull_container_type_t parent_type
parent container type
Definition jsonpull.h:31
Event generated by the JSON pull parser.
Definition jsonpull.h:59
gchar * error_message
Error message, NULL on success.
Definition jsonpull.h:63
cJSON * value
Value for non-container value events.
Definition jsonpull.h:62
A json pull parser.
Definition jsonpull.h:86
char * read_buffer
Stream reading buffer.
Definition jsonpull.h:92
size_t read_buffer_size
Size of the stream reading buffer.
Definition jsonpull.h:93
gvm_json_pull_expect_t expect
Current expected token.
Definition jsonpull.h:89
size_t last_read_size
Size of last stream read.
Definition jsonpull.h:94
GString * parse_buffer
Buffer for parsing values and object keys.
Definition jsonpull.h:97
size_t parse_buffer_limit
Maximum parse buffer size.
Definition jsonpull.h:98
gvm_json_path_elem_t * path_add
Path elem to add in next step.
Definition jsonpull.h:88
size_t read_pos
Position in current read.
Definition jsonpull.h:96
GQueue * path
Path to the current value.
Definition jsonpull.h:87
int last_read_char
Character last read from stream.
Definition jsonpull.h:95
FILE * input_stream
Input stream.
Definition jsonpull.h:91