libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-hl.h
Go to the documentation of this file.
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2026 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 * Streaming syntax highlighting tokenizer
25 *
26 * Hostile-input-hardened, alloc-free, streaming tokenizer for producing
27 * syntax highlighting markup from arbitrary source text. Input is fed in
28 * fragments of any size (including 0 or 1 bytes); classified token pieces
29 * are passed to a caller token sink callback. A second layer provides a
30 * stock token sink that emits strict-CSP-compatible HTML markup (entity-
31 * escaped text wrapped in <span class=...>) through a caller write callback.
32 *
33 * The tokenizer never revises emitted decisions, never backtracks, and holds
34 * O(1) state regardless of input; unterminated constructs at end of input are
35 * closed out with a best-guess classification.
36 */
37
38#if !defined(__LWS_HL_H__)
39#define __LWS_HL_H__
40
41#if !defined(LHL_SCRATCH_SIZE)
42#define LHL_SCRATCH_SIZE 40
43#endif
44#if !defined(LHL_PIECE_MAX)
45#define LHL_PIECE_MAX 96
46#endif
47
69
70struct lws_hl_ctx;
71typedef struct lws_hl_ctx lws_hl_ctx_t;
72
94 const uint8_t *tok, size_t len);
95
96typedef struct lws_hl_ops {
97 const char *name;
99
103
105 const uint8_t **buf, size_t *len);
108
113
121typedef struct lws_hl_ctx {
124 void *user;
125
126 /* private below */
127
128 const uint8_t *chunk; /* current input chunk base */
129 size_t pos; /* scan cursor (offset in chunk) */
130 size_t tok; /* current token start offset */
131 size_t epos; /* emitted watermark offset */
132 uint8_t state; /* driver private */
133 uint8_t scratch_pos; /* driver private */
134 uint8_t flags; /* driver private */
135 uint8_t tokcls; /* driver private */
138
153 lws_hl_token_cb cb, void *user);
154
180lws_hl_parse(lws_hl_ctx_t *ctx, const uint8_t **buf, size_t *len);
181
200
201#if defined(LWS_WITH_HL_LANG_C)
210lws_hl_lang_c(void);
211#endif
212
213#if defined(LWS_WITH_HL_LANG_DIFF)
224lws_hl_lang_diff(void);
225#endif
226
227#if !defined(LHL_HTML_BUF)
228#define LHL_HTML_BUF 832
229#endif
230
231typedef lws_stateful_ret_t (*lws_hl_write_cb)(void *user, const uint8_t *buf,
232 size_t len);
233
244typedef struct lws_hl_html {
245 /* private */
247 void *user;
248 const char * const *cls;
250 size_t buflen;
254
271 const char * const *cls);
272
289lws_hl_html_token(void *user, lws_hl_class_t cls, const uint8_t *tok,
290 size_t len);
291
303
323lws_html_escape(lws_hl_write_cb wc, void *user, const uint8_t *src,
324 size_t len);
325#endif
#define LWS_EXTERN
unsigned char uint8_t
lws_stateful_ret_t
#define LWS_VISIBLE
void * user
Definition lws-hl.h:124
LWS_VISIBLE LWS_EXTERN int lws_hl_construct(lws_hl_ctx_t *ctx, const lws_hl_ops_t *lang, lws_hl_token_cb cb, void *user)
lws_hl_write_cb wc
Definition lws-hl.h:246
const lws_hl_ops_t * ops
Definition lws-hl.h:122
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_html_token(void *user, lws_hl_class_t cls, const uint8_t *tok, size_t len)
uint8_t scratch[LHL_SCRATCH_SIZE]
Definition lws-hl.h:136
const uint8_t * chunk
Definition lws-hl.h:128
size_t pos
Definition lws-hl.h:129
#define LHL_HTML_BUF
Definition lws-hl.h:228
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_parse(lws_hl_ctx_t *ctx, const uint8_t **buf, size_t *len)
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_html_close(lws_hl_html_t *h)
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_finish(lws_hl_ctx_t *ctx)
struct lws_hl_html lws_hl_html_t
#define LHL_SCRATCH_SIZE
Definition lws-hl.h:42
const char *const * cls
Definition lws-hl.h:248
size_t buflen
Definition lws-hl.h:250
uint8_t state
Definition lws-hl.h:132
size_t epos
Definition lws-hl.h:131
size_t tok
Definition lws-hl.h:130
lws_hl_token_cb cb
Definition lws-hl.h:123
LWS_VISIBLE LWS_EXTERN int lws_hl_html_construct(lws_hl_html_t *h, lws_hl_write_cb wc, void *user, const char *const *cls)
struct lws_hl_ctx lws_hl_ctx_t
Definition lws-hl.h:71
lws_hl_class_t last
Definition lws-hl.h:249
uint8_t open
Definition lws-hl.h:251
void * user
Definition lws-hl.h:247
char buf[LHL_HTML_BUF]
Definition lws-hl.h:252
uint8_t flags
Definition lws-hl.h:134
lws_hl_class_t
Definition lws-hl.h:49
@ LHL_CLS_PREPROC
Definition lws-hl.h:58
@ LHL_CLS_IDENT
Definition lws-hl.h:51
@ LHL_CLS_STRING
Definition lws-hl.h:55
@ LHL_CLS_PLAIN
Definition lws-hl.h:50
@ LHL_CLS_COMMENT
Definition lws-hl.h:57
@ LHL_CLS_DIFF_REM
Definition lws-hl.h:63
@ LHL_CLS_CHARLIT
Definition lws-hl.h:56
@ LHL_CLS_COUNT
Definition lws-hl.h:67
@ LHL_CLS_KEYWORD
Definition lws-hl.h:52
@ LHL_CLS_NUMBER
Definition lws-hl.h:54
@ LHL_CLS_TYPE
Definition lws-hl.h:53
@ LHL_CLS_DIFF_ADD
Definition lws-hl.h:62
@ LHL_CLS_DIFF_META
Definition lws-hl.h:65
@ LHL_CLS_DIFF_HUNK
Definition lws-hl.h:64
lws_stateful_ret_t(* lws_hl_write_cb)(void *user, const uint8_t *buf, size_t len)
Definition lws-hl.h:231
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_html_escape(lws_hl_write_cb wc, void *user, const uint8_t *src, size_t len)
struct lws_hl_ops lws_hl_ops_t
uint8_t scratch_pos
Definition lws-hl.h:133
uint8_t tokcls
Definition lws-hl.h:135
lws_stateful_ret_t(* lws_hl_token_cb)(void *user, lws_hl_class_t cls, const uint8_t *tok, size_t len)
Definition lws-hl.h:93
lws_stateful_ret_t(* parse)(lws_hl_ctx_t *ctx, const uint8_t **buf, size_t *len)
Definition lws-hl.h:104
lws_stateful_ret_t(* finish)(lws_hl_ctx_t *ctx)
Definition lws-hl.h:109
const char * name
Definition lws-hl.h:97
int(* construct)(lws_hl_ctx_t *ctx)
Definition lws-hl.h:100