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
87
88#define LEJP_FLAG_CB_IS_VALUE 64
89
117
188LWS_EXTERN signed char _lejp_callback(struct lejp_ctx *ctx, char reason);
189
190typedef signed char (*lejp_callback)(struct lejp_ctx *ctx, char reason);
191
192#ifndef LEJP_MAX_PARSING_STACK_DEPTH
193#define LEJP_MAX_PARSING_STACK_DEPTH 10
194#endif
195#ifndef LEJP_MAX_DEPTH
196#if defined(LWS_ESP_PLATFORM)
197#define LEJP_MAX_DEPTH 16
198#else
199#define LEJP_MAX_DEPTH 64
200#endif
201#endif
202#ifndef LEJP_MAX_INDEX_DEPTH
203#if defined(LWS_ESP_PLATFORM)
204#define LEJP_MAX_INDEX_DEPTH 12
205#else
206#define LEJP_MAX_INDEX_DEPTH 64
207#endif
208#endif
209#ifndef LEJP_MAX_PATH
210#define LEJP_MAX_PATH 192
211#endif
212#ifndef LEJP_STRING_CHUNK
213/* must be >= 30 to assemble floats */
214#define LEJP_STRING_CHUNK 254
215#endif
216
217/*
218 * These are tunable, but every index into the arrays they size is a uint8_t
219 * in struct lejp_ctx, so they are hard-capped: LEJP_MAX_PATH, LEJP_MAX_DEPTH,
220 * LEJP_MAX_INDEX_DEPTH and LEJP_MAX_PARSING_STACK_DEPTH must each be <= 255,
221 * and LEJP_STRING_CHUNK <= 254. Raising one past its cap does not overflow
222 * the array, but the bounds checks stop firing and the parser silently
223 * corrupts what it collected.
224 */
225
226#if LEJP_MAX_PATH > 255 || LEJP_MAX_DEPTH > 255 || \
227 LEJP_MAX_INDEX_DEPTH > 255 || LEJP_MAX_PARSING_STACK_DEPTH > 255 || \
228 LEJP_STRING_CHUNK > 254
229#error "LEJP_ size knob is larger than its uint8_t index can represent"
230#endif
231
233 LEJP_SEEN_MINUS = (1 << 0),
234 LEJP_SEEN_POINT = (1 << 1),
236 LEJP_SEEN_EXP = (1 << 3)
237};
238
240 char s; /* lejp_state stack*/
241 uint8_t p; /* path length, 0 .. LEJP_MAX_PATH - 1 */
242 char i; /* index array length */
243 char b; /* user bitfield */
244};
245
247 void *user; /* private to the stack level */
248 signed char (*callback)(struct lejp_ctx *ctx, char reason);
249 const char * const *paths;
253};
254
260
268
269struct lejp_ctx {
270
271 /* sorted by type for most compact alignment
272 *
273 * pointers
274 */
275 void *user;
276
277 /* arrays */
278
281 uint16_t i[LEJP_MAX_INDEX_DEPTH]; /* index array */
285
287
288 /* size_t */
289
290 size_t path_stride; /* 0 means default ptr size, else stride */
291
292 /* int */
293
295
296 /* short */
297
299 uint16_t uni_hi; /* pending \uD800-\uDBFF surrogate half, or 0 */
300#define LEJP_FLAG_FEAT_OBJECT_INDEXES (1 << 0)
301#define LEJP_FLAG_FEAT_LEADING_WC (1 << 1)
302/*
303 * Reject object key names containing the characters lejp uses to represent
304 * the document structure in ctx->path, ie, '.', '[' and ']'. Without this,
305 * a peer can choose a key name that synthesizes the same path as a completely
306 * different document shape, eg, {"s[].foo":1} produces the same path as
307 * {"s":[{"foo":1}]}. It's optional since a key legitimately containing '.'
308 * (eg, a hostname) is not that unusual; set it if your paths care.
309 */
310#define LEJP_FLAG_FEAT_STRICT_KEY_CHARS (1 << 2)
311/*
312 * Accept lejp's nonstandard '#' to-end-of-line comment extension, outside of
313 * strings, at any depth. RFC 8259 has no comments, so with this set lejp
314 * assigns a meaning to documents every conformant JSON parser rejects, eg,
315 * {"a":1#,"b":2 LF } is {"a":1} to lejp and a syntax error to everybody else.
316 * Wherever the same bytes are also seen by another parser - a JOSE header the
317 * relying party also reads, a policy a management tool also reads - the two
318 * disagree about what the document says, and lejp's view is the one the
319 * security decision is made on. So it's off by default and network-facing
320 * parses must leave it off; it exists for hand-written, locally-owned config
321 * files, and in-tree only lejp-conf.c (the lwsws config parser) sets it.
322 *
323 * Deliberately not part of LEJP_FLAG_LATEST, which means "the best current
324 * behaviours", ie, the strictest.
325 */
326#define LEJP_FLAG_FEAT_COMMENTS (1 << 3)
327#define LEJP_FLAG_LATEST \
328 (LEJP_FLAG_FEAT_OBJECT_INDEXES | \
329 LEJP_FLAG_FEAT_LEADING_WC | \
330 LEJP_FLAG_FEAT_STRICT_KEY_CHARS)
332
333 /* char */
334
338 uint8_t sp; /* stack head */
339 uint8_t ipos; /* index stack depth */
344 uint8_t pst_sp; /* parsing stack head */
346};
347
350 signed char (*callback)(struct lejp_ctx *ctx, char reason),
351 void *user, const char * const *paths, unsigned char paths_count);
352
355
357lejp_parse(struct lejp_ctx *ctx, const unsigned char *json, int len);
358
361 signed char (*callback)(struct lejp_ctx *ctx, char reason));
362
363/*
364 * push the current paths / paths_count and lejp_cb to a stack in the ctx, and
365 * start using the new ones
366 */
368lejp_parser_push(struct lejp_ctx *ctx, void *user, const char * const *paths,
369 unsigned char paths_count, lejp_callback lejp_cb);
370
371/*
372 * pop the previously used paths / paths_count and lejp_cb, and continue
373 * parsing using those as before
374 */
377
378/* exported for use when reevaluating a path for use with a subcontext */
381
383lejp_get_wildcard(struct lejp_ctx *ctx, int wildcard, char *dest, int len);
384
385LWS_VISIBLE LWS_EXTERN const char *
387
389lejp_string_unify(struct lejp_ctx *ctx, struct lwsac **ac);
390
392lejp_string_unify_part(struct lejp_ctx *ctx, struct lwsac **ac, char reason);
unsigned short uint16_t
unsigned int uint32_t
#define LWS_EXTERN
unsigned char uint8_t
#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:290
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)
uint16_t uni_hi
Definition lws-lejp.h:299
struct _lejp_stack st[LEJP_MAX_DEPTH]
Definition lws-lejp.h:280
signed char(* callback)(struct lejp_ctx *ctx, char reason)
Definition lws-lejp.h:248
uint8_t outer_array
Definition lws-lejp.h:345
uint8_t f
Definition lws-lejp.h:337
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:341
uint16_t uni
Definition lws-lejp.h:298
#define LEJP_MAX_PATH
Definition lws-lejp.h:210
#define LEJP_MAX_PARSING_STACK_DEPTH
Definition lws-lejp.h:193
uint8_t sp
Definition lws-lejp.h:338
uint8_t pst_sp
Definition lws-lejp.h:344
uint8_t ipos
Definition lws-lejp.h:339
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_REJECT_MP_KEY_ILLEGAL_CHAR
Definition lws-lejp.h:84
@ 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_PATH_TOO_LONG
Definition lws-lejp.h:85
@ LEJP_REJECT_MP_VAL_NUM_EXP_BAD_EXP
Definition lws-lejp.h:73
uint8_t count_paths
Definition lws-lejp.h:340
lejp_callbacks
Definition lws-lejp.h:90
@ LEJPCB_VAL_FALSE
Definition lws-lejp.h:101
@ LEJPCB_OBJECT_END
Definition lws-lejp.h:113
@ LEJPCB_USER_START
Definition lws-lejp.h:115
@ LEJPCB_START
Definition lws-lejp.h:94
@ LEJPCB_VAL_NUM_FLOAT
Definition lws-lejp.h:104
@ LEJPCB_ARRAY_END
Definition lws-lejp.h:110
@ LEJPCB_OBJECT_START
Definition lws-lejp.h:112
@ LEJPCB_VAL_TRUE
Definition lws-lejp.h:100
@ LEJPCB_VAL_NUM_INT
Definition lws-lejp.h:103
@ LEJPCB_CONSTRUCTED
Definition lws-lejp.h:91
@ LEJPCB_FAILED
Definition lws-lejp.h:96
@ LEJPCB_COMPLETE
Definition lws-lejp.h:95
@ LEJPCB_ARRAY_START
Definition lws-lejp.h:109
@ LEJPCB_VAL_STR_END
Definition lws-lejp.h:107
@ LEJPCB_VAL_NULL
Definition lws-lejp.h:102
@ LEJPCB_VAL_STR_CHUNK
Definition lws-lejp.h:106
@ LEJPCB_VAL_STR_START
Definition lws-lejp.h:105
@ LEJPCB_DESTRUCTED
Definition lws-lejp.h:92
@ LEJPCB_PAIR_NAME
Definition lws-lejp.h:98
lejp_string_piece_t * sph
Definition lws-lejp.h:262
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:232
@ LEJP_SEEN_EXP
Definition lws-lejp.h:236
@ LEJP_SEEN_POINT
Definition lws-lejp.h:234
@ LEJP_SEEN_MINUS
Definition lws-lejp.h:233
@ LEJP_SEEN_POST_POINT
Definition lws-lejp.h:235
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:282
const char * piece
Definition lws-lejp.h:257
void * user
Definition lws-lejp.h:275
const char *const * paths
Definition lws-lejp.h:249
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:286
LWS_EXTERN signed char _lejp_callback(struct lejp_ctx *ctx, char reason)
#define LEJP_STRING_CHUNK
Definition lws-lejp.h:214
uint8_t path_match_len
Definition lws-lejp.h:342
LWS_VISIBLE LWS_EXTERN const char * lejp_error_to_string(int e)
lejp_string_piece_t ** sp_next
Definition lws-lejp.h:263
uint8_t npos
Definition lws-lejp.h:335
uint8_t wildcount
Definition lws-lejp.h:343
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:256
#define LEJP_FLAG_CB_IS_VALUE
Definition lws-lejp.h:88
uint8_t p
Definition lws-lejp.h:241
#define LEJP_MAX_DEPTH
Definition lws-lejp.h:199
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:190
uint16_t i[LEJP_MAX_INDEX_DEPTH]
Definition lws-lejp.h:281
uint16_t flags
Definition lws-lejp.h:331
uint8_t dcount
Definition lws-lejp.h:336
#define LEJP_MAX_INDEX_DEPTH
Definition lws-lejp.h:206
#define LEJP_FLAG_WS_KEEP
Definition lws-lejp.h:38
char buf[LEJP_STRING_CHUNK+1]
Definition lws-lejp.h:284
char path[LEJP_MAX_PATH]
Definition lws-lejp.h:283
uint32_t line
Definition lws-lejp.h:294
struct _lejp_parsing_stack pst[LEJP_MAX_PARSING_STACK_DEPTH]
Definition lws-lejp.h:279
LWS_VISIBLE LWS_EXTERN int lejp_get_wildcard(struct lejp_ctx *ctx, int wildcard, char *dest, int len)