From 115e910aa8796063f9b93c6f645e5e05cac595d6 Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Thu, 1 Aug 2024 12:59:04 +0200 Subject: [PATCH] Fix formatting for JSON parser --- util/jsonpull.c | 253 +++++++++++++++++++----------------------- util/jsonpull.h | 71 ++++++------ util/jsonpull_tests.c | 149 +++++++++++-------------- 3 files changed, 215 insertions(+), 258 deletions(-) diff --git a/util/jsonpull.c b/util/jsonpull.c index 99aa23bd..a51458fd 100644 --- a/util/jsonpull.c +++ b/util/jsonpull.c @@ -4,11 +4,12 @@ */ #include "jsonpull.h" -#include "assert.h" -#define GVM_JSON_CHAR_EOF -1 ///< End of file -#define GVM_JSON_CHAR_ERROR -2 ///< Error reading file -#define GVM_JSON_CHAR_UNDEFINED -3 ///< Undefined state +#include + +#define GVM_JSON_CHAR_EOF -1 ///< End of file +#define GVM_JSON_CHAR_ERROR -2 ///< Error reading file +#define GVM_JSON_CHAR_UNDEFINED -3 ///< Undefined state /** * @brief Escapes a string according to the JSON or JSONPath standard @@ -21,10 +22,11 @@ gvm_json_string_escape (const char *string, gboolean single_quote) return NULL; GString *escaped = g_string_sized_new (strlen (string)); - for (point = (char*)string; *point != 0; point++) + for (point = (char *) string; *point != 0; point++) { - unsigned char character = *point;; - if ((character > 31) && (character != '\\') + unsigned char character = *point; + ; + if ((character > 31) && (character != '\\') && (single_quote || character != '\"') && (!single_quote || character != '\'')) { @@ -34,7 +36,7 @@ gvm_json_string_escape (const char *string, gboolean single_quote) { g_string_append_c (escaped, '\\'); switch (*point) - { + { case '\\': case '\'': case '\"': @@ -57,7 +59,7 @@ gvm_json_string_escape (const char *string, gboolean single_quote) break; default: g_string_append_printf (escaped, "u%04x", character); - } + } } } return g_string_free (escaped, FALSE); @@ -101,7 +103,7 @@ gvm_json_pull_path_elem_free (gvm_json_path_elem_t *elem) void gvm_json_pull_event_init (gvm_json_pull_event_t *event) { - memset (event, 0, sizeof(gvm_json_pull_event_t)); + memset (event, 0, sizeof (gvm_json_pull_event_t)); } /** @@ -113,7 +115,7 @@ void gvm_json_pull_event_reset (gvm_json_pull_event_t *event) { cJSON_free (event->value); - memset (event, 0, sizeof(gvm_json_pull_event_t)); + memset (event, 0, sizeof (gvm_json_pull_event_t)); } /** @@ -125,7 +127,7 @@ void gvm_json_pull_event_cleanup (gvm_json_pull_event_t *event) { cJSON_free (event->value); - memset (event, 0, sizeof(gvm_json_pull_event_t)); + memset (event, 0, sizeof (gvm_json_pull_event_t)); } /** @@ -138,17 +140,16 @@ gvm_json_pull_event_cleanup (gvm_json_pull_event_t *event) */ void gvm_json_pull_parser_init_full (gvm_json_pull_parser_t *parser, - FILE *input_stream, - size_t parse_buffer_limit, + FILE *input_stream, size_t parse_buffer_limit, size_t read_buffer_size) { - assert(parser); + assert (parser); assert (input_stream); - memset (parser, 0, sizeof(gvm_json_pull_parser_t)); + memset (parser, 0, sizeof (gvm_json_pull_parser_t)); if (parse_buffer_limit <= 0) parse_buffer_limit = GVM_JSON_PULL_PARSE_BUFFER_LIMIT; - + if (read_buffer_size <= 0) read_buffer_size = GVM_JSON_PULL_READ_BUFFER_SIZE; @@ -158,7 +159,7 @@ gvm_json_pull_parser_init_full (gvm_json_pull_parser_t *parser, parser->parse_buffer_limit = parse_buffer_limit; parser->parse_buffer = g_string_new (""); parser->read_buffer_size = read_buffer_size; - parser->read_buffer = g_malloc0(read_buffer_size); + parser->read_buffer = g_malloc0 (read_buffer_size); parser->last_read_char = GVM_JSON_CHAR_UNDEFINED; } @@ -169,8 +170,7 @@ gvm_json_pull_parser_init_full (gvm_json_pull_parser_t *parser, * @param[in] input_stream The JSON input stream */ void -gvm_json_pull_parser_init (gvm_json_pull_parser_t *parser, - FILE *input_stream) +gvm_json_pull_parser_init (gvm_json_pull_parser_t *parser, FILE *input_stream) { gvm_json_pull_parser_init_full (parser, input_stream, 0, 0); } @@ -183,12 +183,12 @@ gvm_json_pull_parser_init (gvm_json_pull_parser_t *parser, void gvm_json_pull_parser_cleanup (gvm_json_pull_parser_t *parser) { - assert(parser); + assert (parser); g_queue_free_full (parser->path, (GDestroyNotify) gvm_json_pull_path_elem_free); g_string_free (parser->parse_buffer, TRUE); g_free (parser->read_buffer); - memset (parser, 0, sizeof(gvm_json_pull_parser_t)); + memset (parser, 0, sizeof (gvm_json_pull_parser_t)); } /** @@ -218,10 +218,9 @@ gvm_json_pull_check_parse_buffer_size (const char *value_type, { if (parser->parse_buffer->len >= parser->parse_buffer_limit) { - event->error_message - = g_strdup_printf ("%s exceeds size limit of %zu bytes", - value_type, - parser->parse_buffer_limit); + event->error_message = + g_strdup_printf ("%s exceeds size limit of %zu bytes", value_type, + parser->parse_buffer_limit); event->type = GVM_JSON_PULL_EVENT_ERROR; return 1; } @@ -239,24 +238,22 @@ gvm_json_pull_parser_next_char (gvm_json_pull_parser_t *parser) parser->read_pos++; if (parser->read_pos < parser->last_read_size) { - parser->last_read_char - = (unsigned char) parser->read_buffer[parser->read_pos]; + parser->last_read_char = + (unsigned char) parser->read_buffer[parser->read_pos]; return parser->last_read_char; } else { parser->read_pos = 0; - parser->last_read_size = fread (parser->read_buffer, - 1, - parser->read_buffer_size, - parser->input_stream); + parser->last_read_size = fread ( + parser->read_buffer, 1, parser->read_buffer_size, parser->input_stream); if (ferror (parser->input_stream)) parser->last_read_char = GVM_JSON_CHAR_ERROR; else if (parser->last_read_size <= 0) parser->last_read_char = GVM_JSON_CHAR_EOF; else - parser->last_read_char - = (unsigned char) parser->read_buffer[parser->read_pos]; + parser->last_read_char = + (unsigned char) parser->read_buffer[parser->read_pos]; return parser->last_read_char; } } @@ -276,16 +273,15 @@ 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_bool (*validate_func) (const cJSON *const), cJSON **cjson_value) { - cJSON* parsed_value = cJSON_Parse (parser->parse_buffer->str); + cJSON *parsed_value = cJSON_Parse (parser->parse_buffer->str); *cjson_value = NULL; if (validate_func (parsed_value) == 0) { event->type = GVM_JSON_PULL_EVENT_ERROR; - event->error_message - = g_strdup_printf ("error parsing %s", value_name); + event->error_message = g_strdup_printf ("error parsing %s", value_name); cJSON_free (parsed_value); return 1; } @@ -302,8 +298,7 @@ gvm_json_pull_parse_buffered (gvm_json_pull_parser_t *parser, */ static void gvm_json_pull_handle_read_end (gvm_json_pull_parser_t *parser, - gvm_json_pull_event_t *event, - gboolean allow_eof) + gvm_json_pull_event_t *event, gboolean allow_eof) { if (parser->last_read_char == GVM_JSON_CHAR_ERROR) { @@ -332,8 +327,7 @@ gvm_json_pull_handle_read_end (gvm_json_pull_parser_t *parser, */ static int gvm_json_pull_skip_space (gvm_json_pull_parser_t *parser, - gvm_json_pull_event_t *event, - gboolean allow_eof) + gvm_json_pull_event_t *event, gboolean allow_eof) { while (g_ascii_isspace (parser->last_read_char)) gvm_json_pull_parser_next_char (parser); @@ -359,24 +353,23 @@ gvm_json_pull_skip_space (gvm_json_pull_parser_t *parser, */ static int gvm_json_pull_parse_string (gvm_json_pull_parser_t *parser, - gvm_json_pull_event_t *event, - cJSON **cjson_value) + gvm_json_pull_event_t *event, cJSON **cjson_value) { gboolean escape_next_char = FALSE; g_string_truncate (parser->parse_buffer, 0); g_string_append_c (parser->parse_buffer, '"'); while (gvm_json_pull_parser_next_char (parser) >= 0) - { - if (gvm_json_pull_check_parse_buffer_size ("string", parser, event)) - return 1; - g_string_append_c (parser->parse_buffer, parser->last_read_char); - if (escape_next_char) - escape_next_char = FALSE; - else if (parser->last_read_char == '\\') - escape_next_char = TRUE; - else if (parser->last_read_char == '"') - break; - } + { + if (gvm_json_pull_check_parse_buffer_size ("string", parser, event)) + return 1; + g_string_append_c (parser->parse_buffer, parser->last_read_char); + if (escape_next_char) + escape_next_char = FALSE; + else if (parser->last_read_char == '\\') + escape_next_char = TRUE; + else if (parser->last_read_char == '"') + break; + } if (parser->last_read_char < 0) { @@ -386,10 +379,7 @@ gvm_json_pull_parse_string (gvm_json_pull_parser_t *parser, gvm_json_pull_parser_next_char (parser); - return gvm_json_pull_parse_buffered (parser, - event, - "string", - cJSON_IsString, + return gvm_json_pull_parse_buffered (parser, event, "string", cJSON_IsString, cjson_value); } @@ -407,24 +397,21 @@ gvm_json_pull_parse_string (gvm_json_pull_parser_t *parser, */ static int gvm_json_pull_parse_number (gvm_json_pull_parser_t *parser, - gvm_json_pull_event_t *event, - cJSON **cjson_value) + gvm_json_pull_event_t *event, cJSON **cjson_value) { g_string_truncate (parser->parse_buffer, 0); g_string_append_c (parser->parse_buffer, parser->last_read_char); while (gvm_json_pull_parser_next_char (parser) >= 0) - { - if (gvm_json_pull_check_parse_buffer_size ("number", parser, event)) - return 1; - if (g_ascii_isdigit (parser->last_read_char) - || parser->last_read_char == '.' - || parser->last_read_char == 'e' - || parser->last_read_char == '-' - || parser->last_read_char == '+') - g_string_append_c (parser->parse_buffer, parser->last_read_char); - else - break; - } + { + if (gvm_json_pull_check_parse_buffer_size ("number", parser, event)) + return 1; + if (g_ascii_isdigit (parser->last_read_char) + || parser->last_read_char == '.' || parser->last_read_char == 'e' + || parser->last_read_char == '-' || parser->last_read_char == '+') + g_string_append_c (parser->parse_buffer, parser->last_read_char); + else + break; + } if (parser->last_read_char == GVM_JSON_CHAR_ERROR) { @@ -433,10 +420,7 @@ gvm_json_pull_parse_number (gvm_json_pull_parser_t *parser, return 1; } - return gvm_json_pull_parse_buffered (parser, - event, - "number", - cJSON_IsNumber, + return gvm_json_pull_parse_buffered (parser, event, "number", cJSON_IsNumber, cjson_value); } @@ -454,10 +438,9 @@ gvm_json_pull_parse_number (gvm_json_pull_parser_t *parser, */ static int gvm_json_pull_parse_keyword (gvm_json_pull_parser_t *parser, - gvm_json_pull_event_t *event, - const char *keyword) + gvm_json_pull_event_t *event, const char *keyword) { - for (size_t i = 0; i < strlen(keyword); i++) + for (size_t i = 0; i < strlen (keyword); i++) { if (parser->last_read_char < 0) { @@ -467,8 +450,8 @@ gvm_json_pull_parse_keyword (gvm_json_pull_parser_t *parser, else if (parser->last_read_char != keyword[i]) { event->type = GVM_JSON_PULL_EVENT_ERROR; - event->error_message - = g_strdup_printf ("misspelled keyword '%s'", keyword); + event->error_message = + g_strdup_printf ("misspelled keyword '%s'", keyword); return 1; } gvm_json_pull_parser_next_char (parser); @@ -476,10 +459,10 @@ gvm_json_pull_parse_keyword (gvm_json_pull_parser_t *parser, return 0; } -#define PARSE_VALUE_NEXT_EXPECT \ - if (parser->path->length) \ - parser->expect = GVM_JSON_PULL_EXPECT_COMMA; \ - else \ +#define PARSE_VALUE_NEXT_EXPECT \ + if (parser->path->length) \ + parser->expect = GVM_JSON_PULL_EXPECT_COMMA; \ + else \ parser->expect = GVM_JSON_PULL_EXPECT_EOF; /** @@ -505,7 +488,7 @@ gvm_json_pull_parse_key (gvm_json_pull_parser_t *parser, gvm_json_path_elem_t *path_elem; switch (parser->last_read_char) - { + { case '"': if (gvm_json_pull_parse_string (parser, event, &key_cjson)) return 1; @@ -531,7 +514,7 @@ gvm_json_pull_parse_key (gvm_json_pull_parser_t *parser, g_free (path_elem->key); path_elem->key = key_str; parser->expect = GVM_JSON_PULL_EXPECT_VALUE; - + break; case '}': event->type = GVM_JSON_PULL_EVENT_OBJECT_END; @@ -548,7 +531,7 @@ gvm_json_pull_parse_key (gvm_json_pull_parser_t *parser, event->type = GVM_JSON_PULL_EVENT_ERROR; event->error_message = g_strdup ("unexpected character"); return 1; - } + } return 0; } @@ -570,13 +553,13 @@ gvm_json_pull_parse_comma (gvm_json_pull_parser_t *parser, { if (gvm_json_pull_skip_space (parser, event, FALSE)) return 1; - + gvm_json_path_elem_t *path_elem = NULL; switch (parser->last_read_char) - { + { case ',': path_elem = g_queue_peek_tail (parser->path); - path_elem->index ++; + path_elem->index++; if (path_elem->parent_type == GVM_JSON_PULL_CONTAINER_OBJECT) parser->expect = GVM_JSON_PULL_EXPECT_KEY; else @@ -585,7 +568,7 @@ gvm_json_pull_parse_comma (gvm_json_pull_parser_t *parser, break; case ']': path_elem = g_queue_peek_tail (parser->path); - if (path_elem == NULL + if (path_elem == NULL || path_elem->parent_type != GVM_JSON_PULL_CONTAINER_ARRAY) { event->type = GVM_JSON_PULL_EVENT_ERROR; @@ -600,7 +583,7 @@ gvm_json_pull_parse_comma (gvm_json_pull_parser_t *parser, break; case '}': path_elem = g_queue_peek_tail (parser->path); - if (path_elem == NULL + if (path_elem == NULL || path_elem->parent_type != GVM_JSON_PULL_CONTAINER_OBJECT) { event->type = GVM_JSON_PULL_EVENT_ERROR; @@ -617,7 +600,7 @@ gvm_json_pull_parse_comma (gvm_json_pull_parser_t *parser, event->error_message = g_strdup ("expected comma or end of container"); event->type = GVM_JSON_PULL_EVENT_ERROR; return 1; - } + } return 0; } @@ -638,12 +621,12 @@ gvm_json_pull_parse_value (gvm_json_pull_parser_t *parser, { if (gvm_json_pull_skip_space (parser, event, FALSE)) return 1; - + cJSON *cjson_value = NULL; gvm_json_path_elem_t *path_elem = NULL; switch (parser->last_read_char) - { + { case '"': if (gvm_json_pull_parse_string (parser, event, &cjson_value)) return 1; @@ -675,15 +658,14 @@ gvm_json_pull_parse_value (gvm_json_pull_parser_t *parser, case '[': event->type = GVM_JSON_PULL_EVENT_ARRAY_START; event->value = NULL; - parser->path_add - = gvm_json_pull_path_elem_new (GVM_JSON_PULL_CONTAINER_ARRAY, - parser->path->length); + parser->path_add = gvm_json_pull_path_elem_new ( + GVM_JSON_PULL_CONTAINER_ARRAY, parser->path->length); parser->expect = GVM_JSON_PULL_EXPECT_VALUE; gvm_json_pull_parser_next_char (parser); break; case ']': path_elem = g_queue_peek_tail (parser->path); - if (path_elem == NULL + if (path_elem == NULL || path_elem->parent_type != GVM_JSON_PULL_CONTAINER_ARRAY) { event->type = GVM_JSON_PULL_EVENT_ERROR; @@ -699,9 +681,8 @@ gvm_json_pull_parse_value (gvm_json_pull_parser_t *parser, case '{': event->type = GVM_JSON_PULL_EVENT_OBJECT_START; event->value = NULL; - parser->path_add - = gvm_json_pull_path_elem_new (GVM_JSON_PULL_CONTAINER_OBJECT, - parser->path->length); + parser->path_add = gvm_json_pull_path_elem_new ( + GVM_JSON_PULL_CONTAINER_OBJECT, parser->path->length); parser->expect = GVM_JSON_PULL_EXPECT_KEY; gvm_json_pull_parser_next_char (parser); break; @@ -726,15 +707,15 @@ gvm_json_pull_parse_value (gvm_json_pull_parser_t *parser, event->error_message = g_strdup ("unexpected character"); return 1; } - } + } return 0; } /** * @brief Get the next event from a JSON pull parser. - * + * * Note: This invalidates previous event data like the cJSON value. - * + * * @param[in] parser The JSON pull parser to process until the next event * @param[in] event Structure to store event data in. */ @@ -763,7 +744,7 @@ gvm_json_pull_parser_next (gvm_json_pull_parser_t *parser, g_queue_push_tail (parser->path, parser->path_add); parser->path_add = NULL; } - + // Check for expected end of file if (parser->expect == GVM_JSON_PULL_EXPECT_EOF) { @@ -777,9 +758,8 @@ gvm_json_pull_parser_next (gvm_json_pull_parser_t *parser, else if (parser->last_read_char != GVM_JSON_CHAR_EOF) { event->type = GVM_JSON_PULL_EVENT_ERROR; - event->error_message - = g_strdup_printf ("unexpected character at end of file (%d)", - parser->last_read_char); + event->error_message = g_strdup_printf ( + "unexpected character at end of file (%d)", parser->last_read_char); return; } return; @@ -796,7 +776,7 @@ gvm_json_pull_parser_next (gvm_json_pull_parser_t *parser, if (gvm_json_pull_parse_comma (parser, event)) return; } - + if (parser->expect == GVM_JSON_PULL_EXPECT_KEY) { if (gvm_json_pull_parse_key (parser, event)) @@ -842,20 +822,19 @@ gvm_json_pull_expand_container (gvm_json_pull_parser_t *parser, parser->path_add = NULL; } - if (path_tail - && path_tail->parent_type == GVM_JSON_PULL_CONTAINER_ARRAY) + if (path_tail && path_tail->parent_type == GVM_JSON_PULL_CONTAINER_ARRAY) g_string_append_c (parser->parse_buffer, '['); - else if (path_tail + else if (path_tail && path_tail->parent_type == GVM_JSON_PULL_CONTAINER_OBJECT) g_string_append_c (parser->parse_buffer, '{'); else { if (error_message) - *error_message - = g_strdup ("can only expand after array or object start"); + *error_message = + g_strdup ("can only expand after array or object start"); return NULL; } - + start_depth = path_tail->depth; in_string = escape_next_char = FALSE; in_expanded_container = TRUE; @@ -865,14 +844,14 @@ gvm_json_pull_expand_container (gvm_json_pull_parser_t *parser, if (parser->parse_buffer->len >= parser->parse_buffer_limit) { if (error_message) - *error_message - = g_strdup_printf ("container exceeds size limit of %zu bytes", - parser->parse_buffer_limit); + *error_message = + g_strdup_printf ("container exceeds size limit of %zu bytes", + parser->parse_buffer_limit); return NULL; } - + g_string_append_c (parser->parse_buffer, parser->last_read_char); - + if (escape_next_char) { escape_next_char = FALSE; @@ -885,20 +864,18 @@ gvm_json_pull_expand_container (gvm_json_pull_parser_t *parser, else { switch (parser->last_read_char) - { + { case '"': in_string = TRUE; break; case '[': - path_tail - = gvm_json_pull_path_elem_new (GVM_JSON_PULL_CONTAINER_ARRAY, - parser->path->length); + path_tail = gvm_json_pull_path_elem_new ( + GVM_JSON_PULL_CONTAINER_ARRAY, parser->path->length); g_queue_push_tail (parser->path, path_tail); break; case '{': - path_tail - = gvm_json_pull_path_elem_new (GVM_JSON_PULL_CONTAINER_OBJECT, - parser->path->length); + path_tail = gvm_json_pull_path_elem_new ( + GVM_JSON_PULL_CONTAINER_OBJECT, parser->path->length); g_queue_push_tail (parser->path, path_tail); break; case ']': @@ -906,8 +883,8 @@ gvm_json_pull_expand_container (gvm_json_pull_parser_t *parser, if (path_tail->parent_type != GVM_JSON_PULL_CONTAINER_ARRAY) { if (error_message) - *error_message - = g_strdup ("unexpected closing square bracket"); + *error_message = + g_strdup ("unexpected closing square bracket"); return NULL; } if (path_tail->depth == start_depth) @@ -918,14 +895,14 @@ gvm_json_pull_expand_container (gvm_json_pull_parser_t *parser, if (path_tail->parent_type != GVM_JSON_PULL_CONTAINER_OBJECT) { if (error_message) - *error_message - = g_strdup ("unexpected closing curly brace"); + *error_message = + g_strdup ("unexpected closing curly brace"); return NULL; } if (path_tail->depth == start_depth) in_expanded_container = FALSE; break; - } + } } gvm_json_pull_parser_next_char (parser); } @@ -946,7 +923,7 @@ gvm_json_pull_expand_container (gvm_json_pull_parser_t *parser, expanded = cJSON_Parse (parser->parse_buffer->str); g_string_truncate (parser->parse_buffer, 0); PARSE_VALUE_NEXT_EXPECT - + if (expanded == NULL && error_message) *error_message = g_strdup ("could not parse expanded container"); @@ -984,9 +961,7 @@ gchar * gvm_json_path_to_string (GQueue *path) { GString *path_string = g_string_new ("$"); - g_queue_foreach (path, - (GFunc) gvm_json_path_string_add_elem, - path_string); + g_queue_foreach (path, (GFunc) gvm_json_path_string_add_elem, path_string); return g_string_free (path_string, FALSE); } diff --git a/util/jsonpull.h b/util/jsonpull.h index 01e12aa1..8d8343a0 100644 --- a/util/jsonpull.h +++ b/util/jsonpull.h @@ -3,7 +3,6 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ - #ifndef _GVM_JSONPULL_H #define _GVM_JSONPULL_H @@ -16,26 +15,29 @@ /** * @brief Type of container the parser is currently in */ -typedef enum { - GVM_JSON_PULL_CONTAINER_NONE = 0, ///< No container / document root - GVM_JSON_PULL_CONTAINER_ARRAY, ///< Array - GVM_JSON_PULL_CONTAINER_OBJECT, ///< Object +typedef enum +{ + GVM_JSON_PULL_CONTAINER_NONE = 0, ///< No container / document root + GVM_JSON_PULL_CONTAINER_ARRAY, ///< Array + GVM_JSON_PULL_CONTAINER_OBJECT, ///< Object } gvm_json_pull_container_type_t; /** * @brief Path element types for the JSON pull parser. */ -typedef struct gvm_json_path_elem { +typedef struct gvm_json_path_elem +{ gvm_json_pull_container_type_t parent_type; ///< parent container type - int index; ///< Index of the element within the parent - char *key; ///< Key if element is in an object - int depth; ///< Number of ancestor elements + int index; ///< Index of the element within the parent + char *key; ///< Key if element is in an object + int depth; ///< Number of ancestor elements } gvm_json_path_elem_t; /** * @brief Event types for the JSON pull parser */ -typedef enum { +typedef enum +{ GVM_JSON_PULL_EVENT_UNDEFINED = 0, GVM_JSON_PULL_EVENT_ARRAY_START, GVM_JSON_PULL_EVENT_ARRAY_END, @@ -52,17 +54,19 @@ typedef enum { /** * @brief Event generated by the JSON pull parser. */ -typedef struct { - gvm_json_pull_event_type_t type; ///< Type of event - GQueue *path; ///< Path to the event value - cJSON *value; ///< Value for non-container value events - gchar *error_message; ///< Error message, NULL on success +typedef struct +{ + gvm_json_pull_event_type_t type; ///< Type of event + GQueue *path; ///< Path to the event value + cJSON *value; ///< Value for non-container value events + gchar *error_message; ///< Error message, NULL on success } gvm_json_pull_event_t; /** * @brief Expected token state for the JSON pull parser */ -typedef enum { +typedef enum +{ GVM_JSON_PULL_EXPECT_UNDEFINED = 0, ///< Undefined state GVM_JSON_PULL_EXPECT_VALUE, ///< Expect start of a value GVM_JSON_PULL_EXPECT_KEY, ///< Expect start of a key @@ -77,19 +81,20 @@ typedef enum { /** * @brief A json pull parser */ -typedef struct { - GQueue *path; ///< Path to the current value +typedef struct +{ + GQueue *path; ///< Path to the current value gvm_json_path_elem_t *path_add; ///< Path elem to add in next step gvm_json_pull_expect_t expect; ///< Current expected token - int keyword_pos; ///< Position in a keyword like "true" or "null" - FILE *input_stream; ///< Input stream - char *read_buffer; ///< Stream reading buffer - size_t read_buffer_size; ///< Size of the stream reading buffer - size_t last_read_size; ///< Size of last stream read - int last_read_char; ///< Character last read from stream - size_t read_pos; ///< Position in current read - GString *parse_buffer; ///< Buffer for parsing values and object keys - size_t parse_buffer_limit; ///< Maximum parse buffer size + int keyword_pos; ///< Position in a keyword like "true" or "null" + FILE *input_stream; ///< Input stream + char *read_buffer; ///< Stream reading buffer + size_t read_buffer_size; ///< Size of the stream reading buffer + size_t last_read_size; ///< Size of last stream read + int last_read_char; ///< Character last read from stream + size_t read_pos; ///< Position in current read + GString *parse_buffer; ///< Buffer for parsing values and object keys + size_t parse_buffer_limit; ///< Maximum parse buffer size } gvm_json_pull_parser_t; gchar * @@ -111,8 +116,8 @@ void gvm_json_pull_event_cleanup (gvm_json_pull_event_t *); void -gvm_json_pull_parser_init_full (gvm_json_pull_parser_t *, FILE *, - size_t, size_t); +gvm_json_pull_parser_init_full (gvm_json_pull_parser_t *, FILE *, size_t, + size_t); void gvm_json_pull_parser_init (gvm_json_pull_parser_t *, FILE *); @@ -120,15 +125,13 @@ gvm_json_pull_parser_init (gvm_json_pull_parser_t *, FILE *); void gvm_json_pull_parser_cleanup (gvm_json_pull_parser_t *); -void gvm_json_pull_parser_next(gvm_json_pull_parser_t*, - gvm_json_pull_event_t*); +void +gvm_json_pull_parser_next (gvm_json_pull_parser_t *, gvm_json_pull_event_t *); cJSON * -gvm_json_pull_expand_container (gvm_json_pull_parser_t *, - gchar **); +gvm_json_pull_expand_container (gvm_json_pull_parser_t *, gchar **); gchar * gvm_json_path_to_string (GQueue *path); - #endif /* _GVM_JSONPULL_H */ \ No newline at end of file diff --git a/util/jsonpull_tests.c b/util/jsonpull_tests.c index 18056cd5..808c4333 100644 --- a/util/jsonpull_tests.c +++ b/util/jsonpull_tests.c @@ -5,9 +5,9 @@ #include "jsonpull.c" -#include #include #include +#include Describe (jsonpull); BeforeEach (jsonpull) @@ -17,14 +17,13 @@ AfterEach (jsonpull) { } - /* * Helper function to open a string as a read-only stream. */ static inline FILE * fstropen_r (const char *str) { - return fmemopen ((void*)str, strlen(str), "r"); + return fmemopen ((void *) str, strlen (str), "r"); } static ssize_t @@ -41,42 +40,38 @@ read_with_error_on_eof (void *stream_cookie, char *buf, size_t size) return ret; } - -#define INIT_JSON_PARSER(json_string) \ - gvm_json_pull_event_t event; \ - gvm_json_pull_parser_t parser; \ - FILE *jsonstream; \ - jsonstream = fstropen_r (json_string); \ - gvm_json_pull_event_init (&event); \ +#define INIT_JSON_PARSER(json_string) \ + gvm_json_pull_event_t event; \ + gvm_json_pull_parser_t parser; \ + FILE *jsonstream; \ + jsonstream = fstropen_r (json_string); \ + gvm_json_pull_event_init (&event); \ gvm_json_pull_parser_init_full (&parser, jsonstream, 100, 4); -#define INIT_READ_ERROR_JSON_PARSER(json_string) \ - gvm_json_pull_event_t event; \ - gvm_json_pull_parser_t parser; \ - FILE *jsonstream = fstropen_r (json_string); \ - cookie_io_functions_t io_functions = { \ - .read = read_with_error_on_eof, \ - .write = NULL, \ - .seek = NULL, \ - .close = NULL \ - }; \ - FILE *errorstream = fopencookie (jsonstream, "r", io_functions); \ - gvm_json_pull_event_init (&event); \ +#define INIT_READ_ERROR_JSON_PARSER(json_string) \ + gvm_json_pull_event_t event; \ + gvm_json_pull_parser_t parser; \ + FILE *jsonstream = fstropen_r (json_string); \ + cookie_io_functions_t io_functions = {.read = read_with_error_on_eof, \ + .write = NULL, \ + .seek = NULL, \ + .close = NULL}; \ + FILE *errorstream = fopencookie (jsonstream, "r", io_functions); \ + gvm_json_pull_event_init (&event); \ gvm_json_pull_parser_init_full (&parser, errorstream, 100, 4); -#define CLEANUP_JSON_PARSER \ - gvm_json_pull_event_cleanup (&event); \ - gvm_json_pull_parser_cleanup (&parser); \ - fclose (jsonstream); \ +#define CLEANUP_JSON_PARSER \ + gvm_json_pull_event_cleanup (&event); \ + gvm_json_pull_parser_cleanup (&parser); \ + fclose (jsonstream); -#define CHECK_PATH_EQUALS(expected_path_str) \ - path_str = gvm_json_path_to_string (event.path); \ - assert_that (path_str, is_equal_to_string (expected_path_str)); \ +#define CHECK_PATH_EQUALS(expected_path_str) \ + path_str = gvm_json_path_to_string (event.path); \ + assert_that (path_str, is_equal_to_string (expected_path_str)); \ g_free (path_str); #define JSON_READ_ERROR "error reading JSON stream: Input/output error" - Ensure (jsonpull, can_json_escape_strings) { const char *unescaped_string = "\"'Abc\\\b\f\n\r\t\001Äöü'\""; @@ -100,7 +95,7 @@ Ensure (jsonpull, can_init_parser_with_defaults) { gvm_json_pull_parser_t parser; FILE *strfile = fstropen_r ("[]"); - + gvm_json_pull_parser_init (&parser, strfile); assert_that (parser.input_stream, is_equal_to (strfile)); assert_that (parser.parse_buffer_limit, @@ -154,7 +149,7 @@ Ensure (jsonpull, can_parse_empty_strings) { INIT_JSON_PARSER ("\"\"") - gvm_json_pull_parser_next (&parser, &event); + gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_STRING)); assert_that (event.value->valuestring, is_equal_to_string ("")); @@ -167,10 +162,9 @@ Ensure (jsonpull, can_parse_strings_with_content) { INIT_JSON_PARSER ("\n\"123\\tXYZ\\nÄöü\"\n") - gvm_json_pull_parser_next (&parser, &event); + gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_STRING)); - assert_that (event.value->valuestring, - is_equal_to_string ("123\tXYZ\nÄöü")); + assert_that (event.value->valuestring, is_equal_to_string ("123\tXYZ\nÄöü")); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_EOF)); @@ -339,7 +333,7 @@ Ensure (jsonpull, can_parse_nested_containers) gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ARRAY_START)); CHECK_PATH_EQUALS ("$") - + gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_START)); CHECK_PATH_EQUALS ("$[0]") @@ -369,7 +363,7 @@ Ensure (jsonpull, can_parse_nested_containers) gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ARRAY_END)); CHECK_PATH_EQUALS ("$[0]['B']['C']") - + gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_END)); CHECK_PATH_EQUALS ("$[0]['B']") @@ -395,7 +389,7 @@ Ensure (jsonpull, can_parse_nested_containers) gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ARRAY_END)); CHECK_PATH_EQUALS ("$[1]") - + gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ARRAY_END)); CHECK_PATH_EQUALS ("$") @@ -436,12 +430,12 @@ Ensure (jsonpull, can_expand_arrays) assert_that (cJSON_IsArray (expanded), is_true); child = expanded->child; assert_that (child, is_not_null); - assert_that (cJSON_IsNumber(child), is_true); - assert_that (child->valueint, is_equal_to(1)); + assert_that (cJSON_IsNumber (child), is_true); + assert_that (child->valueint, is_equal_to (1)); child = child->next; assert_that (child, is_null); cJSON_free (expanded); - + // multi-element array gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ARRAY_START)); @@ -452,14 +446,14 @@ Ensure (jsonpull, can_expand_arrays) assert_that (cJSON_IsArray (expanded), is_true); child = expanded->child; assert_that (child, is_not_null); - assert_that (cJSON_IsNumber(child), is_true); - assert_that (child->valueint, is_equal_to(2)); + assert_that (cJSON_IsNumber (child), is_true); + assert_that (child->valueint, is_equal_to (2)); child = child->next; assert_that (child, is_not_null); - assert_that (cJSON_IsArray(child), is_true); - assert_that (child->child->valueint, is_equal_to(3)); + assert_that (cJSON_IsArray (child), is_true); + assert_that (child->child->valueint, is_equal_to (3)); cJSON_free (expanded); - + // string array gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ARRAY_START)); @@ -470,12 +464,12 @@ Ensure (jsonpull, can_expand_arrays) assert_that (cJSON_IsArray (expanded), is_true); child = expanded->child; assert_that (child, is_not_null); - assert_that (cJSON_IsString(child), is_true); - assert_that (child->valuestring, is_equal_to_string("A")); + assert_that (cJSON_IsString (child), is_true); + assert_that (child->valuestring, is_equal_to_string ("A")); child = child->next; assert_that (child, is_not_null); - assert_that (cJSON_IsString(child), is_true); - assert_that (child->valuestring, is_equal_to_string("\"B]")); + assert_that (cJSON_IsString (child), is_true); + assert_that (child->valuestring, is_equal_to_string ("\"B]")); cJSON_free (expanded); // array end and EOF @@ -498,7 +492,7 @@ Ensure (jsonpull, can_expand_objects) gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_START)); CHECK_PATH_EQUALS ("$") - + gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_START)); CHECK_PATH_EQUALS ("$['A']") @@ -516,17 +510,17 @@ Ensure (jsonpull, can_expand_objects) assert_that (cJSON_IsObject (expanded), is_true); child = expanded->child; assert_that (child, is_not_null); - assert_that (cJSON_IsString(child), is_true); + assert_that (cJSON_IsString (child), is_true); assert_that (child->string, is_equal_to_string ("C")); assert_that (child->valuestring, is_equal_to_string ("\"D}")); child = child->next; assert_that (child, is_not_null); - assert_that (cJSON_IsNumber(child), is_true); + assert_that (cJSON_IsNumber (child), is_true); assert_that (child->string, is_equal_to_string ("E")); assert_that (child->valueint, is_equal_to (123)); child = child->next; assert_that (child, is_not_null); - assert_that (cJSON_IsObject(child), is_true); + assert_that (cJSON_IsObject (child), is_true); assert_that (child->string, is_equal_to_string ("F")); assert_that (child->child, is_null); cJSON_free (expanded); @@ -570,8 +564,7 @@ Ensure (jsonpull, fails_for_incomplete_true) gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -613,8 +606,7 @@ Ensure (jsonpull, fails_for_string_eof) gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -682,8 +674,7 @@ Ensure (jsonpull, fails_for_array_eof) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ARRAY_START)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -697,8 +688,7 @@ Ensure (jsonpull, fails_for_array_eof_after_value) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_NUMBER)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -712,8 +702,7 @@ Ensure (jsonpull, fails_for_array_eof_after_comma) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_NUMBER)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -729,7 +718,6 @@ Ensure (jsonpull, fails_for_array_read_error) CLEANUP_JSON_PARSER } - Ensure (jsonpull, fails_for_invalid_array_bracket) { INIT_JSON_PARSER ("[}"); @@ -794,8 +782,7 @@ Ensure (jsonpull, fails_for_object_key_eof) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_START)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -858,8 +845,7 @@ Ensure (jsonpull, fails_for_object_colon_eof) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_START)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -883,8 +869,7 @@ Ensure (jsonpull, fails_for_object_colon_other_char) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_START)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("expected colon")); + assert_that (event.error_message, is_equal_to_string ("expected colon")); CLEANUP_JSON_PARSER } @@ -896,8 +881,7 @@ Ensure (jsonpull, fails_for_object_value_eof) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_OBJECT_START)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -949,8 +933,7 @@ Ensure (jsonpull, fails_for_object_eof_after_value) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_NUMBER)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -979,8 +962,7 @@ Ensure (jsonpull, fails_for_object_eof_after_comma) assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_NUMBER)); gvm_json_pull_parser_next (&parser, &event); assert_that (event.type, is_equal_to (GVM_JSON_PULL_EVENT_ERROR)); - assert_that (event.error_message, - is_equal_to_string ("unexpected EOF")); + assert_that (event.error_message, is_equal_to_string ("unexpected EOF")); CLEANUP_JSON_PARSER } @@ -1017,9 +999,8 @@ Ensure (jsonpull, fails_for_expand_before_container) cjson_value = gvm_json_pull_expand_container (&parser, &error_message); assert_that (cjson_value, is_null); - assert_that (error_message, - is_equal_to_string ("can only expand after" - " array or object start")); + assert_that (error_message, is_equal_to_string ("can only expand after" + " array or object start")); CLEANUP_JSON_PARSER } @@ -1037,9 +1018,8 @@ Ensure (jsonpull, fails_for_expand_after_value) cjson_value = gvm_json_pull_expand_container (&parser, &error_message); assert_that (cjson_value, is_null); - assert_that (error_message, - is_equal_to_string ("can only expand after" - " array or object start")); + assert_that (error_message, is_equal_to_string ("can only expand after" + " array or object start")); CLEANUP_JSON_PARSER } @@ -1145,7 +1125,6 @@ Ensure (jsonpull, fails_for_expand_read_error) CLEANUP_JSON_PARSER } - int main (int argc, char **argv) { @@ -1160,7 +1139,7 @@ main (int argc, char **argv) add_test_with_context (suite, jsonpull, can_parse_false); add_test_with_context (suite, jsonpull, can_parse_true); add_test_with_context (suite, jsonpull, can_parse_null); - + add_test_with_context (suite, jsonpull, can_parse_empty_strings); add_test_with_context (suite, jsonpull, can_parse_strings_with_content);