libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-lejp.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
32struct lejp_ctx;
33struct lwsac;
34
35#if !defined(LWS_ARRAY_SIZE)
36#define LWS_ARRAY_SIZE(_x) (sizeof(_x) / sizeof(_x[0]))
37#endif
38#define LEJP_FLAG_WS_KEEP 64
39#define LEJP_FLAG_WS_COMMENTLINE 32
40
59
85
86#define LEJP_FLAG_CB_IS_VALUE 64
87
115
177LWS_EXTERN signed char _lejp_callback(struct lejp_ctx *ctx, char reason);
178
179typedef signed char (*lejp_callback)(struct lejp_ctx *ctx, char reason);
180
181#ifndef LEJP_MAX_PARSING_STACK_DEPTH
182#define LEJP_MAX_PARSING_STACK_DEPTH 10
183#endif
184#ifndef LEJP_MAX_DEPTH
185#if defined(LWS_ESP_PLATFORM)
186#define LEJP_MAX_DEPTH 16
187#else
188#define LEJP_MAX_DEPTH 64
189#endif
190#endif
191#ifndef LEJP_MAX_INDEX_DEPTH
192#if defined(LWS_ESP_PLATFORM)
193#define LEJP_MAX_INDEX_DEPTH 12
194#else
195#define LEJP_MAX_INDEX_DEPTH 64
196#endif
197#endif
198#ifndef LEJP_MAX_PATH
199#define LEJP_MAX_PATH 192
200#endif
201#ifndef LEJP_STRING_CHUNK
202/* must be >= 30 to assemble floats */
203#define LEJP_STRING_CHUNK 254
204#endif
205
207 LEJP_SEEN_MINUS = (1 << 0),
208 LEJP_SEEN_POINT = (1 << 1),
210 LEJP_SEEN_EXP = (1 << 3)
211};
212
214 char s; /* lejp_state stack*/
215 char p; /* path length */
216 char i; /* index array length */
217 char b; /* user bitfield */
218};
219
221 void *user; /* private to the stack level */
222 signed char (*callback)(struct lejp_ctx *ctx, char reason);
223 const char * const *paths;
224 uint8_t count_paths;
225 uint8_t ppos;
226 uint8_t path_match;
227};
228
234
242
243struct lejp_ctx {
244
245 /* sorted by type for most compact alignment
246 *
247 * pointers
248 */
249 void *user;
250
251 /* arrays */
252
255 uint16_t i[LEJP_MAX_INDEX_DEPTH]; /* index array */
256 uint16_t wild[LEJP_MAX_INDEX_DEPTH]; /* index array */
259
261
262 /* size_t */
263
264 size_t path_stride; /* 0 means default ptr size, else stride */
265
266 /* int */
267
268 uint32_t line;
269
270 /* short */
271
272 uint16_t uni;
273#define LEJP_FLAG_FEAT_OBJECT_INDEXES (1 << 0)
274#define LEJP_FLAG_FEAT_LEADING_WC (1 << 1)
275#define LEJP_FLAG_LATEST \
276 (LEJP_FLAG_FEAT_OBJECT_INDEXES | \
277 LEJP_FLAG_FEAT_LEADING_WC)
278 uint16_t flags;
279
280 /* char */
281
282 uint8_t npos;
283 uint8_t dcount;
284 uint8_t f;
285 uint8_t sp; /* stack head */
286 uint8_t ipos; /* index stack depth */
287 uint8_t count_paths;
288 uint8_t path_match;
290 uint8_t wildcount;
291 uint8_t pst_sp; /* parsing stack head */
292 uint8_t outer_array;
293};
294
297 signed char (*callback)(struct lejp_ctx *ctx, char reason),
298 void *user, const char * const *paths, unsigned char paths_count);
299
302
304lejp_parse(struct lejp_ctx *ctx, const unsigned char *json, int len);
305
308 signed char (*callback)(struct lejp_ctx *ctx, char reason));
309
310/*
311 * push the current paths / paths_count and lejp_cb to a stack in the ctx, and
312 * start using the new ones
313 */
315lejp_parser_push(struct lejp_ctx *ctx, void *user, const char * const *paths,
316 unsigned char paths_count, lejp_callback lejp_cb);
317
318/*
319 * pop the previously used paths / paths_count and lejp_cb, and continue
320 * parsing using those as before
321 */
324
325/* exported for use when reevaluating a path for use with a subcontext */
328
330lejp_get_wildcard(struct lejp_ctx *ctx, int wildcard, char *dest, int len);
331
332LWS_VISIBLE LWS_EXTERN const char *
334
336lejp_string_unify(struct lejp_ctx *ctx, struct lwsac **ac);
337
339lejp_string_unify_part(struct lejp_ctx *ctx, struct lwsac **ac, char reason);
#define LWS_EXTERN
#define LWS_VISIBLE
lejp_states
Definition lws-lejp.h:41
@ LEJP_MP_VALUE_TOK
Definition lws-lejp.h:55
@ LEJP_MP_STRING
Definition lws-lejp.h:45
@ LEJP_M_P
Definition lws-lejp.h:44
@ LEJP_MP_STRING_ESC_U4
Definition lws-lejp.h:50
@ LEJP_MP_VALUE_NUM_EXP
Definition lws-lejp.h:54
@ LEJP_MEMBERS
Definition lws-lejp.h:43
@ LEJP_MP_STRING_ESC_U1
Definition lws-lejp.h:47
@ LEJP_MP_DELIM
Definition lws-lejp.h:51
@ LEJP_IDLE
Definition lws-lejp.h:42
@ LEJP_MP_VALUE_NUM_INT
Definition lws-lejp.h:53
@ LEJP_MP_STRING_ESC_U3
Definition lws-lejp.h:49
@ LEJP_MP_ARRAY_END
Definition lws-lejp.h:57
@ LEJP_MP_COMMA_OR_END
Definition lws-lejp.h:56
@ LEJP_MP_STRING_ESC
Definition lws-lejp.h:46
@ LEJP_MP_VALUE
Definition lws-lejp.h:52
@ LEJP_MP_STRING_ESC_U2
Definition lws-lejp.h:48
size_t path_stride
Definition lws-lejp.h:264
LWS_VISIBLE LWS_EXTERN void lejp_construct(struct lejp_ctx *ctx, signed char(*callback)(struct lejp_ctx *ctx, char reason), void *user, const char *const *paths, unsigned char paths_count)
struct _lejp_stack st[LEJP_MAX_DEPTH]
Definition lws-lejp.h:254
signed char(* callback)(struct lejp_ctx *ctx, char reason)
Definition lws-lejp.h:222
uint8_t outer_array
Definition lws-lejp.h:292
uint8_t f
Definition lws-lejp.h:284
LWS_VISIBLE LWS_EXTERN int lejp_string_unify_part(struct lejp_ctx *ctx, struct lwsac **ac, char reason)
uint8_t path_match
Definition lws-lejp.h:288
uint16_t uni
Definition lws-lejp.h:272
#define LEJP_MAX_PATH
Definition lws-lejp.h:199
#define LEJP_MAX_PARSING_STACK_DEPTH
Definition lws-lejp.h:182
uint8_t sp
Definition lws-lejp.h:285
uint8_t pst_sp
Definition lws-lejp.h:291
uint8_t ipos
Definition lws-lejp.h:286
LWS_VISIBLE LWS_EXTERN int lejp_parser_pop(struct lejp_ctx *ctx)
lejp_reasons
Definition lws-lejp.h:60
@ LEJP_REJECT_ILLEGAL_HEX
Definition lws-lejp.h:68
@ LEJP_REJECT_MP_C_OR_E_NEITHER
Definition lws-lejp.h:81
@ LEJP_REJECT_MP_VAL_NUM_FORMAT
Definition lws-lejp.h:72
@ LEJP_REJECT_MP_ARRAY_END_MISSING
Definition lws-lejp.h:77
@ LEJP_REJECT_UNKNOWN
Definition lws-lejp.h:82
@ LEJP_CONTINUE
Definition lws-lejp.h:61
@ LEJP_REJECT_MP_DELIM_MISSING_COLON
Definition lws-lejp.h:69
@ LEJP_REJECT_MP_C_OR_E_UNDERF
Definition lws-lejp.h:75
@ LEJP_REJECT_MP_STRING_ESC_ILLEGAL_ESC
Definition lws-lejp.h:67
@ LEJP_REJECT_MP_VAL_TOK_UNKNOWN
Definition lws-lejp.h:74
@ LEJP_REJECT_MP_C_OR_E_NOTARRAY
Definition lws-lejp.h:76
@ LEJP_REJECT_MP_VAL_NUM_INT_NO_FRAC
Definition lws-lejp.h:71
@ LEJP_REJECT_MEMBERS_NO_CLOSE
Definition lws-lejp.h:63
@ LEJP_REJECT_MP_DELIM_ISTACK
Definition lws-lejp.h:79
@ LEJP_REJECT_IDLE_NO_BRACE
Definition lws-lejp.h:62
@ LEJP_REJECT_CALLBACK
Definition lws-lejp.h:83
@ LEJP_REJECT_NUM_TOO_LONG
Definition lws-lejp.h:80
@ LEJP_REJECT_MP_STRING_UNDERRUN
Definition lws-lejp.h:65
@ LEJP_REJECT_MP_NO_OPEN_QUOTE
Definition lws-lejp.h:64
@ LEJP_REJECT_MP_ILLEGAL_CTRL
Definition lws-lejp.h:66
@ LEJP_REJECT_STACK_OVERFLOW
Definition lws-lejp.h:78
@ LEJP_REJECT_MP_DELIM_BAD_VALUE_START
Definition lws-lejp.h:70
@ LEJP_REJECT_MP_VAL_NUM_EXP_BAD_EXP
Definition lws-lejp.h:73
uint8_t count_paths
Definition lws-lejp.h:287
lejp_callbacks
Definition lws-lejp.h:88
@ LEJPCB_VAL_FALSE
Definition lws-lejp.h:99
@ LEJPCB_OBJECT_END
Definition lws-lejp.h:111
@ LEJPCB_USER_START
Definition lws-lejp.h:113
@ LEJPCB_START
Definition lws-lejp.h:92
@ LEJPCB_VAL_NUM_FLOAT
Definition lws-lejp.h:102
@ LEJPCB_ARRAY_END
Definition lws-lejp.h:108
@ LEJPCB_OBJECT_START
Definition lws-lejp.h:110
@ LEJPCB_VAL_TRUE
Definition lws-lejp.h:98
@ LEJPCB_VAL_NUM_INT
Definition lws-lejp.h:101
@ LEJPCB_CONSTRUCTED
Definition lws-lejp.h:89
@ LEJPCB_FAILED
Definition lws-lejp.h:94
@ LEJPCB_COMPLETE
Definition lws-lejp.h:93
@ LEJPCB_ARRAY_START
Definition lws-lejp.h:107
@ LEJPCB_VAL_STR_END
Definition lws-lejp.h:105
@ LEJPCB_VAL_NULL
Definition lws-lejp.h:100
@ LEJPCB_VAL_STR_CHUNK
Definition lws-lejp.h:104
@ LEJPCB_VAL_STR_START
Definition lws-lejp.h:103
@ LEJPCB_DESTRUCTED
Definition lws-lejp.h:90
@ LEJPCB_PAIR_NAME
Definition lws-lejp.h:96
lejp_string_piece_t * sph
Definition lws-lejp.h:236
LWS_VISIBLE LWS_EXTERN int lejp_string_unify(struct lejp_ctx *ctx, struct lwsac **ac)
LWS_VISIBLE LWS_EXTERN void lejp_check_path_match(struct lejp_ctx *ctx)
num_flags
Definition lws-lejp.h:206
@ LEJP_SEEN_EXP
Definition lws-lejp.h:210
@ LEJP_SEEN_POINT
Definition lws-lejp.h:208
@ LEJP_SEEN_MINUS
Definition lws-lejp.h:207
@ LEJP_SEEN_POST_POINT
Definition lws-lejp.h:209
LWS_VISIBLE LWS_EXTERN int lejp_parse(struct lejp_ctx *ctx, const unsigned char *json, int len)
uint16_t wild[LEJP_MAX_INDEX_DEPTH]
Definition lws-lejp.h:256
const char * piece
Definition lws-lejp.h:231
void * user
Definition lws-lejp.h:249
const char *const * paths
Definition lws-lejp.h:223
LWS_VISIBLE LWS_EXTERN void lejp_change_callback(struct lejp_ctx *ctx, signed char(*callback)(struct lejp_ctx *ctx, char reason))
struct lejp_string_piece lejp_string_piece_t
lejp_string_unifier_t su
Definition lws-lejp.h:260
LWS_EXTERN signed char _lejp_callback(struct lejp_ctx *ctx, char reason)
#define LEJP_STRING_CHUNK
Definition lws-lejp.h:203
uint8_t path_match_len
Definition lws-lejp.h:289
LWS_VISIBLE LWS_EXTERN const char * lejp_error_to_string(int e)
lejp_string_piece_t ** sp_next
Definition lws-lejp.h:237
uint8_t npos
Definition lws-lejp.h:282
uint8_t wildcount
Definition lws-lejp.h:290
LWS_VISIBLE LWS_EXTERN int lejp_parser_push(struct lejp_ctx *ctx, void *user, const char *const *paths, unsigned char paths_count, lejp_callback lejp_cb)
struct lejp_string_piece * next
Definition lws-lejp.h:230
#define LEJP_FLAG_CB_IS_VALUE
Definition lws-lejp.h:86
#define LEJP_MAX_DEPTH
Definition lws-lejp.h:188
LWS_VISIBLE LWS_EXTERN void lejp_destruct(struct lejp_ctx *ctx)
signed char(* lejp_callback)(struct lejp_ctx *ctx, char reason)
Definition lws-lejp.h:179
uint16_t i[LEJP_MAX_INDEX_DEPTH]
Definition lws-lejp.h:255
uint16_t flags
Definition lws-lejp.h:278
uint8_t dcount
Definition lws-lejp.h:283
#define LEJP_MAX_INDEX_DEPTH
Definition lws-lejp.h:195
#define LEJP_FLAG_WS_KEEP
Definition lws-lejp.h:38
char buf[LEJP_STRING_CHUNK+1]
Definition lws-lejp.h:258
char path[LEJP_MAX_PATH]
Definition lws-lejp.h:257
uint32_t line
Definition lws-lejp.h:268
struct _lejp_parsing_stack pst[LEJP_MAX_PARSING_STACK_DEPTH]
Definition lws-lejp.h:253
LWS_VISIBLE LWS_EXTERN int lejp_get_wildcard(struct lejp_ctx *ctx, int wildcard, char *dest, int len)