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