src/pdfmake_filter.c

filter: Statementstmt 79.0% 759 / 960 Branchbran 62.2% 464 / 746 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 71.6% 1223 / 1706 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_cmap.c ↑ Summary xs/reader.xs »
1 /*
2 * pdfmake_filter.c — Stream filter implementation
3 *
4 * Implements FlateDecode (DEFLATE + zlib) and predictor functions.
5 * RFC 1950 (zlib), RFC 1951 (DEFLATE), PDF §7.4.4
6 */
7  
8 #include "pdfmake_filter.h"
9 #include <string.h>
10 #include <stdlib.h>
11  
12 /*============================================================================
13 * Adler-32 (RFC 1950)
14 *==========================================================================*/
15  
16 #define ADLER_MOD 65521
17  
18 43 uint32_t pdfmake_adler32(const uint8_t *data, size_t len)
19 {
20 43 return pdfmake_adler32_update(1, data, len);
21 }
22  
23 43 uint32_t pdfmake_adler32_update(uint32_t adler, const uint8_t *data, size_t len)
24 {
25 43 uint32_t a = adler & 0xFFFF;
26 43 uint32_t b = (adler >> 16) & 0xFFFF;
27  
28 /* Process in chunks to avoid overflow */
29 411 ▶ while (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (len > 0) { 368 43 411
30 368 size_t chunk = len > 5552 ? 5552 : len;
31 368 len -= chunk;
32  
33 1860246 ▶ while (chunk--) {
Branch: 1 branch
BranchTrueFalseTotal
while (chunk--) { 1859878 368 1860246
34 1859878 a += *data++;
35 1859878 b += a;
36 }
37  
38 368 a %= ADLER_MOD;
39 368 b %= ADLER_MOD;
40 }
41  
42 43 return (b << 16) | a;
43 }
44  
45 /*============================================================================
46 * Flate params
47 *==========================================================================*/
48  
49 157 void pdfmake_flate_params_init(pdfmake_flate_params_t *params)
50 {
51 157 ▶ if (!params) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!params) return; 0 157 157
52 157 params->predictor = 1;
53 157 params->colors = 1;
54 157 params->bits_per_comp = 8;
55 157 params->columns = 1;
56 157 params->early_change = 1;
57 }
58  
59 0 pdfmake_err_t pdfmake_flate_params_from_dict(pdfmake_flate_params_t *params,
60 const pdfmake_obj_t *dict)
61 {
62 0 pdfmake_flate_params_init(params);
63 (void)dict;
64 0 return PDFMAKE_OK;
65 }
66  
67 /* Read one integer entry of a /DecodeParms dict into *slot. */
68 5 static void flate_param_int(pdfmake_arena_t *arena, const pdfmake_obj_t *dict,
69 const char *key, int *slot)
70 {
71 5 uint32_t id = pdfmake_arena_intern_name(arena, key, strlen(key));
72 5 ▶ pdfmake_obj_t *value = id ? pdfmake_dict_get((pdfmake_obj_t *)dict, id) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
pdfmake_obj_t *value = id ? pdfmake_dict_get((pdfmake_obj_t *)dict, id) : NULL; 5 0 5
73 5 ▶ if (!value) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!value) return; 3 2 5
74 2 ▶ if (value->kind == PDFMAKE_INT) *slot = (int)value->as.i;
Branch: 1 branch
BranchTrueFalseTotal
if (value->kind == PDFMAKE_INT) *slot = (int)value->as.i; 2 0 2
75 0 ▶ else if (value->kind == PDFMAKE_REAL) *slot = (int)value->as.r;
Branch: 1 branch
BranchTrueFalseTotal
else if (value->kind == PDFMAKE_REAL) *slot = (int)value->as.r; 0 0 0
76 }
77  
78 134 pdfmake_err_t pdfmake_flate_params_from_dict_arena(pdfmake_flate_params_t *params,
79 pdfmake_arena_t *arena,
80 const pdfmake_obj_t *dict)
81 {
82 134 pdfmake_flate_params_init(params);
83 134 ▶ if (!arena || !dict || dict->kind != PDFMAKE_DICT) return PDFMAKE_OK;
Branch: 3 branches
BranchTrueFalseTotal
if (!arena || !dict || dict->kind != PDFMAKE_DICT) return PDFMAKE_OK; 134 0 134
if (!arena || !dict || dict->kind != PDFMAKE_DICT) return PDFMAKE_OK; 1 133 134
if (!arena || !dict || dict->kind != PDFMAKE_DICT) return PDFMAKE_OK; 0 1 1
84 1 flate_param_int(arena, dict, "Predictor", &params->predictor);
85 1 flate_param_int(arena, dict, "Colors", &params->colors);
86 1 flate_param_int(arena, dict, "BitsPerComponent", &params->bits_per_comp);
87 1 flate_param_int(arena, dict, "Columns", &params->columns);
88 1 flate_param_int(arena, dict, "EarlyChange", &params->early_change);
89 1 ▶ if (params->colors < 1) params->colors = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (params->colors < 1) params->colors = 1; 0 1 1
90 1 ▶ if (params->columns < 1) params->columns = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (params->columns < 1) params->columns = 1; 0 1 1
91 1 ▶ if (params->bits_per_comp < 1) params->bits_per_comp = 8;
Branch: 1 branch
BranchTrueFalseTotal
if (params->bits_per_comp < 1) params->bits_per_comp = 8; 0 1 1
92 1 return PDFMAKE_OK;
93 }
94  
95 /*============================================================================
96 * DEFLATE bit reader
97 *==========================================================================*/
98  
99 typedef struct {
100 const uint8_t *data;
101 size_t len;
102 size_t pos;
103 uint32_t bits;
104 int nbits;
105 } bitreader_t;
106  
107 162 static void bitreader_init(bitreader_t *br, const uint8_t *data, size_t len)
108 {
109 162 br->data = data;
110 162 br->len = len;
111 162 br->pos = 0;
112 162 br->bits = 0;
113 162 br->nbits = 0;
114 162 }
115  
116 7954060 static int bitreader_read(bitreader_t *br, int n)
117 {
118 int val;
119 9242969 ▶ while (br->nbits < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (br->nbits < n) { 1288911 7954058 9242969
120 1288911 ▶ if (br->pos >= br->len) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (br->pos >= br->len) return -1; 2 1288909 1288911
121 1288909 br->bits |= (uint32_t)br->data[br->pos++] << br->nbits;
122 1288909 br->nbits += 8;
123 }
124  
125 7954058 val = br->bits & ((1 << n) - 1);
126 7954058 br->bits >>= n;
127 7954058 br->nbits -= n;
128 7954058 return val;
129 }
130  
131 /* Reserved for dynamic Huffman tree decoding in future phases */
132 __attribute__((unused))
133 0 static int bitreader_read_rev(bitreader_t *br, int n)
134 {
135 int val;
136 int rev;
137 int i;
138 0 val = bitreader_read(br, n);
139 0 ▶ if (val < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) return -1; 0 0 0
140  
141 /* Reverse bits */
142 0 rev = 0;
143 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
144 0 rev = (rev << 1) | (val & 1);
145 0 val >>= 1;
146 }
147 0 return rev;
148 }
149  
150 /*============================================================================
151 * DEFLATE Huffman tables
152 *==========================================================================*/
153  
154 #define HUFFMAN_MAX_BITS 15
155 #define HUFFMAN_MAX_CODES 288
156  
157 typedef struct {
158 uint16_t counts[HUFFMAN_MAX_BITS + 1];
159 uint16_t symbols[HUFFMAN_MAX_CODES];
160 } huffman_t;
161  
162 /* Build Huffman table from code lengths */
163 1705 static int huffman_build(huffman_t *h, const uint8_t *lengths, int n)
164 {
165 int i;
166 uint16_t offsets[HUFFMAN_MAX_BITS + 1];
167 int total;
168 1705 memset(h->counts, 0, sizeof(h->counts));
169  
170 /* Count code lengths */
171 185995 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 184290 1705 185995
172 184290 ▶ if (lengths[i] > HUFFMAN_MAX_BITS) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (lengths[i] > HUFFMAN_MAX_BITS) return -1; 0 184290 184290
173 184290 h->counts[lengths[i]]++;
174 }
175 1705 h->counts[0] = 0;
176  
177 /* Compute symbol table offsets (cumulative counts) */
178 1705 total = 0;
179 27280 ▶ for (i = 1; i <= HUFFMAN_MAX_BITS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= HUFFMAN_MAX_BITS; i++) { 25575 1705 27280
180 25575 offsets[i] = total;
181 25575 total += h->counts[i];
182 }
183  
184 /* Build symbol table - symbols sorted by code length then symbol value */
185 185995 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 184290 1705 185995
186 184290 ▶ if (lengths[i] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lengths[i] > 0) { 116619 67671 184290
187 116619 h->symbols[offsets[lengths[i]]++] = i;
188 }
189 }
190  
191 1705 return 0;
192 }
193  
194 /* Decode one symbol using Huffman table */
195 1429200 static int huffman_decode(huffman_t *h, bitreader_t *br)
196 {
197 1429200 int code = 0;
198 1429200 int first = 0;
199 1429200 int index = 0;
200 int len;
201  
202 7604142 ▶ for (len = 1; len <= HUFFMAN_MAX_BITS; len++) {
Branch: 1 branch
BranchTrueFalseTotal
for (len = 1; len <= HUFFMAN_MAX_BITS; len++) { 7604142 0 7604142
203 7604142 int bit = bitreader_read(br, 1);
204 int count;
205 7604142 ▶ if (bit < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (bit < 0) return -1; 0 7604142 7604142
206  
207 7604142 code = (code << 1) | bit;
208 7604142 count = h->counts[len];
209  
210 7604142 ▶ if (code - first < count) {
Branch: 1 branch
BranchTrueFalseTotal
if (code - first < count) { 1429200 6174942 7604142
211 1429200 return h->symbols[index + (code - first)];
212 }
213  
214 6174942 index += count;
215 6174942 first = (first + count) << 1;
216 }
217  
218 0 return -1; /* Invalid code */
219 }
220  
221 /*============================================================================
222 * DEFLATE fixed Huffman codes
223 *==========================================================================*/
224  
225 static huffman_t fixed_lit_len;
226 static huffman_t fixed_dist;
227 static int fixed_tables_built = 0;
228  
229 162 static void build_fixed_tables(void)
230 {
231 int i;
232 uint8_t lit_lengths[288];
233 uint8_t dist_lengths[32];
234 162 ▶ if (fixed_tables_built) return;
Branch: 1 branch
BranchTrueFalseTotal
if (fixed_tables_built) return; 145 17 162
235  
236 /* Literal/length codes: 0-143=8 bits, 144-255=9 bits, 256-279=7 bits, 280-287=8 bits */
237 2465 ▶ for (i = 0; i <= 143; i++) lit_lengths[i] = 8;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= 143; i++) lit_lengths[i] = 8; 2448 17 2465
238 1921 ▶ for (i = 144; i <= 255; i++) lit_lengths[i] = 9;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 144; i <= 255; i++) lit_lengths[i] = 9; 1904 17 1921
239 425 ▶ for (i = 256; i <= 279; i++) lit_lengths[i] = 7;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 256; i <= 279; i++) lit_lengths[i] = 7; 408 17 425
240 153 ▶ for (i = 280; i <= 287; i++) lit_lengths[i] = 8;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 280; i <= 287; i++) lit_lengths[i] = 8; 136 17 153
241  
242 /* Distance codes: all 5 bits */
243 561 ▶ for (i = 0; i < 32; i++) dist_lengths[i] = 5;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) dist_lengths[i] = 5; 544 17 561
244  
245 17 huffman_build(&fixed_lit_len, lit_lengths, 288);
246 17 huffman_build(&fixed_dist, dist_lengths, 32);
247  
248 17 fixed_tables_built = 1;
249 }
250  
251 /*============================================================================
252 * DEFLATE length/distance tables (RFC 1951)
253 *==========================================================================*/
254  
255 static const int length_base[29] = {
256 3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
257 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
258 67, 83, 99, 115, 131, 163, 195, 227, 258
259 };
260  
261 static const int length_extra[29] = {
262 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
263 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
264 4, 4, 4, 4, 5, 5, 5, 5, 0
265 };
266  
267 static const int dist_base[30] = {
268 1, 2, 3, 4, 5, 7, 9, 13, 17, 25,
269 33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
270 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
271 };
272  
273 static const int dist_extra[30] = {
274 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
275 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
276 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
277 };
278  
279 /* Code length code order (for dynamic Huffman) */
280 static const int cl_order[19] = {
281 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
282 };
283  
284 /*============================================================================
285 * DEFLATE decoder
286 *==========================================================================*/
287  
288 751 static pdfmake_err_t inflate_block(bitreader_t *br, huffman_t *hl, huffman_t *hd,
289 pdfmake_buf_t *out)
290 {
291 int sym;
292 int len;
293 int extra;
294 int dist_sym;
295 int dist;
296 int i;
297 uint8_t byte;
298 for (;;) {
299 998443 sym = huffman_decode(hl, br);
300 998443 ▶ if (sym < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (sym < 0) return PDFMAKE_ECORRUPT; 0 998443 998443
301  
302 998443 ▶ if (sym < 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (sym < 256) { 675351 323092 998443
303 /* Literal byte */
304 675351 ▶ if (pdfmake_buf_append_byte(out, (uint8_t)sym) != PDFMAKE_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(out, (uint8_t)sym) != PDFMAKE_OK) 0 675351 675351
305 0 return PDFMAKE_ENOMEM;
306 }
307 323092 ▶ else if (sym == 256) {
Branch: 1 branch
BranchTrueFalseTotal
else if (sym == 256) { 751 322341 323092
308 /* End of block */
309 751 return PDFMAKE_OK;
310 }
311 else {
312 /* Length/distance pair */
313 322341 sym -= 257;
314 322341 ▶ if (sym >= 29) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (sym >= 29) return PDFMAKE_ECORRUPT; 0 322341 322341
315  
316 322341 len = length_base[sym];
317 322341 ▶ if (length_extra[sym] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length_extra[sym] > 0) { 31346 290995 322341
318 31346 extra = bitreader_read(br, length_extra[sym]);
319 31346 ▶ if (extra < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (extra < 0) return PDFMAKE_ECORRUPT; 0 31346 31346
320 31346 len += extra;
321 }
322  
323 322341 dist_sym = huffman_decode(hd, br);
324 322341 ▶ if (dist_sym < 0 || dist_sym >= 30) return PDFMAKE_ECORRUPT;
Branch: 2 branches
BranchTrueFalseTotal
if (dist_sym < 0 || dist_sym >= 30) return PDFMAKE_ECORRUPT; 322341 0 322341
if (dist_sym < 0 || dist_sym >= 30) return PDFMAKE_ECORRUPT; 0 322341 322341
325  
326 322341 dist = dist_base[dist_sym];
327 322341 ▶ if (dist_extra[dist_sym] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dist_extra[dist_sym] > 0) { 303070 19271 322341
328 303070 extra = bitreader_read(br, dist_extra[dist_sym]);
329 303070 ▶ if (extra < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (extra < 0) return PDFMAKE_ECORRUPT; 0 303070 303070
330 303070 dist += extra;
331 }
332  
333 /* Copy from back-reference */
334 322341 ▶ if ((size_t)dist > out->len) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)dist > out->len) return PDFMAKE_ECORRUPT; 0 322341 322341
335  
336 4146900 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 3824559 322341 4146900
337 3824559 byte = out->data[out->len - dist];
338 3824559 ▶ if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK) 0 3824559 3824559
339 0 return PDFMAKE_ENOMEM;
340 }
341 }
342 }
343 }
344  
345 /* Write `count` copies of `val` into lengths[n..target), saturating at target.
346 * Shared by the three code-length repeat symbols (16/17/18) in the dynamic
347 * Huffman block — each differs only in how `count` and `val` are derived. */
348 static PDFMAKE_INLINE int
349 4241 deflate_fill_run(uint8_t *lengths, int n, int target, int count, uint8_t val)
350 {
351 68333 ▶ while (count-- > 0 && n < target) lengths[n++] = val;
Branch: 2 branches
BranchTrueFalseTotal
while (count-- > 0 && n < target) lengths[n++] = val; 64092 4241 68333
while (count-- > 0 && n < target) lengths[n++] = val; 64092 0 64092
352 4241 return n;
353 }
354  
355 /* btype=0: uncompressed stored block. Aligns to the next byte boundary,
356 * reads <len, ~len> and copies len bytes verbatim into the output. */
357 static pdfmake_err_t
358 3 inflate_stored_block(bitreader_t *br, pdfmake_buf_t *out)
359 {
360 uint16_t len;
361 uint16_t nlen;
362 3 br->bits = 0;
363 3 br->nbits = 0; /* Align to byte boundary */
364  
365 3 ▶ if (br->pos + 4 > br->len) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (br->pos + 4 > br->len) return PDFMAKE_ECORRUPT; 0 3 3
366 3 len = br->data[br->pos] | ((uint16_t)br->data[br->pos + 1] << 8);
367 3 nlen = br->data[br->pos + 2] | ((uint16_t)br->data[br->pos + 3] << 8);
368 3 br->pos += 4;
369  
370 3 ▶ if (len != (uint16_t)~nlen) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (len != (uint16_t)~nlen) return PDFMAKE_ECORRUPT; 0 3 3
371 3 ▶ if (br->pos + len > br->len) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (br->pos + len > br->len) return PDFMAKE_ECORRUPT; 0 3 3
372  
373 3 ▶ if (pdfmake_buf_append(out, br->data + br->pos, len) != PDFMAKE_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, br->data + br->pos, len) != PDFMAKE_OK) 0 3 3
374 0 return PDFMAKE_ENOMEM;
375 3 br->pos += len;
376 3 return PDFMAKE_OK;
377 }
378  
379 /* btype=2: dynamic Huffman block. Decodes the code-length alphabet first,
380 * then uses it to decode the literal/length and distance code tables,
381 * then inflates the block body with those tables. */
382 static pdfmake_err_t
383 557 inflate_dynamic_block(bitreader_t *br, pdfmake_buf_t *out)
384 {
385 int hlit;
386 int hdist;
387 int hclen;
388 uint8_t cl_lengths[19];
389 int i;
390 huffman_t cl_huff;
391 uint8_t lengths[288 + 32];
392 int n;
393 int sym;
394 int extra;
395 huffman_t dyn_lit_len, dyn_dist;
396 557 hlit = bitreader_read(br, 5);
397 557 hdist = bitreader_read(br, 5);
398 557 hclen = bitreader_read(br, 4);
399 557 ▶ if (hlit < 0 || hdist < 0 || hclen < 0) return PDFMAKE_ECORRUPT;
Branch: 3 branches
BranchTrueFalseTotal
if (hlit < 0 || hdist < 0 || hclen < 0) return PDFMAKE_ECORRUPT; 557 0 557
if (hlit < 0 || hdist < 0 || hclen < 0) return PDFMAKE_ECORRUPT; 557 0 557
if (hlit < 0 || hdist < 0 || hclen < 0) return PDFMAKE_ECORRUPT; 0 557 557
400 557 hlit += 257;
401 557 hdist += 1;
402 557 hclen += 4;
403  
404 /* Code-length alphabet (19 symbols, stored in cl_order[]). */
405 557 memset(cl_lengths, 0, sizeof(cl_lengths));
406 8637 ▶ for (i = 0; i < hclen; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < hclen; i++) { 8080 557 8637
407 8080 int code_len = bitreader_read(br, 3);
408 8080 ▶ if (code_len < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (code_len < 0) return PDFMAKE_ECORRUPT; 0 8080 8080
409 8080 cl_lengths[cl_order[i]] = (uint8_t)code_len;
410 }
411  
412 557 ▶ if (huffman_build(&cl_huff, cl_lengths, 19) < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (huffman_build(&cl_huff, cl_lengths, 19) < 0) return PDFMAKE_ECORRUPT; 0 557 557
413  
414 /* Literal/length + distance code lengths. */
415 557 n = 0;
416 108973 ▶ while (n < hlit + hdist) {
Branch: 1 branch
BranchTrueFalseTotal
while (n < hlit + hdist) { 108416 557 108973
417 108416 sym = huffman_decode(&cl_huff, br);
418 108416 ▶ if (sym < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (sym < 0) return PDFMAKE_ECORRUPT; 0 108416 108416
419  
420 108416 ▶ if (sym < 16) {
Branch: 1 branch
BranchTrueFalseTotal
if (sym < 16) { 104175 4241 108416
421 104175 lengths[n++] = (uint8_t)sym;
422 4241 ▶ } else if (sym == 16) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sym == 16) { 2507 1734 4241
423 2507 extra = bitreader_read(br, 2);
424 2507 ▶ if (extra < 0 || n == 0) return PDFMAKE_ECORRUPT;
Branch: 2 branches
BranchTrueFalseTotal
if (extra < 0 || n == 0) return PDFMAKE_ECORRUPT; 2507 0 2507
if (extra < 0 || n == 0) return PDFMAKE_ECORRUPT; 0 2507 2507
425 2507 n = deflate_fill_run(lengths, n, hlit + hdist,
426 2507 extra + 3, lengths[n - 1]);
427 1734 ▶ } else if (sym == 17) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sym == 17) { 1226 508 1734
428 1226 extra = bitreader_read(br, 3);
429 1226 ▶ if (extra < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (extra < 0) return PDFMAKE_ECORRUPT; 0 1226 1226
430 1226 n = deflate_fill_run(lengths, n, hlit + hdist, extra + 3, 0);
431 508 ▶ } else if (sym == 18) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sym == 18) { 508 0 508
432 508 extra = bitreader_read(br, 7);
433 508 ▶ if (extra < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (extra < 0) return PDFMAKE_ECORRUPT; 0 508 508
434 508 n = deflate_fill_run(lengths, n, hlit + hdist, extra + 11, 0);
435 }
436 }
437  
438 557 ▶ if (huffman_build(&dyn_lit_len, lengths, hlit) < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (huffman_build(&dyn_lit_len, lengths, hlit) < 0) return PDFMAKE_ECORRUPT; 0 557 557
439 557 ▶ if (huffman_build(&dyn_dist, lengths + hlit, hdist) < 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (huffman_build(&dyn_dist, lengths + hlit, hdist) < 0) return PDFMAKE_ECORRUPT; 0 557 557
440  
441 557 return inflate_block(br, &dyn_lit_len, &dyn_dist, out);
442 }
443  
444 162 pdfmake_err_t pdfmake_deflate_decode(const uint8_t *in, size_t in_len,
445 pdfmake_buf_t *out)
446 {
447 bitreader_t br;
448 int bfinal;
449 int btype;
450 pdfmake_err_t err;
451 162 ▶ if (!in || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !out) return PDFMAKE_EINVAL; 162 0 162
if (!in || !out) return PDFMAKE_EINVAL; 0 162 162
452  
453 162 bitreader_init(&br, in, in_len);
454 162 build_fixed_tables();
455  
456 do {
457 755 bfinal = bitreader_read(&br, 1);
458 755 btype = bitreader_read(&br, 2);
459 755 ▶ if (bfinal < 0 || btype < 0) return PDFMAKE_ECORRUPT;
Branch: 2 branches
BranchTrueFalseTotal
if (bfinal < 0 || btype < 0) return PDFMAKE_ECORRUPT; 754 1 755
if (bfinal < 0 || btype < 0) return PDFMAKE_ECORRUPT; 0 754 754
460  
461 754 switch (btype) {
462 3 case 0: err = inflate_stored_block(&br, out); break;
463 194 case 1: err = inflate_block(&br, &fixed_lit_len, &fixed_dist, out); break;
464 557 case 2: err = inflate_dynamic_block(&br, out); break;
465 0 default: return PDFMAKE_ECORRUPT; /* btype == 3 is reserved */
466 }
467 754 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 754 754
468 754 ▶ } while (!bfinal);
Branch: 1 branch
BranchTrueFalseTotal
} while (!bfinal); 593 161 754
469  
470 161 return PDFMAKE_OK;
471 }
472  
473 /*============================================================================
474 * DEFLATE encoder (store-only for now, then static Huffman)
475 *==========================================================================*/
476  
477 typedef struct {
478 pdfmake_buf_t *out;
479 uint32_t bits;
480 int nbits;
481 } bitwriter_t;
482  
483 43 static void bitwriter_init(bitwriter_t *bw, pdfmake_buf_t *out)
484 {
485 43 bw->out = out;
486 43 bw->bits = 0;
487 43 bw->nbits = 0;
488 43 }
489  
490 889011 static pdfmake_err_t bitwriter_write(bitwriter_t *bw, int val, int n)
491 {
492 889011 bw->bits |= (uint32_t)val << bw->nbits;
493 889011 bw->nbits += n;
494  
495 1715120 ▶ while (bw->nbits >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
while (bw->nbits >= 8) { 826109 889011 1715120
496 826109 ▶ if (pdfmake_buf_append_byte(bw->out, bw->bits & 0xFF) != PDFMAKE_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(bw->out, bw->bits & 0xFF) != PDFMAKE_OK) 0 826109 826109
497 0 return PDFMAKE_ENOMEM;
498 826109 bw->bits >>= 8;
499 826109 bw->nbits -= 8;
500 }
501  
502 889011 return PDFMAKE_OK;
503 }
504  
505 43 static pdfmake_err_t bitwriter_flush(bitwriter_t *bw)
506 {
507 43 ▶ if (bw->nbits > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bw->nbits > 0) { 27 16 43
508 27 ▶ if (pdfmake_buf_append_byte(bw->out, bw->bits & 0xFF) != PDFMAKE_OK)
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(bw->out, bw->bits & 0xFF) != PDFMAKE_OK) 0 27 27
509 0 return PDFMAKE_ENOMEM;
510 27 bw->bits = 0;
511 27 bw->nbits = 0;
512 }
513 43 return PDFMAKE_OK;
514 }
515  
516 /* Reverse bits for Huffman encoding */
517 3180 static uint16_t reverse_bits(uint16_t val, int n)
518 {
519 3180 uint16_t rev = 0;
520 int i;
521 28600 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 25420 3180 28600
522 25420 rev = (rev << 1) | (val & 1);
523 25420 val >>= 1;
524 }
525 3180 return rev;
526 }
527  
528 /* Fixed Huffman code tables for encoding */
529 static uint16_t fixed_lit_codes[288];
530 static uint8_t fixed_lit_bits[288];
531 static uint16_t fixed_dist_codes[30];
532 static uint8_t fixed_dist_bits[30];
533 static int fixed_encode_tables_built = 0;
534  
535 43 static void build_fixed_encode_tables(void)
536 {
537 int i;
538 uint16_t code;
539 43 ▶ if (fixed_encode_tables_built) return;
Branch: 1 branch
BranchTrueFalseTotal
if (fixed_encode_tables_built) return; 33 10 43
540  
541 /* Literal/length codes */
542 10 code = 0;
543  
544 /* 256-279: 7 bits starting at 0000000 */
545 250 ▶ for (i = 256; i <= 279; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 256; i <= 279; i++) { 240 10 250
546 240 fixed_lit_codes[i] = reverse_bits(code++, 7);
547 240 fixed_lit_bits[i] = 7;
548 }
549  
550 /* 0-143: 8 bits starting at 00110000 */
551 10 code = 0x30;
552 1450 ▶ for (i = 0; i <= 143; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= 143; i++) { 1440 10 1450
553 1440 fixed_lit_codes[i] = reverse_bits(code++, 8);
554 1440 fixed_lit_bits[i] = 8;
555 }
556  
557 /* 280-287: 8 bits starting at 11000000 */
558 10 code = 0xC0;
559 90 ▶ for (i = 280; i <= 287; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 280; i <= 287; i++) { 80 10 90
560 80 fixed_lit_codes[i] = reverse_bits(code++, 8);
561 80 fixed_lit_bits[i] = 8;
562 }
563  
564 /* 144-255: 9 bits starting at 110010000 */
565 10 code = 0x190;
566 1130 ▶ for (i = 144; i <= 255; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 144; i <= 255; i++) { 1120 10 1130
567 1120 fixed_lit_codes[i] = reverse_bits(code++, 9);
568 1120 fixed_lit_bits[i] = 9;
569 }
570  
571 /* Distance codes: all 5 bits starting at 00000 */
572 310 ▶ for (i = 0; i < 30; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 30; i++) { 300 10 310
573 300 fixed_dist_codes[i] = reverse_bits(i, 5);
574 300 fixed_dist_bits[i] = 5;
575 }
576  
577 10 fixed_encode_tables_built = 1;
578 }
579  
580 /* Find length code for a given length (3-258) */
581 260581 static int find_length_code(int length)
582 {
583 int i;
584 713883 ▶ for (i = 0; i < 29; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 29; i++) { 713883 0 713883
585 713883 int max_len = length_base[i] + (1 << length_extra[i]) - 1;
586 713883 ▶ if (length <= max_len) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (length <= max_len) return i; 260581 453302 713883
587 }
588 0 return 28; /* Max length */
589 }
590  
591 /* Find distance code for a given distance (1-32768) */
592 260581 static int find_dist_code(int dist)
593 {
594 int i;
595 6088941 ▶ for (i = 0; i < 30; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 30; i++) { 6088941 0 6088941
596 6088941 int max_dist = dist_base[i] + (1 << dist_extra[i]) - 1;
597 6088941 ▶ if (dist <= max_dist) return i;
Branch: 1 branch
BranchTrueFalseTotal
if (dist <= max_dist) return i; 260581 5828360 6088941
598 }
599 0 return 29; /* Max distance */
600 }
601  
602 /*----------------------------------------------------------------------------
603 * LZ77 hash chain implementation
604 *--------------------------------------------------------------------------*/
605  
606 #define HASH_BITS 15
607 #define HASH_SIZE (1 << HASH_BITS)
608 #define HASH_MASK (HASH_SIZE - 1)
609 #define WINDOW_SIZE 32768
610 #define MIN_MATCH 3
611 #define MAX_MATCH 258
612 #define MAX_CHAIN 128 /* Max hash chain length to search */
613  
614 typedef struct {
615 const uint8_t *data;
616 size_t len;
617 int head[HASH_SIZE];
618 int prev[WINDOW_SIZE];
619 size_t pos;
620 } lz77_t;
621  
622 41 static void lz77_init(lz77_t *lz, const uint8_t *data, size_t len)
623 {
624 41 lz->data = data;
625 41 lz->len = len;
626 41 lz->pos = 0;
627 41 memset(lz->head, -1, sizeof(lz->head));
628 41 memset(lz->prev, -1, sizeof(lz->prev));
629 41 }
630  
631 2219691 static int lz77_hash(const uint8_t *data)
632 {
633 2219691 return ((data[0] << 10) ^ (data[1] << 5) ^ data[2]) & HASH_MASK;
634 }
635  
636 1859922 static void lz77_insert(lz77_t *lz, size_t pos)
637 {
638 int h;
639 1859922 ▶ if (pos + 2 >= lz->len) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pos + 2 >= lz->len) return; 81 1859841 1859922
640  
641 1859841 h = lz77_hash(lz->data + pos);
642 1859841 lz->prev[pos & (WINDOW_SIZE - 1)] = lz->head[h];
643 1859841 lz->head[h] = (int)pos;
644 }
645  
646 359877 static int lz77_find_match(lz77_t *lz, size_t pos, int *match_dist, int *match_len)
647 {
648 int h;
649 int chain_len;
650 int match_pos;
651 int dist;
652 const uint8_t *a;
653 const uint8_t *b;
654 int len;
655 size_t max_len;
656 359877 *match_len = 0;
657 359877 *match_dist = 0;
658  
659 359877 ▶ if (pos + MIN_MATCH > lz->len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pos + MIN_MATCH > lz->len) return 0; 27 359850 359877
660  
661 359850 h = lz77_hash(lz->data + pos);
662 359850 chain_len = 0;
663 359850 match_pos = lz->head[h];
664  
665 9020569 ▶ while (match_pos >= 0 && chain_len < MAX_CHAIN) {
Branch: 2 branches
BranchTrueFalseTotal
while (match_pos >= 0 && chain_len < MAX_CHAIN) { 8840067 180502 9020569
while (match_pos >= 0 && chain_len < MAX_CHAIN) { 8804918 35149 8840067
666 8804918 dist = (int)pos - match_pos;
667 8804918 ▶ if (dist > WINDOW_SIZE) break;
Branch: 1 branch
BranchTrueFalseTotal
if (dist > WINDOW_SIZE) break; 143505 8661413 8804918
668 8661413 ▶ if (dist <= 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (dist <= 0) break; 0 8661413 8661413
669  
670 /* Check match */
671 8661413 a = lz->data + pos;
672 8661413 b = lz->data + match_pos;
673 8661413 len = 0;
674  
675 8661413 max_len = lz->len - pos;
676 8661413 ▶ if (max_len > MAX_MATCH) max_len = MAX_MATCH;
Branch: 1 branch
BranchTrueFalseTotal
if (max_len > MAX_MATCH) max_len = MAX_MATCH; 8655947 5466 8661413
677  
678 53453904 ▶ while ((size_t)len < max_len && a[len] == b[len]) len++;
Branch: 2 branches
BranchTrueFalseTotal
while ((size_t)len < max_len && a[len] == b[len]) len++; 53450885 3019 53453904
while ((size_t)len < max_len && a[len] == b[len]) len++; 44792491 8658394 53450885
679  
680 8661413 ▶ if (len >= MIN_MATCH && len > *match_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (len >= MIN_MATCH && len > *match_len) { 8653107 8306 8661413
if (len >= MIN_MATCH && len > *match_len) { 443890 8209217 8653107
681 443890 *match_len = len;
682 443890 *match_dist = dist;
683 443890 ▶ if (len >= MAX_MATCH) break;
Branch: 1 branch
BranchTrueFalseTotal
if (len >= MAX_MATCH) break; 694 443196 443890
684 }
685  
686 8660719 match_pos = lz->prev[match_pos & (WINDOW_SIZE - 1)];
687 8660719 chain_len++;
688 }
689  
690 359850 return *match_len >= MIN_MATCH;
691 }
692  
693 /*----------------------------------------------------------------------------
694 * DEFLATE encoder with LZ77
695 *--------------------------------------------------------------------------*/
696  
697 44 pdfmake_err_t pdfmake_deflate_encode(const uint8_t *in, size_t in_len,
698 int level,
699 pdfmake_buf_t *out)
700 {
701 bitwriter_t bw;
702 size_t pos;
703 44 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 44 44
704 44 ▶ if (in_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_len == 0) { 1 43 44
705 /* Empty input: single empty stored block */
706 1 uint8_t empty[] = {0x01, 0x00, 0x00, 0xFF, 0xFF};
707 1 return pdfmake_buf_append(out, empty, 5);
708 }
709  
710 43 build_fixed_encode_tables();
711  
712 43 bitwriter_init(&bw, out);
713  
714 43 ▶ if (level == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == 0) { 2 41 43
715 uint16_t len;
716 uint16_t nlen;
717 int bfinal;
718 size_t block_len;
719 /* Store-only: emit stored blocks */
720 2 pos = 0;
721 4 ▶ while (pos < in_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < in_len) { 2 2 4
722 2 block_len = in_len - pos;
723 2 ▶ if (block_len > 65535) block_len = 65535;
Branch: 1 branch
BranchTrueFalseTotal
if (block_len > 65535) block_len = 65535; 0 2 2
724  
725 2 bfinal = (pos + block_len >= in_len) ? 1 : 0;
726 2 bitwriter_write(&bw, bfinal, 1);
727 2 bitwriter_write(&bw, 0, 2); /* btype = 0 (stored) */
728 2 bitwriter_flush(&bw);
729  
730 2 len = (uint16_t)block_len;
731 2 nlen = ~len;
732 2 pdfmake_buf_append_byte(out, len & 0xFF);
733 2 pdfmake_buf_append_byte(out, (len >> 8) & 0xFF);
734 2 pdfmake_buf_append_byte(out, nlen & 0xFF);
735 2 pdfmake_buf_append_byte(out, (nlen >> 8) & 0xFF);
736 2 pdfmake_buf_append(out, in + pos, block_len);
737  
738 2 pos += block_len;
739 }
740 }
741 else {
742 lz77_t lz;
743 /* LZ77 + fixed Huffman (level 1-9 all use this for now) */
744 int match_dist, match_len;
745 int len_code;
746 int dist_code;
747 int extra;
748 int i;
749 41 lz77_init(&lz, in, in_len);
750  
751 /* Single block with fixed Huffman */
752 41 bitwriter_write(&bw, 1, 1); /* bfinal = 1 */
753 41 bitwriter_write(&bw, 1, 2); /* btype = 1 (fixed Huffman) */
754  
755 41 pos = 0;
756 359918 ▶ while (pos < in_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < in_len) { 359877 41 359918
757 359877 ▶ if (lz77_find_match(&lz, pos, &match_dist, &match_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (lz77_find_match(&lz, pos, &match_dist, &match_len)) { 260581 99296 359877
758 /* Emit length/distance pair */
759 260581 len_code = find_length_code(match_len);
760 260581 bitwriter_write(&bw, fixed_lit_codes[257 + len_code],
761 260581 fixed_lit_bits[257 + len_code]);
762 260581 ▶ if (length_extra[len_code] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length_extra[len_code] > 0) { 11951 248630 260581
763 11951 extra = match_len - length_base[len_code];
764 11951 bitwriter_write(&bw, extra, length_extra[len_code]);
765 }
766  
767 260581 dist_code = find_dist_code(match_dist);
768 260581 bitwriter_write(&bw, fixed_dist_codes[dist_code],
769 260581 fixed_dist_bits[dist_code]);
770 260581 ▶ if (dist_extra[dist_code] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (dist_extra[dist_code] > 0) { 256475 4106 260581
771 256475 extra = match_dist - dist_base[dist_code];
772 256475 bitwriter_write(&bw, extra, dist_extra[dist_code]);
773 }
774  
775 /* Insert all positions in the match into hash */
776 2021207 ▶ for (i = 0; i < match_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < match_len; i++) { 1760626 260581 2021207
777 1760626 lz77_insert(&lz, pos + i);
778 }
779 260581 pos += match_len;
780 }
781 else {
782 /* Emit literal */
783 99296 bitwriter_write(&bw, fixed_lit_codes[in[pos]],
784 99296 fixed_lit_bits[in[pos]]);
785 99296 lz77_insert(&lz, pos);
786 99296 pos++;
787 }
788 }
789  
790 /* End of block */
791 41 bitwriter_write(&bw, fixed_lit_codes[256], fixed_lit_bits[256]);
792 41 bitwriter_flush(&bw);
793 }
794  
795 43 return PDFMAKE_OK;
796 }
797  
798 /*============================================================================
799 * zlib wrapper (RFC 1950)
800 *==========================================================================*/
801  
802 157 pdfmake_err_t pdfmake_flate_decode(const uint8_t *in, size_t in_len,
803 const pdfmake_flate_params_t *params,
804 pdfmake_buf_t *out)
805 {
806 uint8_t cmf;
807 uint8_t flg;
808 size_t deflate_len;
809 pdfmake_buf_t raw;
810 pdfmake_err_t err;
811 157 ▶ if (!in || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !out) return PDFMAKE_EINVAL; 157 0 157
if (!in || !out) return PDFMAKE_EINVAL; 0 157 157
812 157 ▶ if (in_len < 6) return PDFMAKE_ECORRUPT; /* Min: 2 header + 4 Adler-32 */
Branch: 1 branch
BranchTrueFalseTotal
if (in_len < 6) return PDFMAKE_ECORRUPT; /* Min: 2 header + 4 Adler-32 */ 0 157 157
813  
814 /* Parse zlib header (CMF + FLG) */
815 157 cmf = in[0];
816 157 flg = in[1];
817  
818 /* Check header checksum */
819 157 ▶ if ((cmf * 256 + flg) % 31 != 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if ((cmf * 256 + flg) % 31 != 0) return PDFMAKE_ECORRUPT; 0 157 157
820  
821 /* Check compression method (CM = 8 = deflate) */
822 157 ▶ if ((cmf & 0x0F) != 8) return PDFMAKE_EUNSUPPORTED;
Branch: 1 branch
BranchTrueFalseTotal
if ((cmf & 0x0F) != 8) return PDFMAKE_EUNSUPPORTED; 0 157 157
823  
824 /* Window size: CINFO = (cmf >> 4), window = 2^(CINFO+8) */
825 /* We support all window sizes */
826  
827 /* FDICT bit (preset dictionary): not supported */
828 157 ▶ if (flg & 0x20) return PDFMAKE_EUNSUPPORTED;
Branch: 1 branch
BranchTrueFalseTotal
if (flg & 0x20) return PDFMAKE_EUNSUPPORTED; 0 157 157
829  
830 /* Decompress DEFLATE data */
831 157 deflate_len = in_len - 6; /* Skip header (2) and trailer (4) */
832 157 ▶ if (pdfmake_buf_init(&raw) != PDFMAKE_OK) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&raw) != PDFMAKE_OK) return PDFMAKE_ENOMEM; 0 157 157
833  
834 157 err = pdfmake_deflate_decode(in + 2, deflate_len, &raw);
835 157 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 1 156 157
836 1 pdfmake_buf_free(&raw);
837 1 return err;
838 }
839  
840 /* Note: Adler-32 checksum verification is skipped.
841 * PDF stream /Length values don't always align with the zlib trailer
842 * position, and many real-world PDFs have mismatched checksums.
843 * The DEFLATE decoder's own consistency checks are sufficient. */
844  
845 /* Apply predictor if needed */
846 157 ▶ if (params && params->predictor > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (params && params->predictor > 1) { 156 0 156
if (params && params->predictor > 1) { 1 155 156
847 pdfmake_buf_t pred_out;
848 1 ▶ if (pdfmake_buf_init(&pred_out) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&pred_out) != PDFMAKE_OK) { 0 1 1
849 0 pdfmake_buf_free(&raw);
850 0 return PDFMAKE_ENOMEM;
851 }
852  
853 1 ▶ if (params->predictor == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->predictor == 2) { 0 1 1
854 0 err = pdfmake_tiff_predictor_decode(params->colors,
855 0 params->bits_per_comp,
856 0 params->columns,
857 0 raw.data, raw.len, &pred_out);
858 1 ▶ } else if (params->predictor >= 10 && params->predictor <= 15) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (params->predictor >= 10 && params->predictor <= 15) { 1 0 1
} else if (params->predictor >= 10 && params->predictor <= 15) { 1 0 1
859 1 err = pdfmake_predictor_decode(params->predictor,
860 1 params->colors,
861 1 params->bits_per_comp,
862 1 params->columns,
863 1 raw.data, raw.len, &pred_out);
864 } else {
865 0 err = PDFMAKE_EINVAL;
866 }
867  
868 1 pdfmake_buf_free(&raw);
869 1 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 0 1 1
870 0 pdfmake_buf_free(&pred_out);
871 0 return err;
872 }
873  
874 /* Move predictor output to main output */
875 1 ▶ if (pdfmake_buf_append(out, pred_out.data, pred_out.len) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, pred_out.data, pred_out.len) != PDFMAKE_OK) { 0 1 1
876 0 pdfmake_buf_free(&pred_out);
877 0 return PDFMAKE_ENOMEM;
878 }
879 1 pdfmake_buf_free(&pred_out);
880 }
881 else {
882 /* No predictor */
883 155 ▶ if (pdfmake_buf_append(out, raw.data, raw.len) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, raw.data, raw.len) != PDFMAKE_OK) { 0 155 155
884 0 pdfmake_buf_free(&raw);
885 0 return PDFMAKE_ENOMEM;
886 }
887 155 pdfmake_buf_free(&raw);
888 }
889  
890 156 return PDFMAKE_OK;
891 }
892  
893 39 pdfmake_err_t pdfmake_flate_encode(const uint8_t *in, size_t in_len,
894 const pdfmake_flate_params_t *params,
895 pdfmake_buf_t *out)
896 {
897 pdfmake_buf_t raw;
898 pdfmake_err_t err;
899 uint32_t adler;
900 39 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 39 39
901  
902 39 ▶ if (pdfmake_buf_init(&raw) != PDFMAKE_OK) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&raw) != PDFMAKE_OK) return PDFMAKE_ENOMEM; 0 39 39
903  
904 /* Apply predictor if needed */
905 39 ▶ if (params && params->predictor > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (params && params->predictor > 1) { 30 9 39
if (params && params->predictor > 1) { 15 15 30
906 15 ▶ if (params->predictor == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->predictor == 2) { 0 15 15
907 0 err = pdfmake_tiff_predictor_encode(params->colors,
908 0 params->bits_per_comp,
909 0 params->columns,
910 in, in_len, &raw);
911 15 ▶ } else if (params->predictor >= 10 && params->predictor <= 15) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (params->predictor >= 10 && params->predictor <= 15) { 15 0 15
} else if (params->predictor >= 10 && params->predictor <= 15) { 15 0 15
912 15 err = pdfmake_predictor_encode(params->predictor,
913 15 params->colors,
914 15 params->bits_per_comp,
915 15 params->columns,
916 in, in_len, &raw);
917 } else {
918 0 pdfmake_buf_free(&raw);
919 0 return PDFMAKE_EINVAL;
920 }
921  
922 15 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 0 15 15
923 0 pdfmake_buf_free(&raw);
924 0 return err;
925 }
926 }
927 else {
928 /* No predictor - copy input */
929 24 ▶ if (pdfmake_buf_append(&raw, in, in_len) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(&raw, in, in_len) != PDFMAKE_OK) { 0 24 24
930 0 pdfmake_buf_free(&raw);
931 0 return PDFMAKE_ENOMEM;
932 }
933 }
934  
935 /* zlib header: CMF=0x78 (deflate, 32K window), FLG=0x9C (level 6, no dict) */
936 /* For checksum: (0x78 * 256 + 0x9C) % 31 = 0 */
937 39 pdfmake_buf_append_byte(out, 0x78);
938 39 pdfmake_buf_append_byte(out, 0x9C);
939  
940 /* DEFLATE compress */
941 39 err = pdfmake_deflate_encode(raw.data, raw.len, 4, out); /* Level 4 */
942 39 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 0 39 39
943 0 pdfmake_buf_free(&raw);
944 0 return err;
945 }
946  
947 /* Adler-32 checksum (big-endian) */
948 39 adler = pdfmake_adler32(raw.data, raw.len);
949 39 pdfmake_buf_append_byte(out, (adler >> 24) & 0xFF);
950 39 pdfmake_buf_append_byte(out, (adler >> 16) & 0xFF);
951 39 pdfmake_buf_append_byte(out, (adler >> 8) & 0xFF);
952 39 pdfmake_buf_append_byte(out, adler & 0xFF);
953  
954 39 pdfmake_buf_free(&raw);
955 39 return PDFMAKE_OK;
956 }
957  
958 /*============================================================================
959 * PNG predictors (§7.4.4.4)
960 *==========================================================================*/
961  
962 /* Paeth predictor function */
963 1978428 static int paeth(int a, int b, int c)
964 {
965 1978428 int p = a + b - c;
966 1978428 int pa = abs(p - a);
967 1978428 int pb = abs(p - b);
968 1978428 int pc = abs(p - c);
969  
970 1978428 ▶ if (pa <= pb && pa <= pc) return a;
Branch: 2 branches
BranchTrueFalseTotal
if (pa <= pb && pa <= pc) return a; 1507812 470616 1978428
if (pa <= pb && pa <= pc) return a; 1478397 29415 1507812
971 500031 ▶ if (pb <= pc) return b;
Branch: 1 branch
BranchTrueFalseTotal
if (pb <= pc) return b; 459846 40185 500031
972 40185 return c;
973 }
974  
975 23 pdfmake_err_t pdfmake_predictor_decode(int predictor,
976 int colors, int bits_per_comp, int columns,
977 const uint8_t *in, size_t in_len,
978 pdfmake_buf_t *out)
979 {
980 int bpp;
981 int row_bytes;
982 int in_row_size;
983 size_t num_rows;
984 uint8_t *prev_row;
985 uint8_t *curr_row;
986 size_t r;
987 int i;
988 23 ▶ if (!in || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !out) return PDFMAKE_EINVAL; 23 0 23
if (!in || !out) return PDFMAKE_EINVAL; 0 23 23
989  
990 /* Calculate bytes per pixel (bpp) and row width */
991 23 bpp = (colors * bits_per_comp + 7) / 8;
992 23 row_bytes = (columns * colors * bits_per_comp + 7) / 8;
993  
994 /* For PNG predictors, each row has a filter type byte prefix */
995 23 in_row_size = row_bytes + 1;
996  
997 23 ▶ if (in_len % in_row_size != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_len % in_row_size != 0) { 0 23 23
998 /* If there's no filter byte, data might be raw (predictor=10, None) */
999 0 ▶ if (predictor == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (predictor == 10) { 0 0 0
1000 0 return pdfmake_buf_append(out, in, in_len);
1001 }
1002 0 return PDFMAKE_ECORRUPT;
1003 }
1004  
1005 23 num_rows = in_len / in_row_size;
1006  
1007 /* Allocate previous row buffer */
1008 23 prev_row = calloc(row_bytes, 1);
1009 23 ▶ if (!prev_row) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!prev_row) return PDFMAKE_ENOMEM; 0 23 23
1010  
1011 /* Allocate current row buffer */
1012 23 curr_row = malloc(row_bytes);
1013 23 ▶ if (!curr_row) {
Branch: 1 branch
BranchTrueFalseTotal
if (!curr_row) { 0 23 23
1014 0 free(prev_row);
1015 0 return PDFMAKE_ENOMEM;
1016 }
1017  
1018 5175 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 5152 23 5175
1019 5152 const uint8_t *row_in = in + r * in_row_size;
1020 5152 int filter_type = row_in[0];
1021 5152 const uint8_t *filtered = row_in + 1;
1022  
1023 /* Apply reverse filter */
1024 1785320 ▶ for (i = 0; i < row_bytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < row_bytes; i++) { 1780168 5152 1785320
1025 uint8_t raw;
1026 1780168 ▶ uint8_t a = (i >= bpp) ? curr_row[i - bpp] : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t a = (i >= bpp) ? curr_row[i - bpp] : 0; 1775016 5152 1780168
1027 1780168 uint8_t b = prev_row[i];
1028 1780168 ▶ uint8_t c = (i >= bpp) ? prev_row[i - bpp] : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t c = (i >= bpp) ? prev_row[i - bpp] : 0; 1775016 5152 1780168
1029  
1030 1780168 switch (filter_type) {
1031 1780048 case 0: /* None */
1032 1780048 raw = filtered[i];
1033 1780048 break;
1034 20 case 1: /* Sub */
1035 20 raw = filtered[i] + a;
1036 20 break;
1037 68 case 2: /* Up */
1038 68 raw = filtered[i] + b;
1039 68 break;
1040 16 case 3: /* Average */
1041 16 raw = filtered[i] + ((a + b) / 2);
1042 16 break;
1043 16 case 4: /* Paeth */
1044 16 raw = filtered[i] + paeth(a, b, c);
1045 16 break;
1046 0 default:
1047 0 free(prev_row);
1048 0 free(curr_row);
1049 0 return PDFMAKE_ECORRUPT;
1050 }
1051  
1052 1780168 curr_row[i] = raw;
1053 }
1054  
1055 /* Output row */
1056 5152 ▶ if (pdfmake_buf_append(out, curr_row, row_bytes) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, curr_row, row_bytes) != PDFMAKE_OK) { 0 5152 5152
1057 0 free(prev_row);
1058 0 free(curr_row);
1059 0 return PDFMAKE_ENOMEM;
1060 }
1061  
1062 /* Current row becomes previous row */
1063 5152 memcpy(prev_row, curr_row, row_bytes);
1064 }
1065  
1066 23 free(prev_row);
1067 23 free(curr_row);
1068 23 return PDFMAKE_OK;
1069 }
1070  
1071 23 pdfmake_err_t pdfmake_predictor_encode(int predictor,
1072 int colors, int bits_per_comp, int columns,
1073 const uint8_t *in, size_t in_len,
1074 pdfmake_buf_t *out)
1075 {
1076 int bpp;
1077 int row_bytes;
1078 size_t num_rows;
1079 uint8_t *prev_row;
1080 uint8_t *filtered;
1081 size_t r;
1082 int i;
1083 int f;
1084 23 ▶ if (!in || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !out) return PDFMAKE_EINVAL; 23 0 23
if (!in || !out) return PDFMAKE_EINVAL; 0 23 23
1085  
1086 23 bpp = (colors * bits_per_comp + 7) / 8;
1087 23 row_bytes = (columns * colors * bits_per_comp + 7) / 8;
1088  
1089 23 ▶ if (in_len % row_bytes != 0) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (in_len % row_bytes != 0) return PDFMAKE_EINVAL; 0 23 23
1090  
1091 23 num_rows = in_len / row_bytes;
1092  
1093 /* Allocate previous row buffer */
1094 23 prev_row = calloc(row_bytes, 1);
1095 23 ▶ if (!prev_row) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!prev_row) return PDFMAKE_ENOMEM; 0 23 23
1096  
1097 /* Allocate filtered row buffer */
1098 23 filtered = malloc(row_bytes);
1099 23 ▶ if (!filtered) {
Branch: 1 branch
BranchTrueFalseTotal
if (!filtered) { 0 23 23
1100 0 free(prev_row);
1101 0 return PDFMAKE_ENOMEM;
1102 }
1103  
1104 5175 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 5152 23 5175
1105 5152 const uint8_t *curr_row = in + r * row_bytes;
1106  
1107 int filter_type;
1108 5152 ▶ if (predictor == 15) {
Branch: 1 branch
BranchTrueFalseTotal
if (predictor == 15) { 5119 33 5152
1109 /* Optimum: try all filters and pick best (simple heuristic: sum of abs values) */
1110 5119 int best_filter = 0;
1111 5119 int best_sum = INT32_MAX;
1112  
1113 30714 ▶ for (f = 0; f <= 4; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f <= 4; f++) { 25595 5119 30714
1114 25595 int sum = 0;
1115 8925775 ▶ for (i = 0; i < row_bytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < row_bytes; i++) { 8900180 25595 8925775
1116 8900180 ▶ uint8_t a = (i >= bpp) ? curr_row[i - bpp] : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t a = (i >= bpp) ? curr_row[i - bpp] : 0; 8874585 25595 8900180
1117 8900180 uint8_t b = prev_row[i];
1118 8900180 ▶ uint8_t c = (i >= bpp) ? prev_row[i - bpp] : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t c = (i >= bpp) ? prev_row[i - bpp] : 0; 8874585 25595 8900180
1119 int val;
1120  
1121 8900180 switch (f) {
1122 1780036 case 0: val = curr_row[i]; break;
1123 1780036 case 1: val = curr_row[i] - a; break;
1124 1780036 case 2: val = curr_row[i] - b; break;
1125 1780036 case 3: val = curr_row[i] - ((a + b) / 2); break;
1126 1780036 case 4: val = curr_row[i] - paeth(a, b, c); break;
1127 0 default: val = 0;
1128 }
1129 8900180 sum += abs((int8_t)val);
1130 }
1131 25595 ▶ if (sum < best_sum) {
Branch: 1 branch
BranchTrueFalseTotal
if (sum < best_sum) { 17141 8454 25595
1132 17141 best_sum = sum;
1133 17141 best_filter = f;
1134 }
1135 }
1136 5119 filter_type = best_filter;
1137 }
1138 else {
1139 33 filter_type = predictor - 10; /* 10=None, 11=Sub, 12=Up, 13=Avg, 14=Paeth */
1140 33 ▶ if (filter_type < 0 || filter_type > 4) filter_type = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (filter_type < 0 || filter_type > 4) filter_type = 0; 30 3 33
if (filter_type < 0 || filter_type > 4) filter_type = 0; 0 30 30
1141 }
1142  
1143 /* Apply filter */
1144 1785320 ▶ for (i = 0; i < row_bytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < row_bytes; i++) { 1780168 5152 1785320
1145 1780168 ▶ uint8_t a = (i >= bpp) ? curr_row[i - bpp] : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t a = (i >= bpp) ? curr_row[i - bpp] : 0; 1775016 5152 1780168
1146 1780168 uint8_t b = prev_row[i];
1147 1780168 ▶ uint8_t c = (i >= bpp) ? prev_row[i - bpp] : 0;
Branch: 1 branch
BranchTrueFalseTotal
uint8_t c = (i >= bpp) ? prev_row[i - bpp] : 0; 1775016 5152 1780168
1148  
1149 1780168 switch (filter_type) {
1150 28 case 0: /* None */
1151 28 filtered[i] = curr_row[i];
1152 28 break;
1153 78320 case 1: /* Sub */
1154 78320 filtered[i] = curr_row[i] - a;
1155 78320 break;
1156 913568 case 2: /* Up */
1157 913568 filtered[i] = curr_row[i] - b;
1158 913568 break;
1159 589876 case 3: /* Average */
1160 589876 filtered[i] = curr_row[i] - ((a + b) / 2);
1161 589876 break;
1162 198376 case 4: /* Paeth */
1163 198376 filtered[i] = curr_row[i] - paeth(a, b, c);
1164 198376 break;
1165 }
1166 }
1167  
1168 /* Output filter type byte + filtered row */
1169 5152 pdfmake_buf_append_byte(out, (uint8_t)filter_type);
1170 5152 ▶ if (pdfmake_buf_append(out, filtered, row_bytes) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, filtered, row_bytes) != PDFMAKE_OK) { 0 5152 5152
1171 0 free(prev_row);
1172 0 free(filtered);
1173 0 return PDFMAKE_ENOMEM;
1174 }
1175  
1176 /* Current row becomes previous row */
1177 5152 memcpy(prev_row, curr_row, row_bytes);
1178 }
1179  
1180 23 free(prev_row);
1181 23 free(filtered);
1182 23 return PDFMAKE_OK;
1183 }
1184  
1185 /*============================================================================
1186 * TIFF predictor 2 (horizontal differencing)
1187 *==========================================================================*/
1188  
1189 1 pdfmake_err_t pdfmake_tiff_predictor_decode(int colors, int bits_per_comp, int columns,
1190 const uint8_t *in, size_t in_len,
1191 pdfmake_buf_t *out)
1192 {
1193 int row_bytes;
1194 size_t num_rows;
1195 uint8_t *row;
1196 size_t r;
1197 int c;
1198 int i;
1199 1 ▶ if (!in || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !out) return PDFMAKE_EINVAL; 1 0 1
if (!in || !out) return PDFMAKE_EINVAL; 0 1 1
1200  
1201 /* Only 8-bit samples supported for now */
1202 1 ▶ if (bits_per_comp != 8) return PDFMAKE_EUNSUPPORTED;
Branch: 1 branch
BranchTrueFalseTotal
if (bits_per_comp != 8) return PDFMAKE_EUNSUPPORTED; 0 1 1
1203  
1204 1 row_bytes = columns * colors;
1205 1 ▶ if (in_len % row_bytes != 0) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (in_len % row_bytes != 0) return PDFMAKE_EINVAL; 0 1 1
1206  
1207 1 num_rows = in_len / row_bytes;
1208  
1209 /* Allocate row buffer */
1210 1 row = malloc(row_bytes);
1211 1 ▶ if (!row) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return PDFMAKE_ENOMEM; 0 1 1
1212  
1213 2 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 1 1 2
1214 1 const uint8_t *row_in = in + r * row_bytes;
1215  
1216 /* First pixel is as-is */
1217 2 ▶ for (c = 0; c < colors; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < colors; c++) { 1 1 2
1218 1 row[c] = row_in[c];
1219 }
1220  
1221 /* Remaining pixels: add previous pixel */
1222 6 ▶ for (i = colors; i < row_bytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = colors; i < row_bytes; i++) { 5 1 6
1223 5 row[i] = row_in[i] + row[i - colors];
1224 }
1225  
1226 1 ▶ if (pdfmake_buf_append(out, row, row_bytes) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, row, row_bytes) != PDFMAKE_OK) { 0 1 1
1227 0 free(row);
1228 0 return PDFMAKE_ENOMEM;
1229 }
1230 }
1231  
1232 1 free(row);
1233 1 return PDFMAKE_OK;
1234 }
1235  
1236 1 pdfmake_err_t pdfmake_tiff_predictor_encode(int colors, int bits_per_comp, int columns,
1237 const uint8_t *in, size_t in_len,
1238 pdfmake_buf_t *out)
1239 {
1240 int row_bytes;
1241 size_t num_rows;
1242 uint8_t *row;
1243 size_t r;
1244 int c;
1245 int i;
1246 1 ▶ if (!in || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || !out) return PDFMAKE_EINVAL; 1 0 1
if (!in || !out) return PDFMAKE_EINVAL; 0 1 1
1247  
1248 /* Only 8-bit samples supported for now */
1249 1 ▶ if (bits_per_comp != 8) return PDFMAKE_EUNSUPPORTED;
Branch: 1 branch
BranchTrueFalseTotal
if (bits_per_comp != 8) return PDFMAKE_EUNSUPPORTED; 0 1 1
1250  
1251 1 row_bytes = columns * colors;
1252 1 ▶ if (in_len % row_bytes != 0) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (in_len % row_bytes != 0) return PDFMAKE_EINVAL; 0 1 1
1253  
1254 1 num_rows = in_len / row_bytes;
1255  
1256 /* Allocate row buffer */
1257 1 row = malloc(row_bytes);
1258 1 ▶ if (!row) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return PDFMAKE_ENOMEM; 0 1 1
1259  
1260 2 ▶ for (r = 0; r < num_rows; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < num_rows; r++) { 1 1 2
1261 1 const uint8_t *row_in = in + r * row_bytes;
1262  
1263 /* First pixel is as-is */
1264 2 ▶ for (c = 0; c < colors; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < colors; c++) { 1 1 2
1265 1 row[c] = row_in[c];
1266 }
1267  
1268 /* Remaining pixels: subtract previous pixel */
1269 6 ▶ for (i = colors; i < row_bytes; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = colors; i < row_bytes; i++) { 5 1 6
1270 5 row[i] = row_in[i] - row_in[i - colors];
1271 }
1272  
1273 1 ▶ if (pdfmake_buf_append(out, row, row_bytes) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, row, row_bytes) != PDFMAKE_OK) { 0 1 1
1274 0 free(row);
1275 0 return PDFMAKE_ENOMEM;
1276 }
1277 }
1278  
1279 1 free(row);
1280 1 return PDFMAKE_OK;
1281 }
1282  
1283 /*============================================================================
1284 * Filter dispatch
1285 *==========================================================================*/
1286  
1287 0 pdfmake_err_t pdfmake_filter_encode(const char *name,
1288 const uint8_t *in, size_t in_len,
1289 const pdfmake_obj_t *params,
1290 pdfmake_buf_t *out)
1291 {
1292 (void)params; /* Unused by most encoders */
1293  
1294 0 ▶ if (!name || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!name || !out) return PDFMAKE_EINVAL; 0 0 0
if (!name || !out) return PDFMAKE_EINVAL; 0 0 0
1295  
1296 0 ▶ if (strcmp(name, "FlateDecode") == 0 || strcmp(name, "Fl") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "FlateDecode") == 0 || strcmp(name, "Fl") == 0) { 0 0 0
if (strcmp(name, "FlateDecode") == 0 || strcmp(name, "Fl") == 0) { 0 0 0
1297 pdfmake_flate_params_t fp;
1298 0 pdfmake_flate_params_from_dict(&fp, params);
1299 0 return pdfmake_flate_encode(in, in_len, &fp, out);
1300 }
1301  
1302 0 ▶ if (strcmp(name, "ASCIIHexDecode") == 0 || strcmp(name, "AHx") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "ASCIIHexDecode") == 0 || strcmp(name, "AHx") == 0) { 0 0 0
if (strcmp(name, "ASCIIHexDecode") == 0 || strcmp(name, "AHx") == 0) { 0 0 0
1303 0 return pdfmake_asciihex_encode(in, in_len, out);
1304 }
1305  
1306 0 ▶ if (strcmp(name, "ASCII85Decode") == 0 || strcmp(name, "A85") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "ASCII85Decode") == 0 || strcmp(name, "A85") == 0) { 0 0 0
if (strcmp(name, "ASCII85Decode") == 0 || strcmp(name, "A85") == 0) { 0 0 0
1307 0 return pdfmake_ascii85_encode(in, in_len, out);
1308 }
1309  
1310 0 ▶ if (strcmp(name, "RunLengthDecode") == 0 || strcmp(name, "RL") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "RunLengthDecode") == 0 || strcmp(name, "RL") == 0) { 0 0 0
if (strcmp(name, "RunLengthDecode") == 0 || strcmp(name, "RL") == 0) { 0 0 0
1311 0 return pdfmake_rle_encode(in, in_len, out);
1312 }
1313  
1314 /* LZWDecode encoding not supported (patent concerns, use FlateDecode) */
1315  
1316 0 return PDFMAKE_EUNSUPPORTED;
1317 }
1318  
1319 static pdfmake_err_t filter_decode_in(pdfmake_arena_t *arena, const char *name,
1320 const uint8_t *in, size_t in_len,
1321 const pdfmake_obj_t *params,
1322 pdfmake_buf_t *out);
1323  
1324 0 pdfmake_err_t pdfmake_filter_decode(const char *name,
1325 const uint8_t *in, size_t in_len,
1326 const pdfmake_obj_t *params,
1327 pdfmake_buf_t *out)
1328 {
1329 0 return filter_decode_in(NULL, name, in, in_len, params, out);
1330 }
1331  
1332 /* Decode one filter. The arena is what makes /DecodeParms readable: without
1333 * it the keys cannot be looked up and the defaults apply. */
1334 135 static pdfmake_err_t filter_decode_in(pdfmake_arena_t *arena, const char *name,
1335 const uint8_t *in, size_t in_len,
1336 const pdfmake_obj_t *params,
1337 pdfmake_buf_t *out)
1338 {
1339 135 ▶ if (!name || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!name || !out) return PDFMAKE_EINVAL; 135 0 135
if (!name || !out) return PDFMAKE_EINVAL; 0 135 135
1340  
1341 135 ▶ if (strcmp(name, "FlateDecode") == 0 || strcmp(name, "Fl") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "FlateDecode") == 0 || strcmp(name, "Fl") == 0) { 1 134 135
if (strcmp(name, "FlateDecode") == 0 || strcmp(name, "Fl") == 0) { 0 1 1
1342 pdfmake_flate_params_t fp;
1343 134 pdfmake_flate_params_from_dict_arena(&fp, arena, params);
1344 134 return pdfmake_flate_decode(in, in_len, &fp, out);
1345 }
1346  
1347 /* The security handler has already decrypted the bytes. */
1348 1 ▶ if (strcmp(name, "Crypt") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "Crypt") == 0) { 0 1 1
1349 0 return pdfmake_buf_append(out, in, in_len);
1350 }
1351  
1352 1 ▶ if (strcmp(name, "ASCIIHexDecode") == 0 || strcmp(name, "AHx") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "ASCIIHexDecode") == 0 || strcmp(name, "AHx") == 0) { 0 1 1
if (strcmp(name, "ASCIIHexDecode") == 0 || strcmp(name, "AHx") == 0) { 0 0 0
1353 1 return pdfmake_asciihex_decode(in, in_len, out);
1354 }
1355  
1356 0 ▶ if (strcmp(name, "ASCII85Decode") == 0 || strcmp(name, "A85") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "ASCII85Decode") == 0 || strcmp(name, "A85") == 0) { 0 0 0
if (strcmp(name, "ASCII85Decode") == 0 || strcmp(name, "A85") == 0) { 0 0 0
1357 0 return pdfmake_ascii85_decode(in, in_len, out);
1358 }
1359  
1360 0 ▶ if (strcmp(name, "LZWDecode") == 0 || strcmp(name, "LZW") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "LZWDecode") == 0 || strcmp(name, "LZW") == 0) { 0 0 0
if (strcmp(name, "LZWDecode") == 0 || strcmp(name, "LZW") == 0) { 0 0 0
1361 pdfmake_flate_params_t fp;
1362 0 pdfmake_flate_params_from_dict_arena(&fp, arena, params);
1363 0 return pdfmake_lzw_decode(in, in_len, &fp, out);
1364 }
1365  
1366 0 ▶ if (strcmp(name, "RunLengthDecode") == 0 || strcmp(name, "RL") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "RunLengthDecode") == 0 || strcmp(name, "RL") == 0) { 0 0 0
if (strcmp(name, "RunLengthDecode") == 0 || strcmp(name, "RL") == 0) { 0 0 0
1367 0 return pdfmake_rle_decode(in, in_len, out);
1368 }
1369  
1370 /* Stub filters */
1371 0 ▶ if (strcmp(name, "CCITTFaxDecode") == 0 || strcmp(name, "CCF") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "CCITTFaxDecode") == 0 || strcmp(name, "CCF") == 0) { 0 0 0
if (strcmp(name, "CCITTFaxDecode") == 0 || strcmp(name, "CCF") == 0) { 0 0 0
1372 0 return pdfmake_ccitt_decode(in, in_len, params, out);
1373 }
1374 0 ▶ if (strcmp(name, "JBIG2Decode") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "JBIG2Decode") == 0) { 0 0 0
1375 0 return pdfmake_jbig2_decode(in, in_len, params, out);
1376 }
1377 0 ▶ if (strcmp(name, "JPXDecode") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(name, "JPXDecode") == 0) { 0 0 0
1378 0 return pdfmake_jpx_decode(in, in_len, params, out);
1379 }
1380  
1381 0 return PDFMAKE_EUNSUPPORTED;
1382 }
1383  
1384 134 pdfmake_err_t pdfmake_filter_chain_decode(pdfmake_arena_t *arena,
1385 const pdfmake_obj_t *filters,
1386 const pdfmake_obj_t *params,
1387 const uint8_t *in, size_t in_len,
1388 pdfmake_buf_t *out)
1389 {
1390 size_t n;
1391 pdfmake_buf_t tmp1, tmp2;
1392 const uint8_t *current_in;
1393 size_t current_len;
1394 pdfmake_buf_t *current_out;
1395 size_t i;
1396 pdfmake_err_t err;
1397 134 ▶ if (!arena || !filters || !out) return PDFMAKE_EINVAL;
Branch: 3 branches
BranchTrueFalseTotal
if (!arena || !filters || !out) return PDFMAKE_EINVAL; 134 0 134
if (!arena || !filters || !out) return PDFMAKE_EINVAL; 134 0 134
if (!arena || !filters || !out) return PDFMAKE_EINVAL; 0 134 134
1398  
1399 /* Single filter (name) */
1400 134 ▶ if (filters->kind == PDFMAKE_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (filters->kind == PDFMAKE_NAME) { 133 1 134
1401 133 const char *name = (const char *)pdfmake_arena_name_bytes(
1402 133 arena, filters->as.name.id);
1403 133 ▶ if (params && params->kind == PDFMAKE_ARRAY)
Branch: 2 branches
BranchTrueFalseTotal
if (params && params->kind == PDFMAKE_ARRAY) 1 132 133
if (params && params->kind == PDFMAKE_ARRAY) 0 1 1
1404 0 params = pdfmake_array_get((pdfmake_obj_t *)params, 0);
1405 133 return filter_decode_in(arena, name, in, in_len, params, out);
1406 }
1407  
1408 /* Array of filters */
1409 1 ▶ if (filters->kind != PDFMAKE_ARRAY) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (filters->kind != PDFMAKE_ARRAY) return PDFMAKE_EINVAL; 0 1 1
1410  
1411 1 n = pdfmake_array_len((pdfmake_obj_t *)filters);
1412 1 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 1 1
1413 0 return pdfmake_buf_append(out, in, in_len);
1414 }
1415  
1416 /* §7.4.1: the array lists the filters in the order they decode. */
1417 1 ▶ if (pdfmake_buf_init(&tmp1) != PDFMAKE_OK) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&tmp1) != PDFMAKE_OK) return PDFMAKE_ENOMEM; 0 1 1
1418 1 ▶ if (pdfmake_buf_init(&tmp2) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&tmp2) != PDFMAKE_OK) { 0 1 1
1419 0 pdfmake_buf_free(&tmp1);
1420 0 return PDFMAKE_ENOMEM;
1421 }
1422  
1423 1 current_in = in;
1424 1 current_len = in_len;
1425 1 current_out = &tmp1;
1426  
1427 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
1428 const char *name;
1429 const pdfmake_obj_t *filter_params;
1430 2 pdfmake_obj_t *filter = pdfmake_array_get((pdfmake_obj_t *)filters, i);
1431 2 ▶ if (!filter || filter->kind != PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (!filter || filter->kind != PDFMAKE_NAME) { 2 0 2
if (!filter || filter->kind != PDFMAKE_NAME) { 0 2 2
1432 0 pdfmake_buf_free(&tmp1);
1433 0 pdfmake_buf_free(&tmp2);
1434 0 return PDFMAKE_EINVAL;
1435 }
1436  
1437 2 name = (const char *)pdfmake_arena_name_bytes(
1438 arena, filter->as.name.id);
1439  
1440 /* Get params for this filter if array */
1441 2 filter_params = NULL;
1442 2 ▶ if (params && params->kind == PDFMAKE_ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (params && params->kind == PDFMAKE_ARRAY) { 0 2 2
if (params && params->kind == PDFMAKE_ARRAY) { 0 0 0
1443 0 filter_params = pdfmake_array_get((pdfmake_obj_t *)params, i);
1444 2 ▶ } else if (params && n == 1) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (params && n == 1) { 0 2 2
} else if (params && n == 1) { 0 0 0
1445 0 filter_params = params;
1446 }
1447  
1448 2 pdfmake_buf_clear(current_out);
1449 2 err = filter_decode_in(arena, name, current_in, current_len,
1450 filter_params, current_out);
1451 2 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 0 2 2
1452 0 pdfmake_buf_free(&tmp1);
1453 0 pdfmake_buf_free(&tmp2);
1454 0 return err;
1455 }
1456  
1457 /* Swap buffers for next iteration */
1458 2 current_in = current_out->data;
1459 2 current_len = current_out->len;
1460 2 ▶ current_out = (current_out == &tmp1) ? &tmp2 : &tmp1;
Branch: 1 branch
BranchTrueFalseTotal
current_out = (current_out == &tmp1) ? &tmp2 : &tmp1; 1 1 2
1461 }
1462  
1463 /* Copy final result to output */
1464 1 err = pdfmake_buf_append(out, current_in, current_len);
1465  
1466 1 pdfmake_buf_free(&tmp1);
1467 1 pdfmake_buf_free(&tmp2);
1468 1 return err;
1469 }
1470  
1471 0 pdfmake_err_t pdfmake_filter_chain_encode(pdfmake_arena_t *arena,
1472 const pdfmake_obj_t *filters,
1473 const pdfmake_obj_t *params,
1474 const uint8_t *in, size_t in_len,
1475 pdfmake_buf_t *out)
1476 {
1477 size_t n;
1478 pdfmake_buf_t tmp1, tmp2;
1479 const uint8_t *current_in;
1480 size_t current_len;
1481 pdfmake_buf_t *current_out;
1482 size_t i;
1483 pdfmake_err_t err;
1484 0 ▶ if (!arena || !filters || !out) return PDFMAKE_EINVAL;
Branch: 3 branches
BranchTrueFalseTotal
if (!arena || !filters || !out) return PDFMAKE_EINVAL; 0 0 0
if (!arena || !filters || !out) return PDFMAKE_EINVAL; 0 0 0
if (!arena || !filters || !out) return PDFMAKE_EINVAL; 0 0 0
1485  
1486 /* Single filter (name) */
1487 0 ▶ if (filters->kind == PDFMAKE_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (filters->kind == PDFMAKE_NAME) { 0 0 0
1488 0 const char *name = (const char *)pdfmake_arena_name_bytes(
1489 0 arena, filters->as.name.id);
1490 0 return pdfmake_filter_encode(name, in, in_len, params, out);
1491 }
1492  
1493 /* Array of filters */
1494 0 ▶ if (filters->kind != PDFMAKE_ARRAY) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (filters->kind != PDFMAKE_ARRAY) return PDFMAKE_EINVAL; 0 0 0
1495  
1496 0 n = pdfmake_array_len((pdfmake_obj_t *)filters);
1497 0 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 0 0
1498 0 return pdfmake_buf_append(out, in, in_len);
1499 }
1500  
1501 /* Encode last to first, so the result decodes in array order. */
1502 0 ▶ if (pdfmake_buf_init(&tmp1) != PDFMAKE_OK) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&tmp1) != PDFMAKE_OK) return PDFMAKE_ENOMEM; 0 0 0
1503 0 ▶ if (pdfmake_buf_init(&tmp2) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&tmp2) != PDFMAKE_OK) { 0 0 0
1504 0 pdfmake_buf_free(&tmp1);
1505 0 return PDFMAKE_ENOMEM;
1506 }
1507  
1508 0 current_in = in;
1509 0 current_len = in_len;
1510 0 current_out = &tmp1;
1511  
1512 0 ▶ for (i = n; i > 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n; i > 0; i--) { 0 0 0
1513 const char *name;
1514 const pdfmake_obj_t *filter_params;
1515 0 pdfmake_obj_t *filter = pdfmake_array_get((pdfmake_obj_t *)filters, i - 1);
1516 0 ▶ if (!filter || filter->kind != PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (!filter || filter->kind != PDFMAKE_NAME) { 0 0 0
if (!filter || filter->kind != PDFMAKE_NAME) { 0 0 0
1517 0 pdfmake_buf_free(&tmp1);
1518 0 pdfmake_buf_free(&tmp2);
1519 0 return PDFMAKE_EINVAL;
1520 }
1521  
1522 0 name = (const char *)pdfmake_arena_name_bytes(
1523 arena, filter->as.name.id);
1524  
1525 /* Get params for this filter if array */
1526 0 filter_params = NULL;
1527 0 ▶ if (params && params->kind == PDFMAKE_ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (params && params->kind == PDFMAKE_ARRAY) { 0 0 0
if (params && params->kind == PDFMAKE_ARRAY) { 0 0 0
1528 0 filter_params = pdfmake_array_get((pdfmake_obj_t *)params, i - 1);
1529 0 ▶ } else if (params && n == 1) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (params && n == 1) { 0 0 0
} else if (params && n == 1) { 0 0 0
1530 0 filter_params = params;
1531 }
1532  
1533 0 pdfmake_buf_clear(current_out);
1534 0 err = pdfmake_filter_encode(name, current_in, current_len,
1535 filter_params, current_out);
1536 0 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 0 0 0
1537 0 pdfmake_buf_free(&tmp1);
1538 0 pdfmake_buf_free(&tmp2);
1539 0 return err;
1540 }
1541  
1542 /* Swap buffers for next iteration */
1543 0 current_in = current_out->data;
1544 0 current_len = current_out->len;
1545 0 ▶ current_out = (current_out == &tmp1) ? &tmp2 : &tmp1;
Branch: 1 branch
BranchTrueFalseTotal
current_out = (current_out == &tmp1) ? &tmp2 : &tmp1; 0 0 0
1546 }
1547  
1548 /* Copy final result to output */
1549 0 err = pdfmake_buf_append(out, current_in, current_len);
1550  
1551 0 pdfmake_buf_free(&tmp1);
1552 0 pdfmake_buf_free(&tmp2);
1553 0 return err;
1554 }
1555  
1556 /*============================================================================
1557 * ASCIIHexDecode — §7.4.2
1558 *==========================================================================*/
1559  
1560 742 static int hex_digit_value(uint8_t c)
1561 {
1562 742 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 742 0 742
if (c >= '0' && c <= '9') return c - '0'; 504 238 742
1563 238 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 238 0 238
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 235 3 238
1564 3 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 3 0 3
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 3 0 3
1565 0 return -1;
1566 }
1567  
1568 9 pdfmake_err_t pdfmake_asciihex_decode(const uint8_t *in, size_t in_len,
1569 pdfmake_buf_t *out)
1570 {
1571 int high;
1572 size_t i;
1573 uint8_t c;
1574 int val;
1575 uint8_t byte;
1576 9 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 9 9
1577 9 ▶ if (!in || in_len == 0) return PDFMAKE_OK;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || in_len == 0) return PDFMAKE_OK; 9 0 9
if (!in || in_len == 0) return PDFMAKE_OK; 0 9 9
1578  
1579 9 high = -1; /* Pending high nibble, -1 = none */
1580  
1581 755 ▶ for (i = 0; i < in_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < in_len; i++) { 754 1 755
1582 754 c = in[i];
1583  
1584 /* End of data marker */
1585 754 ▶ if (c == '>') break;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '>') break; 8 746 754
1586  
1587 /* Skip whitespace */
1588 746 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') {
Branch: 5 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 744 2 746
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 744 0 744
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 742 2 744
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 742 0 742
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 0 742 742
1589 4 continue;
1590 }
1591  
1592 742 val = hex_digit_value(c);
1593 742 ▶ if (val < 0) return PDFMAKE_ECORRUPT; /* Invalid hex digit */
Branch: 1 branch
BranchTrueFalseTotal
if (val < 0) return PDFMAKE_ECORRUPT; /* Invalid hex digit */ 0 742 742
1594  
1595 742 ▶ if (high < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (high < 0) { 372 370 742
1596 372 high = val;
1597 } else {
1598 370 byte = (uint8_t)((high << 4) | val);
1599 370 ▶ if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK) { 0 370 370
1600 0 return PDFMAKE_ENOMEM;
1601 }
1602 370 high = -1;
1603 }
1604 }
1605  
1606 /* Odd final digit → pad with 0 */
1607 9 ▶ if (high >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (high >= 0) { 2 7 9
1608 2 byte = (uint8_t)(high << 4);
1609 2 ▶ if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK) { 0 2 2
1610 0 return PDFMAKE_ENOMEM;
1611 }
1612 }
1613  
1614 9 return PDFMAKE_OK;
1615 }
1616  
1617 4 pdfmake_err_t pdfmake_asciihex_encode(const uint8_t *in, size_t in_len,
1618 pdfmake_buf_t *out)
1619 {
1620 static const char hex_chars[] = "0123456789ABCDEF";
1621 size_t i;
1622 uint8_t byte;
1623  
1624 4 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 4 4
1625  
1626 358 ▶ for (i = 0; i < in_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < in_len; i++) { 354 4 358
1627 354 byte = in[i];
1628 708 if (pdfmake_buf_append_byte(out, hex_chars[byte >> 4]) != PDFMAKE_OK ||
1629 354 pdfmake_buf_append_byte(out, hex_chars[byte & 0x0F]) != PDFMAKE_OK) {
1630 0 return PDFMAKE_ENOMEM;
1631 }
1632 }
1633  
1634 /* End of data marker */
1635 4 return pdfmake_buf_append_byte(out, '>');
1636 }
1637  
1638 /*============================================================================
1639 * ASCII85Decode — §7.4.3
1640 *==========================================================================*/
1641  
1642 /* Powers of 85 for decoding: 85^4, 85^3, 85^2, 85^1, 85^0 */
1643 static const uint32_t pdfmake_pow85[5] = {52200625, 614125, 7225, 85, 1};
1644  
1645 15 pdfmake_err_t pdfmake_ascii85_decode(const uint8_t *in, size_t in_len,
1646 pdfmake_buf_t *out)
1647 {
1648 uint8_t group[5];
1649 int group_len;
1650 size_t i;
1651 uint8_t c;
1652 uint8_t zeros[4];
1653 uint32_t val;
1654 int j;
1655 uint8_t bytes[4];
1656 int out_bytes;
1657 15 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 15 15
1658 15 ▶ if (!in || in_len == 0) return PDFMAKE_OK;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || in_len == 0) return PDFMAKE_OK; 15 0 15
if (!in || in_len == 0) return PDFMAKE_OK; 1 14 15
1659  
1660 14 group_len = 0;
1661 14 zeros[0] = 0; zeros[1] = 0; zeros[2] = 0; zeros[3] = 0;
1662  
1663 795 ▶ for (i = 0; i < in_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < in_len; i++) { 795 0 795
1664 795 c = in[i];
1665  
1666 /* End of data marker */
1667 795 ▶ if (c == '~') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '~') { 14 781 795
1668 14 ▶ if (i + 1 < in_len && in[i + 1] == '>') break;
Branch: 2 branches
BranchTrueFalseTotal
if (i + 1 < in_len && in[i + 1] == '>') break; 14 0 14
if (i + 1 < in_len && in[i + 1] == '>') break; 14 0 14
1669 /* Lone ~ is an error, but be lenient */
1670 0 continue;
1671 }
1672  
1673 /* Skip whitespace */
1674 781 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') {
Branch: 5 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 769 12 781
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 769 0 769
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 757 12 769
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 757 0 757
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f') { 0 757 757
1675 24 continue;
1676 }
1677  
1678 /* 'z' shorthand = 4 zero bytes (only valid when group is empty) */
1679 757 ▶ if (c == 'z') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == 'z') { 1 756 757
1680 1 ▶ if (group_len != 0) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (group_len != 0) return PDFMAKE_ECORRUPT; 0 1 1
1681 1 ▶ if (pdfmake_buf_append(out, zeros, 4) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, zeros, 4) != PDFMAKE_OK) { 0 1 1
1682 0 return PDFMAKE_ENOMEM;
1683 }
1684 1 continue;
1685 }
1686  
1687 /* Valid ASCII85 chars are 33 ('!') to 117 ('u') */
1688 756 ▶ if (c < 33 || c > 117) return PDFMAKE_ECORRUPT;
Branch: 2 branches
BranchTrueFalseTotal
if (c < 33 || c > 117) return PDFMAKE_ECORRUPT; 756 0 756
if (c < 33 || c > 117) return PDFMAKE_ECORRUPT; 0 756 756
1689  
1690 756 group[group_len++] = c - 33;
1691  
1692 756 ▶ if (group_len == 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (group_len == 5) { 145 611 756
1693 /* Decode 5 chars → 4 bytes */
1694 145 val = 0;
1695 870 ▶ for (j = 0; j < 5; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 5; j++) { 725 145 870
1696 725 val += group[j] * pdfmake_pow85[j];
1697 }
1698  
1699 145 bytes[0] = (val >> 24) & 0xFF;
1700 145 bytes[1] = (val >> 16) & 0xFF;
1701 145 bytes[2] = (val >> 8) & 0xFF;
1702 145 bytes[3] = val & 0xFF;
1703  
1704 145 ▶ if (pdfmake_buf_append(out, bytes, 4) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, bytes, 4) != PDFMAKE_OK) { 0 145 145
1705 0 return PDFMAKE_ENOMEM;
1706 }
1707 145 group_len = 0;
1708 }
1709 }
1710  
1711 /* Handle final short group (2-4 chars → 1-3 bytes) */
1712 14 ▶ if (group_len > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (group_len > 1) { 10 4 14
1713 /* Pad with 'u' (84) to make 5 chars */
1714 29 ▶ for (j = group_len; j < 5; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = group_len; j < 5; j++) { 19 10 29
1715 19 group[j] = 84; /* 'u' - 33 */
1716 }
1717  
1718 10 val = 0;
1719 60 ▶ for (j = 0; j < 5; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 5; j++) { 50 10 60
1720 50 val += group[j] * pdfmake_pow85[j];
1721 }
1722  
1723 10 out_bytes = group_len - 1; /* 2 chars → 1 byte, etc. */
1724 10 bytes[0] = (val >> 24) & 0xFF;
1725 10 bytes[1] = (val >> 16) & 0xFF;
1726 10 bytes[2] = (val >> 8) & 0xFF;
1727 10 bytes[3] = val & 0xFF;
1728  
1729 10 ▶ if (pdfmake_buf_append(out, bytes, out_bytes) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, bytes, out_bytes) != PDFMAKE_OK) { 0 10 10
1730 0 return PDFMAKE_ENOMEM;
1731 }
1732 4 ▶ } else if (group_len == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (group_len == 1) { 0 4 4
1733 /* Single trailing char is invalid */
1734 0 return PDFMAKE_ECORRUPT;
1735 }
1736  
1737 14 return PDFMAKE_OK;
1738 }
1739  
1740 12 pdfmake_err_t pdfmake_ascii85_encode(const uint8_t *in, size_t in_len,
1741 pdfmake_buf_t *out)
1742 {
1743 size_t i;
1744 uint32_t val;
1745 uint8_t chars[5];
1746 size_t remain;
1747 size_t j;
1748 12 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 12 12
1749  
1750 12 i = 0;
1751 154 ▶ while (i + 4 <= in_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 4 <= in_len) { 142 12 154
1752 /* Encode 4 bytes → 5 chars */
1753 142 val = ((uint32_t)in[i] << 24) |
1754 142 ((uint32_t)in[i+1] << 16) |
1755 142 ((uint32_t)in[i+2] << 8) |
1756 142 (uint32_t)in[i+3];
1757  
1758 142 ▶ if (val == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (val == 0) { 1 141 142
1759 /* Use 'z' shorthand for 4 zero bytes */
1760 1 ▶ if (pdfmake_buf_append_byte(out, 'z') != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(out, 'z') != PDFMAKE_OK) { 0 1 1
1761 0 return PDFMAKE_ENOMEM;
1762 }
1763 } else {
1764 141 chars[4] = (val % 85) + 33; val /= 85;
1765 141 chars[3] = (val % 85) + 33; val /= 85;
1766 141 chars[2] = (val % 85) + 33; val /= 85;
1767 141 chars[1] = (val % 85) + 33; val /= 85;
1768 141 chars[0] = (val % 85) + 33;
1769  
1770 141 ▶ if (pdfmake_buf_append(out, chars, 5) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, chars, 5) != PDFMAKE_OK) { 0 141 141
1771 0 return PDFMAKE_ENOMEM;
1772 }
1773 }
1774 142 i += 4;
1775 }
1776  
1777 /* Handle final short group (1-3 bytes → 2-4 chars) */
1778 12 remain = in_len - i;
1779 12 ▶ if (remain > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remain > 0) { 9 3 12
1780 9 val = 0;
1781 27 ▶ for (j = 0; j < remain; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < remain; j++) { 18 9 27
1782 18 val |= (uint32_t)in[i + j] << (24 - j * 8);
1783 }
1784  
1785 9 chars[4] = (val % 85) + 33; val /= 85;
1786 9 chars[3] = (val % 85) + 33; val /= 85;
1787 9 chars[2] = (val % 85) + 33; val /= 85;
1788 9 chars[1] = (val % 85) + 33; val /= 85;
1789 9 chars[0] = (val % 85) + 33;
1790  
1791 /* Output remain+1 chars for remain bytes */
1792 9 ▶ if (pdfmake_buf_append(out, chars, remain + 1) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, chars, remain + 1) != PDFMAKE_OK) { 0 9 9
1793 0 return PDFMAKE_ENOMEM;
1794 }
1795 }
1796  
1797 /* End of data marker */
1798 12 return pdfmake_buf_append(out, (const uint8_t *)"~>", 2);
1799 }
1800  
1801 /*============================================================================
1802 * LZWDecode — §7.4.4
1803 *==========================================================================*/
1804  
1805 #define LZW_CLEAR_CODE 256
1806 #define LZW_EOD_CODE 257
1807 #define LZW_MAX_CODE 4095 /* 12-bit codes max */
1808  
1809 typedef struct {
1810 int16_t prefix; /* Index of prefix string, or -1 for single byte */
1811 uint8_t suffix; /* Final byte */
1812 uint16_t length; /* Total string length */
1813 } lzw_entry_t;
1814  
1815 4 pdfmake_err_t pdfmake_lzw_decode(const uint8_t *in, size_t in_len,
1816 const pdfmake_flate_params_t *params,
1817 pdfmake_buf_t *out)
1818 {
1819 int early_change;
1820 int i;
1821 int next_code;
1822 int code_bits;
1823 int prev_code;
1824 size_t bit_pos;
1825 int b;
1826 size_t byte_idx;
1827 int bit_idx;
1828 uint8_t first_byte;
1829 int c;
1830 lzw_entry_t *table;
1831 uint8_t *stack;
1832 pdfmake_buf_t decoded_pred;
1833 pdfmake_err_t err;
1834 4 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 4 4
1835 4 ▶ if (!in || in_len == 0) return PDFMAKE_OK;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || in_len == 0) return PDFMAKE_OK; 4 0 4
if (!in || in_len == 0) return PDFMAKE_OK; 0 4 4
1836  
1837 4 ▶ early_change = params ? params->early_change : 1;
Branch: 1 branch
BranchTrueFalseTotal
early_change = params ? params->early_change : 1; 4 0 4
1838  
1839 /* String table: each entry is (prefix_code, suffix_byte, length) */
1840 4 table = malloc(sizeof(lzw_entry_t) * (LZW_MAX_CODE + 1));
1841 4 ▶ if (!table) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!table) return PDFMAKE_ENOMEM; 0 4 4
1842  
1843 /* Stack for reversing strings during output */
1844 4 stack = malloc(LZW_MAX_CODE + 1);
1845 4 ▶ if (!stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (!stack) { 0 4 4
1846 0 free(table);
1847 0 return PDFMAKE_ENOMEM;
1848 }
1849  
1850 /* Initialize table with single-byte entries 0-255 */
1851 1028 ▶ for (i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) { 1024 4 1028
1852 1024 table[i].prefix = -1;
1853 1024 table[i].suffix = (uint8_t)i;
1854 1024 table[i].length = 1;
1855 }
1856  
1857 4 next_code = 258; /* First available code after clear/eod */
1858 4 code_bits = 9; /* Current code width */
1859 4 prev_code = -1; /* Previous code, -1 = none */
1860  
1861 /* Bit reader state */
1862 4 bit_pos = 0; /* Current bit position in input */
1863  
1864 /* Helper to read next code (MSB first, unlike DEFLATE) */
1865 #define READ_CODE(code) do { \
1866 if (bit_pos + code_bits > in_len * 8) { code = LZW_EOD_CODE; break; } \
1867 code = 0; \
1868 for (b = 0; b < code_bits; b++) { \
1869 byte_idx = (bit_pos + b) / 8; \
1870 bit_idx = 7 - ((bit_pos + b) % 8); \
1871 code = (code << 1) | ((in[byte_idx] >> bit_idx) & 1); \
1872 } \
1873 bit_pos += code_bits; \
1874 } while(0)
1875  
1876 /* Output string for a code (uses stack to reverse) */
1877 #define OUTPUT_STRING(code) do { \
1878 int stack_pos = 0; \
1879 int c = code; \
1880 while (c >= 0) { \
1881 stack[stack_pos++] = table[c].suffix; \
1882 c = table[c].prefix; \
1883 } \
1884 while (stack_pos > 0) { \
1885 if (pdfmake_buf_append_byte(out, stack[--stack_pos]) != PDFMAKE_OK) { \
1886 free(table); free(stack); return PDFMAKE_ENOMEM; \
1887 } \
1888 } \
1889 } while(0)
1890  
1891 17 for (;;) {
1892 int code;
1893 201 ▶ READ_CODE(code);
Branch: 2 branches
BranchTrueFalseTotal
READ_CODE(code); 1 20 21
READ_CODE(code); 180 20 200
1894  
1895 21 ▶ if (code == LZW_EOD_CODE) break;
Branch: 1 branch
BranchTrueFalseTotal
if (code == LZW_EOD_CODE) break; 4 17 21
1896  
1897 17 ▶ if (code == LZW_CLEAR_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (code == LZW_CLEAR_CODE) { 4 13 17
1898 /* Reset table */
1899 4 next_code = 258;
1900 4 code_bits = 9;
1901 4 prev_code = -1;
1902 4 continue;
1903 }
1904  
1905 /* First code after clear must be < 256 */
1906 13 ▶ if (prev_code < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev_code < 0) { 3 10 13
1907 3 ▶ if (code >= 256) {
Branch: 1 branch
BranchTrueFalseTotal
if (code >= 256) { 0 3 3
1908 0 free(table);
1909 0 free(stack);
1910 0 return PDFMAKE_ECORRUPT;
1911 }
1912 9 ▶ OUTPUT_STRING(code);
Branch: 3 branches
BranchTrueFalseTotal
OUTPUT_STRING(code); 3 3 6
OUTPUT_STRING(code); 0 3 3
OUTPUT_STRING(code); 3 3 6
1913 3 prev_code = code;
1914 3 continue;
1915 }
1916  
1917 10 ▶ if (code < next_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (code < next_code) { 8 2 10
1918 /* Code is in table */
1919 36 ▶ OUTPUT_STRING(code);
Branch: 3 branches
BranchTrueFalseTotal
OUTPUT_STRING(code); 14 8 22
OUTPUT_STRING(code); 0 14 14
OUTPUT_STRING(code); 14 8 22
1920 /* Get first byte of current string */
1921 8 c = code;
1922 14 ▶ while (table[c].prefix >= 0) c = table[c].prefix;
Branch: 1 branch
BranchTrueFalseTotal
while (table[c].prefix >= 0) c = table[c].prefix; 6 8 14
1923 8 first_byte = table[c].suffix;
1924 2 ▶ } else if (code == next_code) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (code == next_code) { 2 0 2
1925 /* Special case: code not yet in table (KwKwK) */
1926 2 c = prev_code;
1927 2 ▶ while (table[c].prefix >= 0) c = table[c].prefix;
Branch: 1 branch
BranchTrueFalseTotal
while (table[c].prefix >= 0) c = table[c].prefix; 0 2 2
1928 2 first_byte = table[c].suffix;
1929 /* Output prev_string + first_byte */
1930 6 ▶ OUTPUT_STRING(prev_code);
Branch: 3 branches
BranchTrueFalseTotal
OUTPUT_STRING(prev_code); 2 2 4
OUTPUT_STRING(prev_code); 0 2 2
OUTPUT_STRING(prev_code); 2 2 4
1931 2 ▶ if (pdfmake_buf_append_byte(out, first_byte) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(out, first_byte) != PDFMAKE_OK) { 0 2 2
1932 0 free(table);
1933 0 free(stack);
1934 0 return PDFMAKE_ENOMEM;
1935 }
1936 } else {
1937 /* Invalid code */
1938 0 free(table);
1939 0 free(stack);
1940 0 return PDFMAKE_ECORRUPT;
1941 }
1942  
1943 /* Add new entry: prev_string + first_byte */
1944 10 ▶ if (next_code <= LZW_MAX_CODE) {
Branch: 1 branch
BranchTrueFalseTotal
if (next_code <= LZW_MAX_CODE) { 10 0 10
1945 10 table[next_code].prefix = prev_code;
1946 10 table[next_code].suffix = first_byte;
1947 10 table[next_code].length = table[prev_code].length + 1;
1948 10 next_code++;
1949  
1950 /* Increase code size based on EarlyChange parameter */
1951 10 ▶ if (early_change) {
Branch: 1 branch
BranchTrueFalseTotal
if (early_change) { 10 0 10
1952 /* Increase BEFORE next_code reaches limit */
1953 10 ▶ if (next_code == (1 << code_bits) && code_bits < 12) {
Branch: 2 branches
BranchTrueFalseTotal
if (next_code == (1 << code_bits) && code_bits < 12) { 0 10 10
if (next_code == (1 << code_bits) && code_bits < 12) { 0 0 0
1954 0 code_bits++;
1955 }
1956 } else {
1957 /* Increase AFTER next_code reaches limit */
1958 0 ▶ if (next_code == (1 << code_bits) + 1 && code_bits < 12) {
Branch: 2 branches
BranchTrueFalseTotal
if (next_code == (1 << code_bits) + 1 && code_bits < 12) { 0 0 0
if (next_code == (1 << code_bits) + 1 && code_bits < 12) { 0 0 0
1959 0 code_bits++;
1960 }
1961 }
1962 }
1963  
1964 10 prev_code = code;
1965 }
1966  
1967 #undef READ_CODE
1968 #undef OUTPUT_STRING
1969  
1970 4 free(table);
1971 4 free(stack);
1972  
1973 /* Apply predictor if specified */
1974 4 ▶ if (params && params->predictor > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (params && params->predictor > 1) { 4 0 4
if (params && params->predictor > 1) { 0 4 4
1975 0 ▶ if (pdfmake_buf_init(&decoded_pred) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&decoded_pred) != PDFMAKE_OK) { 0 0 0
1976 0 return PDFMAKE_ENOMEM;
1977 }
1978  
1979 0 ▶ if (params->predictor == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (params->predictor == 2) { 0 0 0
1980 0 err = pdfmake_tiff_predictor_decode(params->colors, params->bits_per_comp,
1981 0 params->columns, out->data, out->len,
1982 &decoded_pred);
1983 0 ▶ } else if (params->predictor >= 10 && params->predictor <= 15) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (params->predictor >= 10 && params->predictor <= 15) { 0 0 0
} else if (params->predictor >= 10 && params->predictor <= 15) { 0 0 0
1984 0 err = pdfmake_predictor_decode(params->predictor, params->colors,
1985 0 params->bits_per_comp, params->columns,
1986 0 out->data, out->len, &decoded_pred);
1987 } else {
1988 0 pdfmake_buf_free(&decoded_pred);
1989 0 return PDFMAKE_EINVAL;
1990 }
1991  
1992 0 ▶ if (err != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) { 0 0 0
1993 0 pdfmake_buf_free(&decoded_pred);
1994 0 return err;
1995 }
1996  
1997 /* Swap buffers */
1998 0 pdfmake_buf_free(out);
1999 0 *out = decoded_pred;
2000 }
2001  
2002 4 return PDFMAKE_OK;
2003 }
2004  
2005 /*============================================================================
2006 * RunLengthDecode — §7.4.5
2007 *==========================================================================*/
2008  
2009 13 pdfmake_err_t pdfmake_rle_decode(const uint8_t *in, size_t in_len,
2010 pdfmake_buf_t *out)
2011 {
2012 size_t i;
2013 uint8_t len_byte;
2014 size_t count;
2015 uint8_t byte;
2016 size_t j;
2017 13 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 13 13
2018 13 ▶ if (!in || in_len == 0) return PDFMAKE_OK;
Branch: 2 branches
BranchTrueFalseTotal
if (!in || in_len == 0) return PDFMAKE_OK; 13 0 13
if (!in || in_len == 0) return PDFMAKE_OK; 1 12 13
2019  
2020 12 i = 0;
2021 34 ▶ while (i < in_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < in_len) { 33 1 34
2022 33 len_byte = in[i++];
2023  
2024 33 ▶ if (len_byte == 128) {
Branch: 1 branch
BranchTrueFalseTotal
if (len_byte == 128) { 11 22 33
2025 /* EOD marker */
2026 11 break;
2027 22 ▶ } else if (len_byte <= 127) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (len_byte <= 127) { 14 8 22
2028 /* Literal run: copy next len_byte+1 bytes */
2029 14 count = len_byte + 1;
2030 14 ▶ if (i + count > in_len) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (i + count > in_len) return PDFMAKE_ECORRUPT; 0 14 14
2031 14 ▶ if (pdfmake_buf_append(out, in + i, count) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out, in + i, count) != PDFMAKE_OK) { 0 14 14
2032 0 return PDFMAKE_ENOMEM;
2033 }
2034 14 i += count;
2035 } else {
2036 /* Repeat run: repeat next byte 257-len_byte times */
2037 8 count = 257 - len_byte;
2038 8 ▶ if (i >= in_len) return PDFMAKE_ECORRUPT;
Branch: 1 branch
BranchTrueFalseTotal
if (i >= in_len) return PDFMAKE_ECORRUPT; 0 8 8
2039 8 byte = in[i++];
2040 646 ▶ for (j = 0; j < count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) { 638 8 646
2041 638 ▶ if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append_byte(out, byte) != PDFMAKE_OK) { 0 638 638
2042 0 return PDFMAKE_ENOMEM;
2043 }
2044 }
2045 }
2046 }
2047  
2048 12 return PDFMAKE_OK;
2049 }
2050  
2051 6 pdfmake_err_t pdfmake_rle_encode(const uint8_t *in, size_t in_len,
2052 pdfmake_buf_t *out)
2053 {
2054 size_t i;
2055 size_t run_len;
2056 uint8_t len_byte;
2057 size_t lit_start;
2058 size_t lit_len;
2059 6 ▶ if (!out) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return PDFMAKE_EINVAL; 0 6 6
2060 6 ▶ if (!in || in_len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!in || in_len == 0) { 6 0 6
if (!in || in_len == 0) { 1 5 6
2061 /* Empty input: just EOD */
2062 1 return pdfmake_buf_append_byte(out, 128);
2063 }
2064  
2065 5 i = 0;
2066 22 ▶ while (i < in_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < in_len) { 17 5 22
2067 /* Look for repeat run (3+ consecutive identical bytes) */
2068 17 run_len = 1;
2069 519 ▶ while (i + run_len < in_len && in[i + run_len] == in[i] && run_len < 128) {
Branch: 3 branches
BranchTrueFalseTotal
while (i + run_len < in_len && in[i + run_len] == in[i] && run_len < 128) { 516 3 519
while (i + run_len < in_len && in[i + run_len] == in[i] && run_len < 128) { 505 11 516
while (i + run_len < in_len && in[i + run_len] == in[i] && run_len < 128) { 502 3 505
2070 502 run_len++;
2071 }
2072  
2073 17 ▶ if (run_len >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (run_len >= 3) { 6 11 17
2074 /* Emit repeat run */
2075 6 len_byte = (uint8_t)(257 - run_len);
2076 12 if (pdfmake_buf_append_byte(out, len_byte) != PDFMAKE_OK ||
2077 6 pdfmake_buf_append_byte(out, in[i]) != PDFMAKE_OK) {
2078 0 return PDFMAKE_ENOMEM;
2079 }
2080 6 i += run_len;
2081 } else {
2082 /* Collect literal run (non-repeating bytes) */
2083 11 lit_start = i;
2084 11 lit_len = 0;
2085  
2086 1081 ▶ while (i < in_len && lit_len < 128) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < in_len && lit_len < 128) { 1077 4 1081
while (i < in_len && lit_len < 128) { 1070 7 1077
2087 /* Check if next 3+ bytes are identical (start repeat run) */
2088 1070 ▶ if (i + 2 < in_len && in[i] == in[i+1] && in[i] == in[i+2]) {
Branch: 3 branches
BranchTrueFalseTotal
if (i + 2 < in_len && in[i] == in[i+1] && in[i] == in[i+2]) { 1063 7 1070
if (i + 2 < in_len && in[i] == in[i+1] && in[i] == in[i+2]) { 0 1063 1063
if (i + 2 < in_len && in[i] == in[i+1] && in[i] == in[i+2]) { 0 0 0
2089 0 break;
2090 }
2091 1070 i++;
2092 1070 lit_len++;
2093 }
2094  
2095 11 ▶ if (lit_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lit_len > 0) { 11 0 11
2096 11 len_byte = (uint8_t)(lit_len - 1);
2097 22 if (pdfmake_buf_append_byte(out, len_byte) != PDFMAKE_OK ||
2098 11 pdfmake_buf_append(out, in + lit_start, lit_len) != PDFMAKE_OK) {
2099 0 return PDFMAKE_ENOMEM;
2100 }
2101 }
2102 }
2103 }
2104  
2105 /* EOD marker */
2106 5 return pdfmake_buf_append_byte(out, 128);
2107 }
2108  
2109 /*============================================================================
2110 * Remaining stub filters (CCITTFaxDecode implemented in pdfmake_ccitt.c)
2111 *==========================================================================*/
2112  
2113 /* JBIG2 is complex - stub for now, requires external library */
2114 0 pdfmake_err_t pdfmake_jbig2_decode(const uint8_t *in, size_t in_len,
2115 const pdfmake_obj_t *params,
2116 pdfmake_buf_t *out)
2117 {
2118 (void)in; (void)in_len; (void)params; (void)out;
2119 0 return PDFMAKE_EUNSUPPORTED;
2120 }
2121  
2122 0 pdfmake_err_t pdfmake_jpx_decode(const uint8_t *in, size_t in_len,
2123 const pdfmake_obj_t *params,
2124 pdfmake_buf_t *out)
2125 {
2126 /* JPXDecode: passthrough (JPEG 2000 data used as-is by PDF readers) */
2127 (void)params;
2128 0 return pdfmake_buf_append(out, in, in_len);
2129 }
« src/pdfmake_cmap.c ↑ Summary xs/reader.xs »