src/pdfmake_tokenizer.c

filter: Statementstmt 93.5% 245 / 262 Branchbran 79.5% 159 / 200 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.4% 404 / 462 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_font_std14.c ↑ Summary src/pdfmake_markup.c »
1 /*
2 * pdfmake_tokenizer.c — PDF lexical tokenizer per §7.2
3 */
4  
5 #include "pdfmake_tokenizer.h"
6 #include <string.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9  
10 /*============================================================================
11 * Character classification table (§7.2.2)
12 *
13 * PDF whitespace: NUL(0), TAB(9), LF(10), FF(12), CR(13), SPACE(32)
14 * PDF delimiters: ( ) < > [ ] { } / %
15 *==========================================================================*/
16  
17 const uint8_t pdfmake_char_class[256] = {
18 /* 0x00-0x0F */
19 PDFMAKE_CC_WHITESPACE, /* NUL */
20 PDFMAKE_CC_REGULAR, /* SOH */
21 PDFMAKE_CC_REGULAR, /* STX */
22 PDFMAKE_CC_REGULAR, /* ETX */
23 PDFMAKE_CC_REGULAR, /* EOT */
24 PDFMAKE_CC_REGULAR, /* ENQ */
25 PDFMAKE_CC_REGULAR, /* ACK */
26 PDFMAKE_CC_REGULAR, /* BEL */
27 PDFMAKE_CC_REGULAR, /* BS */
28 PDFMAKE_CC_WHITESPACE, /* TAB */
29 PDFMAKE_CC_WHITESPACE, /* LF */
30 PDFMAKE_CC_REGULAR, /* VT */
31 PDFMAKE_CC_WHITESPACE, /* FF */
32 PDFMAKE_CC_WHITESPACE, /* CR */
33 PDFMAKE_CC_REGULAR, /* SO */
34 PDFMAKE_CC_REGULAR, /* SI */
35  
36 /* 0x10-0x1F */
37 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
38 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
39 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
40 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
41  
42 /* 0x20-0x2F: SPACE ! " # $ % & ' ( ) * + , - . / */
43 PDFMAKE_CC_WHITESPACE, /* SPACE */
44 PDFMAKE_CC_REGULAR, /* ! */
45 PDFMAKE_CC_REGULAR, /* " */
46 PDFMAKE_CC_REGULAR, /* # */
47 PDFMAKE_CC_REGULAR, /* $ */
48 PDFMAKE_CC_DELIMITER, /* % comment start */
49 PDFMAKE_CC_REGULAR, /* & */
50 PDFMAKE_CC_REGULAR, /* ' */
51 PDFMAKE_CC_DELIMITER, /* ( */
52 PDFMAKE_CC_DELIMITER, /* ) */
53 PDFMAKE_CC_REGULAR, /* * */
54 PDFMAKE_CC_SIGN, /* + */
55 PDFMAKE_CC_REGULAR, /* , */
56 PDFMAKE_CC_SIGN, /* - */
57 PDFMAKE_CC_REGULAR, /* . */
58 PDFMAKE_CC_DELIMITER, /* / name start */
59  
60 /* 0x30-0x3F: 0-9 : ; < = > ? */
61 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 0 */
62 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 1 */
63 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 2 */
64 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 3 */
65 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 4 */
66 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 5 */
67 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 6 */
68 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 7 */
69 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 8 */
70 PDFMAKE_CC_DIGIT | PDFMAKE_CC_HEX, /* 9 */
71 PDFMAKE_CC_REGULAR, /* : */
72 PDFMAKE_CC_REGULAR, /* ; */
73 PDFMAKE_CC_DELIMITER, /* < */
74 PDFMAKE_CC_REGULAR, /* = */
75 PDFMAKE_CC_DELIMITER, /* > */
76 PDFMAKE_CC_REGULAR, /* ? */
77  
78 /* 0x40-0x4F: @ A-O */
79 PDFMAKE_CC_REGULAR, /* @ */
80 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* A */
81 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* B */
82 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* C */
83 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* D */
84 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* E */
85 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* F */
86 PDFMAKE_CC_ALPHA, /* G */
87 PDFMAKE_CC_ALPHA, /* H */
88 PDFMAKE_CC_ALPHA, /* I */
89 PDFMAKE_CC_ALPHA, /* J */
90 PDFMAKE_CC_ALPHA, /* K */
91 PDFMAKE_CC_ALPHA, /* L */
92 PDFMAKE_CC_ALPHA, /* M */
93 PDFMAKE_CC_ALPHA, /* N */
94 PDFMAKE_CC_ALPHA, /* O */
95  
96 /* 0x50-0x5F: P-Z [ \ ] ^ _ */
97 PDFMAKE_CC_ALPHA, /* P */
98 PDFMAKE_CC_ALPHA, /* Q */
99 PDFMAKE_CC_ALPHA, /* R */
100 PDFMAKE_CC_ALPHA, /* S */
101 PDFMAKE_CC_ALPHA, /* T */
102 PDFMAKE_CC_ALPHA, /* U */
103 PDFMAKE_CC_ALPHA, /* V */
104 PDFMAKE_CC_ALPHA, /* W */
105 PDFMAKE_CC_ALPHA, /* X */
106 PDFMAKE_CC_ALPHA, /* Y */
107 PDFMAKE_CC_ALPHA, /* Z */
108 PDFMAKE_CC_DELIMITER, /* [ */
109 PDFMAKE_CC_REGULAR, /* \ */
110 PDFMAKE_CC_DELIMITER, /* ] */
111 PDFMAKE_CC_REGULAR, /* ^ */
112 PDFMAKE_CC_REGULAR, /* _ */
113  
114 /* 0x60-0x6F: ` a-o */
115 PDFMAKE_CC_REGULAR, /* ` */
116 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* a */
117 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* b */
118 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* c */
119 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* d */
120 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* e */
121 PDFMAKE_CC_ALPHA | PDFMAKE_CC_HEX, /* f */
122 PDFMAKE_CC_ALPHA, /* g */
123 PDFMAKE_CC_ALPHA, /* h */
124 PDFMAKE_CC_ALPHA, /* i */
125 PDFMAKE_CC_ALPHA, /* j */
126 PDFMAKE_CC_ALPHA, /* k */
127 PDFMAKE_CC_ALPHA, /* l */
128 PDFMAKE_CC_ALPHA, /* m */
129 PDFMAKE_CC_ALPHA, /* n */
130 PDFMAKE_CC_ALPHA, /* o */
131  
132 /* 0x70-0x7F: p-z { | } ~ DEL */
133 PDFMAKE_CC_ALPHA, /* p */
134 PDFMAKE_CC_ALPHA, /* q */
135 PDFMAKE_CC_ALPHA, /* r */
136 PDFMAKE_CC_ALPHA, /* s */
137 PDFMAKE_CC_ALPHA, /* t */
138 PDFMAKE_CC_ALPHA, /* u */
139 PDFMAKE_CC_ALPHA, /* v */
140 PDFMAKE_CC_ALPHA, /* w */
141 PDFMAKE_CC_ALPHA, /* x */
142 PDFMAKE_CC_ALPHA, /* y */
143 PDFMAKE_CC_ALPHA, /* z */
144 PDFMAKE_CC_DELIMITER, /* { */
145 PDFMAKE_CC_REGULAR, /* | */
146 PDFMAKE_CC_DELIMITER, /* } */
147 PDFMAKE_CC_REGULAR, /* ~ */
148 PDFMAKE_CC_REGULAR, /* DEL */
149  
150 /* 0x80-0xFF: High bytes - all regular in PDF */
151 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
152 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
153 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
154 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
155 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
156 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
157 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
158 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
159 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
160 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
161 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
162 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
163 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
164 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
165 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
166 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
167 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
168 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
169 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
170 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
171 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
172 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
173 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
174 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
175 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
176 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
177 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
178 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
179 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
180 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
181 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
182 PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR, PDFMAKE_CC_REGULAR,
183 };
184  
185 /*============================================================================
186 * Token kind names (for debugging)
187 *==========================================================================*/
188  
189 static const char *tok_kind_names[] = {
190 "EOF", "WS", "COMMENT",
191 "INT", "REAL", "NAME", "LSTR", "HSTR",
192 "ARR_OPEN", "ARR_CLOSE", "DICT_OPEN", "DICT_CLOSE",
193 "KW_NULL", "KW_TRUE", "KW_FALSE",
194 "KW_OBJ", "KW_ENDOBJ", "KW_STREAM", "KW_ENDSTREAM",
195 "KW_R", "KW_XREF", "KW_TRAILER", "KW_STARTXREF",
196 "ERROR"
197 };
198  
199 0 const char *pdfmake_tok_kind_name(pdfmake_tok_kind_t kind) {
200 0 ▶ if (kind >= 0 && kind <= PDFMAKE_TOK_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (kind >= 0 && kind <= PDFMAKE_TOK_ERROR) { 0 0 0
201 0 return tok_kind_names[kind];
202 }
203 0 return "UNKNOWN";
204 }
205  
206 /*============================================================================
207 * Tokenizer initialization
208 *==========================================================================*/
209  
210 2126 void pdfmake_tokenizer_init(pdfmake_tokenizer_t *t,
211 const uint8_t *buf, size_t len) {
212 2126 t->buf = buf;
213 2126 t->pos = 0;
214 2126 t->len = len;
215 2126 t->has_peeked = 0;
216 2126 }
217  
218 0 void pdfmake_tokenizer_seek(pdfmake_tokenizer_t *t, size_t pos) {
219 0 t->pos = pos < t->len ? pos : t->len;
220 0 t->has_peeked = 0; /* Invalidate peek cache */
221 0 }
222  
223 /*============================================================================
224 * Whitespace lexer (§7.2.2)
225 *==========================================================================*/
226  
227 71846 static pdfmake_tok_t lex_whitespace(pdfmake_tokenizer_t *t) {
228 pdfmake_tok_t tok;
229 71846 tok.kind = PDFMAKE_TOK_WS;
230 71846 tok.offset = t->pos;
231  
232 155786 ▶ while (t->pos < t->len && pdfmake_is_whitespace(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_whitespace(t->buf[t->pos])) { 155696 90 155786
while (t->pos < t->len && pdfmake_is_whitespace(t->buf[t->pos])) { 83940 71756 155696
233 83940 t->pos++;
234 }
235  
236 71846 tok.length = t->pos - tok.offset;
237 71846 return tok;
238 }
239  
240 /*============================================================================
241 * Comment lexer (§7.2.3)
242 * % through end of line (LF or CR or CRLF)
243 *==========================================================================*/
244  
245 299 static pdfmake_tok_t lex_comment(pdfmake_tokenizer_t *t) {
246 pdfmake_tok_t tok;
247 299 tok.kind = PDFMAKE_TOK_COMMENT;
248 299 tok.offset = t->pos;
249  
250 299 t->pos++; /* Skip % */
251  
252 10476 ▶ while (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->pos < t->len) { 10466 10 10476
253 10466 uint8_t c = t->buf[t->pos];
254 10466 ▶ if (c == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') { 113 10353 10466
255 113 t->pos++;
256 113 break;
257 }
258 10353 ▶ if (c == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\r') { 176 10177 10353
259 176 t->pos++;
260 176 ▶ if (t->pos < t->len && t->buf[t->pos] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] == '\n') { 176 0 176
if (t->pos < t->len && t->buf[t->pos] == '\n') { 32 144 176
261 32 t->pos++;
262 }
263 176 break;
264 }
265 10177 t->pos++;
266 }
267  
268 299 tok.length = t->pos - tok.offset;
269 299 return tok;
270 }
271  
272 /*============================================================================
273 * Number lexer (§7.3.3)
274 * Integers: [+-]?\d+
275 * Reals: [+-]?\d*\.\d* (at least one digit somewhere)
276 * No scientific notation in PDF.
277 *==========================================================================*/
278  
279 42305 static pdfmake_tok_t lex_number(pdfmake_tokenizer_t *t) {
280 pdfmake_tok_t tok;
281 42305 int is_real = 0;
282 42305 int has_digits = 0;
283 42305 int is_negative = 0;
284 42305 double val = 0.0;
285 42305 double frac = 0.0;
286 42305 double frac_div = 1.0;
287 42305 int in_frac = 0;
288 42305 int64_t ival = 0;
289 size_t i;
290  
291 42305 tok.offset = t->pos;
292  
293 /* Optional sign */
294 42305 ▶ if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) {
Branch: 3 branches
BranchTrueFalseTotal
if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) { 42305 0 42305
if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) { 42305 0 42305
if (t->pos < t->len && (t->buf[t->pos] == '+' || t->buf[t->pos] == '-')) { 2422 39883 42305
295 2422 ▶ if (t->buf[t->pos] == '-') is_negative = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (t->buf[t->pos] == '-') is_negative = 1; 2422 0 2422
296 2422 t->pos++;
297 }
298  
299 /* Digits before decimal point */
300 172738 ▶ while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 172738 0 172738
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 130433 42305 172738
301 130433 has_digits = 1;
302 130433 t->pos++;
303 }
304  
305 /* Decimal point */
306 42305 ▶ if (t->pos < t->len && t->buf[t->pos] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] == '.') { 42305 0 42305
if (t->pos < t->len && t->buf[t->pos] == '.') { 4860 37445 42305
307 4860 is_real = 1;
308 4860 t->pos++;
309  
310 /* Digits after decimal point */
311 15849 ▶ while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 15849 0 15849
while (t->pos < t->len && pdfmake_is_digit(t->buf[t->pos])) { 10989 4860 15849
312 10989 has_digits = 1;
313 10989 t->pos++;
314 }
315 }
316  
317 42305 tok.length = t->pos - tok.offset;
318  
319 42305 ▶ if (!has_digits) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_digits) { 0 42305 42305
320 /* Just a sign or just a dot — error */
321 0 tok.kind = PDFMAKE_TOK_ERROR;
322 0 return tok;
323 }
324  
325 /* Parse the number */
326 42305 ▶ if (is_real) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_real) { 4860 37445 42305
327 4860 tok.kind = PDFMAKE_TOK_REAL;
328 /* Parse directly — no need for strtod locale issues */
329 30933 ▶ for (i = tok.offset; i < tok.offset + tok.length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = tok.offset; i < tok.offset + tok.length; i++) { 26073 4860 30933
330 26073 uint8_t c = t->buf[i];
331 26073 ▶ if (c == '+' || c == '-') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '+' || c == '-') continue; 26073 0 26073
if (c == '+' || c == '-') continue; 2118 23955 26073
332 23955 ▶ if (c == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '.') { 4860 19095 23955
333 4860 in_frac = 1;
334 4860 continue;
335 }
336 19095 ▶ if (in_frac) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_frac) { 10989 8106 19095
337 10989 frac_div *= 10.0;
338 10989 frac += (c - '0') / frac_div;
339 } else {
340 8106 val = val * 10.0 + (c - '0');
341 }
342 }
343 4860 ▶ tok.payload.real_val = is_negative ? -(val + frac) : (val + frac);
Branch: 1 branch
BranchTrueFalseTotal
tok.payload.real_val = is_negative ? -(val + frac) : (val + frac); 2118 2742 4860
344 } else {
345 37445 tok.kind = PDFMAKE_TOK_INT;
346 160076 ▶ for (i = tok.offset; i < tok.offset + tok.length; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = tok.offset; i < tok.offset + tok.length; i++) { 122631 37445 160076
347 122631 uint8_t c = t->buf[i];
348 122631 ▶ if (c == '+' || c == '-') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '+' || c == '-') continue; 122631 0 122631
if (c == '+' || c == '-') continue; 304 122327 122631
349 122327 ival = ival * 10 + (c - '0');
350 }
351 37445 ▶ tok.payload.int_val = is_negative ? -ival : ival;
Branch: 1 branch
BranchTrueFalseTotal
tok.payload.int_val = is_negative ? -ival : ival; 304 37141 37445
352 }
353  
354 42305 return tok;
355 }
356  
357 /*============================================================================
358 * Name lexer (§7.3.5)
359 * /Name with #XX hex escape
360 *==========================================================================*/
361  
362 12275 static pdfmake_tok_t lex_name(pdfmake_tokenizer_t *t) {
363 pdfmake_tok_t tok;
364 12275 tok.kind = PDFMAKE_TOK_NAME;
365 12275 tok.offset = t->pos;
366  
367 12275 t->pos++; /* Skip / */
368  
369 /* Name continues until whitespace or delimiter */
370 83319 ▶ while (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->pos < t->len) { 83319 0 83319
371 83319 uint8_t c = t->buf[t->pos];
372 83319 ▶ if (pdfmake_is_whitespace(c) || pdfmake_is_delimiter(c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (pdfmake_is_whitespace(c) || pdfmake_is_delimiter(c)) { 72310 11009 83319
if (pdfmake_is_whitespace(c) || pdfmake_is_delimiter(c)) { 71044 1266 72310
373 break;
374 }
375 71044 ▶ if (c == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '#') { 33 71011 71044
376 /* Hex escape: skip #XX */
377 66 if (t->pos + 2 < t->len &&
378 34 ▶ pdfmake_is_hex(t->buf[t->pos + 1]) &&
Branch: 1 branch
BranchTrueFalseTotal
pdfmake_is_hex(t->buf[t->pos + 1]) && 1 0 1
379 1 pdfmake_is_hex(t->buf[t->pos + 2])) {
380 1 t->pos += 3;
381 } else {
382 /* Invalid escape — treat # as regular char */
383 32 t->pos++;
384 }
385 } else {
386 71011 t->pos++;
387 }
388 }
389  
390 12275 tok.length = t->pos - tok.offset;
391 12275 return tok;
392 }
393  
394 /*============================================================================
395 * Literal string lexer (§7.3.4.2)
396 * (string) with balanced parens and escapes
397 *==========================================================================*/
398  
399 3884 static pdfmake_tok_t lex_literal_string(pdfmake_tokenizer_t *t) {
400 pdfmake_tok_t tok;
401 int depth;
402 3884 tok.kind = PDFMAKE_TOK_LSTR;
403 3884 tok.offset = t->pos;
404  
405 3884 t->pos++; /* Skip opening ( */
406 3884 depth = 1;
407  
408 2738358 ▶ while (t->pos < t->len && depth > 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && depth > 0) { 2738326 32 2738358
while (t->pos < t->len && depth > 0) { 2734474 3852 2738326
409 2734474 uint8_t c = t->buf[t->pos];
410  
411 2734474 ▶ if (c == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') { 10176 2724298 2734474
412 10176 depth++;
413 10176 t->pos++;
414 2724298 ▶ } else if (c == ')') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == ')') { 13484 2710814 2724298
415 13484 depth--;
416 13484 t->pos++;
417 2710814 ▶ } else if (c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '\\') { 10005 2700809 2710814
418 /* Escape sequence */
419 10005 t->pos++;
420 10005 ▶ if (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->pos < t->len) { 10005 0 10005
421 10005 uint8_t esc = t->buf[t->pos];
422 10005 ▶ if (esc >= '0' && esc <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (esc >= '0' && esc <= '7') { 8193 1812 10005
if (esc >= '0' && esc <= '7') { 128 8065 8193
423 /* Octal: 1-3 digits */
424 128 t->pos++;
425 128 ▶ if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') {
Branch: 3 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 128 0 128
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 128 0 128
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 0 128 128
426 0 t->pos++;
427 0 ▶ if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') {
Branch: 3 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 0 0 0
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 0 0 0
if (t->pos < t->len && t->buf[t->pos] >= '0' && t->buf[t->pos] <= '7') { 0 0 0
428 0 t->pos++;
429 }
430 }
431 9877 ▶ } else if (esc == '\r') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (esc == '\r') { 64 9813 9877
432 /* Line continuation: \CR or \CRLF */
433 64 t->pos++;
434 64 ▶ if (t->pos < t->len && t->buf[t->pos] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos < t->len && t->buf[t->pos] == '\n') { 64 0 64
if (t->pos < t->len && t->buf[t->pos] == '\n') { 0 64 64
435 0 t->pos++;
436 }
437 9813 ▶ } else if (esc == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (esc == '\n') { 96 9717 9813
438 /* Line continuation: \LF */
439 96 t->pos++;
440 } else {
441 /* Single-char escape: n r t b f ( ) \ or unknown */
442 9717 t->pos++;
443 }
444 }
445 } else {
446 2700809 t->pos++;
447 }
448 }
449  
450 3884 tok.length = t->pos - tok.offset;
451  
452 3884 ▶ if (depth != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth != 0) { 32 3852 3884
453 /* Unbalanced parens */
454 32 tok.kind = PDFMAKE_TOK_ERROR;
455 }
456  
457 3884 return tok;
458 }
459  
460 /*============================================================================
461 * Hex string lexer (§7.3.4.3)
462 * <hex> with whitespace ignored
463 *==========================================================================*/
464  
465 839 static pdfmake_tok_t lex_hex_string(pdfmake_tokenizer_t *t) {
466 pdfmake_tok_t tok;
467 839 tok.kind = PDFMAKE_TOK_HSTR;
468 839 tok.offset = t->pos;
469  
470 839 t->pos++; /* Skip < */
471  
472 23624 ▶ while (t->pos < t->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->pos < t->len) { 23624 0 23624
473 23624 uint8_t c = t->buf[t->pos];
474  
475 23624 ▶ if (c == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '>') { 679 22945 23624
476 679 t->pos++;
477 679 break;
478 }
479  
480 /* Skip whitespace inside hex string */
481 22945 ▶ if (pdfmake_is_whitespace(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_is_whitespace(c)) { 0 22945 22945
482 0 t->pos++;
483 0 continue;
484 }
485  
486 /* Must be hex digit */
487 22945 ▶ if (!pdfmake_is_hex(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_is_hex(c)) { 160 22785 22945
488 /* Invalid character in hex string */
489 160 tok.kind = PDFMAKE_TOK_ERROR;
490 160 t->pos++;
491 160 break;
492 }
493  
494 22785 t->pos++;
495 }
496  
497 839 tok.length = t->pos - tok.offset;
498 839 return tok;
499 }
500  
501 /*============================================================================
502 * Keyword/identifier lexer
503 * Identifies: null, true, false, obj, endobj, stream, endstream, R,
504 * xref, trailer, startxref
505 *==========================================================================*/
506  
507 /* Keyword table for fast lookup */
508 typedef struct {
509 const char *text;
510 size_t len;
511 pdfmake_tok_kind_t kind;
512 } keyword_entry_t;
513  
514 static const keyword_entry_t keywords[] = {
515 {"null", 4, PDFMAKE_TOK_KW_NULL},
516 {"true", 4, PDFMAKE_TOK_KW_TRUE},
517 {"false", 5, PDFMAKE_TOK_KW_FALSE},
518 {"obj", 3, PDFMAKE_TOK_KW_OBJ},
519 {"endobj", 6, PDFMAKE_TOK_KW_ENDOBJ},
520 {"stream", 6, PDFMAKE_TOK_KW_STREAM},
521 {"endstream", 9, PDFMAKE_TOK_KW_ENDSTREAM},
522 {"R", 1, PDFMAKE_TOK_KW_R},
523 {"xref", 4, PDFMAKE_TOK_KW_XREF},
524 {"trailer", 7, PDFMAKE_TOK_KW_TRAILER},
525 {"startxref", 9, PDFMAKE_TOK_KW_STARTXREF},
526 {NULL, 0, PDFMAKE_TOK_ERROR}
527 };
528  
529 20373 static pdfmake_tok_t lex_keyword_or_error(pdfmake_tokenizer_t *t) {
530 pdfmake_tok_t tok;
531 const keyword_entry_t *kw;
532 20373 tok.offset = t->pos;
533  
534 /* Read until whitespace or delimiter */
535 92235 ▶ while (t->pos < t->len && pdfmake_is_regular(t->buf[t->pos])) {
Branch: 2 branches
BranchTrueFalseTotal
while (t->pos < t->len && pdfmake_is_regular(t->buf[t->pos])) { 92232 3 92235
while (t->pos < t->len && pdfmake_is_regular(t->buf[t->pos])) { 71862 20370 92232
536 71862 t->pos++;
537 }
538  
539 20373 tok.length = t->pos - tok.offset;
540  
541 /* Check against keyword table */
542 201023 ▶ for (kw = keywords; kw->text != NULL; kw++) {
Branch: 1 branch
BranchTrueFalseTotal
for (kw = keywords; kw->text != NULL; kw++) { 189177 11846 201023
543 189177 ▶ if (tok.length == kw->len &&
Branch: 1 branch
BranchTrueFalseTotal
if (tok.length == kw->len && 17760 171417 189177
544 17760 ▶ memcmp(t->buf + tok.offset, kw->text, kw->len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(t->buf + tok.offset, kw->text, kw->len) == 0) { 8527 9233 17760
545 8527 tok.kind = kw->kind;
546  
547 /* For 'stream', compute body offset */
548 8527 ▶ if (tok.kind == PDFMAKE_TOK_KW_STREAM) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.kind == PDFMAKE_TOK_KW_STREAM) { 333 8194 8527
549 /* §7.3.8.1: stream keyword followed by EOL (LF or CRLF)
550 * Body starts after the EOL */
551 333 size_t body_start = t->pos;
552  
553 /* Skip optional whitespace before EOL (lenient) */
554 333 ▶ while (body_start < t->len &&
Branch: 1 branch
BranchTrueFalseTotal
while (body_start < t->len && 333 0 333
555 333 ▶ (t->buf[body_start] == ' ' || t->buf[body_start] == '\t')) {
Branch: 2 branches
BranchTrueFalseTotal
(t->buf[body_start] == ' ' || t->buf[body_start] == '\t')) { 0 333 333
(t->buf[body_start] == ' ' || t->buf[body_start] == '\t')) { 0 333 333
556 0 body_start++;
557 }
558  
559 /* Must have EOL */
560 333 ▶ if (body_start < t->len && t->buf[body_start] == '\r') {
Branch: 2 branches
BranchTrueFalseTotal
if (body_start < t->len && t->buf[body_start] == '\r') { 333 0 333
if (body_start < t->len && t->buf[body_start] == '\r') { 189 144 333
561 189 body_start++;
562 189 ▶ if (body_start < t->len && t->buf[body_start] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (body_start < t->len && t->buf[body_start] == '\n') { 189 0 189
if (body_start < t->len && t->buf[body_start] == '\n') { 189 0 189
563 189 body_start++;
564 }
565 /* Note: CR alone is technically not spec-compliant but
566 * we accept it for Adobe compatibility */
567 144 ▶ } else if (body_start < t->len && t->buf[body_start] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (body_start < t->len && t->buf[body_start] == '\n') { 144 0 144
} else if (body_start < t->len && t->buf[body_start] == '\n') { 144 0 144
568 144 body_start++;
569 }
570  
571 333 tok.payload.body_offset = body_start;
572 }
573  
574 8527 return tok;
575 }
576 }
577  
578 /* Not a keyword — error (bare identifier not valid in PDF) */
579 11846 tok.kind = PDFMAKE_TOK_ERROR;
580 11846 return tok;
581 }
582  
583 /*============================================================================
584 * Main tokenizer
585 *==========================================================================*/
586  
587 209393 pdfmake_tok_t pdfmake_tok_next(pdfmake_tokenizer_t *t) {
588 pdfmake_tok_t tok;
589 uint8_t c;
590 /* Return peeked token if available */
591 209393 ▶ if (t->has_peeked) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->has_peeked) { 47761 161632 209393
592 47761 t->has_peeked = 0;
593 47761 return t->peeked;
594 }
595  
596 /* EOF check */
597 161632 ▶ if (t->pos >= t->len) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->pos >= t->len) { 217 161415 161632
598 217 tok.kind = PDFMAKE_TOK_EOF;
599 217 tok.offset = t->len;
600 217 tok.length = 0;
601 217 return tok;
602 }
603  
604 161415 c = t->buf[t->pos];
605  
606 /* Whitespace */
607 161415 ▶ if (pdfmake_is_whitespace(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_is_whitespace(c)) { 71846 89569 161415
608 71846 return lex_whitespace(t);
609 }
610  
611 /* Comment */
612 89569 ▶ if (c == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '%') { 299 89270 89569
613 299 return lex_comment(t);
614 }
615  
616 /* Number (starts with digit, +, -, or .) */
617 89270 ▶ if (pdfmake_is_digit(c) ||
Branch: 2 branches
BranchTrueFalseTotal
if (pdfmake_is_digit(c) || 49388 39882 89270
if (pdfmake_is_digit(c) || 49388 0 49388
618 49388 ▶ ((c == '+' || c == '-' || c == '.') &&
Branch: 2 branches
BranchTrueFalseTotal
((c == '+' || c == '-' || c == '.') && 46966 2422 49388
((c == '+' || c == '-' || c == '.') && 1 46965 46966
619 4846 t->pos + 1 < t->len &&
620 2423 ▶ (pdfmake_is_digit(t->buf[t->pos + 1]) || t->buf[t->pos + 1] == '.'))) {
Branch: 1 branch
BranchTrueFalseTotal
(pdfmake_is_digit(t->buf[t->pos + 1]) || t->buf[t->pos + 1] == '.'))) { 0 0 0
621 42305 return lex_number(t);
622 }
623  
624 /* Name */
625 46965 ▶ if (c == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '/') { 12275 34690 46965
626 12275 return lex_name(t);
627 }
628  
629 /* Literal string */
630 34690 ▶ if (c == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '(') { 3884 30806 34690
631 3884 return lex_literal_string(t);
632 }
633  
634 /* Hex string or dict delimiter */
635 30806 ▶ if (c == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '<') { 3495 27311 30806
636 3495 ▶ if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '<') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '<') { 3495 0 3495
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '<') { 2656 839 3495
637 /* << dict open */
638 2656 tok.kind = PDFMAKE_TOK_DICT_OPEN;
639 2656 tok.offset = t->pos;
640 2656 tok.length = 2;
641 2656 t->pos += 2;
642 2656 return tok;
643 }
644 839 return lex_hex_string(t);
645 }
646  
647 /* Dict close */
648 27311 ▶ if (c == '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '>') { 2816 24495 27311
649 2816 ▶ if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '>') {
Branch: 2 branches
BranchTrueFalseTotal
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '>') { 2816 0 2816
if (t->pos + 1 < t->len && t->buf[t->pos + 1] == '>') { 2656 160 2816
650 2656 tok.kind = PDFMAKE_TOK_DICT_CLOSE;
651 2656 tok.offset = t->pos;
652 2656 tok.length = 2;
653 2656 t->pos += 2;
654 2656 return tok;
655 }
656 /* Lone > is an error */
657 160 tok.kind = PDFMAKE_TOK_ERROR;
658 160 tok.offset = t->pos;
659 160 tok.length = 1;
660 160 t->pos++;
661 160 return tok;
662 }
663  
664 /* Array delimiters */
665 24495 ▶ if (c == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '[') { 1720 22775 24495
666 1720 tok.kind = PDFMAKE_TOK_ARR_OPEN;
667 1720 tok.offset = t->pos;
668 1720 tok.length = 1;
669 1720 t->pos++;
670 1720 return tok;
671 }
672  
673 22775 ▶ if (c == ']') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ']') { 2114 20661 22775
674 2114 tok.kind = PDFMAKE_TOK_ARR_CLOSE;
675 2114 tok.offset = t->pos;
676 2114 tok.length = 1;
677 2114 t->pos++;
678 2114 return tok;
679 }
680  
681 /* Braces (not used in PDF content, but are delimiters) */
682 20661 ▶ if (c == '{' || c == '}') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '{' || c == '}') { 20501 160 20661
if (c == '{' || c == '}') { 96 20405 20501
683 256 tok.kind = PDFMAKE_TOK_ERROR;
684 256 tok.offset = t->pos;
685 256 tok.length = 1;
686 256 t->pos++;
687 256 return tok;
688 }
689  
690 /* Close paren without open (error) */
691 20405 ▶ if (c == ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ')') { 32 20373 20405
692 32 tok.kind = PDFMAKE_TOK_ERROR;
693 32 tok.offset = t->pos;
694 32 tok.length = 1;
695 32 t->pos++;
696 32 return tok;
697 }
698  
699 /* Keyword or unknown identifier */
700 20373 return lex_keyword_or_error(t);
701 }
702  
703 /*============================================================================
704 * Peek
705 *==========================================================================*/
706  
707 55463 void pdfmake_tok_peek(pdfmake_tokenizer_t *t, pdfmake_tok_t *out) {
708 55463 ▶ if (!t->has_peeked) {
Branch: 1 branch
BranchTrueFalseTotal
if (!t->has_peeked) { 48405 7058 55463
709 48405 t->peeked = pdfmake_tok_next(t);
710 48405 t->has_peeked = 1;
711 }
712 55463 *out = t->peeked;
713 55463 }
714  
715 /*============================================================================
716 * Skip whitespace/comments
717 *==========================================================================*/
718  
719 75562 pdfmake_tok_t pdfmake_tok_next_significant(pdfmake_tokenizer_t *t) {
720 pdfmake_tok_t tok;
721 do {
722 114571 tok = pdfmake_tok_next(t);
723 114571 ▶ } while (tok.kind == PDFMAKE_TOK_WS || tok.kind == PDFMAKE_TOK_COMMENT);
Branch: 2 branches
BranchTrueFalseTotal
} while (tok.kind == PDFMAKE_TOK_WS || tok.kind == PDFMAKE_TOK_COMMENT); 38841 75730 114571
} while (tok.kind == PDFMAKE_TOK_WS || tok.kind == PDFMAKE_TOK_COMMENT); 168 75562 75730
724 75562 return tok;
725 }
726  
727 34482 void pdfmake_tok_peek_significant(pdfmake_tokenizer_t *t, pdfmake_tok_t *out) {
728 /* Skip whitespace/comments, then peek */
729 while (1) {
730 55463 pdfmake_tok_peek(t, out);
731 55463 ▶ if (out->kind != PDFMAKE_TOK_WS && out->kind != PDFMAKE_TOK_COMMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (out->kind != PDFMAKE_TOK_WS && out->kind != PDFMAKE_TOK_COMMENT) { 34483 20980 55463
if (out->kind != PDFMAKE_TOK_WS && out->kind != PDFMAKE_TOK_COMMENT) { 34482 1 34483
732 34482 break;
733 }
734 20981 pdfmake_tok_next(t); /* Consume and continue */
735 }
736 34482 }
« src/pdfmake_font_std14.c ↑ Summary src/pdfmake_markup.c »