src/pdfmake_markup.c

filter: Statementstmt 96.2% 385 / 400 Branchbran 82.5% 312 / 378 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.5% 697 / 778 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_tokenizer.c ↑ Summary src/pdfmake_crypt.c »
1 /*
2 * pdfmake_markup.c - document markup parser.
3 *
4 * One pass, no backtracking, no recovery. See pdfmake_markup.h for why the
5 * grammar is closed and why every failure is a hard error with a position.
6 */
7  
8 #include "pdfmake_markup.h"
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13  
14 /*----------------------------------------------------------------------------
15 * Tag table
16 *--------------------------------------------------------------------------*/
17  
18 typedef struct {
19 const char *name;
20 pdfmake_markup_tag_t id;
21 uint32_t flags;
22 } tag_ent_t;
23  
24 static const tag_ent_t TAGS[] = {
25 { "doc", PDFMAKE_MK_DOC, PDFMAKE_MKF_CONTAINER },
26 { "style", PDFMAKE_MK_STYLE, PDFMAKE_MKF_VOID },
27 { "page", PDFMAKE_MK_PAGE, PDFMAKE_MKF_CONTAINER },
28 { "pagebreak", PDFMAKE_MK_PAGEBREAK, PDFMAKE_MKF_VOID },
29 { "header", PDFMAKE_MK_HEADER, PDFMAKE_MKF_CONTAINER },
30 { "footer", PDFMAKE_MK_FOOTER, PDFMAKE_MKF_CONTAINER },
31 { "h1", PDFMAKE_MK_H1, 0 },
32 { "h2", PDFMAKE_MK_H2, 0 },
33 { "h3", PDFMAKE_MK_H3, 0 },
34 { "h4", PDFMAKE_MK_H4, 0 },
35 { "h5", PDFMAKE_MK_H5, 0 },
36 { "h6", PDFMAKE_MK_H6, 0 },
37 { "p", PDFMAKE_MK_P, 0 },
38 { "text", PDFMAKE_MK_TEXT, 0 },
39 { "hr", PDFMAKE_MK_HR, PDFMAKE_MKF_VOID },
40 { "box", PDFMAKE_MK_BOX, PDFMAKE_MKF_CONTAINER },
41 { "img", PDFMAKE_MK_IMG, PDFMAKE_MKF_VOID },
42 { "row", PDFMAKE_MK_ROW, PDFMAKE_MKF_CONTAINER },
43 { "cell", PDFMAKE_MK_CELL, 0 },
44 { "table", PDFMAKE_MK_TABLE, PDFMAKE_MKF_CONTAINER },
45 { "tr", PDFMAKE_MK_TR, PDFMAKE_MKF_CONTAINER },
46 { "th", PDFMAKE_MK_TH, 0 },
47 { "td", PDFMAKE_MK_TD, 0 },
48 { "bookmark", PDFMAKE_MK_BOOKMARK, PDFMAKE_MKF_VOID },
49 { "b", PDFMAKE_MK_B, PDFMAKE_MKF_INLINE },
50 { "i", PDFMAKE_MK_I, PDFMAKE_MKF_INLINE },
51 { "span", PDFMAKE_MK_SPAN, PDFMAKE_MKF_INLINE }
52 };
53  
54 #define TAG_COUNT (sizeof(TAGS) / sizeof(TAGS[0]))
55  
56 21945 const char *pdfmake_markup_tag_name(pdfmake_markup_tag_t tag) {
57 size_t i;
58 325892 ▶ for (i = 0; i < TAG_COUNT; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < TAG_COUNT; i++) 325892 0 325892
59 325892 ▶ if (TAGS[i].id == tag) return TAGS[i].name;
Branch: 1 branch
BranchTrueFalseTotal
if (TAGS[i].id == tag) return TAGS[i].name; 21945 303947 325892
60 0 return NULL;
61 }
62  
63 27002 pdfmake_markup_tag_t pdfmake_markup_tag_id(const char *name, size_t len) {
64 size_t i;
65 27002 ▶ if (!name) return PDFMAKE_MK_INVALID;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return PDFMAKE_MK_INVALID; 0 27002 27002
66 392233 ▶ for (i = 0; i < TAG_COUNT; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < TAG_COUNT; i++) { 391967 266 392233
67 391967 ▶ if (strlen(TAGS[i].name) == len && memcmp(TAGS[i].name, name, len) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(TAGS[i].name) == len && memcmp(TAGS[i].name, name, len) == 0) 160775 231192 391967
if (strlen(TAGS[i].name) == len && memcmp(TAGS[i].name, name, len) == 0) 26736 134039 160775
68 26736 return TAGS[i].id;
69 }
70 266 return PDFMAKE_MK_INVALID;
71 }
72  
73 27525 uint32_t pdfmake_markup_tag_flags(pdfmake_markup_tag_t tag) {
74 size_t i;
75 379716 ▶ for (i = 0; i < TAG_COUNT; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < TAG_COUNT; i++) 379716 0 379716
76 379716 ▶ if (TAGS[i].id == tag) return TAGS[i].flags;
Branch: 1 branch
BranchTrueFalseTotal
if (TAGS[i].id == tag) return TAGS[i].flags; 27525 352191 379716
77 0 return 0;
78 }
79  
80 /*----------------------------------------------------------------------------
81 * Scanner state
82 *--------------------------------------------------------------------------*/
83  
84 typedef struct {
85 const char *p; /* cursor */
86 const char *end;
87 uint32_t line;
88 uint32_t col;
89 pdfmake_markup_doc_t *doc;
90 int depth;
91 } scan_t;
92  
93 2109914 static int at_end(const scan_t *s) { return s->p >= s->end; }
94  
95 1174965 ▶static char peek(const scan_t *s) { return at_end(s) ? '\0' : *s->p; }
Branch: 1 branch
BranchTrueFalseTotal
static char peek(const scan_t *s) { return at_end(s) ? '\0' : *s->p; } 1174961 4 1174965
96  
97 104592 static char peek_at(const scan_t *s, size_t off) {
98 104592 ▶ return (s->p + off >= s->end) ? '\0' : s->p[off];
Branch: 1 branch
BranchTrueFalseTotal
return (s->p + off >= s->end) ? '\0' : s->p[off]; 104592 0 104592
99 }
100  
101 440986 static void advance(scan_t *s) {
102 440986 ▶ if (at_end(s)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (at_end(s)) return; 0 440986 440986
103 440986 ▶ if (*s->p == '\n') { s->line++; s->col = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s->p == '\n') { s->line++; s->col = 1; } 1412 439574 440986
104 439574 else { s->col++; }
105 440986 s->p++;
106 }
107  
108 307122 static int is_space(char c) {
109 307122 ▶ return c == ' ' || c == '\t' || c == '\r' || c == '\n';
Branch: 4 branches
BranchTrueFalseTotal
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; 253300 53822 307122
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; 253300 0 253300
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; 253300 0 253300
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; 1404 251896 253300
110 }
111  
112 /* Names are ASCII letters, digits, '-' and '_', starting with a letter. */
113 144215 static int is_name_start(char c) {
114 144215 ▶ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
Branch: 4 branches
BranchTrueFalseTotal
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); 107199 37016 144215
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); 0 107199 107199
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); 2 37014 37016
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); 2 0 2
115 }
116  
117 117534 static int is_name_char(char c) {
118 117534 ▶ return is_name_start(c) || (c >= '0' && c <= '9') || c == '-' || c == '_';
Branch: 5 branches
BranchTrueFalseTotal
return is_name_start(c) || (c >= '0' && c <= '9') || c == '-' || c == '_'; 36842 80692 117534
return is_name_start(c) || (c >= '0' && c <= '9') || c == '-' || c == '_'; 13775 23067 36842
return is_name_start(c) || (c >= '0' && c <= '9') || c == '-' || c == '_'; 11211 2564 13775
return is_name_start(c) || (c >= '0' && c <= '9') || c == '-' || c == '_'; 34272 6 34278
return is_name_start(c) || (c >= '0' && c <= '9') || c == '-' || c == '_'; 0 34272 34272
119 }
120  
121 38351 static void skip_space(scan_t *s) {
122 40473 ▶ while (!at_end(s) && is_space(peek(s))) advance(s);
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && is_space(peek(s))) advance(s); 39797 676 40473
while (!at_end(s) && is_space(peek(s))) advance(s); 2122 37675 39797
123 38351 }
124  
125 /* Record the first error and stop. Later errors are noise: a parser without
126 * recovery cannot say anything trustworthy about what follows. */
127 1934 static void fail(scan_t *s, uint32_t line, uint32_t col, const char *fmt, ...) {
128 va_list ap;
129 1934 ▶ if (!s->doc->ok) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->doc->ok) return; 0 1934 1934
130 1934 s->doc->ok = 0;
131 1934 s->doc->err_line = line;
132 1934 s->doc->err_col = col;
133 1934 va_start(ap, fmt);
134 1934 vsnprintf(s->doc->err, PDFMAKE_MARKUP_ERR_LEN, fmt, ap);
135 1934 va_end(ap);
136 1934 s->p = s->end; /* stop the scan */
137 }
138  
139 /*----------------------------------------------------------------------------
140 * Nodes
141 *--------------------------------------------------------------------------*/
142  
143 30535 static pdfmake_markup_node_t *node_new(scan_t *s, pdfmake_markup_kind_t kind,
144 uint32_t line, uint32_t col) {
145 pdfmake_markup_node_t *n = (pdfmake_markup_node_t *)
146 30535 pdfmake_arena_calloc(s->doc->arena, sizeof(*n));
147 30535 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 30535 30535
148 30535 n->kind = kind;
149 30535 n->line = line;
150 30535 n->col = col;
151 30535 return n;
152 }
153  
154 28258 static void node_append(pdfmake_markup_node_t *parent,
155 pdfmake_markup_node_t *child) {
156 28258 child->parent = parent;
157 28258 ▶ if (parent->last_child) {
Branch: 1 branch
BranchTrueFalseTotal
if (parent->last_child) { 21320 6938 28258
158 21320 parent->last_child->next_sibling = child;
159 21320 parent->last_child = child;
160 } else {
161 6938 parent->first_child = parent->last_child = child;
162 }
163 28258 }
164  
165 /*----------------------------------------------------------------------------
166 * Entities
167 *
168 * A fixed five plus numeric character references. No entity table beyond
169 * that: &nbsp; and friends are a slope that ends in an HTML spec.
170 *--------------------------------------------------------------------------*/
171  
172 /* Encode a code point as UTF-8 into out, returning bytes written. */
173 4 static size_t utf8_encode(uint32_t cp, char *out) {
174 4 ▶ if (cp < 0x80) { out[0] = (char)cp; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) { out[0] = (char)cp; return 1; } 2 2 4
175 2 ▶ if (cp < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x800) { 1 1 2
176 1 out[0] = (char)(0xC0 | (cp >> 6));
177 1 out[1] = (char)(0x80 | (cp & 0x3F));
178 1 return 2;
179 }
180 1 ▶ if (cp < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x10000) { 1 0 1
181 1 out[0] = (char)(0xE0 | (cp >> 12));
182 1 out[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
183 1 out[2] = (char)(0x80 | (cp & 0x3F));
184 1 return 3;
185 }
186 0 out[0] = (char)(0xF0 | (cp >> 18));
187 0 out[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
188 0 out[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
189 0 out[3] = (char)(0x80 | (cp & 0x3F));
190 0 return 4;
191 }
192  
193 /*
194 * Decode one entity at s->p (which points at '&'). Writes to out and returns
195 * bytes written, or 0 on error (already reported).
196 */
197 411 static size_t decode_entity(scan_t *s, char *out) {
198 411 uint32_t line = s->line, col = s->col;
199 411 const char *start = s->p;
200 size_t n;
201  
202 411 advance(s); /* '&' */
203  
204 411 ▶ if (peek(s) == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '#') { 9 402 411
205 9 uint32_t cp = 0;
206 9 int digits = 0;
207 9 int hex = 0;
208 9 advance(s);
209 9 ▶ if (peek(s) == 'x' || peek(s) == 'X') { hex = 1; advance(s); }
Branch: 2 branches
BranchTrueFalseTotal
if (peek(s) == 'x' || peek(s) == 'X') { hex = 1; advance(s); } 6 3 9
if (peek(s) == 'x' || peek(s) == 'X') { hex = 1; advance(s); } 0 6 6
210 32 ▶ while (!at_end(s) && peek(s) != ';') {
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && peek(s) != ';') { 32 0 32
while (!at_end(s) && peek(s) != ';') { 25 7 32
211 25 char c = peek(s);
212 uint32_t d;
213 25 ▶ if (c >= '0' && c <= '9') d = (uint32_t)(c - '0');
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') d = (uint32_t)(c - '0'); 25 0 25
if (c >= '0' && c <= '9') d = (uint32_t)(c - '0'); 21 4 25
214 4 ▶ else if (hex && c >= 'a' && c <= 'f') d = (uint32_t)(c - 'a' + 10);
Branch: 3 branches
BranchTrueFalseTotal
else if (hex && c >= 'a' && c <= 'f') d = (uint32_t)(c - 'a' + 10); 3 1 4
else if (hex && c >= 'a' && c <= 'f') d = (uint32_t)(c - 'a' + 10); 0 3 3
else if (hex && c >= 'a' && c <= 'f') d = (uint32_t)(c - 'a' + 10); 0 0 0
215 4 ▶ else if (hex && c >= 'A' && c <= 'F') d = (uint32_t)(c - 'A' + 10);
Branch: 3 branches
BranchTrueFalseTotal
else if (hex && c >= 'A' && c <= 'F') d = (uint32_t)(c - 'A' + 10); 3 1 4
else if (hex && c >= 'A' && c <= 'F') d = (uint32_t)(c - 'A' + 10); 3 0 3
else if (hex && c >= 'A' && c <= 'F') d = (uint32_t)(c - 'A' + 10); 3 0 3
216 else {
217 1 ▶ fail(s, line, col,
Branch: 1 branch
BranchTrueFalseTotal
fail(s, line, col, 0 1 1
218 "bad character reference: '%c' is not a %s digit",
219 c, hex ? "hex" : "decimal");
220 1 return 0;
221 }
222 24 ▶ if (cp > 0x10FFFF) { /* clamp before overflow */
Branch: 1 branch
BranchTrueFalseTotal
if (cp > 0x10FFFF) { /* clamp before overflow */ 1 23 24
223 1 fail(s, line, col, "character reference out of range");
224 1 return 0;
225 }
226 23 ▶ cp = cp * (hex ? 16u : 10u) + d;
Branch: 1 branch
BranchTrueFalseTotal
cp = cp * (hex ? 16u : 10u) + d; 10 13 23
227 23 digits++;
228 23 advance(s);
229 }
230 7 ▶ if (peek(s) != ';') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) != ';') { 0 7 7
231 0 fail(s, line, col, "unterminated character reference: missing ';'");
232 0 return 0;
233 }
234 7 advance(s); /* ';' */
235 7 ▶ if (!digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (!digits) { 1 6 7
236 1 fail(s, line, col, "empty character reference");
237 1 return 0;
238 }
239 6 ▶ if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) {
Branch: 4 branches
BranchTrueFalseTotal
if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) { 5 1 6
if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) { 5 0 5
if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) { 1 4 5
if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) { 1 0 1
240 2 fail(s, line, col,
241 "character reference is not a valid code point: %lu",
242 (unsigned long)cp);
243 2 return 0;
244 }
245 4 return utf8_encode(cp, out);
246 }
247  
248 /* Named: read up to ';' */
249 {
250 402 const char *nstart = s->p;
251 size_t nlen;
252 1465 ▶ while (!at_end(s) && peek(s) != ';' && is_name_char(peek(s))) advance(s);
Branch: 3 branches
BranchTrueFalseTotal
while (!at_end(s) && peek(s) != ';' && is_name_char(peek(s))) advance(s); 1465 0 1465
while (!at_end(s) && peek(s) != ';' && is_name_char(peek(s))) advance(s); 1153 312 1465
while (!at_end(s) && peek(s) != ';' && is_name_char(peek(s))) advance(s); 1063 90 1153
253 402 nlen = (size_t)(s->p - nstart);
254 402 ▶ if (peek(s) != ';') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) != ';') { 90 312 402
255 90 fail(s, line, col,
256 "unterminated entity: missing ';' (write &amp; for a literal &)");
257 90 return 0;
258 }
259 312 advance(s); /* ';' */
260  
261 312 ▶ if (nlen == 2 && memcmp(nstart, "lt", 2) == 0) { out[0] = '<'; return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (nlen == 2 && memcmp(nstart, "lt", 2) == 0) { out[0] = '<'; return 1; } 6 306 312
if (nlen == 2 && memcmp(nstart, "lt", 2) == 0) { out[0] = '<'; return 1; } 1 5 6
262 311 ▶ if (nlen == 2 && memcmp(nstart, "gt", 2) == 0) { out[0] = '>'; return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (nlen == 2 && memcmp(nstart, "gt", 2) == 0) { out[0] = '>'; return 1; } 5 306 311
if (nlen == 2 && memcmp(nstart, "gt", 2) == 0) { out[0] = '>'; return 1; } 1 4 5
263 310 ▶ if (nlen == 3 && memcmp(nstart, "amp", 3) == 0) { out[0] = '&'; return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (nlen == 3 && memcmp(nstart, "amp", 3) == 0) { out[0] = '&'; return 1; } 299 11 310
if (nlen == 3 && memcmp(nstart, "amp", 3) == 0) { out[0] = '&'; return 1; } 299 0 299
264 11 ▶ if (nlen == 4 && memcmp(nstart, "quot", 4) == 0) { out[0] = '"'; return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (nlen == 4 && memcmp(nstart, "quot", 4) == 0) { out[0] = '"'; return 1; } 5 6 11
if (nlen == 4 && memcmp(nstart, "quot", 4) == 0) { out[0] = '"'; return 1; } 2 3 5
265 9 ▶ if (nlen == 4 && memcmp(nstart, "apos", 4) == 0) { out[0] = '\''; return 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (nlen == 4 && memcmp(nstart, "apos", 4) == 0) { out[0] = '\''; return 1; } 3 6 9
if (nlen == 4 && memcmp(nstart, "apos", 4) == 0) { out[0] = '\''; return 1; } 2 1 3
266  
267 7 n = (size_t)(s->p - start);
268 7 ▶ fail(s, line, col,
Branch: 1 branch
BranchTrueFalseTotal
fail(s, line, col, 7 0 7
269 "unknown entity '%.*s' (only &amp; &lt; &gt; &quot; &apos; and "
270 "&#NNN; are defined)",
271 (int)(n > 32 ? 32 : n), start);
272 7 return 0;
273 }
274 }
275  
276 /*----------------------------------------------------------------------------
277 * Text
278 *--------------------------------------------------------------------------*/
279  
280 /* Read a text run up to the next '<'. Returns NULL and leaves ok==0 on a bad
281 * entity; returns NULL with ok==1 when the run is dropped as layout
282 * whitespace. */
283 3720 static pdfmake_markup_node_t *parse_text(scan_t *s,
284 pdfmake_markup_node_t *parent) {
285 3720 uint32_t line = s->line, col = s->col;
286 3720 const char *start = s->p;
287 char *buf;
288 3720 size_t cap, len = 0;
289 3720 int all_space = 1;
290 pdfmake_markup_node_t *n;
291  
292 /* Decoded output is never longer than the source run. */
293 {
294 3720 const char *q = s->p;
295 273281 ▶ while (q < s->end && *q != '<') q++;
Branch: 2 branches
BranchTrueFalseTotal
while (q < s->end && *q != '<') q++; 273254 27 273281
while (q < s->end && *q != '<') q++; 269561 3693 273254
296 3720 cap = (size_t)(q - s->p) + 4;
297 }
298 3720 buf = (char *)pdfmake_arena_alloc(s->doc->arena, cap + 1);
299 3720 ▶ if (!buf) { fail(s, line, col, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) { fail(s, line, col, "out of memory"); return NULL; } 0 3720 3720
300  
301 271354 ▶ while (!at_end(s) && peek(s) != '<') {
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && peek(s) != '<') { 271330 24 271354
while (!at_end(s) && peek(s) != '<') { 267726 3604 271330
302 267726 ▶ if (peek(s) == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '&') { 401 267325 267726
303 401 size_t w = decode_entity(s, buf + len);
304 401 ▶ if (!w) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!w) return NULL; 92 309 401
305 309 len += w;
306 309 all_space = 0;
307 } else {
308 267325 char c = peek(s);
309 267325 ▶ if (!is_space(c)) all_space = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!is_space(c)) all_space = 0; 214221 53104 267325
310 267325 buf[len++] = c;
311 267325 advance(s);
312 }
313 }
314 3628 buf[len] = '\0';
315  
316 (void)start;
317  
318 /* Whitespace between the children of a container is indentation, not
319 * content. Inside a text-bearing element it is a real space and has to
320 * survive - "Total <b>due</b>" would otherwise lose its gap. */
321 3628 ▶ if (all_space &&
Branch: 1 branch
BranchTrueFalseTotal
if (all_space && 1330 2298 3628
322 1330 ▶ (pdfmake_markup_tag_flags(parent->tag) & PDFMAKE_MKF_CONTAINER))
Branch: 1 branch
BranchTrueFalseTotal
(pdfmake_markup_tag_flags(parent->tag) & PDFMAKE_MKF_CONTAINER)) 1317 13 1330
323 1317 return NULL;
324  
325 2311 n = node_new(s, PDFMAKE_MARKUP_TEXT, line, col);
326 2311 ▶ if (!n) { fail(s, line, col, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { fail(s, line, col, "out of memory"); return NULL; } 0 2311 2311
327 2311 n->text = buf;
328 2311 n->text_len = len;
329 2311 return n;
330 }
331  
332 /*----------------------------------------------------------------------------
333 * Attributes
334 *--------------------------------------------------------------------------*/
335  
336 2201 static pdfmake_markup_attr_t *parse_attr(scan_t *s) {
337 2201 uint32_t line = s->line, col = s->col;
338 const char *nstart;
339 size_t nlen;
340 char quote;
341 char *buf;
342 2201 size_t cap, len = 0;
343 pdfmake_markup_attr_t *a;
344  
345 2201 nstart = s->p;
346 10743 ▶ while (!at_end(s) && is_name_char(peek(s))) advance(s);
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && is_name_char(peek(s))) advance(s); 10743 0 10743
while (!at_end(s) && is_name_char(peek(s))) advance(s); 8542 2201 10743
347 2201 nlen = (size_t)(s->p - nstart);
348 2201 ▶ if (!nlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nlen) { 143 2058 2201
349 143 fail(s, line, col, "expected an attribute name, found '%c'", peek(s));
350 143 return NULL;
351 }
352  
353 2058 skip_space(s);
354 2058 ▶ if (peek(s) != '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) != '=') { 125 1933 2058
355 125 fail(s, line, col,
356 "attribute '%.*s' has no value (valueless attributes are not "
357 "allowed; write %.*s=\"1\")",
358 (int)nlen, nstart, (int)nlen, nstart);
359 125 return NULL;
360 }
361 1933 advance(s);
362 1933 skip_space(s);
363  
364 1933 quote = peek(s);
365 1933 ▶ if (quote != '"' && quote != '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (quote != '"' && quote != '\'') { 28 1905 1933
if (quote != '"' && quote != '\'') { 25 3 28
366 25 fail(s, s->line, s->col,
367 "attribute '%.*s' value must be quoted", (int)nlen, nstart);
368 25 return NULL;
369 }
370 1908 advance(s);
371  
372 {
373 1908 const char *q = s->p;
374 7404 ▶ while (q < s->end && *q != quote) q++;
Branch: 2 branches
BranchTrueFalseTotal
while (q < s->end && *q != quote) q++; 7381 23 7404
while (q < s->end && *q != quote) q++; 5496 1885 7381
375 1908 cap = (size_t)(q - s->p) + 4;
376 }
377 1908 buf = (char *)pdfmake_arena_alloc(s->doc->arena, cap + 1);
378 1908 ▶ if (!buf) { fail(s, line, col, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) { fail(s, line, col, "out of memory"); return NULL; } 0 1908 1908
379  
380 6465 ▶ while (!at_end(s) && peek(s) != quote) {
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && peek(s) != quote) { 6464 1 6465
while (!at_end(s) && peek(s) != quote) { 4603 1861 6464
381 4603 ▶ if (peek(s) == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '<') { 36 4567 4603
382 /* Nearly always an unterminated quote rather than a literal '<',
383 * so lead with that reading; the scanner cannot tell which, and
384 * naming only the rarer cause sends the author looking in the
385 * wrong place. */
386 36 fail(s, s->line, s->col,
387 "'<' inside the value of attribute '%.*s': the opening quote "
388 "is probably unclosed (write &lt; for a literal '<')",
389 (int)nlen, nstart);
390 36 return NULL;
391 }
392 4567 ▶ if (peek(s) == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '&') { 10 4557 4567
393 10 size_t w = decode_entity(s, buf + len);
394 10 ▶ if (!w) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!w) return NULL; 10 0 10
395 0 len += w;
396 } else {
397 4557 buf[len++] = peek(s);
398 4557 advance(s);
399 }
400 }
401 1862 ▶ if (peek(s) != quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) != quote) { 1 1861 1862
402 1 fail(s, line, col, "unterminated value for attribute '%.*s'",
403 (int)nlen, nstart);
404 1 return NULL;
405 }
406 1861 advance(s);
407 1861 buf[len] = '\0';
408  
409 a = (pdfmake_markup_attr_t *)
410 1861 pdfmake_arena_calloc(s->doc->arena, sizeof(*a));
411 1861 ▶ if (!a) { fail(s, line, col, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!a) { fail(s, line, col, "out of memory"); return NULL; } 0 1861 1861
412 1861 a->name = pdfmake_arena_memdup(s->doc->arena, nstart, nlen + 1);
413 1861 ▶ if (!a->name) { fail(s, line, col, "out of memory"); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!a->name) { fail(s, line, col, "out of memory"); return NULL; } 0 1861 1861
414 1861 ((char *)a->name)[nlen] = '\0';
415 1861 a->name_len = nlen;
416 1861 a->value = buf;
417 1861 a->value_len = len;
418 1861 a->line = line;
419 1861 a->col = col;
420 1861 return a;
421 }
422  
423 /*----------------------------------------------------------------------------
424 * Elements
425 *--------------------------------------------------------------------------*/
426  
427 static void parse_node(scan_t *s, pdfmake_markup_node_t *parent);
428  
429 /* Comments are skipped. They are the one piece of syntax here that exists
430 * purely for whoever maintains the template. */
431 24777 static int skip_comment(scan_t *s) {
432 24777 uint32_t line = s->line, col = s->col;
433 24786 ▶ if (!(peek(s) == '<' && peek_at(s, 1) == '!' &&
Branch: 1 branch
BranchTrueFalseTotal
if (!(peek(s) == '<' && peek_at(s, 1) == '!' && 24777 0 24777
434 9 peek_at(s, 2) == '-' && peek_at(s, 3) == '-'))
435 24773 return 0;
436 4 advance(s); advance(s); advance(s); advance(s);
437 47 ▶ while (!at_end(s)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!at_end(s)) { 46 1 47
438 46 ▶ if (peek(s) == '-' && peek_at(s, 1) == '-' && peek_at(s, 2) == '>') {
Branch: 3 branches
BranchTrueFalseTotal
if (peek(s) == '-' && peek_at(s, 1) == '-' && peek_at(s, 2) == '>') { 3 43 46
if (peek(s) == '-' && peek_at(s, 1) == '-' && peek_at(s, 2) == '>') { 3 0 3
if (peek(s) == '-' && peek_at(s, 1) == '-' && peek_at(s, 2) == '>') { 3 0 3
439 3 advance(s); advance(s); advance(s);
440 3 return 1;
441 }
442 43 advance(s);
443 }
444 1 fail(s, line, col, "unterminated comment: missing '-->'");
445 1 return 1;
446 }
447  
448 26681 static void parse_element(scan_t *s, pdfmake_markup_node_t *parent) {
449 26681 uint32_t line = s->line, col = s->col;
450 const char *nstart;
451 size_t nlen;
452 pdfmake_markup_tag_t tag;
453 pdfmake_markup_node_t *n;
454 26681 pdfmake_markup_attr_t *last = NULL;
455 26681 int self_closing = 0;
456  
457 26681 advance(s); /* '<' */
458  
459 26681 nstart = s->p;
460 26681 ▶ if (!is_name_start(peek(s))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_name_start(peek(s))) { 172 26509 26681
461 172 fail(s, line, col,
462 "expected a tag name after '<' (write &lt; for a literal '<')");
463 172 return;
464 }
465 85517 ▶ while (!at_end(s) && is_name_char(peek(s))) advance(s);
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && is_name_char(peek(s))) advance(s); 85517 0 85517
while (!at_end(s) && is_name_char(peek(s))) advance(s); 59008 26509 85517
466 26509 nlen = (size_t)(s->p - nstart);
467  
468 26509 tag = pdfmake_markup_tag_id(nstart, nlen);
469 26509 ▶ if (tag == PDFMAKE_MK_INVALID) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == PDFMAKE_MK_INVALID) { 194 26315 26509
470 194 fail(s, line, col, "unknown tag '<%.*s>'", (int)nlen, nstart);
471 194 return;
472 }
473  
474 26315 n = node_new(s, PDFMAKE_MARKUP_ELEM, line, col);
475 26315 ▶ if (!n) { fail(s, line, col, "out of memory"); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { fail(s, line, col, "out of memory"); return; } 0 26315 26315
476 26315 n->tag = tag;
477  
478 /* attributes */
479 for (;;) {
480 28175 skip_space(s);
481 28175 ▶ if (at_end(s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (at_end(s)) { 0 28175 28175
482 0 fail(s, line, col, "unterminated <%.*s>", (int)nlen, nstart);
483 0 return;
484 }
485 28175 ▶ if (peek(s) == '>') { advance(s); break; }
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '>') { advance(s); break; } 5642 22533 28175
486 22533 ▶ if (peek(s) == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '/') { 20332 2201 22533
487 20332 advance(s);
488 20332 ▶ if (peek(s) != '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) != '>') { 27 20305 20332
489 27 fail(s, s->line, s->col, "expected '>' after '/'");
490 27 return;
491 }
492 20305 advance(s);
493 20305 self_closing = 1;
494 20305 break;
495 }
496 {
497 2201 pdfmake_markup_attr_t *a = parse_attr(s);
498 pdfmake_markup_attr_t *prev;
499 2201 ▶ if (!a) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!a) return; 340 1861 2201
500 /* A repeated attribute is a typo with a silent winner. Refuse it
501 * rather than pick one. */
502 126629 ▶ for (prev = n->attrs; prev; prev = prev->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (prev = n->attrs; prev; prev = prev->next) { 124769 1860 126629
503 124769 ▶ if (prev->name_len == a->name_len &&
Branch: 1 branch
BranchTrueFalseTotal
if (prev->name_len == a->name_len && 84243 40526 124769
504 84243 ▶ memcmp(prev->name, a->name, a->name_len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(prev->name, a->name, a->name_len) == 0) { 1 84242 84243
505 1 fail(s, a->line, a->col,
506 "duplicate attribute '%.*s' on <%.*s> (first at "
507 "line %lu)",
508 1 (int)a->name_len, a->name, (int)nlen, nstart,
509 1 (unsigned long)prev->line);
510 1 return;
511 }
512 }
513 1860 ▶ if (last) last->next = a; else n->attrs = a;
Branch: 1 branch
BranchTrueFalseTotal
if (last) last->next = a; else n->attrs = a; 514 1346 1860
514 1860 last = a;
515 }
516 }
517  
518 25947 node_append(parent, n);
519  
520 25947 ▶ if (pdfmake_markup_tag_flags(tag) & PDFMAKE_MKF_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_markup_tag_flags(tag) & PDFMAKE_MKF_VOID) { 20291 5656 25947
521 /* A void element may be written <hr/> or <hr>; either way it takes
522 * no children and no close tag. */
523 20291 ▶ if (!self_closing) {
Branch: 1 branch
BranchTrueFalseTotal
if (!self_closing) { 7 20284 20291
524 /* <hr></hr> is tolerated. Anything else following - including a
525 * close tag belonging to an ancestor - is not ours to consume, so
526 * the scan position is restored and the parent decides. */
527 7 const char *save_p = s->p;
528 7 uint32_t save_line = s->line, save_col = s->col;
529 7 skip_space(s);
530 7 ▶ if (peek(s) == '<' && peek_at(s, 1) == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (peek(s) == '<' && peek_at(s, 1) == '/') { 3 4 7
if (peek(s) == '<' && peek_at(s, 1) == '/') { 3 0 3
531 const char *cstart;
532 size_t clen;
533 3 advance(s); advance(s);
534 3 cstart = s->p;
535 12 ▶ while (!at_end(s) && is_name_char(peek(s))) advance(s);
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && is_name_char(peek(s))) advance(s); 12 0 12
while (!at_end(s) && is_name_char(peek(s))) advance(s); 9 3 12
536 3 clen = (size_t)(s->p - cstart);
537 3 ▶ if (clen == nlen && memcmp(cstart, nstart, nlen) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (clen == nlen && memcmp(cstart, nstart, nlen) == 0) { 1 2 3
if (clen == nlen && memcmp(cstart, nstart, nlen) == 0) { 1 0 1
538 1 skip_space(s);
539 1 ▶ if (peek(s) == '>') { advance(s); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '>') { advance(s); return; } 1 0 1
540 }
541 }
542 6 s->p = save_p; s->line = save_line; s->col = save_col;
543 }
544 20290 return;
545 }
546  
547 5656 ▶ if (self_closing) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self_closing) return; 21 5635 5656
548  
549 /* children */
550 5635 ▶ if (++s->depth > PDFMAKE_MARKUP_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (++s->depth > PDFMAKE_MARKUP_MAX_DEPTH) { 1 5634 5635
551 1 fail(s, line, col, "markup nested deeper than %d levels",
552 PDFMAKE_MARKUP_MAX_DEPTH);
553 1 return;
554 }
555  
556 for (;;) {
557 31993 ▶ if (at_end(s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (at_end(s)) { 28 31965 31993
558 28 ▶ if (s->doc->ok)
Branch: 1 branch
BranchTrueFalseTotal
if (s->doc->ok) 28 0 28
559 28 fail(s, line, col, "unclosed <%.*s> opened at line %lu",
560 (int)nlen, nstart, (unsigned long)line);
561 28 return;
562 }
563 31965 ▶ if (peek(s) == '<' && peek_at(s, 1) == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (peek(s) == '<' && peek_at(s, 1) == '/') { 28245 3720 31965
if (peek(s) == '<' && peek_at(s, 1) == '/') { 3470 24775 28245
564 3470 uint32_t cline = s->line, ccol = s->col;
565 const char *cstart;
566 size_t clen;
567 3470 advance(s); advance(s);
568 3470 cstart = s->p;
569 12275 ▶ while (!at_end(s) && is_name_char(peek(s))) advance(s);
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(s) && is_name_char(peek(s))) advance(s); 12272 3 12275
while (!at_end(s) && is_name_char(peek(s))) advance(s); 8805 3467 12272
570 3470 clen = (size_t)(s->p - cstart);
571 3470 skip_space(s);
572 3470 ▶ if (peek(s) != '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) != '>') { 357 3113 3470
573 357 fail(s, cline, ccol, "expected '>' to close </%.*s",
574 (int)clen, cstart);
575 357 return;
576 }
577 3113 advance(s);
578 3113 ▶ if (clen != nlen || memcmp(cstart, nstart, nlen) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (clen != nlen || memcmp(cstart, nstart, nlen) != 0) { 2938 175 3113
if (clen != nlen || memcmp(cstart, nstart, nlen) != 0) { 36 2902 2938
579 /* A close tag naming a void element is a specific mistake
580 * with a specific fix, and deserves to be told apart from an
581 * ordinary mismatch. */
582 211 pdfmake_markup_tag_t ctag = pdfmake_markup_tag_id(cstart, clen);
583 211 ▶ if (ctag != PDFMAKE_MK_INVALID &&
Branch: 1 branch
BranchTrueFalseTotal
if (ctag != PDFMAKE_MK_INVALID && 140 71 211
584 140 ▶ (pdfmake_markup_tag_flags(ctag) & PDFMAKE_MKF_VOID)) {
Branch: 1 branch
BranchTrueFalseTotal
(pdfmake_markup_tag_flags(ctag) & PDFMAKE_MKF_VOID)) { 1 139 140
585 1 fail(s, cline, ccol,
586 "<%.*s> takes no children: it is written <%.*s/> and "
587 "has no closing tag",
588 (int)clen, cstart, (int)clen, cstart);
589 1 return;
590 }
591 210 fail(s, cline, ccol,
592 "</%.*s> closes <%.*s> opened at line %lu",
593 (int)clen, cstart, (int)nlen, nstart, (unsigned long)line);
594 210 return;
595 }
596 2902 s->depth--;
597 2902 return;
598 }
599 28495 parse_node(s, n);
600 28495 ▶ if (!s->doc->ok) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s->doc->ok) return; 2136 26359 28495
601 }
602 }
603  
604 28495 static void parse_node(scan_t *s, pdfmake_markup_node_t *parent) {
605 28495 ▶ if (peek(s) == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(s) == '<') { 24775 3720 28495
606 24775 ▶ if (skip_comment(s)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (skip_comment(s)) return; 2 24773 24775
607 24773 ▶ if (peek_at(s, 1) == '!' || peek_at(s, 1) == '?') {
Branch: 2 branches
BranchTrueFalseTotal
if (peek_at(s, 1) == '!' || peek_at(s, 1) == '?') { 24772 1 24773
if (peek_at(s, 1) == '!' || peek_at(s, 1) == '?') { 0 24772 24772
608 1 fail(s, s->line, s->col,
609 "processing instructions and declarations are not part of "
610 "this markup");
611 1 return;
612 }
613 24772 parse_element(s, parent);
614 } else {
615 3720 pdfmake_markup_node_t *t = parse_text(s, parent);
616 3720 ▶ if (t) node_append(parent, t);
Branch: 1 branch
BranchTrueFalseTotal
if (t) node_append(parent, t); 2311 1409 3720
617 }
618 }
619  
620 /*----------------------------------------------------------------------------
621 * Entry point
622 *--------------------------------------------------------------------------*/
623  
624 /*
625 * Validate UTF-8 before parsing, reporting the position of the first bad
626 * byte. The alternative is copying invalid bytes into text nodes and letting
627 * them surface as mojibake in somebody's invoice, which is precisely the
628 * class of "renders slightly wrong" failure this parser exists to prevent.
629 *
630 * Overlong forms, surrogates and anything above U+10FFFF are rejected along
631 * with truncated sequences: they are all ways of smuggling a character past
632 * a checker that only looks at the shortest form.
633 */
634 2416 static int validate_utf8(scan_t *s, const char *p, const char *end) {
635 2416 uint32_t line = 1, col = 1;
636  
637 495414 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 493193 2221 495414
638 493193 unsigned char c = (unsigned char)*p;
639 int extra;
640 uint32_t cp;
641  
642 493193 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 492988 205 493193
643 492988 ▶ if (c == '\n') { line++; col = 1; } else { col++; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') { line++; col = 1; } else { col++; } 2382 490606 492988
644 492988 p++;
645 492988 continue;
646 }
647  
648 205 ▶ if ((c & 0xE0) == 0xC0) { extra = 1; cp = c & 0x1Fu; }
Branch: 1 branch
BranchTrueFalseTotal
if ((c & 0xE0) == 0xC0) { extra = 1; cp = c & 0x1Fu; } 5 200 205
649 200 ▶ else if ((c & 0xF0) == 0xE0) { extra = 2; cp = c & 0x0Fu; }
Branch: 1 branch
BranchTrueFalseTotal
else if ((c & 0xF0) == 0xE0) { extra = 2; cp = c & 0x0Fu; } 10 190 200
650 190 ▶ else if ((c & 0xF8) == 0xF0) { extra = 3; cp = c & 0x07u; }
Branch: 1 branch
BranchTrueFalseTotal
else if ((c & 0xF8) == 0xF0) { extra = 3; cp = c & 0x07u; } 0 190 190
651 else {
652 190 fail(s, line, col, "invalid UTF-8: unexpected byte 0x%02X", c);
653 190 return 0;
654 }
655  
656 15 ▶ if (p + extra >= end) {
Branch: 1 branch
BranchTrueFalseTotal
if (p + extra >= end) { 1 14 15
657 1 fail(s, line, col, "invalid UTF-8: truncated sequence");
658 1 return 0;
659 }
660  
661 {
662 int i;
663 35 ▶ for (i = 1; i <= extra; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= extra; i++) { 23 12 35
664 23 unsigned char cc = (unsigned char)p[i];
665 23 ▶ if ((cc & 0xC0) != 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ((cc & 0xC0) != 0x80) { 2 21 23
666 2 fail(s, line, col,
667 "invalid UTF-8: byte 0x%02X is not a continuation", cc);
668 2 return 0;
669 }
670 21 cp = (cp << 6) | (cc & 0x3Fu);
671 }
672 }
673  
674 12 ▶ if ((extra == 1 && cp < 0x80) ||
Branch: 3 branches
BranchTrueFalseTotal
if ((extra == 1 && cp < 0x80) || 3 9 12
if ((extra == 1 && cp < 0x80) || 2 1 3
if ((extra == 1 && cp < 0x80) || 9 2 11
675 11 ▶ (extra == 2 && cp < 0x800) ||
Branch: 2 branches
BranchTrueFalseTotal
(extra == 2 && cp < 0x800) || 9 0 9
(extra == 2 && cp < 0x800) || 0 11 11
676 0 ▶ (extra == 3 && cp < 0x10000)) {
Branch: 1 branch
BranchTrueFalseTotal
(extra == 3 && cp < 0x10000)) { 0 0 0
677 1 fail(s, line, col, "invalid UTF-8: overlong encoding");
678 1 return 0;
679 }
680 11 ▶ if (cp >= 0xD800 && cp <= 0xDFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (cp >= 0xD800 && cp <= 0xDFFF) { 3 8 11
if (cp >= 0xD800 && cp <= 0xDFFF) { 1 2 3
681 1 fail(s, line, col, "invalid UTF-8: encoded surrogate U+%04lX",
682 (unsigned long)cp);
683 1 return 0;
684 }
685 10 ▶ if (cp > 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp > 0x10FFFF) { 0 10 10
686 0 fail(s, line, col, "invalid UTF-8: code point above U+10FFFF");
687 0 return 0;
688 }
689  
690 10 p += extra + 1;
691 10 col++;
692 }
693 2221 return 1;
694 }
695  
696 2416 pdfmake_markup_doc_t *pdfmake_markup_parse(const char *src, size_t len) {
697 pdfmake_markup_doc_t *doc;
698 scan_t s;
699  
700 2416 doc = (pdfmake_markup_doc_t *)calloc(1, sizeof(*doc));
701 2416 ▶ if (!doc) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return NULL; 0 2416 2416
702 2416 doc->arena = pdfmake_arena_new();
703 2416 ▶ if (!doc->arena) { free(doc); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->arena) { free(doc); return NULL; } 0 2416 2416
704 2416 doc->ok = 1;
705  
706 2416 ▶ if (!src) { src = ""; len = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!src) { src = ""; len = 0; } 0 2416 2416
707  
708 2416 s.p = src;
709 2416 s.end = src + len;
710 2416 s.line = 1;
711 2416 s.col = 1;
712 2416 s.doc = doc;
713 2416 s.depth = 0;
714  
715 2416 ▶ if (!validate_utf8(&s, s.p, s.end)) return doc;
Branch: 1 branch
BranchTrueFalseTotal
if (!validate_utf8(&s, s.p, s.end)) return doc; 195 2221 2416
716  
717 /* Skip a UTF-8 BOM: editors add it, and it is not the author's fault. */
718 2221 ▶ if (len >= 3 && (unsigned char)src[0] == 0xEF &&
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 3 && (unsigned char)src[0] == 0xEF && 2031 190 2221
if (len >= 3 && (unsigned char)src[0] == 0xEF && 2 2029 2031
719 2 ▶ (unsigned char)src[1] == 0xBB && (unsigned char)src[2] == 0xBF) {
Branch: 2 branches
BranchTrueFalseTotal
(unsigned char)src[1] == 0xBB && (unsigned char)src[2] == 0xBF) { 2 0 2
(unsigned char)src[1] == 0xBB && (unsigned char)src[2] == 0xBF) { 2 0 2
720 2 s.p += 3;
721 }
722  
723 2221 skip_space(&s);
724 6256 ▶ while (!at_end(&s) && s.doc->ok && peek(&s) == '<' &&
Branch: 1 branch
BranchTrueFalseTotal
while (!at_end(&s) && s.doc->ok && peek(&s) == '<' && 2031 0 2031
725 4226 ▶ peek_at(&s, 1) == '!' && peek_at(&s, 2) == '-') {
Branch: 2 branches
BranchTrueFalseTotal
peek_at(&s, 1) == '!' && peek_at(&s, 2) == '-') { 2031 191 2222
peek_at(&s, 1) == '!' && peek_at(&s, 2) == '-') { 1 0 1
726 1 skip_comment(&s);
727 1 skip_space(&s);
728 }
729  
730 2221 ▶ if (at_end(&s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (at_end(&s)) { 191 2030 2221
731 191 ▶ if (doc->ok)
Branch: 1 branch
BranchTrueFalseTotal
if (doc->ok) 191 0 191
732 191 fail(&s, 1, 1, "empty document: expected <doc>");
733 191 return doc;
734 }
735  
736 2030 ▶ if (peek(&s) != '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (peek(&s) != '<') { 28 2002 2030
737 28 fail(&s, s.line, s.col, "text outside <doc>");
738 28 return doc;
739 }
740  
741 {
742 /* The root must be <doc>. Checking here rather than in parse_element
743 * keeps the error specific: "expected <doc>" beats "unknown tag". */
744 2002 const char *q = s.p + 1;
745 2002 size_t qlen = 0;
746 7837 ▶ while (q + qlen < s.end && is_name_char(q[qlen])) qlen++;
Branch: 2 branches
BranchTrueFalseTotal
while (q + qlen < s.end && is_name_char(q[qlen])) qlen++; 7837 0 7837
while (q + qlen < s.end && is_name_char(q[qlen])) qlen++; 5835 2002 7837
747 2002 ▶ if (qlen != 3 || memcmp(q, "doc", 3) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (qlen != 3 || memcmp(q, "doc", 3) != 0) { 1909 93 2002
if (qlen != 3 || memcmp(q, "doc", 3) != 0) { 0 1909 1909
748 93 ▶ fail(&s, s.line, s.col,
Branch: 1 branch
BranchTrueFalseTotal
fail(&s, s.line, s.col, 93 0 93
749 "the root element must be <doc>, found '<%.*s>'",
750 (int)(qlen > 32 ? 32 : qlen), q);
751 93 return doc;
752 }
753 }
754  
755 {
756 pdfmake_markup_node_t *holder =
757 1909 node_new(&s, PDFMAKE_MARKUP_ELEM, 1, 1);
758 1909 ▶ if (!holder) { fail(&s, 1, 1, "out of memory"); return doc; }
Branch: 1 branch
BranchTrueFalseTotal
if (!holder) { fail(&s, 1, 1, "out of memory"); return doc; } 0 1909 1909
759 1909 holder->tag = PDFMAKE_MK_INVALID;
760 1909 parse_element(&s, holder);
761 1909 ▶ if (doc->ok) doc->root = holder->first_child;
Branch: 1 branch
BranchTrueFalseTotal
if (doc->ok) doc->root = holder->first_child; 484 1425 1909
762 }
763  
764 1909 ▶ if (doc->ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (doc->ok) { 484 1425 1909
765 484 skip_space(&s);
766 486 ▶ while (!at_end(&s) && peek(&s) == '<' && peek_at(&s, 1) == '!' &&
Branch: 2 branches
BranchTrueFalseTotal
while (!at_end(&s) && peek(&s) == '<' && peek_at(&s, 1) == '!' && 3 482 485
while (!at_end(&s) && peek(&s) == '<' && peek_at(&s, 1) == '!' && 2 1 3
767 1 peek_at(&s, 2) == '-') {
768 1 skip_comment(&s);
769 1 skip_space(&s);
770 }
771 484 ▶ if (!at_end(&s))
Branch: 1 branch
BranchTrueFalseTotal
if (!at_end(&s)) 2 482 484
772 2 fail(&s, s.line, s.col, "content after </doc>");
773 }
774  
775 1909 return doc;
776 }
777  
778 2416 void pdfmake_markup_free(pdfmake_markup_doc_t *doc) {
779 2416 ▶ if (!doc) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) return; 0 2416 2416
780 2416 pdfmake_arena_free(doc->arena);
781 2416 free(doc);
782 }
« src/pdfmake_tokenizer.c ↑ Summary src/pdfmake_crypt.c »