Greenbone Vulnerability Management Libraries: util/jsonpull.c File Reference

Greenbone Vulnerability Management Libraries 23.1.2

Macros | Functions
jsonpull.c File Reference
`#include "jsonpull.h"`
`#include <assert.h>`

Go to the source code of this file.

## Macros
#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.

## Functions
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.
void | gvm_json_pull_path_elem_free (gvm_json_path_elem_t *elem)
| Frees a JSON path element.
void | gvm_json_pull_event_init (gvm_json_pull_event_t *event)
| Initializes a JSON pull event data structure.
void | gvm_json_pull_event_cleanup (gvm_json_pull_event_t *event)
| Frees all data of JSON pull event data structure.
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.
void | gvm_json_pull_parser_init (gvm_json_pull_parser_t *parser, FILE *input_stream)
| Initializes a JSON pull parser with default buffer sizes.
void | gvm_json_pull_parser_cleanup (gvm_json_pull_parser_t *parser)
| Frees the data of a JSON pull parser.
static gchar * | gvm_json_read_stream_error_str ()
| Generates message for an error that occurred reading the JSON stream.
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.
static int | gvm_json_pull_parser_next_char (gvm_json_pull_parser_t *parser)
| Reads the next character in a pull parser input stream.
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.
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.
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.
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.
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.
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.
static void | parse_value_next_expect (gvm_json_pull_parser_t *parser)
| Updates the expectation for a JSON pull parser according to the path.
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.
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.
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.
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.
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.
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.
gchar * | gvm_json_path_to_string (GQueue *path)
| Converts a path as used by a JSON pull parser to a JSONPath string.

## Macro Definition Documentation

## ◆ GVM_JSON_CHAR_EOF

#define GVM_JSON_CHAR_EOF -1

End of file.

## ◆ GVM_JSON_CHAR_ERROR

#define GVM_JSON_CHAR_ERROR -2

Error reading file.

## ◆ GVM_JSON_CHAR_UNDEFINED

#define GVM_JSON_CHAR_UNDEFINED -3

Undefined state.

## Function Documentation

## ◆ gvm_json_path_string_add_elem()

void gvm_json_path_string_add_elem | ( | gvm_json_path_elem_t * | path_elem,
|  | GString * | path_string )
| static

Appends a string path element to a JSONPath string.

Parameters

  [in] | path_elem | The path element to append
  [in] | path_string | The path string to append to

## ◆ gvm_json_path_to_string()

gchar * gvm_json_path_to_string | ( | GQueue * | path | ) |

Converts a path as used by a JSON pull parser to a JSONPath string.

Parameters

  [in] | path | The path to convert

Returns
  Newly allocated string of the path in JSONPath bracket notation

## ◆ gvm_json_pull_check_parse_buffer_size()

int gvm_json_pull_check_parse_buffer_size | ( | const char * | value_type,
|  | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event )
| static

Checks if the parse buffer limit of a JSON pull parser is reached.

Parameters

  [in] | value_type | The value type to include in the error message
  [in] | parser | The parser to check the parse buffer of
  [in] | event | Event data for error status and message if needed

Returns
  0 if buffer size is okay, 1 if limit was reached

## ◆ gvm_json_pull_event_cleanup()

void gvm_json_pull_event_cleanup | ( | gvm_json_pull_event_t * | event | ) |

Frees all data of JSON pull event data structure.

Parameters

  [in] | event | The event structure to clean up

## ◆ gvm_json_pull_event_init()

void gvm_json_pull_event_init | ( | gvm_json_pull_event_t * | event | ) |

Initializes a JSON pull event data structure.

Parameters

  [in] | event | The event structure to initialize

## ◆ gvm_json_pull_expand_container()

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.

This should be called after an array or object start event.

Parameters

  [in] | parser | Parser to get the current container element from
  [out] | error_message | Error message output

Returns
  The expanded container as a cJSON object if successful, else NULL

## ◆ gvm_json_pull_handle_read_end()

void gvm_json_pull_handle_read_end | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event,
|  | gboolean | allow_eof )
| static

Handles error or EOF after reading a character in JSON pull parser.

Parameters

  [in] | parser | Parser to get the last read character from
  [in] | event | Event data to set EOF or error status in
  [in] | allow_eof | Whether to allow EOF, generate error on EOF if FALSE

## ◆ gvm_json_pull_parse_buffered()

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 )
| static

Tries to parse the buffer content of a JSON pull parser.

Parameters

  [in] | parser | The parser to use the parse buffer of
  [in] | event | Event set error of if necessary
  [in] | value_name | Name of the value for error message if needed
  [in] | validate_func | Function for validating the parsed value
  [out] | cjson_value | Return of the parsed cJSON object on success

Returns
  0 success, 1 error

## ◆ gvm_json_pull_parse_comma()

int gvm_json_pull_parse_comma | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event )
| static

Handles the case that a comma is expected in a JSON pull parser.

This will continue the parsing until a comma or the end of the current array/object was reached or an error occurred.

Parameters

  [in] | parser | Parser to process
  [in] | event | Event data to set error or end of object status in

Returns
  1 if an error occurred, 0 otherwise

## ◆ gvm_json_pull_parse_key()

int gvm_json_pull_parse_key | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event )
| static

