10#define GVM_JSON_CHAR_EOF -1
11#define GVM_JSON_CHAR_ERROR -2
12#define GVM_JSON_CHAR_UNDEFINED -3
29 GString *escaped = g_string_sized_new (strlen (
string));
30 for (point = (
char *)
string; *point != 0; point++)
32 unsigned char character = *point;
34 if ((character > 31) && (character !=
'\\')
35 && (single_quote ? (character !=
'\'') : (character !=
'\"')))
37 g_string_append_c (escaped, character);
41 g_string_append_c (escaped,
'\\');
47 g_string_append_c (escaped, *point);
50 g_string_append_c (escaped,
'b');
53 g_string_append_c (escaped,
'f');
56 g_string_append_c (escaped,
'n');
59 g_string_append_c (escaped,
'r');
62 g_string_append_c (escaped,
't');
65 g_string_append_printf (escaped,
"u%04x", character);
69 return g_string_free (escaped, FALSE);
86 new_elem->
depth = depth;
121 cJSON_free (event->
value);
135 cJSON_free (event->
value);
151 FILE *input_stream,
size_t parse_buffer_limit,
152 size_t read_buffer_size)
155 assert (input_stream);
158 if (parse_buffer_limit <= 0)
161 if (read_buffer_size <= 0)
165 parser->
path = g_queue_new ();
170 parser->
read_buffer = g_malloc0 (read_buffer_size);
195 g_queue_free_full (parser->
path,
210 return g_strdup_printf (
"error reading JSON stream: %s", strerror (errno));
229 event->error_message =
230 g_strdup_printf (
"%s exceeds size limit of %zu bytes", value_type,
285 const char *value_name,
286 cJSON_bool (*validate_func) (
const cJSON *
const),
289 cJSON *parsed_value = cJSON_Parse (parser->
parse_buffer->str);
291 if (validate_func (parsed_value) == 0)
294 event->error_message = g_strdup_printf (
"error parsing %s", value_name);
295 cJSON_free (parsed_value);
298 *cjson_value = parsed_value;
322 event->error_message = g_strdup (
"unexpected EOF");
368 gboolean escape_next_char = FALSE;
376 if (escape_next_char)
377 escape_next_char = FALSE;
379 escape_next_char = TRUE;
453 for (
size_t i = 0; i < strlen (keyword); i++)
463 event->error_message =
464 g_strdup_printf (
"misspelled keyword '%s'", keyword);
480 if (parser->
path->length)
504 cJSON *key_cjson = NULL;
513 key_str = g_strdup (key_cjson->valuestring);
514 cJSON_free (key_cjson);
525 event->error_message = g_strdup_printf (
"expected colon");
531 path_elem = g_queue_peek_tail (parser->
path);
532 g_free (path_elem->
key);
533 path_elem->
key = key_str;
546 event->error_message = g_strdup (
"unexpected closing square bracket");
550 event->error_message = g_strdup (
"unexpected character");
579 path_elem = g_queue_peek_tail (parser->
path);
588 path_elem = g_queue_peek_tail (parser->
path);
589 if (path_elem == NULL
593 event->error_message = g_strdup (
"unexpected closing square bracket");
603 path_elem = g_queue_peek_tail (parser->
path);
604 if (path_elem == NULL
608 event->error_message = g_strdup (
"unexpected closing curly brace");
618 event->error_message = g_strdup (
"expected comma or end of container");
643 cJSON *cjson_value = NULL;
652 event->value = cjson_value;
659 event->value = cJSON_CreateNull ();
666 event->value = cJSON_CreateFalse ();
673 event->value = cJSON_CreateTrue ();
685 path_elem = g_queue_peek_tail (parser->
path);
686 if (path_elem == NULL
690 event->error_message = g_strdup (
"unexpected closing square bracket");
709 event->error_message = g_strdup (
"unexpected closing curly brace");
719 event->value = cjson_value;
725 event->error_message = g_strdup (
"unexpected character");
757 event->path = parser->
path;
779 event->error_message = g_strdup_printf (
780 "unexpected character at end of file (%d)", parser->
last_read_char);
816 gchar **error_message)
821 gboolean in_string, escape_next_char, in_expanded_container;
827 *error_message = NULL;
833 g_queue_push_tail (parser->
path, path_tail);
846 g_strdup (
"can only expand after array or object start");
850 start_depth = path_tail->
depth;
851 in_string = escape_next_char = FALSE;
852 in_expanded_container = TRUE;
860 g_strdup_printf (
"container exceeds size limit of %zu bytes",
867 if (escape_next_char)
869 escape_next_char = FALSE;
886 g_queue_push_tail (parser->
path, path_tail);
891 g_queue_push_tail (parser->
path, path_tail);
894 path_tail = g_queue_pop_tail (parser->
path);
899 g_strdup (
"unexpected closing square bracket");
902 if (path_tail->
depth == start_depth)
903 in_expanded_container = FALSE;
906 path_tail = g_queue_pop_tail (parser->
path);
911 g_strdup (
"unexpected closing curly brace");
914 if (path_tail->
depth == start_depth)
915 in_expanded_container = FALSE;
931 *error_message = g_strdup (
"unexpected EOF");
939 if (expanded == NULL && error_message)
940 *error_message = g_strdup (
"could not parse expanded container");
953 GString *path_string)
958 g_string_append_printf (path_string,
"['%s']", escaped_key);
959 g_free (escaped_key);
962 g_string_append_printf (path_string,
"[%d]", path_elem->
index);
975 GString *path_string = g_string_new (
"$");
977 return g_string_free (path_string, FALSE);
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:409
void gvm_json_pull_parser_cleanup(gvm_json_pull_parser_t *parser)
Frees the data of a JSON pull parser.
Definition jsonpull.c:192
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:150
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:365
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:339
gchar * gvm_json_string_escape(const char *string, gboolean single_quote)
Escapes a string according to the JSON or JSONPath standard.
Definition jsonpull.c:23
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:569
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:246
void gvm_json_pull_event_cleanup(gvm_json_pull_event_t *event)
Frees all data of JSON pull event data structure.
Definition jsonpull.c:133
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:637
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:741
#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:181
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:498
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:310
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:973
#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:815
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:450
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:952
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:283
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:478
#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:223
void gvm_json_pull_path_elem_free(gvm_json_path_elem_t *elem)
Frees a JSON path element.
Definition jsonpull.c:96
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:81
void gvm_json_pull_event_init(gvm_json_pull_event_t *event)
Initializes a JSON pull event data structure.
Definition jsonpull.c:108
static gchar * gvm_json_read_stream_error_str()
Generates message for an error that occurred reading the JSON stream.
Definition jsonpull.c:208
void gvm_json_pull_event_reset(gvm_json_pull_event_t *event)
Resets a JSON pull event data structure for reuse.
Definition jsonpull.c:119
#define GVM_JSON_PULL_READ_BUFFER_SIZE
Definition jsonpull.h:79
#define GVM_JSON_PULL_PARSE_BUFFER_LIMIT
Definition jsonpull.h:77
gvm_json_pull_container_type_t
Type of container the parser is currently in.
Definition jsonpull.h:19
@ GVM_JSON_PULL_CONTAINER_OBJECT
Object.
Definition jsonpull.h:22
@ GVM_JSON_PULL_CONTAINER_ARRAY
Array.
Definition jsonpull.h:21
@ GVM_JSON_PULL_EXPECT_VALUE
Expect start of a value.
Definition jsonpull.h:71
@ GVM_JSON_PULL_EXPECT_KEY
Expect start of a key.
Definition jsonpull.h:72
@ GVM_JSON_PULL_EXPECT_EOF
Expect end of file.
Definition jsonpull.h:74
@ GVM_JSON_PULL_EXPECT_COMMA
Expect comma or container end brace.
Definition jsonpull.h:73
@ GVM_JSON_PULL_EVENT_STRING
Definition jsonpull.h:46
@ GVM_JSON_PULL_EVENT_OBJECT_START
Definition jsonpull.h:44
@ GVM_JSON_PULL_EVENT_ERROR
Definition jsonpull.h:51
@ GVM_JSON_PULL_EVENT_NULL
Definition jsonpull.h:49
@ GVM_JSON_PULL_EVENT_EOF
Definition jsonpull.h:50
@ GVM_JSON_PULL_EVENT_NUMBER
Definition jsonpull.h:47
@ GVM_JSON_PULL_EVENT_ARRAY_END
Definition jsonpull.h:43
@ GVM_JSON_PULL_EVENT_OBJECT_END
Definition jsonpull.h:45
@ GVM_JSON_PULL_EVENT_ARRAY_START
Definition jsonpull.h:42
@ GVM_JSON_PULL_EVENT_BOOLEAN
Definition jsonpull.h:48
Path element types for the JSON pull parser.
Definition jsonpull.h:29
int depth
Number of ancestor elements.
Definition jsonpull.h:33
int index
Index of the element within the parent.
Definition jsonpull.h:31
char * key
Key if element is in an object.
Definition jsonpull.h:32
gvm_json_pull_container_type_t parent_type
parent container type
Definition jsonpull.h:30
Event generated by the JSON pull parser.
Definition jsonpull.h:58
gchar * error_message
Error message, NULL on success.
Definition jsonpull.h:62
cJSON * value
Value for non-container value events.
Definition jsonpull.h:61
A json pull parser.
Definition jsonpull.h:85
char * read_buffer
Stream reading buffer.
Definition jsonpull.h:91
size_t read_buffer_size
Size of the stream reading buffer.
Definition jsonpull.h:92
gvm_json_pull_expect_t expect
Current expected token.
Definition jsonpull.h:88
size_t last_read_size
Size of last stream read.
Definition jsonpull.h:93
GString * parse_buffer
Buffer for parsing values and object keys.
Definition jsonpull.h:96
size_t parse_buffer_limit
Maximum parse buffer size.
Definition jsonpull.h:97
gvm_json_path_elem_t * path_add
Path elem to add in next step.
Definition jsonpull.h:87
size_t read_pos
Position in current read.
Definition jsonpull.h:95
GQueue * path
Path to the current value.
Definition jsonpull.h:86
int last_read_char
Character last read from stream.
Definition jsonpull.h:94
FILE * input_stream
Input stream.
Definition jsonpull.h:90