Flipper Zero Firmware
Loading...
Searching...
No Matches
frozen.h
1/*
2 * Copyright (c) 2004-2013 Sergey Lyubka <[email protected]>
3 * Copyright (c) 2018 Cesanta Software Limited
4 * All rights reserved
5 *
6 * Licensed under the Apache License, Version 2.0 (the ""License"");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an ""AS IS"" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef CS_FROZEN_FROZEN_H_
20#define CS_FROZEN_FROZEN_H_
21
22#ifdef __cplusplus
23extern "C" {
24#endif /* __cplusplus */
25
26#include <stdarg.h>
27#include <stddef.h>
28#include <stdio.h>
29
30#if defined(_WIN32) && _MSC_VER < 1700
31typedef int bool;
32enum { false = 0, true = 1 };
33#else
34#include <stdbool.h>
35#endif
36
37/* JSON token type */
38enum json_token_type {
39 JSON_TYPE_INVALID = 0, /* memsetting to 0 should create INVALID value */
40 JSON_TYPE_STRING,
41 JSON_TYPE_NUMBER,
42 JSON_TYPE_TRUE,
43 JSON_TYPE_FALSE,
44 JSON_TYPE_NULL,
45 JSON_TYPE_OBJECT_START,
46 JSON_TYPE_OBJECT_END,
47 JSON_TYPE_ARRAY_START,
48 JSON_TYPE_ARRAY_END,
49
50 JSON_TYPES_CNT
51};
52
53/*
54 * Structure containing token type and value. Used in `json_walk()` and
55 * `json_scanf()` with the format specifier `%T`.
56 */
57struct json_token {
58 const char* ptr; /* Points to the beginning of the value */
59 int len; /* Value length */
60 enum json_token_type type; /* Type of the token, possible values are above */
61};
62
63#define JSON_INVALID_TOKEN \
64 { 0, 0, JSON_TYPE_INVALID }
65
66/* Error codes */
67#define JSON_STRING_INVALID -1
68#define JSON_STRING_INCOMPLETE -2
69
70/*
71 * Callback-based SAX-like API.
72 *
73 * Property name and length is given only if it's available: i.e. if current
74 * event is an object's property. In other cases, `name` is `NULL`. For
75 * example, name is never given:
76 * - For the first value in the JSON string;
77 * - For events JSON_TYPE_OBJECT_END and JSON_TYPE_ARRAY_END
78 *
79 * E.g. for the input `{ "foo": 123, "bar": [ 1, 2, { "baz": true } ] }`,
80 * the sequence of callback invocations will be as follows:
81 *
82 * - type: JSON_TYPE_OBJECT_START, name: NULL, path: "", value: NULL
83 * - type: JSON_TYPE_NUMBER, name: "foo", path: ".foo", value: "123"
84 * - type: JSON_TYPE_ARRAY_START, name: "bar", path: ".bar", value: NULL
85 * - type: JSON_TYPE_NUMBER, name: "0", path: ".bar[0]", value: "1"
86 * - type: JSON_TYPE_NUMBER, name: "1", path: ".bar[1]", value: "2"
87 * - type: JSON_TYPE_OBJECT_START, name: "2", path: ".bar[2]", value: NULL
88 * - type: JSON_TYPE_TRUE, name: "baz", path: ".bar[2].baz", value: "true"
89 * - type: JSON_TYPE_OBJECT_END, name: NULL, path: ".bar[2]", value: "{ \"baz\":
90 *true }"
91 * - type: JSON_TYPE_ARRAY_END, name: NULL, path: ".bar", value: "[ 1, 2, {
92 *\"baz\": true } ]"
93 * - type: JSON_TYPE_OBJECT_END, name: NULL, path: "", value: "{ \"foo\": 123,
94 *\"bar\": [ 1, 2, { \"baz\": true } ] }"
95 */
96typedef void (*json_walk_callback_t)(
97 void* callback_data,
98 const char* name,
99 size_t name_len,
100 const char* path,
101 const struct json_token* token);
102
103/*
104 * Parse `json_string`, invoking `callback` in a way similar to SAX parsers;
105 * see `json_walk_callback_t`.
106 * Return number of processed bytes, or a negative error code.
107 */
108int json_walk(
109 const char* json_string,
110 int json_string_length,
111 json_walk_callback_t callback,
112 void* callback_data);
113
114/*
115 * JSON generation API.
116 * struct json_out abstracts output, allowing alternative printing plugins.
117 */
118struct json_out {
119 int (*printer)(struct json_out*, const char* str, size_t len);
120 union {
121 struct {
122 char* buf;
123 size_t size;
124 size_t len;
125 } buf;
126 void* data;
127 FILE* fp;
128 } u;
129};
130
131extern int json_printer_buf(struct json_out*, const char*, size_t);
132extern int json_printer_file(struct json_out*, const char*, size_t);
133
134#define JSON_OUT_BUF(buf, len) \
135 { \
136 json_printer_buf, { \
137 { buf, len, 0 } \
138 } \
139 }
140#define JSON_OUT_FILE(fp) \
141 { \
142 json_printer_file, { \
143 { (char*)fp, 0, 0 } \
144 } \
145 }
146
147typedef int (*json_printf_callback_t)(struct json_out*, va_list* ap);
148
149/*
150 * Generate formatted output into a given sting buffer.
151 * This is a superset of printf() function, with extra format specifiers:
152 * - `%B` print json boolean, `true` or `false`. Accepts an `int`.
153 * - `%Q` print quoted escaped string or `null`. Accepts a `const char *`.
154 * - `%.*Q` same as `%Q`, but with length. Accepts `int`, `const char *`
155 * - `%V` print quoted base64-encoded string. Accepts a `const char *`, `int`.
156 * - `%H` print quoted hex-encoded string. Accepts a `int`, `const char *`.
157 * - `%M` invokes a json_printf_callback_t function. That callback function
158 * can consume more parameters.
159 *
160 * Return number of bytes printed. If the return value is bigger than the
161 * supplied buffer, that is an indicator of overflow. In the overflow case,
162 * overflown bytes are not printed.
163 */
164int json_printf(struct json_out*, const char* fmt, ...);
165int json_vprintf(struct json_out*, const char* fmt, va_list ap);
166
167/*
168 * Same as json_printf, but prints to a file.
169 * File is created if does not exist. File is truncated if already exists.
170 */
171int json_fprintf(const char* file_name, const char* fmt, ...);
172int json_vfprintf(const char* file_name, const char* fmt, va_list ap);
173
174/*
175 * Print JSON into an allocated 0-terminated string.
176 * Return allocated string, or NULL on error.
177 * Example:
178 *
179 * ```c
180 * char *str = json_asprintf("{a:%H}", 3, "abc");
181 * printf("%s\n", str); // Prints "616263"
182 * free(str);
183 * ```
184 */
185char* json_asprintf(const char* fmt, ...);
186char* json_vasprintf(const char* fmt, va_list ap);
187
188/*
189 * Helper %M callback that prints contiguous C arrays.
190 * Consumes void *array_ptr, size_t array_size, size_t elem_size, char *fmt
191 * Return number of bytes printed.
192 */
193int json_printf_array(struct json_out*, va_list* ap);
194
195/*
196 * Scan JSON string `str`, performing scanf-like conversions according to `fmt`.
197 * This is a `scanf()` - like function, with following differences:
198 *
199 * 1. Object keys in the format string may be not quoted, e.g. "{key: %d}"
200 * 2. Order of keys in an object is irrelevant.
201 * 3. Several extra format specifiers are supported:
202 * - %B: consumes `int *` (or `char *`, if `sizeof(bool) == sizeof(char)`),
203 * expects boolean `true` or `false`.
204 * - %Q: consumes `char **`, expects quoted, JSON-encoded string. Scanned
205 * string is malloc-ed, caller must free() the string.
206 * - %V: consumes `char **`, `int *`. Expects base64-encoded string.
207 * Result string is base64-decoded, malloced and NUL-terminated.
208 * The length of result string is stored in `int *` placeholder.
209 * Caller must free() the result.
210 * - %H: consumes `int *`, `char **`.
211 * Expects a hex-encoded string, e.g. "fa014f".
212 * Result string is hex-decoded, malloced and NUL-terminated.
213 * The length of the result string is stored in `int *` placeholder.
214 * Caller must free() the result.
215 * - %M: consumes custom scanning function pointer and
216 * `void *user_data` parameter - see json_scanner_t definition.
217 * - %T: consumes `struct json_token *`, fills it out with matched token.
218 *
219 * Return number of elements successfully scanned & converted.
220 * Negative number means scan error.
221 */
222int json_scanf(const char* str, int str_len, const char* fmt, ...);
223int json_vscanf(const char* str, int str_len, const char* fmt, va_list ap);
224
225/* json_scanf's %M handler */
226typedef void (*json_scanner_t)(const char* str, int len, void* user_data);
227
228/*
229 * Helper function to scan array item with given path and index.
230 * Fills `token` with the matched JSON token.
231 * Return -1 if no array element found, otherwise non-negative token length.
232 */
233int json_scanf_array_elem(
234 const char* s,
235 int len,
236 const char* path,
237 int index,
238 struct json_token* token);
239
240/*
241 * Unescape JSON-encoded string src,slen into dst, dlen.
242 * src and dst may overlap.
243 * If destination buffer is too small (or zero-length), result string is not
244 * written but the length is counted nevertheless (similar to snprintf).
245 * Return the length of unescaped string in bytes.
246 */
247int json_unescape(const char* src, int slen, char* dst, int dlen);
248
249/*
250 * Escape a string `str`, `str_len` into the printer `out`.
251 * Return the number of bytes printed.
252 */
253int json_escape(struct json_out* out, const char* str, size_t str_len);
254
255/*
256 * Read the whole file in memory.
257 * Return malloc-ed file content, or NULL on error. The caller must free().
258 */
259char* json_fread(const char* file_name);
260
261/*
262 * Update given JSON string `s,len` by changing the value at given `json_path`.
263 * The result is saved to `out`. If `json_fmt` == NULL, that deletes the key.
264 * If path is not present, missing keys are added. Array path without an
265 * index pushes a value to the end of an array.
266 * Return 1 if the string was changed, 0 otherwise.
267 *
268 * Example: s is a JSON string { "a": 1, "b": [ 2 ] }
269 * json_setf(s, len, out, ".a", "7"); // { "a": 7, "b": [ 2 ] }
270 * json_setf(s, len, out, ".b", "7"); // { "a": 1, "b": 7 }
271 * json_setf(s, len, out, ".b[]", "7"); // { "a": 1, "b": [ 2,7 ] }
272 * json_setf(s, len, out, ".b", NULL); // { "a": 1 }
273 */
274int json_setf(
275 const char* s,
276 int len,
277 struct json_out* out,
278 const char* json_path,
279 const char* json_fmt,
280 ...);
281
282int json_vsetf(
283 const char* s,
284 int len,
285 struct json_out* out,
286 const char* json_path,
287 const char* json_fmt,
288 va_list ap);
289
290/*
291 * Pretty-print JSON string `s,len` into `out`.
292 * Return number of processed bytes in `s`.
293 */
294int json_prettify(const char* s, int len, struct json_out* out);
295
296/*
297 * Prettify JSON file `file_name`.
298 * Return number of processed bytes, or negative number of error.
299 * On error, file content is not modified.
300 */
301int json_prettify_file(const char* file_name);
302
303/*
304 * Iterate over an object at given JSON `path`.
305 * On each iteration, fill the `key` and `val` tokens. It is OK to pass NULL
306 * for `key`, or `val`, in which case they won't be populated.
307 * Return an opaque value suitable for the next iteration, or NULL when done.
308 *
309 * Example:
310 *
311 * ```c
312 * void *h = NULL;
313 * struct json_token key, val;
314 * while ((h = json_next_key(s, len, h, ".foo", &key, &val)) != NULL) {
315 * printf("[%.*s] -> [%.*s]\n", key.len, key.ptr, val.len, val.ptr);
316 * }
317 * ```
318 */
319void* json_next_key(
320 const char* s,
321 int len,
322 void* handle,
323 const char* path,
324 struct json_token* key,
325 struct json_token* val);
326
327/*
328 * Iterate over an array at given JSON `path`.
329 * Similar to `json_next_key`, but fills array index `idx` instead of `key`.
330 */
331void* json_next_elem(
332 const char* s,
333 int len,
334 void* handle,
335 const char* path,
336 int* idx,
337 struct json_token* val);
338
339#ifndef JSON_MAX_PATH_LEN
340#define JSON_MAX_PATH_LEN 256
341#endif
342
343#ifndef JSON_MINIMAL
344#define JSON_MINIMAL 0
345#endif
346
347#ifndef JSON_ENABLE_BASE64
348#define JSON_ENABLE_BASE64 !JSON_MINIMAL
349#endif
350
351#ifndef JSON_ENABLE_HEX
352#define JSON_ENABLE_HEX !JSON_MINIMAL
353#endif
354
355#ifdef __cplusplus
356}
357#endif /* __cplusplus */
358
359#endif /* CS_FROZEN_FROZEN_H_ */
Definition frozen.h:118
Definition frozen.h:57