src/pdfmake_cmap.c

filter: Statementstmt 89.5% 258 / 288 Branchbran 54.2% 179 / 330 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.7% 437 / 618 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/document.xs ↑ Summary src/pdfmake_filter.c »
1 /*
2 * pdfmake_cmap.c — PDF ToUnicode CMap parser implementation.
3 */
4  
5 #include "pdfmake_cmap.h"
6 #include <stdlib.h>
7 #include <string.h>
8  
9 /*============================================================================
10 * Data structures
11 *==========================================================================*/
12  
13 typedef struct {
14 uint32_t code;
15 uint8_t uni_count; /* 1..PDFMAKE_CMAP_MAX_UNI */
16 uint32_t uni[PDFMAKE_CMAP_MAX_UNI];
17 } cmap_entry_t;
18  
19 typedef struct {
20 uint32_t lo;
21 uint32_t hi;
22 uint32_t start_uni; /* incremented per code in range */
23 } cmap_range_t;
24  
25 struct pdfmake_cmap {
26 /* Sorted single-code entries */
27 cmap_entry_t *entries;
28 size_t entry_count;
29 size_t entry_cap;
30  
31 /* Sorted range entries (lo ≤ code ≤ hi, unicode = start_uni + (code - lo)) */
32 cmap_range_t *ranges;
33 size_t range_count;
34 size_t range_cap;
35  
36 /* Per-range unicode override arrays (for array form of bfrange):
37 * index into this pool from range->start_uni when -1 is used.
38 * Simpler: we store array-form ranges as expanded entries. */
39  
40 int code_width; /* 1 or 2 */
41 pdfmake_arena_t *arena;
42 };
43  
44 /*============================================================================
45 * Internal helpers — tokenizer
46 *==========================================================================*/
47  
48 typedef struct {
49 const uint8_t *p;
50 const uint8_t *end;
51 } cmap_tok_t;
52  
53 2237 static void tok_skip_ws(cmap_tok_t *t) {
54 3203 ▶ while (t->p < t->end) {
Branch: 1 branch
BranchTrueFalseTotal
while (t->p < t->end) { 3181 22 3203
55 3181 uint8_t c = *t->p;
56 3181 ▶ if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') {
Branch: 5 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') { 2471 710 3181
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') { 2471 0 2471
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') { 2471 0 2471
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') { 2215 256 2471
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') { 0 2215 2215
57 966 t->p++;
58 2215 ▶ } else if (c == '%') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c == '%') { 0 2215 2215
59 /* Comment to end of line */
60 0 ▶ while (t->p < t->end && *t->p != '\n' && *t->p != '\r')
Branch: 3 branches
BranchTrueFalseTotal
while (t->p < t->end && *t->p != '\n' && *t->p != '\r') 0 0 0
while (t->p < t->end && *t->p != '\n' && *t->p != '\r') 0 0 0
while (t->p < t->end && *t->p != '\n' && *t->p != '\r') 0 0 0
61 0 t->p++;
62 } else {
63 2215 break;
64 }
65 }
66 2237 }
67  
68 0 static int is_hex(uint8_t c) {
69 0 ▶ return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
Branch: 6 branches
BranchTrueFalseTotal
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 0 0 0
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 0 0 0
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 0 0 0
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 0 0 0
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 0 0 0
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 0 0 0
70 }
71  
72 600 static int hex_val(uint8_t c) {
73 600 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 600 0 600
if (c >= '0' && c <= '9') return c - '0'; 567 33 600
74 33 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 33 33
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 0 0
75 33 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 33 0 33
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 33 0 33
76 0 return -1;
77 }
78  
79 /* Parse a hex string <...> into a sequence of big-endian bytes.
80 * Bytes past out_cap are read and dropped, so a long string is cut short
81 * and the tokenizer still ends up past it.
82 * Returns number of bytes written, or -1 on error.
83 * Leaves t->p pointing past the closing '>'. */
84 204 static int tok_hex_string(cmap_tok_t *t, uint8_t *out, size_t out_cap) {
85 size_t written;
86 int nibble_hi;
87 uint8_t c;
88 int v;
89  
90 204 tok_skip_ws(t);
91 204 ▶ if (t->p >= t->end || *t->p != '<') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (t->p >= t->end || *t->p != '<') return -1; 204 0 204
if (t->p >= t->end || *t->p != '<') return -1; 0 204 204
92 204 t->p++; /* skip '<' */
93  
94 204 written = 0;
95 204 nibble_hi = -1;
96  
97 806 ▶ while (t->p < t->end && *t->p != '>') {
Branch: 2 branches
BranchTrueFalseTotal
while (t->p < t->end && *t->p != '>') { 806 0 806
while (t->p < t->end && *t->p != '>') { 602 204 806
98 602 c = *t->p++;
99 602 ▶ if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue;
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue; 600 2 602
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue; 600 0 600
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue; 600 0 600
if (c == ' ' || c == '\t' || c == '\r' || c == '\n') continue; 0 600 600
100 600 v = hex_val(c);
101 600 ▶ if (v < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) return -1; 0 600 600
102 600 ▶ if (nibble_hi < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nibble_hi < 0) { 300 300 600
103 300 nibble_hi = v;
104 } else {
105 300 ▶ if (written < out_cap)
Branch: 1 branch
BranchTrueFalseTotal
if (written < out_cap) 300 0 300
106 300 out[written++] = (uint8_t)((nibble_hi << 4) | v);
107 300 nibble_hi = -1;
108 }
109 }
110 /* Handle odd nibble count — pad with 0 per PDF spec */
111 204 ▶ if (nibble_hi >= 0 && written < out_cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (nibble_hi >= 0 && written < out_cap) { 0 204 204
if (nibble_hi >= 0 && written < out_cap) { 0 0 0
112 0 out[written++] = (uint8_t)(nibble_hi << 4);
113 }
114 204 ▶ if (t->p >= t->end || *t->p != '>') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (t->p >= t->end || *t->p != '>') return -1; 204 0 204
if (t->p >= t->end || *t->p != '>') return -1; 0 204 204
115 204 t->p++; /* skip '>' */
116  
117 204 return (int)written;
118 }
119  
120 /* Parse a hex string as a single big-endian integer (1-4 bytes).
121 * Returns byte width on success, -1 on failure. */
122 141 static int tok_hex_int(cmap_tok_t *t, uint32_t *out_value) {
123 uint8_t buf[5];
124 int n;
125 uint32_t v;
126 int i;
127  
128 141 n = tok_hex_string(t, buf, sizeof(buf));
129 141 ▶ if (n < 0 || n > 4) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || n > 4) return -1; 141 0 141
if (n < 0 || n > 4) return -1; 0 141 141
130 141 v = 0;
131 291 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 150 141 291
132 150 v = (v << 8) | buf[i];
133 }
134 141 *out_value = v;
135 141 return n;
136 }
137  
138 /* Parse a hex string as a sequence of BE16 Unicode code units.
139 * Handles surrogate pairs → codepoint collapse.
140 * Returns count of codepoints, at most out_cap: a longer string keeps its
141 * first out_cap. 0 is the empty string <>, -1 an error. */
142 63 static int tok_hex_unicode(cmap_tok_t *t, uint32_t *out, size_t out_cap) {
143 uint8_t buf[PDFMAKE_CMAP_MAX_UNI * 4];
144 int n;
145 size_t written;
146 int i;
147 uint16_t hi;
148 uint32_t cp;
149 uint16_t lo;
150  
151 63 n = tok_hex_string(t, buf, sizeof(buf));
152 63 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 0 63 63
153  
154 63 written = 0;
155 63 i = 0;
156 138 ▶ while (i + 1 < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 1 < n) { 75 63 138
157 75 hi = ((uint16_t)buf[i] << 8) | buf[i + 1];
158 75 i += 2;
159 75 cp = hi;
160  
161 /* Surrogate pair? */
162 75 ▶ if (hi >= 0xD800 && hi <= 0xDBFF && i + 1 < n) {
Branch: 3 branches
BranchTrueFalseTotal
if (hi >= 0xD800 && hi <= 0xDBFF && i + 1 < n) { 3 72 75
if (hi >= 0xD800 && hi <= 0xDBFF && i + 1 < n) { 0 3 3
if (hi >= 0xD800 && hi <= 0xDBFF && i + 1 < n) { 0 0 0
163 0 lo = ((uint16_t)buf[i] << 8) | buf[i + 1];
164 0 ▶ if (lo >= 0xDC00 && lo <= 0xDFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (lo >= 0xDC00 && lo <= 0xDFFF) { 0 0 0
if (lo >= 0xDC00 && lo <= 0xDFFF) { 0 0 0
165 0 cp = 0x10000 + ((hi - 0xD800) << 10) + (lo - 0xDC00);
166 0 i += 2;
167 }
168 }
169  
170 75 ▶ if (written >= out_cap) break;
Branch: 1 branch
BranchTrueFalseTotal
if (written >= out_cap) break; 0 75 75
171 75 out[written++] = cp;
172 }
173 63 return (int)written;
174 }
175  
176 /* Parse an integer count (e.g. "42 beginbfchar"). */
177 637 static int tok_int(cmap_tok_t *t, long *out) {
178 const uint8_t *start;
179 int negative;
180 long v;
181 int any;
182  
183 637 tok_skip_ws(t);
184 637 start = t->p;
185 637 negative = 0;
186 637 ▶ if (t->p < t->end && (*t->p == '-' || *t->p == '+')) {
Branch: 3 branches
BranchTrueFalseTotal
if (t->p < t->end && (*t->p == '-' || *t->p == '+')) { 637 0 637
if (t->p < t->end && (*t->p == '-' || *t->p == '+')) { 637 0 637
if (t->p < t->end && (*t->p == '-' || *t->p == '+')) { 0 637 637
187 0 ▶ if (*t->p == '-') negative = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (*t->p == '-') negative = 1; 0 0 0
188 0 t->p++;
189 }
190 637 v = 0;
191 637 any = 0;
192 750 ▶ while (t->p < t->end && *t->p >= '0' && *t->p <= '9') {
Branch: 3 branches
BranchTrueFalseTotal
while (t->p < t->end && *t->p >= '0' && *t->p <= '9') { 750 0 750
while (t->p < t->end && *t->p >= '0' && *t->p <= '9') { 433 317 750
while (t->p < t->end && *t->p >= '0' && *t->p <= '9') { 113 320 433
193 113 v = v * 10 + (*t->p - '0');
194 113 t->p++;
195 113 any = 1;
196 }
197 637 ▶ if (!any) { t->p = start; return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if (!any) { t->p = start; return -1; } 546 91 637
198 91 ▶ *out = negative ? -v : v;
Branch: 1 branch
BranchTrueFalseTotal
*out = negative ? -v : v; 0 91 91
199 91 return 0;
200 }
201  
202 /* Check if upcoming tokens are a keyword (followed by delimiter). Advances
203 * the tokenizer past the keyword on match; leaves position unchanged otherwise. */
204 494 static int tok_match_keyword(cmap_tok_t *t, const char *kw) {
205 const uint8_t *save;
206 size_t kwlen;
207 uint8_t c;
208  
209 494 save = t->p;
210 494 tok_skip_ws(t);
211 494 kwlen = strlen(kw);
212 494 ▶ if ((size_t)(t->end - t->p) < kwlen) { t->p = save; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(t->end - t->p) < kwlen) { t->p = save; return 0; } 0 494 494
213 494 ▶ if (memcmp(t->p, kw, kwlen) != 0) { t->p = save; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(t->p, kw, kwlen) != 0) { t->p = save; return 0; } 402 92 494
214 /* Must be followed by delimiter */
215 92 ▶ if (t->p + kwlen < t->end) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->p + kwlen < t->end) { 92 0 92
216 92 c = t->p[kwlen];
217 92 ▶ if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n' ||
Branch: 6 branches
BranchTrueFalseTotal
if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n' || 74 18 92
if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n' || 74 0 74
if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n' || 74 0 74
if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n' || 0 74 74
if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n' || 0 0 0
if (!(c == ' ' || c == '\t' || c == '\r' || c == '\n' || 0 0 0
218 0 ▶ c == '<' || c == '[' || c == '/' || c == '%')) {
Branch: 2 branches
BranchTrueFalseTotal
c == '<' || c == '[' || c == '/' || c == '%')) { 0 0 0
c == '<' || c == '[' || c == '/' || c == '%')) { 0 0 0
219 0 t->p = save;
220 0 return 0;
221 }
222 }
223 92 t->p += kwlen;
224 92 return 1;
225 }
226  
227 /*============================================================================
228 * CMap population
229 *==========================================================================*/
230  
231 27 static int cmap_reserve_entries(pdfmake_cmap_t *cm, size_t need) {
232 size_t new_cap;
233 cmap_entry_t *n;
234  
235 27 ▶ if (need <= cm->entry_cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (need <= cm->entry_cap) return 0; 23 4 27
236 4 ▶ new_cap = cm->entry_cap ? cm->entry_cap : 64;
Branch: 1 branch
BranchTrueFalseTotal
new_cap = cm->entry_cap ? cm->entry_cap : 64; 0 4 4
237 4 ▶ while (new_cap < need) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < need) new_cap *= 2; 0 4 4
238 4 n = realloc(cm->entries, new_cap * sizeof(cmap_entry_t));
239 4 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 0 4 4
240 4 cm->entries = n;
241 4 cm->entry_cap = new_cap;
242 4 return 0;
243 }
244  
245 34 static int cmap_reserve_ranges(pdfmake_cmap_t *cm, size_t need) {
246 size_t new_cap;
247 cmap_range_t *n;
248  
249 34 ▶ if (need <= cm->range_cap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (need <= cm->range_cap) return 0; 14 20 34
250 20 ▶ new_cap = cm->range_cap ? cm->range_cap : 16;
Branch: 1 branch
BranchTrueFalseTotal
new_cap = cm->range_cap ? cm->range_cap : 16; 0 20 20
251 20 ▶ while (new_cap < need) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < need) new_cap *= 2; 0 20 20
252 20 n = realloc(cm->ranges, new_cap * sizeof(cmap_range_t));
253 20 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 0 20 20
254 20 cm->ranges = n;
255 20 cm->range_cap = new_cap;
256 20 return 0;
257 }
258  
259 27 static int cmap_add_entry(pdfmake_cmap_t *cm, uint32_t code,
260 const uint32_t *uni, size_t uni_count) {
261 cmap_entry_t *e;
262 size_t i;
263  
264 27 ▶ if (uni_count == 0 || uni_count > PDFMAKE_CMAP_MAX_UNI) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (uni_count == 0 || uni_count > PDFMAKE_CMAP_MAX_UNI) return -1; 27 0 27
if (uni_count == 0 || uni_count > PDFMAKE_CMAP_MAX_UNI) return -1; 0 27 27
265 27 ▶ if (cmap_reserve_entries(cm, cm->entry_count + 1) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (cmap_reserve_entries(cm, cm->entry_count + 1) < 0) return -1; 0 27 27
266 27 e = &cm->entries[cm->entry_count++];
267 27 e->code = code;
268 27 e->uni_count = (uint8_t)uni_count;
269 68 ▶ for (i = 0; i < uni_count; i++) e->uni[i] = uni[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < uni_count; i++) e->uni[i] = uni[i]; 41 27 68
270 27 return 0;
271 }
272  
273 34 static int cmap_add_range(pdfmake_cmap_t *cm,
274 uint32_t lo, uint32_t hi, uint32_t start_uni) {
275 cmap_range_t *r;
276  
277 34 ▶ if (hi < lo) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (hi < lo) return -1; 0 34 34
278 34 ▶ if (cmap_reserve_ranges(cm, cm->range_count + 1) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (cmap_reserve_ranges(cm, cm->range_count + 1) < 0) return -1; 0 34 34
279 34 r = &cm->ranges[cm->range_count++];
280 34 r->lo = lo;
281 34 r->hi = hi;
282 34 r->start_uni = start_uni;
283 34 return 0;
284 }
285  
286 /*============================================================================
287 * Operator handlers
288 *==========================================================================*/
289  
290 /* Parse: N beginbfchar (code) (uni) ... endbfchar
291 * or N begincidchar (code) (uni) ... endcidchar */
292 4 static int parse_bfchar_section(cmap_tok_t *t, pdfmake_cmap_t *cm, long count) {
293 long i;
294 uint32_t code;
295 int code_w;
296 uint32_t uni[PDFMAKE_CMAP_MAX_UNI];
297 int n;
298  
299 /* The section runs to its end keyword. The count in front of it is
300 * not trusted: producers get it wrong, and a miscount must not cost
301 * the rest of the map. */
302 (void)count;
303 (void)i;
304 for (;;) {
305 29 tok_skip_ws(t);
306 54 if (tok_match_keyword(t, "endbfchar") ||
307 25 tok_match_keyword(t, "endcidchar")) {
308 4 return 0;
309 }
310 25 code_w = tok_hex_int(t, &code);
311 25 ▶ if (code_w < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (code_w < 0) return -1; 0 25 25
312 25 ▶ if (cm->code_width == 0 || code_w > cm->code_width)
Branch: 2 branches
BranchTrueFalseTotal
if (cm->code_width == 0 || code_w > cm->code_width) 25 0 25
if (cm->code_width == 0 || code_w > cm->code_width) 0 25 25
313 0 cm->code_width = code_w;
314  
315 25 n = tok_hex_unicode(t, uni, PDFMAKE_CMAP_MAX_UNI);
316 25 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 0 25 25
317 25 ▶ if (n == 0) continue; /* <> gives the code no text */
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) continue; /* <> gives the code no text */ 2 23 25
318  
319 23 ▶ if (cmap_add_entry(cm, code, uni, (size_t)n) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (cmap_add_entry(cm, code, uni, (size_t)n) < 0) return -1; 0 23 23
320 }
321 }
322  
323 /* Parse: N beginbfrange (lo) (hi) (uni) ... endbfrange
324 * or N beginbfrange (lo) (hi) [<uni1> <uni2> ...] endbfrange
325 * Array form: each code in [lo..hi] maps to the corresponding array entry. */
326 20 static int parse_bfrange_section(cmap_tok_t *t, pdfmake_cmap_t *cm, long count) {
327 long i;
328 uint32_t lo, hi;
329 int lw, hw;
330 int w;
331 uint32_t uni[PDFMAKE_CMAP_MAX_UNI];
332 int n;
333 uint32_t c;
334 uint32_t u[PDFMAKE_CMAP_MAX_UNI];
335 int j;
336 uint32_t delta;
337 uint32_t uni2[PDFMAKE_CMAP_MAX_UNI];
338 int n2;
339  
340 (void)count;
341 (void)i;
342 for (;;) {
343 56 tok_skip_ws(t);
344 92 if (tok_match_keyword(t, "endbfrange") ||
345 36 tok_match_keyword(t, "endcidrange")) {
346 20 return 0;
347 }
348 36 lw = tok_hex_int(t, &lo);
349 36 hw = tok_hex_int(t, &hi);
350 36 ▶ if (lw < 0 || hw < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (lw < 0 || hw < 0) return -1; 36 0 36
if (lw < 0 || hw < 0) return -1; 0 36 36
351 36 w = lw > hw ? lw : hw;
352 36 ▶ if (cm->code_width == 0 || w > cm->code_width) cm->code_width = w;
Branch: 2 branches
BranchTrueFalseTotal
if (cm->code_width == 0 || w > cm->code_width) cm->code_width = w; 36 0 36
if (cm->code_width == 0 || w > cm->code_width) cm->code_width = w; 0 36 36
353  
354 36 tok_skip_ws(t);
355 36 ▶ if (t->p >= t->end) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (t->p >= t->end) return -1; 0 36 36
356  
357 36 ▶ if (*t->p == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (*t->p == '<') { 34 2 36
358 /* Range form: single unicode start */
359 34 n = tok_hex_unicode(t, uni, PDFMAKE_CMAP_MAX_UNI);
360 34 ▶ if (n < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) return -1; 0 34 34
361 34 ▶ if (n == 0 || hi < lo) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || hi < lo) continue; 34 0 34
if (n == 0 || hi < lo) continue; 0 34 34
362  
363 34 ▶ if (n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) { 34 0 34
364 /* Simple single-codepoint range — store as compact range */
365 34 ▶ if (cmap_add_range(cm, lo, hi, uni[0]) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (cmap_add_range(cm, lo, hi, uni[0]) < 0) return -1; 0 34 34
366 } else {
367 /* Multi-codepoint sequence: only the first code uses the full
368 * sequence; subsequent codes would increment the last codepoint
369 * per the spec, but that's rarely used. Expand as entries. */
370 /* A range wider than two bytes of codes is not a real
371 * one, and expanding it would not end. */
372 0 ▶ if (hi - lo > 0xFFFF) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (hi - lo > 0xFFFF) continue; 0 0 0
373 0 ▶ for (c = lo; c <= hi; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = lo; c <= hi; c++) { 0 0 0
374 0 ▶ for (j = 0; j < n; j++) u[j] = uni[j];
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) u[j] = uni[j]; 0 0 0
375 /* Increment the last codepoint */
376 0 delta = c - lo;
377 0 ▶ if (n > 0) u[n - 1] += delta;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) u[n - 1] += delta; 0 0 0
378 0 ▶ if (cmap_add_entry(cm, c, u, (size_t)n) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (cmap_add_entry(cm, c, u, (size_t)n) < 0) return -1; 0 0 0
379 }
380 }
381 2 ▶ } else if (*t->p == '[') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*t->p == '[') { 2 0 2
382 /* Array form */
383 2 t->p++; /* skip '[' */
384 /* One string per code. The array is read to its bracket, so
385 * one that is shorter or longer than the range costs only
386 * the codes it has no string for. */
387 2 c = lo;
388 for (;;) {
389 6 tok_skip_ws(t);
390 6 ▶ if (t->p >= t->end) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (t->p >= t->end) return -1; 0 6 6
391 6 ▶ if (*t->p == ']') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*t->p == ']') break; 2 4 6
392 4 ▶ if (*t->p != '<') return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (*t->p != '<') return -1; 0 4 4
393 4 n2 = tok_hex_unicode(t, uni2, PDFMAKE_CMAP_MAX_UNI);
394 4 ▶ if (n2 < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n2 < 0) return -1; 0 4 4
395 4 ▶ if (n2 > 0 && c >= lo && c <= hi) {
Branch: 3 branches
BranchTrueFalseTotal
if (n2 > 0 && c >= lo && c <= hi) { 4 0 4
if (n2 > 0 && c >= lo && c <= hi) { 4 0 4
if (n2 > 0 && c >= lo && c <= hi) { 4 0 4
396 4 ▶ if (cmap_add_entry(cm, c, uni2, (size_t)n2) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (cmap_add_entry(cm, c, uni2, (size_t)n2) < 0) return -1; 0 4 4
397 }
398 4 ▶ if (c <= hi) c++;
Branch: 1 branch
BranchTrueFalseTotal
if (c <= hi) c++; 4 0 4
399 }
400 2 t->p++; /* skip ']' */
401 } else {
402 0 return -1;
403 }
404 }
405 }
406  
407 /* Parse: N begincodespacerange <lo> <hi> ... endcodespacerange
408 * We use this only to detect the maximum code width. */
409 22 static int parse_codespace_section(cmap_tok_t *t, pdfmake_cmap_t *cm, long count) {
410 long i;
411 uint32_t lo, hi;
412 int lw, hw;
413 int w;
414  
415 (void)count;
416 (void)i;
417 for (;;) {
418 44 tok_skip_ws(t);
419 44 ▶ if (tok_match_keyword(t, "endcodespacerange")) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (tok_match_keyword(t, "endcodespacerange")) return 0; 22 22 44
420 22 lw = tok_hex_int(t, &lo);
421 22 hw = tok_hex_int(t, &hi);
422 22 ▶ if (lw < 0 || hw < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (lw < 0 || hw < 0) return -1; 22 0 22
if (lw < 0 || hw < 0) return -1; 0 22 22
423 22 w = lw > hw ? lw : hw;
424 22 ▶ if (cm->code_width == 0 || w > cm->code_width) cm->code_width = w;
Branch: 2 branches
BranchTrueFalseTotal
if (cm->code_width == 0 || w > cm->code_width) cm->code_width = w; 0 22 22
if (cm->code_width == 0 || w > cm->code_width) cm->code_width = w; 0 0 0
425 }
426 }
427  
428 /*============================================================================
429 * Sorting / lookup
430 *==========================================================================*/
431  
432 36 static int entry_cmp(const void *a, const void *b) {
433 36 uint32_t ca = ((const cmap_entry_t *)a)->code;
434 36 uint32_t cb = ((const cmap_entry_t *)b)->code;
435 36 return (ca > cb) - (ca < cb);
436 }
437  
438 14 static int range_cmp(const void *a, const void *b) {
439 14 uint32_t la = ((const cmap_range_t *)a)->lo;
440 14 uint32_t lb = ((const cmap_range_t *)b)->lo;
441 14 return (la > lb) - (la < lb);
442 }
443  
444 /*============================================================================
445 * Public API
446 *==========================================================================*/
447  
448 22 pdfmake_cmap_t *pdfmake_cmap_parse(pdfmake_arena_t *arena,
449 const uint8_t *data, size_t len) {
450 pdfmake_cmap_t *cm;
451 cmap_tok_t tok;
452 const uint8_t *save;
453 long count;
454 int rc;
455 cmap_entry_t *arena_entries;
456 cmap_range_t *arena_ranges;
457  
458 22 ▶ if (!arena || !data || len == 0) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!arena || !data || len == 0) return NULL; 22 0 22
if (!arena || !data || len == 0) return NULL; 22 0 22
if (!arena || !data || len == 0) return NULL; 0 22 22
459  
460 22 cm = pdfmake_arena_alloc(arena, sizeof(*cm));
461 22 ▶ if (!cm) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!cm) return NULL; 0 22 22
462 22 memset(cm, 0, sizeof(*cm));
463 22 cm->arena = arena;
464  
465 22 tok.p = data;
466 22 tok.end = data + len;
467  
468 /* Scan for operator sections; skip everything else.
469 * We look for "N begin<op>" patterns. */
470 659 ▶ while (tok.p < tok.end) {
Branch: 1 branch
BranchTrueFalseTotal
while (tok.p < tok.end) { 659 0 659
471 659 tok_skip_ws(&tok);
472 659 ▶ if (tok.p >= tok.end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (tok.p >= tok.end) break; 22 637 659
473  
474 /* Try to parse an integer followed by a keyword */
475 637 save = tok.p;
476 637 ▶ if (tok_int(&tok, &count) == 0 && count > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (tok_int(&tok, &count) == 0 && count > 0) { 91 546 637
if (tok_int(&tok, &count) == 0 && count > 0) { 72 19 91
477 72 tok_skip_ws(&tok);
478 72 rc = 0;
479 140 if (tok_match_keyword(&tok, "beginbfchar") ||
480 68 tok_match_keyword(&tok, "begincidchar")) {
481 4 rc = parse_bfchar_section(&tok, cm, count);
482 116 } else if (tok_match_keyword(&tok, "beginbfrange") ||
483 48 tok_match_keyword(&tok, "begincidrange")) {
484 20 rc = parse_bfrange_section(&tok, cm, count);
485 48 ▶ } else if (tok_match_keyword(&tok, "begincodespacerange")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (tok_match_keyword(&tok, "begincodespacerange")) { 22 26 48
486 22 rc = parse_codespace_section(&tok, cm, count);
487 } else {
488 /* Not a section we care about; advance past the keyword (if any)
489 * by skipping one token so we don't loop forever. */
490 26 while (tok.p < tok.end &&
491 126 ▶ *tok.p != ' ' && *tok.p != '\t' &&
Branch: 2 branches
BranchTrueFalseTotal
*tok.p != ' ' && *tok.p != '\t' && 104 22 126
*tok.p != ' ' && *tok.p != '\t' && 104 0 104
492 230 ▶ *tok.p != '\r' && *tok.p != '\n' &&
Branch: 3 branches
BranchTrueFalseTotal
*tok.p != '\r' && *tok.p != '\n' && 126 0 126
*tok.p != '\r' && *tok.p != '\n' && 104 0 104
*tok.p != '\r' && *tok.p != '\n' && 100 4 104
493 100 ▶ *tok.p != '%') {
Branch: 1 branch
BranchTrueFalseTotal
*tok.p != '%') { 100 0 100
494 100 tok.p++;
495 }
496 26 continue;
497 }
498 46 ▶ if (rc != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) { 0 46 46
499 /* Parse error — bail out with what we have so far */
500 0 break;
501 }
502 } else {
503 /* Not a "N begin*" — skip one token and continue */
504 565 tok.p = save;
505 565 while (tok.p < tok.end &&
506 4598 ▶ *tok.p != ' ' && *tok.p != '\t' &&
Branch: 2 branches
BranchTrueFalseTotal
*tok.p != ' ' && *tok.p != '\t' && 4145 453 4598
*tok.p != ' ' && *tok.p != '\t' && 4145 0 4145
507 8743 ▶ *tok.p != '\r' && *tok.p != '\n' &&
Branch: 3 branches
BranchTrueFalseTotal
*tok.p != '\r' && *tok.p != '\n' && 4598 0 4598
*tok.p != '\r' && *tok.p != '\n' && 4145 0 4145
*tok.p != '\r' && *tok.p != '\n' && 4033 112 4145
508 4033 ▶ *tok.p != '%') {
Branch: 1 branch
BranchTrueFalseTotal
*tok.p != '%') { 4033 0 4033
509 4033 tok.p++;
510 }
511 565 ▶ if (tok.p == save) tok.p++; /* safety: always make progress */
Branch: 1 branch
BranchTrueFalseTotal
if (tok.p == save) tok.p++; /* safety: always make progress */ 0 565 565
512 }
513 }
514  
515 /* Sort for binary-search lookup */
516 22 ▶ if (cm->entry_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cm->entry_count > 1) { 4 18 22
517 4 qsort(cm->entries, cm->entry_count, sizeof(cmap_entry_t), entry_cmp);
518 }
519 22 ▶ if (cm->range_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cm->range_count > 1) { 14 8 22
520 14 qsort(cm->ranges, cm->range_count, sizeof(cmap_range_t), range_cmp);
521 }
522  
523 22 ▶ if (cm->code_width == 0) cm->code_width = 2; /* default to 2-byte */
Branch: 1 branch
BranchTrueFalseTotal
if (cm->code_width == 0) cm->code_width = 2; /* default to 2-byte */ 0 22 22
524  
525 /* Copy the heap-allocated tables into the arena so the whole CMap is
526 * arena-owned and freed with the document. */
527 22 ▶ if (cm->entry_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cm->entry_count > 0) { 4 18 22
528 4 arena_entries = pdfmake_arena_alloc(
529 4 arena, cm->entry_count * sizeof(cmap_entry_t));
530 4 ▶ if (arena_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (arena_entries) { 4 0 4
531 4 memcpy(arena_entries, cm->entries,
532 4 cm->entry_count * sizeof(cmap_entry_t));
533 4 free(cm->entries);
534 4 cm->entries = arena_entries;
535 4 cm->entry_cap = cm->entry_count;
536 }
537 }
538 22 ▶ if (cm->range_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cm->range_count > 0) { 20 2 22
539 20 arena_ranges = pdfmake_arena_alloc(
540 20 arena, cm->range_count * sizeof(cmap_range_t));
541 20 ▶ if (arena_ranges) {
Branch: 1 branch
BranchTrueFalseTotal
if (arena_ranges) { 20 0 20
542 20 memcpy(arena_ranges, cm->ranges,
543 20 cm->range_count * sizeof(cmap_range_t));
544 20 free(cm->ranges);
545 20 cm->ranges = arena_ranges;
546 20 cm->range_cap = cm->range_count;
547 }
548 }
549  
550 22 return cm;
551 }
552  
553 240 int pdfmake_cmap_lookup(const pdfmake_cmap_t *cmap,
554 uint32_t code,
555 uint32_t *out,
556 size_t *out_count) {
557 240 ▶ if (!cmap || !out || !out_count) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!cmap || !out || !out_count) return 0; 240 0 240
if (!cmap || !out || !out_count) return 0; 240 0 240
if (!cmap || !out || !out_count) return 0; 0 240 240
558  
559 /* Binary search single-code entries first */
560 240 ▶ if (cmap->entry_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmap->entry_count > 0) { 40 200 240
561 40 size_t lo = 0, hi = cmap->entry_count;
562 108 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 102 6 108
563 102 size_t mid = (lo + hi) / 2;
564 102 uint32_t mc = cmap->entries[mid].code;
565 102 ▶ if (mc == code) {
Branch: 1 branch
BranchTrueFalseTotal
if (mc == code) { 34 68 102
566 34 const cmap_entry_t *e = &cmap->entries[mid];
567 size_t k;
568 84 ▶ for (k = 0; k < e->uni_count; k++) out[k] = e->uni[k];
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < e->uni_count; k++) out[k] = e->uni[k]; 50 34 84
569 34 *out_count = e->uni_count;
570 34 return 1;
571 }
572 68 ▶ if (mc < code) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (mc < code) lo = mid + 1; 36 32 68
573 32 else hi = mid;
574 }
575 }
576  
577 /* Binary search ranges */
578 206 ▶ if (cmap->range_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmap->range_count > 0) { 206 0 206
579 206 size_t lo = 0, hi = cmap->range_count;
580 /* Find last range with range.lo <= code */
581 502 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 296 206 502
582 296 size_t mid = (lo + hi) / 2;
583 296 ▶ if (cmap->ranges[mid].lo <= code) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cmap->ranges[mid].lo <= code) lo = mid + 1; 206 90 296
584 90 else hi = mid;
585 }
586 206 ▶ if (lo > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lo > 0) { 206 0 206
587 206 const cmap_range_t *r = &cmap->ranges[lo - 1];
588 206 ▶ if (code >= r->lo && code <= r->hi) {
Branch: 2 branches
BranchTrueFalseTotal
if (code >= r->lo && code <= r->hi) { 206 0 206
if (code >= r->lo && code <= r->hi) { 206 0 206
589 206 out[0] = r->start_uni + (code - r->lo);
590 206 *out_count = 1;
591 206 return 1;
592 }
593 }
594 }
595  
596 0 return 0;
597 }
598  
599 0 int pdfmake_cmap_code_width(const pdfmake_cmap_t *cmap) {
600 0 ▶ return cmap ? cmap->code_width : 0;
Branch: 1 branch
BranchTrueFalseTotal
return cmap ? cmap->code_width : 0; 0 0 0
601 }
602  
603 0 size_t pdfmake_cmap_size(const pdfmake_cmap_t *cmap) {
604 0 ▶ if (!cmap) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!cmap) return 0; 0 0 0
605 0 return cmap->entry_count + cmap->range_count;
606 }
« xs/document.xs ↑ Summary src/pdfmake_filter.c »