Greenbone Vulnerability Management Libraries 22.12.2
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

gchar * gvm_json_string_escape (const char *string, gboolean single_quote)
 Escapes a string according to the JSON or JSONPath standard.
 
gvm_json_path_elem_tgvm_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_reset (gvm_json_pull_event_t *event)
 Resets a JSON pull event data structure for reuse.
 
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()

static 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_elemThe path element to append
[in]path_stringThe 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]pathThe path to convert
Returns
Newly allocated string of the path in JSONPath bracket notation

◆ gvm_json_pull_check_parse_buffer_size()

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

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

Parameters
[in]value_typeThe value type to include in the error message
[in]parserThe parser to check the parse buffer of
[in]eventEvent 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]eventThe 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]eventThe event structure to initialize

◆ gvm_json_pull_event_reset()

void gvm_json_pull_event_reset ( gvm_json_pull_event_t * event)

Resets a JSON pull event data structure for reuse.

Parameters
[in]eventThe event structure to reset

◆ 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]parserParser to get the current container element from
[out]error_messageError message output
Returns
The expanded container as a cJSON object if successful, else NULL

◆ gvm_json_pull_handle_read_end()

static 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]parserParser to get the last read character from
[in]eventEvent data to set EOF or error status in
[in]allow_eofWhether to allow EOF, generate error on EOF if FALSE

◆ gvm_json_pull_parse_buffered()

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

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

Parameters
[in]parserThe parser to use the parse buffer of
[in]eventEvent set error of if necessary
[in]value_nameName of the value for error message if needed
[in]validate_funcFunction for validating the parsed value
[out]cjson_valueReturn of the parsed cJSON object on success
Returns
0 success, 1 error

◆ gvm_json_pull_parse_comma()

static 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]parserParser to process
[in]eventEvent data to set error or end of object status in
Returns
1 if an error occurred, 0 otherwise

◆ gvm_json_pull_parse_key()

static 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]parserParser to process
[in]eventEvent data to set error or end of object status in
Returns
1 if an error occurred, 0 otherwise

◆ gvm_json_pull_parse_keyword()

static 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]parserParser to handle the keyword value in
[in]eventEvent data to set EOF or error status in
[in]keywordThe expected keyword, e.g. "null", "true", "false".
Returns
1 if an error occurred, 0 otherwise

◆ gvm_json_pull_parse_number()

static 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]parserParser to handle the number value in
[in]eventEvent data to set EOF or error status in
[out]cjson_valueThe cJSON value for the number on success.
Returns
1 if an error occurred, 0 otherwise

◆ gvm_json_pull_parse_string()

static 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]parserParser to handle the string value in
[in]eventEvent data to set EOF or error status in
[out]cjson_valueThe cJSON value for the string on success
Returns
1 if an error occurred (including EOF), 0 otherwise

◆ gvm_json_pull_parse_value()

static 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]parserParser to process
[in]eventEvent 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]parserThe 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]parserThe parser data structure to initialize
[in]input_streamThe 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]parserThe parser data structure to initialize
[in]input_streamThe JSON input stream
[in]parse_buffer_limitMaximum buffer size for parsing values
[in]read_buffer_sizeBuffer 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]parserThe JSON pull parser to process until the next event
[in]eventStructure to store event data in.

◆ gvm_json_pull_parser_next_char()

static 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]parserThe 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]elemThe 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_typeType of the parent (array, object, none/root)
[in]depthThe depth in the document tree
Returns
The newly allocated path element

◆ gvm_json_pull_skip_space()

static 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]parserParser to skip the whitespaces in
[in]eventEvent data to set EOF or error status in
[in]allow_eofWhether 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()

static 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

◆ gvm_json_string_escape()

gchar * gvm_json_string_escape ( const char * string,
gboolean single_quote )

Escapes a string according to the JSON or JSONPath standard.

Parameters
[in]stringThe string to escape
[in]single_quoteWhether to escape single quotes
Returns
The escaped string

◆ parse_value_next_expect()

static 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]parserThe parser to update.