jsonpull.h
Go to the documentation of this file.
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef _GVM_JSONPULL_H
#define _GVM_JSONPULL_H
#define _GNU_SOURCE
#include "json.h"
#include <glib.h>
#include <stdio.h>
typedef enum
{
GVM_JSON_PULL_CONTAINER_NONE = 0,
GVM_JSON_PULL_CONTAINER_ARRAY,
GVM_JSON_PULL_CONTAINER_OBJECT,
} gvm_json_pull_container_type_t;
typedef struct gvm_json_path_elem
{
gvm_json_pull_container_type_t parent_type;
int index;
char *key;
int depth;
} gvm_json_path_elem_t;
typedef enum
{
GVM_JSON_PULL_EVENT_UNDEFINED = 0,
GVM_JSON_PULL_EVENT_ARRAY_START,
GVM_JSON_PULL_EVENT_ARRAY_END,
GVM_JSON_PULL_EVENT_OBJECT_START,
GVM_JSON_PULL_EVENT_OBJECT_END,
GVM_JSON_PULL_EVENT_STRING,
GVM_JSON_PULL_EVENT_NUMBER,
GVM_JSON_PULL_EVENT_BOOLEAN,
GVM_JSON_PULL_EVENT_NULL,
GVM_JSON_PULL_EVENT_EOF,
GVM_JSON_PULL_EVENT_ERROR,
} gvm_json_pull_event_type_t;
typedef struct
{
gvm_json_pull_event_type_t type;
GQueue *path;
cJSON *value;
gchar *error_message;
} gvm_json_pull_event_t;
typedef enum
{
GVM_JSON_PULL_EXPECT_UNDEFINED = 0,
GVM_JSON_PULL_EXPECT_VALUE,
GVM_JSON_PULL_EXPECT_KEY,
GVM_JSON_PULL_EXPECT_COMMA,
GVM_JSON_PULL_EXPECT_EOF
} gvm_json_pull_expect_t;
#define GVM_JSON_PULL_PARSE_BUFFER_LIMIT 10485760
#define GVM_JSON_PULL_READ_BUFFER_SIZE 4096
typedef struct
{
GQueue *path;
gvm_json_path_elem_t *path_add;
gvm_json_pull_expect_t expect;
int keyword_pos;
FILE *input_stream;
char *read_buffer;
size_t read_buffer_size;
size_t last_read_size;
int last_read_char;
size_t read_pos;
GString *parse_buffer;
size_t parse_buffer_limit;
} gvm_json_pull_parser_t;
gvm_json_path_elem_t *
gvm_json_pull_path_elem_new (gvm_json_pull_container_type_t, int);
void
gvm_json_pull_path_elem_free (gvm_json_path_elem_t *);
void
gvm_json_pull_event_init (gvm_json_pull_event_t *);
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);
void
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 *);
cJSON *
gvm_json_pull_expand_container (gvm_json_pull_parser_t *, gchar **);
gchar *
gvm_json_path_to_string (GQueue *path);
#endif /* _GVM_JSONPULL_H */
json.h
gvm_json_pull_expand_container
cJSON * gvm_json_pull_expand_container(gvm_json_pull_parser_t *, gchar **)
Expands the current array or object of a JSON pull parser.
Definition jsonpull.c:744
gvm_json_pull_parser_next
void gvm_json_pull_parser_next(gvm_json_pull_parser_t *, gvm_json_pull_event_t *)
Get the next event from a JSON pull parser.
Definition jsonpull.c:669
gvm_json_pull_path_elem_free
void gvm_json_pull_path_elem_free(gvm_json_path_elem_t *)
Frees a JSON path element.
Definition jsonpull.c:38
gvm_json_pull_parser_init_full
void gvm_json_pull_parser_init_full(gvm_json_pull_parser_t *, FILE *, size_t, size_t)
Initializes a JSON pull parser.
Definition jsonpull.c:78
gvm_json_pull_path_elem_new
gvm_json_path_elem_t * gvm_json_pull_path_elem_new(gvm_json_pull_container_type_t, int)
Creates a new JSON path element.
Definition jsonpull.c:23
gvm_json_pull_container_type_t
gvm_json_pull_container_type_t
Type of container the parser is currently in.
Definition jsonpull.h:20
GVM_JSON_PULL_CONTAINER_NONE
@ GVM_JSON_PULL_CONTAINER_NONE
No container / document root.
Definition jsonpull.h:21
GVM_JSON_PULL_CONTAINER_OBJECT
@ GVM_JSON_PULL_CONTAINER_OBJECT
Object.
Definition jsonpull.h:23
GVM_JSON_PULL_CONTAINER_ARRAY
@ GVM_JSON_PULL_CONTAINER_ARRAY
Array.
Definition jsonpull.h:22
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.
Definition jsonpull.c:902
gvm_json_pull_parser_cleanup
void gvm_json_pull_parser_cleanup(gvm_json_pull_parser_t *)
Frees the data of a JSON pull parser.
Definition jsonpull.c:120
gvm_json_pull_parser_init
void gvm_json_pull_parser_init(gvm_json_pull_parser_t *, FILE *)
Initializes a JSON pull parser with default buffer sizes.
Definition jsonpull.c:109
gvm_json_pull_expect_t
gvm_json_pull_expect_t
Expected token state for the JSON pull parser.
Definition jsonpull.h:70
GVM_JSON_PULL_EXPECT_UNDEFINED
@ GVM_JSON_PULL_EXPECT_UNDEFINED
Undefined state.
Definition jsonpull.h:71
GVM_JSON_PULL_EXPECT_VALUE
@ GVM_JSON_PULL_EXPECT_VALUE
Expect start of a value.
Definition jsonpull.h:72
GVM_JSON_PULL_EXPECT_KEY
@ GVM_JSON_PULL_EXPECT_KEY
Expect start of a key.
Definition jsonpull.h:73
GVM_JSON_PULL_EXPECT_EOF
@ GVM_JSON_PULL_EXPECT_EOF
Expect end of file.
Definition jsonpull.h:75
GVM_JSON_PULL_EXPECT_COMMA
@ GVM_JSON_PULL_EXPECT_COMMA
Expect comma or container end brace.
Definition jsonpull.h:74
gvm_json_pull_event_init
void gvm_json_pull_event_init(gvm_json_pull_event_t *)
Initializes a JSON pull event data structure.
Definition jsonpull.c:50
gvm_json_pull_event_cleanup
void gvm_json_pull_event_cleanup(gvm_json_pull_event_t *)
Frees all data of JSON pull event data structure.
Definition jsonpull.c:61
gvm_json_pull_event_type_t
gvm_json_pull_event_type_t
Event types for the JSON pull parser.
Definition jsonpull.h:41
GVM_JSON_PULL_EVENT_STRING
@ GVM_JSON_PULL_EVENT_STRING
Definition jsonpull.h:47
GVM_JSON_PULL_EVENT_OBJECT_START
@ GVM_JSON_PULL_EVENT_OBJECT_START
Definition jsonpull.h:45
GVM_JSON_PULL_EVENT_ERROR
@ GVM_JSON_PULL_EVENT_ERROR
Definition jsonpull.h:52
GVM_JSON_PULL_EVENT_NULL
@ GVM_JSON_PULL_EVENT_NULL
Definition jsonpull.h:50
GVM_JSON_PULL_EVENT_EOF
@ GVM_JSON_PULL_EVENT_EOF
Definition jsonpull.h:51
GVM_JSON_PULL_EVENT_NUMBER
@ GVM_JSON_PULL_EVENT_NUMBER
Definition jsonpull.h:48
GVM_JSON_PULL_EVENT_ARRAY_END
@ GVM_JSON_PULL_EVENT_ARRAY_END
Definition jsonpull.h:44
GVM_JSON_PULL_EVENT_OBJECT_END
@ GVM_JSON_PULL_EVENT_OBJECT_END
Definition jsonpull.h:46
GVM_JSON_PULL_EVENT_ARRAY_START
@ GVM_JSON_PULL_EVENT_ARRAY_START
Definition jsonpull.h:43
GVM_JSON_PULL_EVENT_BOOLEAN
@ GVM_JSON_PULL_EVENT_BOOLEAN
Definition jsonpull.h:49
GVM_JSON_PULL_EVENT_UNDEFINED
@ GVM_JSON_PULL_EVENT_UNDEFINED
Definition jsonpull.h:42
gvm_json_path_elem_t
struct gvm_json_path_elem gvm_json_path_elem_t
Path element types for the JSON pull parser.
gvm_json_path_elem
Path element types for the JSON pull parser.
Definition jsonpull.h:30
gvm_json_path_elem::depth
int depth
Number of ancestor elements.
Definition jsonpull.h:34
gvm_json_path_elem::index
int index
Index of the element within the parent.
Definition jsonpull.h:32
gvm_json_path_elem::key
char * key
Key if element is in an object.
Definition jsonpull.h:33
gvm_json_path_elem::parent_type
gvm_json_pull_container_type_t parent_type
parent container type
Definition jsonpull.h:31
gvm_json_pull_event_t
Event generated by the JSON pull parser.
Definition jsonpull.h:59
gvm_json_pull_event_t::error_message
gchar * error_message
Error message, NULL on success.
Definition jsonpull.h:63
gvm_json_pull_event_t::value
cJSON * value
Value for non-container value events.
Definition jsonpull.h:62
gvm_json_pull_event_t::path
GQueue * path
Path to the event value.
Definition jsonpull.h:61
gvm_json_pull_event_t::type
gvm_json_pull_event_type_t type
Type of event.
Definition jsonpull.h:60
gvm_json_pull_parser_t
A json pull parser.
Definition jsonpull.h:86
gvm_json_pull_parser_t::read_buffer
char * read_buffer
Stream reading buffer.
Definition jsonpull.h:92
gvm_json_pull_parser_t::read_buffer_size
size_t read_buffer_size
Size of the stream reading buffer.
Definition jsonpull.h:93
gvm_json_pull_parser_t::expect
gvm_json_pull_expect_t expect
Current expected token.
Definition jsonpull.h:89
gvm_json_pull_parser_t::last_read_size
size_t last_read_size
Size of last stream read.
Definition jsonpull.h:94
gvm_json_pull_parser_t::keyword_pos
int keyword_pos
Position in a keyword like "true" or "null".
Definition jsonpull.h:90
gvm_json_pull_parser_t::parse_buffer
GString * parse_buffer
Buffer for parsing values and object keys.
Definition jsonpull.h:97
gvm_json_pull_parser_t::parse_buffer_limit
size_t parse_buffer_limit
Maximum parse buffer size.
Definition jsonpull.h:98
gvm_json_pull_parser_t::path_add
gvm_json_path_elem_t * path_add
Path elem to add in next step.
Definition jsonpull.h:88
gvm_json_pull_parser_t::read_pos
size_t read_pos
Position in current read.
Definition jsonpull.h:96
gvm_json_pull_parser_t::path
GQueue * path
Path to the current value.
Definition jsonpull.h:87
gvm_json_pull_parser_t::last_read_char
int last_read_char
Character last read from stream.
Definition jsonpull.h:95
gvm_json_pull_parser_t::input_stream
FILE * input_stream
Input stream.
Definition jsonpull.h:91