Handles the case that an object key is expected in a JSON pull parser.

This will continue the parsing until the value is expected, the end of the current object was reached or an error occurred.

Parameters

  [in] | parser | Parser to process
  [in] | event | Event data to set error or end of object status in

Returns
  1 if an error occurred, 0 otherwise

## ◆ gvm_json_pull_parse_keyword()

int gvm_json_pull_parse_keyword | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event,
|  | const char * | keyword )
| static

Parses a keyword value in a JSON pull parser.

The parser is expected to be at the first character of the keyword and will be at the first character after the keyword on success.

Parameters

  [in] | parser | Parser to handle the keyword value in
  [in] | event | Event data to set EOF or error status in
  [in] | keyword | The expected keyword, e.g. "null", "true", "false".

Returns
  1 if an error occurred, 0 otherwise

## ◆ gvm_json_pull_parse_number()

int gvm_json_pull_parse_number | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event,
|  | cJSON ** | cjson_value )
| static

Parses a number in a JSON pull parser.

The parser is expected to be at the first character of the number and will be at the first non-number character on success.

Parameters

  [in] | parser | Parser to handle the number value in
  [in] | event | Event data to set EOF or error status in
  [out] | cjson_value | The cJSON value for the number on success.

Returns
  1 if an error occurred, 0 otherwise

## ◆ gvm_json_pull_parse_string()

int gvm_json_pull_parse_string | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event,
|  | cJSON ** | cjson_value )
| static

Parses a string in a JSON pull parser.

The parser is expected to be at the opening quote mark and will be at the character after the closing quote mark on success.

Parameters

  [in] | parser | Parser to handle the string value in
  [in] | event | Event data to set EOF or error status in
  [out] | cjson_value | The cJSON value for the string on success

Returns
  1 if an error occurred (including EOF), 0 otherwise

## ◆ gvm_json_pull_parse_value()

int gvm_json_pull_parse_value | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event )
| static

Handles the case that a value is expected in a JSON pull parser.

This will continue the parsing until a value or the end of the current array/object was parsed or an error occurred.

Parameters

  [in] | parser | Parser to process
  [in] | event | Event data to set error or end of object status in

Returns
  1 if an error occurred, 0 otherwise

## ◆ gvm_json_pull_parser_cleanup()

void gvm_json_pull_parser_cleanup | ( | gvm_json_pull_parser_t * | parser | ) |

Frees the data of a JSON pull parser.

Parameters

  [in] | parser | The parser data structure to free the data of

## ◆ gvm_json_pull_parser_init()

void gvm_json_pull_parser_init | ( | gvm_json_pull_parser_t * | parser,
|  | FILE * | input_stream )

Initializes a JSON pull parser with default buffer sizes.

Parameters

  [in] | parser | The parser data structure to initialize
  [in] | input_stream | The JSON input stream

## ◆ gvm_json_pull_parser_init_full()

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.

Parameters

  [in] | parser | The parser data structure to initialize
  [in] | input_stream | The JSON input stream
  [in] | parse_buffer_limit | Maximum buffer size for parsing values
  [in] | read_buffer_size | Buffer size for reading from the stream

## ◆ gvm_json_pull_parser_next()

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.

Note: This invalidates previous event data like the cJSON value.

Parameters

  [in] | parser | The JSON pull parser to process until the next event
  [in] | event | Structure to store event data in.

## ◆ gvm_json_pull_parser_next_char()

int gvm_json_pull_parser_next_char | ( | gvm_json_pull_parser_t * | parser | ) |
| static

Reads the next character in a pull parser input stream.

Parameters

  [in] | parser | The parser to read the next character from

Returns
  The character code, GVM_JSON_CHAR_ERROR or GVM_JSON_CHAR_EOF.

## ◆ gvm_json_pull_path_elem_free()

void gvm_json_pull_path_elem_free | ( | gvm_json_path_elem_t * | elem | ) |

Frees a JSON path element.

Parameters

  [in] | elem | The element to free

## ◆ gvm_json_pull_path_elem_new()

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.

Parameters

  [in] | parent_type | Type of the parent (array, object, none/root)
  [in] | depth | The depth in the document tree

Returns
  The newly allocated path element

## ◆ gvm_json_pull_skip_space()

int gvm_json_pull_skip_space | ( | gvm_json_pull_parser_t * | parser,
|  | gvm_json_pull_event_t * | event,
|  | gboolean | allow_eof )
| static

Skips whitespaces in the input stream of a JSON pull parser.

The parser will be at the first non-whitespace character on success.

Parameters

  [in] | parser | Parser to skip the whitespaces in
  [in] | event | Event data to set EOF or error status in
  [in] | allow_eof | Whether to allow EOF, generate error on EOF if FALSE

Returns
  1 if EOF was reached or an error occurred, 0 otherwise

## ◆ gvm_json_read_stream_error_str()

gchar * gvm_json_read_stream_error_str | ( |  | ) |
| static

Generates message for an error that occurred reading the JSON stream.

Returns
  The newly allocated error message

## ◆ parse_value_next_expect()

void parse_value_next_expect | ( | gvm_json_pull_parser_t * | parser | ) |
| static

Updates the expectation for a JSON pull parser according to the path.

Parameters

  [in] | parser | The parser to update.