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_free (event->
value);
79 FILE *input_stream,
size_t parse_buffer_limit,
80 size_t read_buffer_size)
83 assert (input_stream);
86 if (parse_buffer_limit <= 0)
89 if (read_buffer_size <= 0)
93 parser->
path = g_queue_new ();
123 g_queue_free_full (parser->
path,
138 return g_strdup_printf (
"error reading JSON stream: %s", strerror (errno));
157 event->error_message =
158 g_strdup_printf (
"%s exceeds size limit of %zu bytes", value_type,
213 const char *value_name,
214 cJSON_bool (*validate_func) (
const cJSON *
const),
217 cJSON *parsed_value = cJSON_Parse (parser->
parse_buffer->str);
219 if (validate_func (parsed_value) == 0)
222 event->error_message = g_strdup_printf (
"error parsing %s", value_name);
223 cJSON_free (parsed_value);
226 *cjson_value = parsed_value;
250 event->error_message = g_strdup (
"unexpected EOF");
296 gboolean escape_next_char = FALSE;
304 if (escape_next_char)
305 escape_next_char = FALSE;
307 escape_next_char = TRUE;
381 for (
size_t i = 0; i < strlen (keyword); i++)
391 event->error_message =
392 g_strdup_printf (
"misspelled keyword '%s'", keyword);
408 if (parser->
path->length)
432 cJSON *key_cjson = NULL;
441 key_str = g_strdup (key_cjson->valuestring);
442 cJSON_free (key_cjson);
453 event->error_message = g_strdup_printf (
"expected colon");
459 path_elem = g_queue_peek_tail (parser->
path);
460 g_free (path_elem->
key);
461 path_elem->
key = key_str;
474 event->error_message = g_strdup (
"unexpected closing square bracket");
478 event->error_message = g_strdup (
"unexpected character");
507 path_elem = g_queue_peek_tail (parser->
path);
516 path_elem = g_queue_peek_tail (parser->
path);
517 if (path_elem == NULL
521 event->error_message = g_strdup (
"unexpected closing square bracket");
531 path_elem = g_queue_peek_tail (parser->
path);
532 if (path_elem == NULL
536 event->error_message = g_strdup (
"unexpected closing curly brace");
546 event->error_message = g_strdup (
"expected comma or end of container");
571 cJSON *cjson_value = NULL;
580 event->value = cjson_value;
587 event->value = cJSON_CreateNull ();
594 event->value = cJSON_CreateFalse ();
601 event->value = cJSON_CreateTrue ();
613 path_elem = g_queue_peek_tail (parser->
path);
614 if (path_elem == NULL
618 event->error_message = g_strdup (
"unexpected closing square bracket");
637 event->error_message = g_strdup (
"unexpected closing curly brace");
647 event->value = cjson_value;
653 event->error_message = g_strdup (
"unexpected character");
686 event->path = parser->
path;
708 event->error_message = g_strdup_printf (
709 "unexpected character at end of file (%d)", parser->
last_read_char);
745 gchar **error_message)
750 gboolean in_string, escape_next_char, in_expanded_container;
756 *error_message = NULL;
762 g_queue_push_tail (parser->
path, path_tail);
775 g_strdup (
"can only expand after array or object start");
779 start_depth = path_tail->
depth;
780 in_string = escape_next_char = FALSE;
781 in_expanded_container = TRUE;
789 g_strdup_printf (
"container exceeds size limit of %zu bytes",
796 if (escape_next_char)
798 escape_next_char = FALSE;
815 g_queue_push_tail (parser->
path, path_tail);
820 g_queue_push_tail (parser->
path, path_tail);
823 path_tail = g_queue_pop_tail (parser->
path);
828 g_strdup (
"unexpected closing square bracket");
831 if (path_tail->
depth == start_depth)
832 in_expanded_container = FALSE;
835 path_tail = g_queue_pop_tail (parser->
path);
840 g_strdup (
"unexpected closing curly brace");
843 if (path_tail->
depth == start_depth)
844 in_expanded_container = FALSE;
860 *error_message = g_strdup (
"unexpected EOF");
868 if (expanded == NULL && error_message)
869 *error_message = g_strdup (
"could not parse expanded container");
882 GString *path_string)
887 g_string_append_printf (path_string,
"['%s']", escaped_key);
888 g_free (escaped_key);
891 g_string_append_printf (path_string,
"[%d]", path_elem->
index);
904 GString *path_string = g_string_new (
"$");
906 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:337
void gvm_json_pull_parser_cleanup(gvm_json_pull_parser_t *parser)
Frees the data of a JSON pull parser.
Definition jsonpull.c:120
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:78
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:293
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:267
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:497
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:174
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:565
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:669
#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:109
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:426
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:238
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:902
#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:744
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:378
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:881
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:211
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:406
#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:151
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:136
#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