File: jsonpull.h

package info (click to toggle)
gvm-libs 22.34.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,968 kB
  • sloc: ansic: 39,015; makefile: 26
file content (132 lines) | stat: -rw-r--r-- 3,815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* 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>

/**
 * @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
} gvm_json_pull_container_type_t;

/**
 * @brief Path element types for the JSON pull parser.
 */
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
} gvm_json_path_elem_t;

/**
 * @brief Event types for the JSON pull parser
 */
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;

/**
 * @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
} gvm_json_pull_event_t;

/**
 * @brief Expected token state for the JSON pull parser
 */
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
  GVM_JSON_PULL_EXPECT_COMMA,         ///< Expect comma or container end brace
  GVM_JSON_PULL_EXPECT_EOF            ///< Expect end of file
} gvm_json_pull_expect_t;

#define GVM_JSON_PULL_PARSE_BUFFER_LIMIT 10485760

#define GVM_JSON_PULL_READ_BUFFER_SIZE 4096

/**
 * @brief A json pull parser
 */
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
} 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 */