Greenbone Vulnerability Management Libraries: util/jsonpull.h Source File

Greenbone Vulnerability Management Libraries 23.7.0

jsonpull.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#ifndef _GVM_UTIL_JSONPULL_H
7#define _GVM_UTIL_JSONPULL_H
8
9#define _GNU_SOURCE
10
11#include "json.h"
12
13#include <glib.h>
14#include <stdio.h>
15
19 typedef enum
20{
21 GVM_JSON_PULL_CONTAINER_NONE = 0,
22 GVM_JSON_PULL_CONTAINER_ARRAY,
23 GVM_JSON_PULL_CONTAINER_OBJECT,
24} gvm_json_pull_container_type_t;
25
29 typedef struct gvm_json_path_elem
30{
31 gvm_json_pull_container_type_t parent_type;
32 int index;
33 char *key;
34 int depth;
35} gvm_json_path_elem_t;
36
40 typedef enum
41{
42 GVM_JSON_PULL_EVENT_UNDEFINED = 0,
43 GVM_JSON_PULL_EVENT_ARRAY_START,
44 GVM_JSON_PULL_EVENT_ARRAY_END,
45 GVM_JSON_PULL_EVENT_OBJECT_START,
46 GVM_JSON_PULL_EVENT_OBJECT_END,
47 GVM_JSON_PULL_EVENT_STRING,
48 GVM_JSON_PULL_EVENT_NUMBER,
49 GVM_JSON_PULL_EVENT_BOOLEAN,
50 GVM_JSON_PULL_EVENT_NULL,
51 GVM_JSON_PULL_EVENT_EOF,
52 GVM_JSON_PULL_EVENT_ERROR,
53} gvm_json_pull_event_type_t;
54
58 typedef struct
59{
60 gvm_json_pull_event_type_t type;
61 GQueue *path;
62 cJSON *value;
63 gchar *error_message;
64} gvm_json_pull_event_t;
65
69 typedef enum
70{
71 GVM_JSON_PULL_EXPECT_UNDEFINED = 0,
72 GVM_JSON_PULL_EXPECT_VALUE,
73 GVM_JSON_PULL_EXPECT_KEY,
74 GVM_JSON_PULL_EXPECT_COMMA,
75 GVM_JSON_PULL_EXPECT_EOF
76} gvm_json_pull_expect_t;
77
78#define GVM_JSON_PULL_PARSE_BUFFER_LIMIT 10485760
79
80#define GVM_JSON_PULL_READ_BUFFER_SIZE 4096
81
85 typedef struct
86{
87 GQueue *path;
88 gvm_json_path_elem_t *path_add;
89 gvm_json_pull_expect_t expect;
90 int keyword_pos;
91 FILE *input_stream;
92 char *read_buffer;
93 size_t read_buffer_size;
94 size_t last_read_size;
95 int last_read_char;
96 size_t read_pos;
97 GString *parse_buffer;
98 size_t parse_buffer_limit;
99} gvm_json_pull_parser_t;
100
101 gvm_json_path_elem_t *
102 gvm_json_pull_path_elem_new (gvm_json_pull_container_type_t, int);
103
104void
105 gvm_json_pull_path_elem_free (gvm_json_path_elem_t *);
106
107void
108 gvm_json_pull_event_init (gvm_json_pull_event_t *);
109
110void
111 gvm_json_pull_event_cleanup (gvm_json_pull_event_t *);
112
113void
114 gvm_json_pull_parser_init_full (gvm_json_pull_parser_t *, FILE *, size_t,
115 size_t);
116
117void
118 gvm_json_pull_parser_init (gvm_json_pull_parser_t *, FILE *);
119
120void
121 gvm_json_pull_parser_cleanup (gvm_json_pull_parser_t *);
122
123void
124 gvm_json_pull_parser_next (gvm_json_pull_parser_t *, gvm_json_pull_event_t *);
125
126cJSON *
127 gvm_json_pull_expand_container (gvm_json_pull_parser_t *, gchar **);
128
129gchar *
130 gvm_json_path_to_string (GQueue *path);
131
132#endif /* not _GVM_UTIL_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:745
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:668
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:77
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:909
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:119
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:108
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