1#include "mupdf/fitz.h"
2
3#include <string.h>
4#include <limits.h>
5
6/* Fax G3/G4 decoder */
7
8/* TODO: uncompressed */
9
10/*
11<raph> the first 2^(initialbits) entries map bit patterns to decodes
12<raph> let's say initial_bits is 8 for the sake of example
13<raph> and that the code is 1001
14<raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 }
15<raph> because those are all the bytes that start with the code
16<raph> and the 4 is the length of the code
17... if (n_bits > initial_bits) ...
18<raph> anyway, in that case, it basically points to a mini table
19<raph> the n_bits is the maximum length of all codes beginning with that byte
20<raph> so 2^(n_bits - initial_bits) is the size of the mini-table
21<raph> peter came up with this, and it makes sense
22*/
23
24typedef struct cfd_node_s cfd_node;
25
26struct cfd_node_s
27{
28 short val;
29 short nbits;
30};
31
32enum
33{
34 cfd_white_initial_bits = 8,
35 cfd_black_initial_bits = 7,
36 cfd_2d_initial_bits = 7,
37 cfd_uncompressed_initial_bits = 6 /* must be 6 */
38};
39
40/* non-run codes in tables */
41enum
42{
43 ERROR = -1,
44 ZEROS = -2, /* EOL follows, possibly with more padding first */
45 UNCOMPRESSED = -3
46};
47
48/* semantic codes for cf_2d_decode */
49enum
50{
51 P = -4,
52 H = -5,
53 VR3 = 0,
54 VR2 = 1,
55 VR1 = 2,
56 V0 = 3,
57 VL1 = 4,
58 VL2 = 5,
59 VL3 = 6
60};
61
62/* White decoding table. */
63static const cfd_node cf_white_decode[] = {
64 {256,12},{272,12},{29,8},{30,8},{45,8},{46,8},{22,7},{22,7},
65 {23,7},{23,7},{47,8},{48,8},{13,6},{13,6},{13,6},{13,6},{20,7},
66 {20,7},{33,8},{34,8},{35,8},{36,8},{37,8},{38,8},{19,7},{19,7},
67 {31,8},{32,8},{1,6},{1,6},{1,6},{1,6},{12,6},{12,6},{12,6},{12,6},
68 {53,8},{54,8},{26,7},{26,7},{39,8},{40,8},{41,8},{42,8},{43,8},
69 {44,8},{21,7},{21,7},{28,7},{28,7},{61,8},{62,8},{63,8},{0,8},
70 {320,8},{384,8},{10,5},{10,5},{10,5},{10,5},{10,5},{10,5},{10,5},
71 {10,5},{11,5},{11,5},{11,5},{11,5},{11,5},{11,5},{11,5},{11,5},
72 {27,7},{27,7},{59,8},{60,8},{288,9},{290,9},{18,7},{18,7},{24,7},
73 {24,7},{49,8},{50,8},{51,8},{52,8},{25,7},{25,7},{55,8},{56,8},
74 {57,8},{58,8},{192,6},{192,6},{192,6},{192,6},{1664,6},{1664,6},
75 {1664,6},{1664,6},{448,8},{512,8},{292,9},{640,8},{576,8},{294,9},
76 {296,9},{298,9},{300,9},{302,9},{256,7},{256,7},{2,4},{2,4},{2,4},
77 {2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},{2,4},
78 {2,4},{2,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},
79 {3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{3,4},{128,5},{128,5},{128,5},
80 {128,5},{128,5},{128,5},{128,5},{128,5},{8,5},{8,5},{8,5},{8,5},
81 {8,5},{8,5},{8,5},{8,5},{9,5},{9,5},{9,5},{9,5},{9,5},{9,5},{9,5},
82 {9,5},{16,6},{16,6},{16,6},{16,6},{17,6},{17,6},{17,6},{17,6},
83 {4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},{4,4},
84 {4,4},{4,4},{4,4},{4,4},{4,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},
85 {5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},
86 {14,6},{14,6},{14,6},{14,6},{15,6},{15,6},{15,6},{15,6},{64,5},
87 {64,5},{64,5},{64,5},{64,5},{64,5},{64,5},{64,5},{6,4},{6,4},
88 {6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},{6,4},
89 {6,4},{6,4},{6,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},
90 {7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{7,4},{-2,3},{-2,3},
91 {-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
92 {-1,0},{-1,0},{-1,0},{-1,0},{-3,4},{1792,3},{1792,3},{1984,4},
93 {2048,4},{2112,4},{2176,4},{2240,4},{2304,4},{1856,3},{1856,3},
94 {1920,3},{1920,3},{2368,4},{2432,4},{2496,4},{2560,4},{1472,1},
95 {1536,1},{1600,1},{1728,1},{704,1},{768,1},{832,1},{896,1},
96 {960,1},{1024,1},{1088,1},{1152,1},{1216,1},{1280,1},{1344,1},
97 {1408,1}
98};
99
100/* Black decoding table. */
101static const cfd_node cf_black_decode[] = {
102 {128,12},{160,13},{224,12},{256,12},{10,7},{11,7},{288,12},{12,7},
103 {9,6},{9,6},{8,6},{8,6},{7,5},{7,5},{7,5},{7,5},{6,4},{6,4},{6,4},
104 {6,4},{6,4},{6,4},{6,4},{6,4},{5,4},{5,4},{5,4},{5,4},{5,4},{5,4},
105 {5,4},{5,4},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},
106 {1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{1,3},{4,3},{4,3},{4,3},{4,3},
107 {4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},
108 {4,3},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},
109 {3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},
110 {3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},{3,2},
111 {2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},
112 {2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},
113 {2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},{2,2},
114 {-2,4},{-2,4},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
115 {-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-3,5},{1792,4},
116 {1792,4},{1984,5},{2048,5},{2112,5},{2176,5},{2240,5},{2304,5},
117 {1856,4},{1856,4},{1920,4},{1920,4},{2368,5},{2432,5},{2496,5},
118 {2560,5},{18,3},{18,3},{18,3},{18,3},{18,3},{18,3},{18,3},{18,3},
119 {52,5},{52,5},{640,6},{704,6},{768,6},{832,6},{55,5},{55,5},
120 {56,5},{56,5},{1280,6},{1344,6},{1408,6},{1472,6},{59,5},{59,5},
121 {60,5},{60,5},{1536,6},{1600,6},{24,4},{24,4},{24,4},{24,4},
122 {25,4},{25,4},{25,4},{25,4},{1664,6},{1728,6},{320,5},{320,5},
123 {384,5},{384,5},{448,5},{448,5},{512,6},{576,6},{53,5},{53,5},
124 {54,5},{54,5},{896,6},{960,6},{1024,6},{1088,6},{1152,6},{1216,6},
125 {64,3},{64,3},{64,3},{64,3},{64,3},{64,3},{64,3},{64,3},{13,1},
126 {13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{13,1},
127 {13,1},{13,1},{13,1},{13,1},{13,1},{13,1},{23,4},{23,4},{50,5},
128 {51,5},{44,5},{45,5},{46,5},{47,5},{57,5},{58,5},{61,5},{256,5},
129 {16,3},{16,3},{16,3},{16,3},{17,3},{17,3},{17,3},{17,3},{48,5},
130 {49,5},{62,5},{63,5},{30,5},{31,5},{32,5},{33,5},{40,5},{41,5},
131 {22,4},{22,4},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},
132 {14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},{14,1},
133 {15,2},{15,2},{15,2},{15,2},{15,2},{15,2},{15,2},{15,2},{128,5},
134 {192,5},{26,5},{27,5},{28,5},{29,5},{19,4},{19,4},{20,4},{20,4},
135 {34,5},{35,5},{36,5},{37,5},{38,5},{39,5},{21,4},{21,4},{42,5},
136 {43,5},{0,3},{0,3},{0,3},{0,3}
137};
138
139/* 2-D decoding table. */
140static const cfd_node cf_2d_decode[] = {
141 {128,11},{144,10},{6,7},{0,7},{5,6},{5,6},{1,6},{1,6},{-4,4},
142 {-4,4},{-4,4},{-4,4},{-4,4},{-4,4},{-4,4},{-4,4},{-5,3},{-5,3},
143 {-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{-5,3},
144 {-5,3},{-5,3},{-5,3},{-5,3},{-5,3},{4,3},{4,3},{4,3},{4,3},{4,3},
145 {4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},{4,3},
146 {2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},{2,3},
147 {2,3},{2,3},{2,3},{2,3},{2,3},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
148 {3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
149 {3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
150 {3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
151 {3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
152 {3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},{3,1},
153 {3,1},{3,1},{3,1},{-2,4},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
154 {-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},
155 {-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-1,0},{-3,3}
156};
157
158/* bit magic */
159
160static inline int getbit(const unsigned char *buf, int x)
161{
162 return ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1;
163}
164
165static const unsigned char mask[8] = {
166 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01, 0
167};
168
169static const unsigned char clz[256] = {
170 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
171 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
172 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
173 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
174 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
175 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
176 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
177 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
186};
187
188static inline int
189find_changing(const unsigned char *line, int x, int w)
190{
191 int a, b, m, W;
192
193 if (!line)
194 return w;
195
196 /* We assume w > 0, -1 <= x < w */
197 if (x < 0)
198 {
199 x = 0;
200 m = 0xFF;
201 }
202 else
203 {
204 /* Mask out the bits we've already used (including the one
205 * we started from) */
206 m = mask[x & 7];
207 }
208 /* We have 'w' pixels (bits) in line. The last pixel that can be
209 * safely accessed is the (w-1)th bit of line.
210 * By taking W = w>>3, we know that the first W bytes of line are
211 * full, with w&7 stray bits following. */
212 W = w>>3;
213 x >>= 3;
214 a = line[x]; /* Safe as x < w => x <= w-1 => x>>3 <= (w-1)>>3 */
215 b = a ^ (a>>1);
216 b &= m;
217 if (x >= W)
218 {
219 /* Within the last byte already */
220 x = (x<<3) + clz[b];
221 if (x > w)
222 x = w;
223 return x;
224 }
225 while (b == 0)
226 {
227 if (++x >= W)
228 goto nearend;
229 b = a & 1;
230 a = line[x];
231 b = (b<<7) ^ a ^ (a>>1);
232 }
233 return (x<<3) + clz[b];
234nearend:
235 /* We have less than a byte to go. If no stray bits, exit now. */
236 if ((x<<3) == w)
237 return w;
238 b = a&1;
239 a = line[x];
240 b = (b<<7) ^ a ^ (a>>1);
241 x = (x<<3) + clz[b];
242 if (x > w)
243 x = w;
244 return x;
245}
246
247static inline int
248find_changing_color(const unsigned char *line, int x, int w, int color)
249{
250 if (!line || x >= w)
251 return w;
252
253 x = find_changing(line, (x > 0 || !color) ? x : -1, w);
254
255 if (x < w && getbit(line, x) != color)
256 x = find_changing(line, x, w);
257
258 return x;
259}
260
261static const unsigned char lm[8] = {
262 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01
263};
264
265static const unsigned char rm[8] = {
266 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE
267};
268
269static inline void setbits(unsigned char *line, int x0, int x1)
270{
271 int a0, a1, b0, b1, a;
272
273 if (x1 <= x0)
274 return;
275
276 a0 = x0 >> 3;
277 a1 = x1 >> 3;
278
279 b0 = x0 & 7;
280 b1 = x1 & 7;
281
282 if (a0 == a1)
283 {
284 if (b1)
285 line[a0] |= lm[b0] & rm[b1];
286 }
287 else
288 {
289 line[a0] |= lm[b0];
290 for (a = a0 + 1; a < a1; a++)
291 line[a] = 0xFF;
292 if (b1)
293 line[a1] |= rm[b1];
294 }
295}
296
297typedef struct fz_faxd_s fz_faxd;
298
299enum
300{
301 STATE_INIT, /* initial state, optionally waiting for EOL */
302 STATE_NORMAL, /* neutral state, waiting for any code */
303 STATE_MAKEUP, /* got a 1d makeup code, waiting for terminating code */
304 STATE_EOL, /* at eol, needs output buffer space */
305 STATE_H1, STATE_H2, /* in H part 1 and 2 (both makeup and terminating codes) */
306 STATE_DONE /* all done */
307};
308
309struct fz_faxd_s
310{
311 fz_stream *chain;
312
313 int k;
314 int end_of_line;
315 int encoded_byte_align;
316 int columns;
317 int rows;
318 int end_of_block;
319 int black_is_1;
320
321 int stride;
322 int ridx;
323
324 int bidx;
325 unsigned int word;
326
327 int stage;
328
329 int a, c, dim, eolc;
330 unsigned char *ref;
331 unsigned char *dst;
332 unsigned char *rp, *wp;
333
334 unsigned char buffer[4096];
335};
336
337static inline void eat_bits(fz_faxd *fax, int nbits)
338{
339 fax->word <<= nbits;
340 fax->bidx += nbits;
341}
342
343static int
344fill_bits(fz_context *ctx, fz_faxd *fax)
345{
346 /* The longest length of bits we'll ever need is 13. Never read more
347 * than we need to avoid unnecessary overreading of the end of the
348 * stream. */
349 while (fax->bidx > (32-13))
350 {
351 int c = fz_read_byte(ctx, fax->chain);
352 if (c == EOF)
353 return EOF;
354 fax->bidx -= 8;
355 fax->word |= c << fax->bidx;
356 }
357 return 0;
358}
359
360static int
361get_code(fz_context *ctx, fz_faxd *fax, const cfd_node *table, int initialbits)
362{
363 unsigned int word = fax->word;
364 int tidx = word >> (32 - initialbits);
365 int val = table[tidx].val;
366 int nbits = table[tidx].nbits;
367
368 if (nbits > initialbits)
369 {
370 int mask = (1 << (32 - initialbits)) - 1;
371 tidx = val + ((word & mask) >> (32 - nbits));
372 val = table[tidx].val;
373 nbits = initialbits + table[tidx].nbits;
374 }
375
376 eat_bits(fax, nbits);
377
378 return val;
379}
380
381/* decode one 1d code */
382static void
383dec1d(fz_context *ctx, fz_faxd *fax)
384{
385 int code;
386
387 if (fax->a == -1)
388 fax->a = 0;
389
390 if (fax->c)
391 code = get_code(ctx, fax, cf_black_decode, cfd_black_initial_bits);
392 else
393 code = get_code(ctx, fax, cf_white_decode, cfd_white_initial_bits);
394
395 if (code == UNCOMPRESSED)
396 fz_throw(ctx, FZ_ERROR_GENERIC, "uncompressed data in faxd");
397
398 if (code < 0)
399 fz_throw(ctx, FZ_ERROR_GENERIC, "negative code in 1d faxd");
400
401 if (fax->a + code > fax->columns)
402 fz_throw(ctx, FZ_ERROR_GENERIC, "overflow in 1d faxd");
403
404 if (fax->c)
405 setbits(fax->dst, fax->a, fax->a + code);
406
407 fax->a += code;
408
409 if (code < 64)
410 {
411 fax->c = !fax->c;
412 fax->stage = STATE_NORMAL;
413 }
414 else
415 fax->stage = STATE_MAKEUP;
416}
417
418/* decode one 2d code */
419static void
420dec2d(fz_context *ctx, fz_faxd *fax)
421{
422 int code, b1, b2;
423
424 if (fax->stage == STATE_H1 || fax->stage == STATE_H2)
425 {
426 if (fax->a == -1)
427 fax->a = 0;
428
429 if (fax->c)
430 code = get_code(ctx, fax, cf_black_decode, cfd_black_initial_bits);
431 else
432 code = get_code(ctx, fax, cf_white_decode, cfd_white_initial_bits);
433
434 if (code == UNCOMPRESSED)
435 fz_throw(ctx, FZ_ERROR_GENERIC, "uncompressed data in faxd");
436
437 if (code < 0)
438 fz_throw(ctx, FZ_ERROR_GENERIC, "negative code in 2d faxd");
439
440 if (fax->a + code > fax->columns)
441 fz_throw(ctx, FZ_ERROR_GENERIC, "overflow in 2d faxd");
442
443 if (fax->c)
444 setbits(fax->dst, fax->a, fax->a + code);
445
446 fax->a += code;
447
448 if (code < 64)
449 {
450 fax->c = !fax->c;
451 if (fax->stage == STATE_H1)
452 fax->stage = STATE_H2;
453 else if (fax->stage == STATE_H2)
454 fax->stage = STATE_NORMAL;
455 }
456
457 return;
458 }
459
460 code = get_code(ctx, fax, cf_2d_decode, cfd_2d_initial_bits);
461
462 switch (code)
463 {
464 case H:
465 fax->stage = STATE_H1;
466 break;
467
468 case P:
469 b1 = find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
470 if (b1 >= fax->columns)
471 b2 = fax->columns;
472 else
473 b2 = find_changing(fax->ref, b1, fax->columns);
474 if (fax->c) setbits(fax->dst, fax->a, b2);
475 fax->a = b2;
476 break;
477
478 case V0:
479 b1 = find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
480 if (fax->c) setbits(fax->dst, fax->a, b1);
481 fax->a = b1;
482 fax->c = !fax->c;
483 break;
484
485 case VR1:
486 b1 = 1 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
487 if (b1 >= fax->columns) b1 = fax->columns;
488 if (fax->c) setbits(fax->dst, fax->a, b1);
489 fax->a = b1;
490 fax->c = !fax->c;
491 break;
492
493 case VR2:
494 b1 = 2 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
495 if (b1 >= fax->columns) b1 = fax->columns;
496 if (fax->c) setbits(fax->dst, fax->a, b1);
497 fax->a = b1;
498 fax->c = !fax->c;
499 break;
500
501 case VR3:
502 b1 = 3 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
503 if (b1 >= fax->columns) b1 = fax->columns;
504 if (fax->c) setbits(fax->dst, fax->a, b1);
505 fax->a = b1;
506 fax->c = !fax->c;
507 break;
508
509 case VL1:
510 b1 = -1 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
511 if (b1 < 0) b1 = 0;
512 if (fax->c) setbits(fax->dst, fax->a, b1);
513 fax->a = b1;
514 fax->c = !fax->c;
515 break;
516
517 case VL2:
518 b1 = -2 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
519 if (b1 < 0) b1 = 0;
520 if (fax->c) setbits(fax->dst, fax->a, b1);
521 fax->a = b1;
522 fax->c = !fax->c;
523 break;
524
525 case VL3:
526 b1 = -3 + find_changing_color(fax->ref, fax->a, fax->columns, !fax->c);
527 if (b1 < 0) b1 = 0;
528 if (fax->c) setbits(fax->dst, fax->a, b1);
529 fax->a = b1;
530 fax->c = !fax->c;
531 break;
532
533 case UNCOMPRESSED:
534 fz_throw(ctx, FZ_ERROR_GENERIC, "uncompressed data in faxd");
535
536 case ERROR:
537 fz_throw(ctx, FZ_ERROR_GENERIC, "invalid code in 2d faxd");
538
539 default:
540 fz_throw(ctx, FZ_ERROR_GENERIC, "invalid code in 2d faxd (%d)", code);
541 }
542}
543
544static int
545next_faxd(fz_context *ctx, fz_stream *stm, size_t max)
546{
547 fz_faxd *fax = stm->state;
548 unsigned char *p = fax->buffer;
549 unsigned char *ep;
550 unsigned char *tmp;
551
552 if (max > sizeof(fax->buffer))
553 max = sizeof(fax->buffer);
554 ep = p + max;
555 if (fax->stage == STATE_INIT && fax->end_of_line)
556 {
557 fill_bits(ctx, fax);
558 if ((fax->word >> (32 - 12)) != 1)
559 {
560 fz_warn(ctx, "faxd stream doesn't start with EOL");
561 while (!fill_bits(ctx, fax) && (fax->word >> (32 - 12)) != 1)
562 eat_bits(fax, 1);
563 }
564 if ((fax->word >> (32 - 12)) != 1)
565 fz_throw(ctx, FZ_ERROR_GENERIC, "initial EOL not found");
566 }
567
568 if (fax->stage == STATE_INIT)
569 fax->stage = STATE_NORMAL;
570
571 if (fax->stage == STATE_DONE)
572 return EOF;
573
574 if (fax->stage == STATE_EOL)
575 goto eol;
576
577loop:
578
579 if (fill_bits(ctx, fax))
580 {
581 if (fax->bidx > 31)
582 {
583 if (fax->a > 0)
584 goto eol;
585 goto rtc;
586 }
587 }
588
589 if ((fax->word >> (32 - 12)) == 0)
590 {
591 eat_bits(fax, 1);
592 goto loop;
593 }
594
595 if ((fax->word >> (32 - 12)) == 1)
596 {
597 eat_bits(fax, 12);
598 fax->eolc ++;
599
600 if (fax->k > 0)
601 {
602 if (fax->a == -1)
603 fax->a = 0;
604 if ((fax->word >> (32 - 1)) == 1)
605 fax->dim = 1;
606 else
607 fax->dim = 2;
608 eat_bits(fax, 1);
609 }
610 }
611 else if (fax->k > 0 && fax->a == -1)
612 {
613 fax->a = 0;
614 if ((fax->word >> (32 - 1)) == 1)
615 fax->dim = 1;
616 else
617 fax->dim = 2;
618 eat_bits(fax, 1);
619 }
620 else if (fax->dim == 1)
621 {
622 fax->eolc = 0;
623 fz_try(ctx)
624 {
625 dec1d(ctx, fax);
626 }
627 fz_catch(ctx)
628 {
629 goto error;
630 }
631 }
632 else if (fax->dim == 2)
633 {
634 fax->eolc = 0;
635 fz_try(ctx)
636 {
637 dec2d(ctx, fax);
638 }
639 fz_catch(ctx)
640 {
641 goto error;
642 }
643 }
644
645 /* no eol check after makeup codes nor in the middle of an H code */
646 if (fax->stage == STATE_MAKEUP || fax->stage == STATE_H1 || fax->stage == STATE_H2)
647 goto loop;
648
649 /* check for eol conditions */
650 if (fax->eolc || fax->a >= fax->columns)
651 {
652 if (fax->a > 0)
653 goto eol;
654 if (fax->eolc == (fax->k < 0 ? 2 : 6))
655 goto rtc;
656 }
657
658 goto loop;
659
660eol:
661 fax->stage = STATE_EOL;
662
663 if (fax->black_is_1)
664 {
665 while (fax->rp < fax->wp && p < ep)
666 *p++ = *fax->rp++;
667 }
668 else
669 {
670 while (fax->rp < fax->wp && p < ep)
671 *p++ = *fax->rp++ ^ 0xff;
672 }
673
674 if (fax->rp < fax->wp)
675 {
676 stm->rp = fax->buffer;
677 stm->wp = p;
678 stm->pos += (p - fax->buffer);
679 if (p == fax->buffer)
680 return EOF;
681 return *stm->rp++;
682 }
683
684 tmp = fax->ref;
685 fax->ref = fax->dst;
686 fax->dst = tmp;
687 memset(fax->dst, 0, fax->stride);
688
689 fax->rp = fax->dst;
690 fax->wp = fax->dst + fax->stride;
691
692 fax->stage = STATE_NORMAL;
693 fax->c = 0;
694 fax->a = -1;
695 fax->ridx ++;
696
697 if (!fax->end_of_block && fax->rows && fax->ridx >= fax->rows)
698 goto rtc;
699
700 /* we have not read dim from eol, make a guess */
701 if (fax->k > 0 && !fax->eolc && fax->a == -1)
702 {
703 if (fax->ridx % fax->k == 0)
704 fax->dim = 1;
705 else
706 fax->dim = 2;
707 }
708
709 /* if end_of_line & encoded_byte_align, EOLs are *not* optional */
710 if (fax->encoded_byte_align)
711 {
712 if (fax->end_of_line)
713 eat_bits(fax, (12 - fax->bidx) & 7);
714 else
715 eat_bits(fax, (8 - fax->bidx) & 7);
716 }
717
718 /* no more space in output, don't decode the next row yet */
719 if (p == fax->buffer + max)
720 {
721 stm->rp = fax->buffer;
722 stm->wp = p;
723 stm->pos += (p - fax->buffer);
724 if (p == fax->buffer)
725 return EOF;
726 return *stm->rp++;
727 }
728
729 goto loop;
730
731error:
732 /* decode the remaining pixels up to where the error occurred */
733 if (fax->black_is_1)
734 {
735 while (fax->rp < fax->wp && p < ep)
736 *p++ = *fax->rp++;
737 }
738 else
739 {
740 while (fax->rp < fax->wp && p < ep)
741 *p++ = *fax->rp++ ^ 0xff;
742 }
743 /* fallthrough */
744
745rtc:
746 fax->stage = STATE_DONE;
747 stm->rp = fax->buffer;
748 stm->wp = p;
749 stm->pos += (p - fax->buffer);
750 if (p == fax->buffer)
751 return EOF;
752 return *stm->rp++;
753}
754
755static void
756close_faxd(fz_context *ctx, void *state_)
757{
758 fz_faxd *fax = (fz_faxd *)state_;
759 int i;
760
761 /* if we read any extra bytes, try to put them back */
762 i = (32 - fax->bidx) / 8;
763 while (i--)
764 fz_unread_byte(ctx, fax->chain);
765
766 fz_drop_stream(ctx, fax->chain);
767 fz_free(ctx, fax->ref);
768 fz_free(ctx, fax->dst);
769 fz_free(ctx, fax);
770}
771
772/* Default: columns = 1728, end_of_block = 1, the rest = 0 */
773fz_stream *
774fz_open_faxd(fz_context *ctx, fz_stream *chain,
775 int k, int end_of_line, int encoded_byte_align,
776 int columns, int rows, int end_of_block, int black_is_1)
777{
778 fz_faxd *fax;
779
780 if (columns < 0 || columns >= INT_MAX - 7)
781 fz_throw(ctx, FZ_ERROR_GENERIC, "too many columns lead to an integer overflow (%d)", columns);
782
783 fax = fz_malloc_struct(ctx, fz_faxd);
784 fz_try(ctx)
785 {
786 fax->ref = NULL;
787 fax->dst = NULL;
788
789 fax->k = k;
790 fax->end_of_line = end_of_line;
791 fax->encoded_byte_align = encoded_byte_align;
792 fax->columns = columns;
793 fax->rows = rows;
794 fax->end_of_block = end_of_block;
795 fax->black_is_1 = black_is_1;
796
797 fax->stride = ((fax->columns - 1) >> 3) + 1;
798 fax->ridx = 0;
799 fax->bidx = 32;
800 fax->word = 0;
801
802 fax->stage = STATE_INIT;
803 fax->a = -1;
804 fax->c = 0;
805 fax->dim = fax->k < 0 ? 2 : 1;
806 fax->eolc = 0;
807
808 fax->ref = fz_malloc(ctx, fax->stride);
809 fax->dst = fz_malloc(ctx, fax->stride);
810 fax->rp = fax->dst;
811 fax->wp = fax->dst + fax->stride;
812
813 memset(fax->ref, 0, fax->stride);
814 memset(fax->dst, 0, fax->stride);
815
816 fax->chain = fz_keep_stream(ctx, chain);
817 }
818 fz_catch(ctx)
819 {
820 fz_free(ctx, fax->dst);
821 fz_free(ctx, fax->ref);
822 fz_free(ctx, fax);
823 fz_rethrow(ctx);
824 }
825
826 return fz_new_stream(ctx, fax, next_faxd, close_faxd);
827}
828