libwebsockets
Lightweight C library for HTML5 websockets
Toggle main menu visibility
Loading...
Searching...
No Matches
lws-md.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 markdown to structured events renderer
25
*
26
* Hostile-input-hardened, alloc-free, streaming renderer for the well-behaved
27
* markdown subset used by readme and blog content: headings, paragraphs,
28
* fenced and indented code blocks, blockquotes, lists, pipe tables, rules,
29
* inline code / emphasis / strong, links, images and autolinks.
30
*
31
* Markdown input is never passed through as markup: the driver only issues
32
* structural events from a closed vocabulary plus data events for text, urls,
33
* image alt text and fence info strings. All markup synthesis and all
34
* security decisions about data bytes belong to the sink; the stock html sink
35
* (lws_md_html_t) entity-escapes everything and applies a url scheme policy,
36
* producing strict-CSP-compatible output with no inline styles or scripts.
37
*
38
* Structure that cannot be decided without a line of lookahead (is this the
39
* table header row?) is held in a side buffer and the decision deferred until
40
* the start of the next line; giving up on the decision past the hold cap
41
* reclassifies the held line as text, it never drops content.
42
*/
43
44
#if !defined(LMD_LINE_MAX)
45
#define LMD_LINE_MAX 8192
46
#endif
47
#if !defined(LMD_HOLD_MAX)
48
#define LMD_HOLD_MAX LMD_LINE_MAX
49
#endif
50
#if !defined(LMD_INFO_MAX)
51
#define LMD_INFO_MAX 32
52
#endif
53
#if !defined(LMD_NEST_MAX)
54
#define LMD_NEST_MAX 24
55
#endif
56
#if !defined(LMD_TEXT_PIECE)
57
#define LMD_TEXT_PIECE 256
58
#endif
59
61
typedef
enum
{
62
LMD_EL_NONE
,
63
64
LMD_EL_H
,
65
LMD_EL_P
,
66
LMD_EL_BQ
,
67
LMD_EL_UL
,
68
LMD_EL_OL
,
69
LMD_EL_LI
,
70
LMD_EL_TABLE
,
71
LMD_EL_TR
,
72
LMD_EL_CELL
,
73
LMD_EL_HR
,
74
LMD_EL_CODE
,
75
76
/* inline */
77
78
LMD_EL_EM
,
79
LMD_EL_STRONG
,
80
LMD_EL_CS
,
81
LMD_EL_A
,
82
LMD_EL_IMG
,
83
}
lws_md_el_t
;
84
86
typedef
enum
{
87
LMD_EV_TEXT
= 1,
88
LMD_EV_URL
,
89
LMD_EV_ALT
,
90
LMD_EV_INFO
,
91
LMD_EV_BEGIN
,
92
LMD_EV_END
,
93
}
lws_md_ev_t
;
94
116
typedef
lws_stateful_ret_t
(*
lws_md_ev_cb
)(
void
*user,
lws_md_ev_t
ev,
117
lws_md_el_t
el,
unsigned
int
aux,
118
const
uint8_t
*data,
size_t
len);
119
127
typedef
struct
lws_md_ctx
{
128
lws_md_ev_cb
cb
;
129
void
*
user
;
130
131
/* private below */
132
133
uint32_t
evseq
;
/* issue index within the txn in flight */
134
uint32_t
evdone
;
/* watermark of accepted events */
135
136
uint8_t
txn
;
/* 0 idle, 1 line, 2 hold flush, 3 finish */
137
uint8_t
in_link
;
/* inside link text: no nested links */
138
139
uint8_t
para
;
/* paragraph open */
140
uint8_t
list
;
/* 0 none, else LMD_EL_UL / _OL */
141
uint8_t
li
;
/* inside a list item */
142
uint8_t
table
;
/* inside a table */
143
uint8_t
hold
;
/* line held for table decision */
144
uint8_t
bq
;
/* open blockquote depth */
145
uint8_t
code_ind
;
/* inside an indented code block */
146
147
uint8_t
fence
;
/* inside a fenced code block */
148
uint8_t
fchr
;
/* the fence character */
149
uint8_t
flen
;
/* the opening fence length */
150
uint8_t
fmatch
;
/* fence chars matched at line start */
151
uint8_t
fbol
;
/* at start of a fence body line */
152
uint8_t
fclose
;
/* swallowing a closing fence line */
153
154
uint8_t
over
;
/* past the cap, streaming a line */
155
156
uint8_t
fin_n
;
/* finish: closers remaining */
157
uint8_t
fin_list
[3 + 2 *
LMD_NEST_MAX
];
158
159
uint32_t
llen
;
/* staged line length */
160
uint32_t
hlen
;
/* held line length */
161
162
uint32_t
infolen
;
163
char
info
[
LMD_INFO_MAX
];
164
165
char
line
[
LMD_LINE_MAX
];
166
char
holdb
[
LMD_HOLD_MAX
];
167
}
lws_md_ctx_t
;
168
179
LWS_VISIBLE
LWS_EXTERN
int
180
lws_md_construct
(
lws_md_ctx_t
*ctx,
lws_md_ev_cb
cb,
void
*user);
181
200
LWS_VISIBLE
LWS_EXTERN
lws_stateful_ret_t
201
lws_md_parse
(
lws_md_ctx_t
*ctx,
const
uint8_t
**buf,
size_t
*len);
202
220
LWS_VISIBLE
LWS_EXTERN
lws_stateful_ret_t
221
lws_md_finish
(
lws_md_ctx_t
*ctx);
222
223
#if !defined(LMD_URL_MAX)
224
#define LMD_URL_MAX 768
225
#endif
226
#if !defined(LMD_HTML_BUF)
227
#define LMD_HTML_BUF 4096
228
#endif
229
230
typedef
lws_stateful_ret_t
(*
lws_md_write_cb
)(
void
*user,
const
uint8_t
*buf,
231
size_t
len);
232
247
typedef
size_t (*
lws_md_resolve_cb
)(
void
*user,
int
is_image,
248
const
char
*url,
size_t
len,
249
char
*dest,
size_t
dest_len);
250
251
#if defined(LWS_WITH_HL)
252
#include "
lws-hl.h
"
253
#endif
254
270
typedef
struct
lws_md_html
{
271
/* private */
272
lws_md_write_cb
wc
;
273
void
*
user
;
274
lws_md_resolve_cb
resolve
;
275
void
*
resolve_user
;
276
277
#if defined(LWS_WITH_HL)
278
lws_hl_ctx_t
hlctx;
279
lws_hl_html_t
hlhtml;
280
uint8_t
hl_on;
/* bridging a fenced code block */
281
uint8_t
hl_fin;
/* hl finish issued */
282
uint8_t
hl_closed;
283
uint8_t
hl_hold;
/* a byte held for the tokenizer */
284
uint8_t
hl_holdb;
285
size_t
hl_off;
/* fed offset into current TEXT */
286
#endif
287
288
char
url
[
LMD_URL_MAX
];
289
size_t
url_len
;
290
uint8_t
pending
;
/* 0 none, 1 A, 2 IMG */
291
uint8_t
a_open
;
/* the <a...> tag flushed */
292
uint8_t
alt_open
;
/* inside the img alt attr */
293
uint8_t
code_open
;
/* inside a fence */
294
uint8_t
pre_done
;
/* the <pre><code> flushed */
295
char
info
[
LMD_INFO_MAX
];
296
uint8_t
infolen
;
297
298
char
buf
[
LMD_HTML_BUF
];
299
size_t
buflen
;
300
}
lws_md_html_t
;
301
314
LWS_VISIBLE
LWS_EXTERN
int
315
lws_md_html_construct
(
lws_md_html_t
*h,
lws_md_write_cb
wc,
void
*user,
316
lws_md_resolve_cb
resolve,
void
*resolve_user);
317
333
LWS_VISIBLE
LWS_EXTERN
lws_stateful_ret_t
334
lws_md_html_event
(
void
*user,
lws_md_ev_t
ev,
lws_md_el_t
el,
335
unsigned
int
aux,
const
uint8_t
*data,
size_t
len);
336
347
LWS_VISIBLE
LWS_EXTERN
lws_stateful_ret_t
348
lws_md_html_close
(
lws_md_html_t
*h);
uint32_t
unsigned int uint32_t
Definition
libwebsockets.h:744
LWS_EXTERN
#define LWS_EXTERN
Definition
libwebsockets.h:327
uint8_t
unsigned char uint8_t
Definition
libwebsockets.h:746
lws_stateful_ret_t
lws_stateful_ret_t
Definition
libwebsockets.h:802
LWS_VISIBLE
#define LWS_VISIBLE
Definition
libwebsockets.h:322
lws-hl.h
lws_hl_html_t
struct lws_hl_html lws_hl_html_t
lws_hl_ctx_t
struct lws_hl_ctx lws_hl_ctx_t
Definition
lws-hl.h:71
LMD_HOLD_MAX
#define LMD_HOLD_MAX
Definition
lws-md.h:48
LMD_NEST_MAX
#define LMD_NEST_MAX
Definition
lws-md.h:54
lws_md_ctx::evseq
uint32_t evseq
Definition
lws-md.h:133
lws_md_html::wc
lws_md_write_cb wc
Definition
lws-md.h:272
lws_md_ctx::fmatch
uint8_t fmatch
Definition
lws-md.h:150
LMD_LINE_MAX
#define LMD_LINE_MAX
Definition
lws-md.h:45
lws_md_ctx::fbol
uint8_t fbol
Definition
lws-md.h:151
lws_md_ev_cb
lws_stateful_ret_t(* lws_md_ev_cb)(void *user, lws_md_ev_t ev, lws_md_el_t el, unsigned int aux, const uint8_t *data, size_t len)
Definition
lws-md.h:116
lws_md_ctx::over
uint8_t over
Definition
lws-md.h:154
lws_md_ctx::flen
uint8_t flen
Definition
lws-md.h:149
LMD_URL_MAX
#define LMD_URL_MAX
Definition
lws-md.h:224
lws_md_write_cb
lws_stateful_ret_t(* lws_md_write_cb)(void *user, const uint8_t *buf, size_t len)
Definition
lws-md.h:230
lws_md_ctx::fin_n
uint8_t fin_n
Definition
lws-md.h:156
lws_md_ctx::info
char info[LMD_INFO_MAX]
Definition
lws-md.h:163
LMD_INFO_MAX
#define LMD_INFO_MAX
Definition
lws-md.h:51
lws_md_ctx::in_link
uint8_t in_link
Definition
lws-md.h:137
lws_md_html_close
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_html_close(lws_md_html_t *h)
lws_md_ctx::code_ind
uint8_t code_ind
Definition
lws-md.h:145
lws_md_ctx::table
uint8_t table
Definition
lws-md.h:142
lws_md_ctx::bq
uint8_t bq
Definition
lws-md.h:144
lws_md_finish
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_finish(lws_md_ctx_t *ctx)
lws_md_html::user
void * user
Definition
lws-md.h:273
lws_md_html::pre_done
uint8_t pre_done
Definition
lws-md.h:294
lws_md_parse
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_parse(lws_md_ctx_t *ctx, const uint8_t **buf, size_t *len)
lws_md_html::info
char info[LMD_INFO_MAX]
Definition
lws-md.h:295
lws_md_html_construct
LWS_VISIBLE LWS_EXTERN int lws_md_html_construct(lws_md_html_t *h, lws_md_write_cb wc, void *user, lws_md_resolve_cb resolve, void *resolve_user)
lws_md_html::buflen
size_t buflen
Definition
lws-md.h:299
lws_md_html::infolen
uint8_t infolen
Definition
lws-md.h:296
lws_md_el_t
lws_md_el_t
Definition
lws-md.h:61
LMD_EL_STRONG
@ LMD_EL_STRONG
Definition
lws-md.h:79
LMD_EL_IMG
@ LMD_EL_IMG
Definition
lws-md.h:82
LMD_EL_NONE
@ LMD_EL_NONE
Definition
lws-md.h:62
LMD_EL_OL
@ LMD_EL_OL
Definition
lws-md.h:68
LMD_EL_TABLE
@ LMD_EL_TABLE
Definition
lws-md.h:70
LMD_EL_EM
@ LMD_EL_EM
Definition
lws-md.h:78
LMD_EL_CELL
@ LMD_EL_CELL
Definition
lws-md.h:72
LMD_EL_TR
@ LMD_EL_TR
Definition
lws-md.h:71
LMD_EL_BQ
@ LMD_EL_BQ
Definition
lws-md.h:66
LMD_EL_H
@ LMD_EL_H
Definition
lws-md.h:64
LMD_EL_CS
@ LMD_EL_CS
Definition
lws-md.h:80
LMD_EL_HR
@ LMD_EL_HR
Definition
lws-md.h:73
LMD_EL_LI
@ LMD_EL_LI
Definition
lws-md.h:69
LMD_EL_A
@ LMD_EL_A
Definition
lws-md.h:81
LMD_EL_P
@ LMD_EL_P
Definition
lws-md.h:65
LMD_EL_CODE
@ LMD_EL_CODE
Definition
lws-md.h:74
LMD_EL_UL
@ LMD_EL_UL
Definition
lws-md.h:67
lws_md_ctx::user
void * user
Definition
lws-md.h:129
lws_md_html::pending
uint8_t pending
Definition
lws-md.h:290
lws_md_ctx::line
char line[LMD_LINE_MAX]
Definition
lws-md.h:165
lws_md_ctx_t
struct lws_md_ctx lws_md_ctx_t
lws_md_html::buf
char buf[LMD_HTML_BUF]
Definition
lws-md.h:298
lws_md_resolve_cb
size_t(* lws_md_resolve_cb)(void *user, int is_image, const char *url, size_t len, char *dest, size_t dest_len)
Definition
lws-md.h:247
lws_md_ctx::cb
lws_md_ev_cb cb
Definition
lws-md.h:128
lws_md_ev_t
lws_md_ev_t
Definition
lws-md.h:86
LMD_EV_TEXT
@ LMD_EV_TEXT
Definition
lws-md.h:87
LMD_EV_BEGIN
@ LMD_EV_BEGIN
Definition
lws-md.h:91
LMD_EV_INFO
@ LMD_EV_INFO
Definition
lws-md.h:90
LMD_EV_URL
@ LMD_EV_URL
Definition
lws-md.h:88
LMD_EV_ALT
@ LMD_EV_ALT
Definition
lws-md.h:89
LMD_EV_END
@ LMD_EV_END
Definition
lws-md.h:92
lws_md_ctx::fclose
uint8_t fclose
Definition
lws-md.h:152
lws_md_ctx::txn
uint8_t txn
Definition
lws-md.h:136
lws_md_html_event
LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_md_html_event(void *user, lws_md_ev_t ev, lws_md_el_t el, unsigned int aux, const uint8_t *data, size_t len)
lws_md_html::url
char url[LMD_URL_MAX]
Definition
lws-md.h:288
lws_md_ctx::infolen
uint32_t infolen
Definition
lws-md.h:162
lws_md_html::a_open
uint8_t a_open
Definition
lws-md.h:291
lws_md_html::url_len
size_t url_len
Definition
lws-md.h:289
lws_md_ctx::li
uint8_t li
Definition
lws-md.h:141
lws_md_ctx::fence
uint8_t fence
Definition
lws-md.h:147
lws_md_html_t
struct lws_md_html lws_md_html_t
lws_md_ctx::hold
uint8_t hold
Definition
lws-md.h:143
lws_md_html::code_open
uint8_t code_open
Definition
lws-md.h:293
lws_md_ctx::para
uint8_t para
Definition
lws-md.h:139
lws_md_html::alt_open
uint8_t alt_open
Definition
lws-md.h:292
lws_md_ctx::fchr
uint8_t fchr
Definition
lws-md.h:148
lws_md_html::resolve
lws_md_resolve_cb resolve
Definition
lws-md.h:274
lws_md_html::resolve_user
void * resolve_user
Definition
lws-md.h:275
lws_md_ctx::llen
uint32_t llen
Definition
lws-md.h:159
lws_md_ctx::list
uint8_t list
Definition
lws-md.h:140
lws_md_ctx::holdb
char holdb[LMD_HOLD_MAX]
Definition
lws-md.h:166
lws_md_ctx::hlen
uint32_t hlen
Definition
lws-md.h:160
lws_md_ctx::fin_list
uint8_t fin_list[3+2 *LMD_NEST_MAX]
Definition
lws-md.h:157
lws_md_ctx::evdone
uint32_t evdone
Definition
lws-md.h:134
lws_md_construct
LWS_VISIBLE LWS_EXTERN int lws_md_construct(lws_md_ctx_t *ctx, lws_md_ev_cb cb, void *user)
LMD_HTML_BUF
#define LMD_HTML_BUF
Definition
lws-md.h:227
lws_md_ctx
Definition
lws-md.h:127
lws_md_html
Definition
lws-md.h:270
include
libwebsockets
lws-md.h
Generated on
for libwebsockets by
1.18.0