]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_json.h
Add theme plugin duplicate file check. (#2078)
[Ventoy.git] / Plugson / src / Core / ventoy_json.h
1 /******************************************************************************
2 * ventoy_json.h
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 #ifndef __VENTOY_JSON_H__
21 #define __VENTOY_JSON_H__
22
23 #define JSON_NEW_ITEM(pstJson, ret) \
24 { \
25 (pstJson) = (VTOY_JSON *)zalloc(sizeof(VTOY_JSON)); \
26 if (NULL == (pstJson)) \
27 { \
28 vdebug("Failed to alloc memory for json."); \
29 return (ret); \
30 } \
31 }
32
33 #if defined(_MSC_VER) || defined(WIN32)
34 #define ssprintf(curpos, buf, len, fmt, ...) \
35 curpos += scnprintf(buf + curpos, len - curpos, fmt, ##__VA_ARGS__)
36
37 #define VTOY_JSON_IS_SKIPABLE(c) (((c) <= 32) ? 1 : 0)
38
39 #define VTOY_JSON_PRINT_PREFIX(uiDepth, ...) \
40 { \
41 uint32_t _uiLoop = 0; \
42 for (_uiLoop = 0; _uiLoop < (uiDepth); _uiLoop++) \
43 { \
44 ssprintf(uiCurPos, pcBuf, uiBufLen, " "); \
45 } \
46 ssprintf(uiCurPos, pcBuf, uiBufLen, ##__VA_ARGS__); \
47 }
48 #else
49
50 #define ssprintf(curpos, buf, len, fmt, args...) \
51 curpos += scnprintf(buf + curpos, len - curpos, fmt, ##args)
52
53 #define VTOY_JSON_IS_SKIPABLE(c) (((c) <= 32) ? 1 : 0)
54
55 #define VTOY_JSON_PRINT_PREFIX(uiDepth, args...) \
56 { \
57 uint32_t _uiLoop = 0; \
58 for (_uiLoop = 0; _uiLoop < (uiDepth); _uiLoop++) \
59 { \
60 ssprintf(uiCurPos, pcBuf, uiBufLen, " "); \
61 } \
62 ssprintf(uiCurPos, pcBuf, uiBufLen, ##args); \
63 }
64 #endif
65
66
67 #define VTOY_JSON_SUCCESS_RET "{ \"result\" : \"success\" }"
68 #define VTOY_JSON_FAILED_RET "{ \"result\" : \"failed\" }"
69 #define VTOY_JSON_INVALID_RET "{ \"result\" : \"invalidfmt\" }"
70 #define VTOY_JSON_TOKEN_ERR_RET "{ \"result\" : \"tokenerror\" }"
71 #define VTOY_JSON_EXIST_RET "{ \"result\" : \"exist\" }"
72 #define VTOY_JSON_TIMEOUT_RET "{ \"result\" : \"timeout\" }"
73 #define VTOY_JSON_DUPLICATE "{ \"result\" : \"duplicate\" }"
74 #define VTOY_JSON_BUSY_RET "{ \"result\" : \"busy\" }"
75 #define VTOY_JSON_INUSE_RET "{ \"result\" : \"inuse\" }"
76 #define VTOY_JSON_NOTFOUND_RET "{ \"result\" : \"notfound\" }"
77 #define VTOY_JSON_NOTRUNNING_RET "{ \"result\" : \"notrunning\" }"
78 #define VTOY_JSON_NOT_READY_RET "{ \"result\" : \"notready\" }"
79 #define VTOY_JSON_NOT_SUPPORT_RET "{ \"result\" : \"notsupport\" }"
80 #define VTOY_JSON_MBR_2TB_RET "{ \"result\" : \"mbr2tb\" }"
81 #define VTOY_JSON_INVALID_RSV_RET "{ \"result\" : \"reserve_invalid\" }"
82 #define VTOY_JSON_FILE_NOT_FOUND_RET "{ \"result\" : \"file_not_found\" }"
83
84 typedef enum tagJSON_TYPE
85 {
86 JSON_TYPE_NUMBER = 0,
87 JSON_TYPE_STRING,
88 JSON_TYPE_BOOL,
89 JSON_TYPE_ARRAY,
90 JSON_TYPE_OBJECT,
91 JSON_TYPE_NULL,
92 JSON_TYPE_BUTT
93 }JSON_TYPE;
94
95 typedef struct tagVTOY_JSON
96 {
97 struct tagVTOY_JSON *pstPrev;
98 struct tagVTOY_JSON *pstNext;
99 struct tagVTOY_JSON *pstChild;
100
101 JSON_TYPE enDataType;
102 union
103 {
104 char *pcStrVal;
105 int iNumVal;
106 uint64_t lValue;
107 }unData;
108
109 char *pcName;
110 }VTOY_JSON;
111
112 #define VTOY_JSON_FMT_BEGIN(uiCurPos, pcBuf, uiBufLen) \
113 {\
114 uint32_t __uiCurPos = (uiCurPos);\
115 uint32_t __uiBufLen = (uiBufLen);\
116 char *__pcBuf = (pcBuf);
117
118 #define VTOY_JSON_FMT_END(uiCurPos) \
119 (uiCurPos) = __uiCurPos;\
120 }
121
122 #define VTOY_JSON_FMT_OBJ_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{")
123 #define VTOY_JSON_FMT_OBJ_BEGIN_L(P) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{", P)
124 #define VTOY_JSON_FMT_OBJ_BEGIN_N() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "{\n")
125 #define VTOY_JSON_FMT_OBJ_BEGIN_LN(P) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{\n", P)
126
127 #define VTOY_JSON_FMT_OBJ_END() \
128 {\
129 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
130 {\
131 __uiCurPos -= 1;\
132 }\
133 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
134 }
135
136 #define VTOY_JSON_FMT_OBJ_ENDEX() \
137 {\
138 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
139 {\
140 __uiCurPos -= 1;\
141 }\
142 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "},");\
143 }
144
145
146
147 #define VTOY_JSON_FMT_KEY(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\":", (Key))
148 #define VTOY_JSON_FMT_KEY_L(P, Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\":", P, (Key))
149
150
151 #define VTOY_JSON_FMT_ITEM(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\",", (Item))
152 #define VTOY_JSON_FMT_ITEM_L(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",", P, (Item))
153 #define VTOY_JSON_FMT_ITEM_LN(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",\n", P, (Item))
154 #define VTOY_JSON_FMT_ITEM_PATH_LN(P, Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\",\n", P, ventoy_real_path(Item))
155
156 #define VTOY_JSON_FMT_ITEM_INT(Item) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%d,", (Item))
157
158
159 #define VTOY_JSON_FMT_COMA() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",")
160 #define VTOY_JSON_FMT_COMA_N(cnt) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")
161 #define VTOY_JSON_FMT_COMA_N_CNT(cnt) if ((cnt) > 0) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",\n")
162
163
164 #define VTOY_JSON_FMT_APPEND_BEGIN() \
165 { \
166 if ('}' == *(__pcBuf + (__uiCurPos - 1)))\
167 {\
168 __uiCurPos -= 1;\
169 }\
170 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, ",");\
171 }
172
173 #define VTOY_JSON_FMT_APPEND_END() \
174 { \
175 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "}");\
176 }
177
178 #define VTOY_JSON_FMT_ARY_BEGIN() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[")
179 #define VTOY_JSON_FMT_ARY_BEGIN_N() ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "[\n")
180
181 #define VTOY_JSON_FMT_ARY_END() \
182 {\
183 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
184 {\
185 __uiCurPos -= 1;\
186 }\
187 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "]");\
188 }
189 #define VTOY_JSON_FMT_ARY_ENDEX() \
190 {\
191 if (',' == *(__pcBuf + (__uiCurPos - 1)))\
192 {\
193 __uiCurPos -= 1;\
194 }\
195 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "],");\
196 }
197
198
199
200 #define VTOY_JSON_FMT_OBJ_END_L(P) \
201 {\
202 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
203 {\
204 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
205 __uiCurPos -= 1;\
206 }\
207 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s}%s", P);\
208 }
209 #define VTOY_JSON_FMT_OBJ_ENDEX_L(P) \
210 {\
211 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
212 {\
213 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
214 __uiCurPos -= 1;\
215 }\
216 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s},%s", P);\
217 }
218
219 #define VTOY_JSON_FMT_OBJ_END_LN(P) \
220 {\
221 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
222 {\
223 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
224 __uiCurPos -= 1;\
225 }\
226 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s}\n", P);\
227 }
228 #define VTOY_JSON_FMT_OBJ_ENDEX_LN(P) \
229 {\
230 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
231 {\
232 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
233 __uiCurPos -= 1;\
234 }\
235 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s},\n", P);\
236 }
237
238
239
240
241 #define VTOY_JSON_FMT_ARY_END_L(P) \
242 {\
243 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
244 {\
245 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
246 __uiCurPos -= 1;\
247 }\
248 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s]", P);\
249 }
250
251 #define VTOY_JSON_FMT_ARY_END_LN(P) \
252 {\
253 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
254 {\
255 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
256 __uiCurPos -= 1;\
257 }\
258 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s]\n", P);\
259 }
260
261 #define VTOY_JSON_FMT_ARY_ENDEX_L(P) \
262 {\
263 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
264 {\
265 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
266 __uiCurPos -= 1;\
267 }\
268 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s],", P);\
269 }
270
271
272 #define VTOY_JSON_FMT_ARY_ENDEX_LN(P) \
273 {\
274 if ('\n' == *(__pcBuf + (__uiCurPos - 1)) && ',' == *(__pcBuf + (__uiCurPos - 2)))\
275 {\
276 *(__pcBuf + (__uiCurPos - 2)) = '\n';\
277 __uiCurPos -= 1;\
278 }\
279 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s],\n", P);\
280 }
281
282
283
284
285 #define VTOY_JSON_FMT_UINT64(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %llu,", Key, (_ull)Val)
286
287 #define VTOY_JSON_FMT_ULONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %lu,", Key, Val)
288 #define VTOY_JSON_FMT_LONG(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %ld,", Key, Val)
289
290 #define VTOY_JSON_FMT_UINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %u,", Key, Val)
291 #define VTOY_JSON_FMT_UINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %u,\n", Key, Val)
292 #define VTOY_JSON_FMT_UINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %u,", P, Key, Val)
293 #define VTOY_JSON_FMT_UINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %u,\n", P, Key, Val)
294
295
296 #define VTOY_JSON_FMT_STRUINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%u\",", Key, Val)
297 #define VTOY_JSON_FMT_STRUINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%u\",\n", Key, Val)
298 #define VTOY_JSON_FMT_STRUINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%u\",", P, Key, Val)
299 #define VTOY_JSON_FMT_STRUINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%u\",\n", P, Key, Val)
300
301 #define VTOY_JSON_FMT_STRSINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%d\",", Key, Val)
302 #define VTOY_JSON_FMT_STRSINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%d\",\n", Key, Val)
303 #define VTOY_JSON_FMT_STRSINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%d\",", P, Key, Val)
304 #define VTOY_JSON_FMT_STRSINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%d\",\n", P, Key, Val)
305
306 #define VTOY_JSON_FMT_CTRL_INT(Prefix, Key, Field) \
307 if (def->Field != data->Field) \
308 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%d\" },\n", Prefix, Key, data->Field)
309
310 #define VTOY_JSON_FMT_CTRL_STRN(P, Key, Field) \
311 if (strcmp(def->Field, data->Field)) \
312 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, data->Field)
313
314 #define VTOY_JSON_FMT_CTRL_STRN_STR(P, Key, ptr) \
315 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, ptr)
316
317 #define VTOY_JSON_FMT_CTRL_PUB_STRN(P, Key, Field) \
318 if (strcmp(def->Field, g_pub_path)) \
319 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s{ \"%s\": \"%s\" },\n", P, Key, g_pub_path)
320
321
322 #define VTOY_JSON_FMT_DIFF_STRN(P, Key, Field) \
323 if (strcmp(def->Field, data->Field)) \
324 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, data->Field)
325
326
327 #define VTOY_JSON_FMT_STRINT64(Key, Val) \
328 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%llu\",", Key, Val)
329
330 #define VTOY_JSON_FMT_SINT(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %d,", Key, Val)
331 #define VTOY_JSON_FMT_SINT_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %d,\n", Key, Val)
332 #define VTOY_JSON_FMT_SINT_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %d,", P, Key, Val)
333 #define VTOY_JSON_FMT_SINT_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": %d,\n", P, Key, Val)
334
335 #define VTOY_JSON_FMT_DUBL(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %.1lf,", Key, Val)
336 #define VTOY_JSON_FMT_DUBL2(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": %10.02lf,", Key, Val)
337
338 #define VTOY_JSON_FMT_STRN(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%s\",", Key, Val)
339 #define VTOY_JSON_FMT_STRN_N(Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": \"%s\",\n", Key, Val)
340 #define VTOY_JSON_FMT_STRN_L(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",", P, Key, Val)
341 #define VTOY_JSON_FMT_STRN_LN(P, Key, Val) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, Val)
342 #define VTOY_JSON_FMT_STRN_PATH_LN(P, Key, Val) \
343 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": \"%s\",\n", P, Key, ventoy_real_path(Val))
344
345 int vtoy_json_escape_string(char *buf, int buflen, const char *str, int newline);
346
347 #define VTOY_JSON_FMT_STRN_EX_LN(P, Key, Val) \
348 {\
349 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "%s\"%s\": ", P, Key);\
350 __uiCurPos += vtoy_json_escape_string(__pcBuf + __uiCurPos, __uiBufLen - __uiCurPos, Val, 1);\
351 }
352
353
354 #define VTOY_JSON_FMT_NULL(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": null,", Key)
355
356 #define VTOY_JSON_FMT_TRUE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": true,", (Key))
357 #define VTOY_JSON_FMT_FALSE(Key) ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": false,", (Key))
358
359 #define VTOY_JSON_FMT_BOOL(Key, Val) \
360 {\
361 if (0 == (Val))\
362 {\
363 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": false,", (Key));\
364 }\
365 else \
366 {\
367 ssprintf(__uiCurPos, __pcBuf, __uiBufLen, "\"%s\": true,", (Key));\
368 }\
369 }
370
371 typedef struct tagVTOY_JSON_PARSE
372 {
373 char *pcKey;
374 void *pDataBuf;
375 uint32_t uiBufSize;
376 }VTOY_JSON_PARSE_S;
377
378 #define JSON_SUCCESS 0
379 #define JSON_FAILED 1
380 #define JSON_NOT_FOUND 2
381
382 int vtoy_json_parse_value
383 (
384 char *pcNewStart,
385 char *pcRawStart,
386 VTOY_JSON *pstJson,
387 const char *pcData,
388 const char **ppcEnd
389 );
390 VTOY_JSON * vtoy_json_create(void);
391 int vtoy_json_parse(VTOY_JSON *pstJson, const char *szJsonData);
392 int vtoy_json_parse_ex(VTOY_JSON *pstJson, const char *szJsonData, int szLen);
393 int vtoy_json_destroy(VTOY_JSON *pstJson);
394 VTOY_JSON *vtoy_json_find_item
395 (
396 VTOY_JSON *pstJson,
397 JSON_TYPE enDataType,
398 const char *szKey
399 );
400 int vtoy_json_scan_parse
401 (
402 const VTOY_JSON *pstJson,
403 uint32_t uiParseNum,
404 VTOY_JSON_PARSE_S *pstJsonParse
405 );
406 int vtoy_json_get_int
407 (
408 VTOY_JSON *pstJson,
409 const char *szKey,
410 int *piValue
411 );
412 int vtoy_json_get_uint
413 (
414 VTOY_JSON *pstJson,
415 const char *szKey,
416 uint32_t *puiValue
417 );
418 int vtoy_json_get_uint64
419 (
420 VTOY_JSON *pstJson,
421 const char *szKey,
422 uint64_t *pui64Value
423 );
424 int vtoy_json_get_bool
425 (
426 VTOY_JSON *pstJson,
427 const char *szKey,
428 uint8_t *pbValue
429 );
430 int vtoy_json_get_string
431 (
432 VTOY_JSON *pstJson,
433 const char *szKey,
434 uint32_t uiBufLen,
435 char *pcBuf
436 );
437 const char * vtoy_json_get_string_ex(VTOY_JSON *pstJson, const char *szKey);
438
439 #endif /* __VENTOY_JSON_H__ */
440