1 /*
2  * pts_fax.c -- a compact CCITTFax compressor and uncompressor) implementation
3  * compiled by pts@fazekas.hu at Sun Jul  7 19:51:42 CEST 2002
4  *
5  * algorithm ripped from GNU Ghostscript, implementation and (C):
6  *
7 Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All
8 rights reserved.
9 
10 GNU Ghostscript is free software; you can redistribute it and/or
11 modify it under the terms of version 2 of the GNU General Public
12 License as published by the Free Software Foundation.
13 
14 GNU Ghostscript is distributed in the hope that it will be
15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program so you can know your rights and responsibilities.
21 It should be in a file named doc/COPYING. If not, write to the
22 Free Software Foundation, Inc., 59 Temple Place Suite 330, Boston, MA
23 02111-1307, USA.
24  */
25 
26 #include "pts_fax.h"
27 #if 0 /* doesn't work in C++ */
28 #  define intern static
29 #  define impl static
30 #  define intern_const static const
31 #  define impl_const static const
32 #else
33 #  define intern static
34 #  define impl static
35 #  define intern_const extern const
36 #  define impl_const const
37 #endif
38 /* #define cxxintern extern */
39 
40 #ifdef __GNUC__
41 #ifndef __clang__
42 #pragma implementation
43 #endif
44 #endif
45 
46 /* misc_types.h by pts@fazekas.hu at Sat Jul  6 19:20:17 CEST 2002 */
47 #ifndef MISC_TYPES_H
48 #define MISC_TYPES_H 1
49 
50 /* #include "config2.h" */
51 
52 #define BEGIN	do {
53 #define END	} while (0)
54 #ifndef DO_NOTHING
55 #  define DO_NOTHING BEGIN END
56 #endif
57 
58 #  define if_debug0(c,s) DO_NOTHING
59 #  define if_debug1(c,s,a1) DO_NOTHING
60 #  define if_debug2(c,s,a1,a2) DO_NOTHING
61 #  define if_debug3(c,s,a1,a2,a3) DO_NOTHING
62 #  define if_debug4(c,s,a1,a2,a3,a4) DO_NOTHING
63 #  define if_debug5(c,s,a1,a2,a3,a4,a5) DO_NOTHING
64 #  define if_debug6(c,s,a1,a2,a3,a4,a5,a6) DO_NOTHING
65 #  define if_debug7(c,s,a1,a2,a3,a4,a5,a6,a7) DO_NOTHING
66 #  define if_debug8(c,s,a1,a2,a3,a4,a5,a6,a7,a8) DO_NOTHING
67 #  define if_debug9(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9) DO_NOTHING
68 #  define if_debug10(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) DO_NOTHING
69 #  define if_debug11(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11) DO_NOTHING
70 #  define if_debug12(c,s,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) DO_NOTHING
71 
72 /* to the one that all the compilers seem to have.... */
73 #ifndef min
74 #  define min(a, b) (((a) < (b)) ? (a) : (b))
75 #endif
76 #ifndef max
77 #  define max(a, b) (((a) > (b)) ? (a) : (b))
78 #endif
79 
80 /* Define a standard way to round values to a (constant) modulus. */
81 #define ROUND_DOWN(value, modulus)\
82   ( (modulus) & ((modulus) - 1) ?	/* not a power of 2 */\
83     (value) - (value) % (modulus) :\
84     (value) & -(modulus) )
85 #define ROUND_UP(value, modulus)\
86   ( (modulus) & ((modulus) - 1) ?	/* not a power of 2 */\
87     ((value) + ((modulus) - 1)) / (modulus) * (modulus) :\
88     ((value) + ((modulus) - 1)) & -(modulus) )
89 
90 #define countof(a) (sizeof(a) / sizeof((a)[0]))
91 
92 #define gs_alloc_bytes(a, n, c) ss->xalloc_(n)
93 #define gs_free_object(a, ptr, c) ss->free_(ptr)
94 
95 #endif /* gstypes.h */
96 /* end of former misc_types.h */
97 
98 /* gsbittab.h */
99 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
100 /* Interface to tables for bit operations */
101 
102 #ifndef gsbittab_INCLUDED
103 #  define gsbittab_INCLUDED
104 
105 /*
106  * Generate tables for transforming 2, 4, 6, or 8 bits.
107  */
108 #define btab2_(v0,v2,v1)\
109   v0,v1+v0,v2+v0,v2+v1+v0
110 #define bit_table_2(v0,v2,v1) btab2_(v0,v2,v1)
111 #define btab4_(v0,v8,v4,v2,v1)\
112   btab2_(v0,v2,v1), btab2_(v4+v0,v2,v1),\
113   btab2_(v8+v0,v2,v1), btab2_(v8+v4+v0,v2,v1)
114 #define bit_table_4(v0,v8,v4,v2,v1) btab4_(v0,v8,v4,v2,v1)
115 #define btab6_(v0,v20,v10,v8,v4,v2,v1)\
116   btab4_(v0,v8,v4,v2,v1), btab4_(v10+v0,v8,v4,v2,v1),\
117   btab4_(v20+v0,v8,v4,v2,v1), btab4_(v20+v10+v0,v8,v4,v2,v1)
118 #define bit_table_6(v0,v20,v10,v8,v4,v2,v1) btab6_(v0,v20,v10,v8,v4,v2,v1)
119 #define bit_table_8(v0,v80,v40,v20,v10,v8,v4,v2,v1)\
120   btab6_(v0,v20,v10,v8,v4,v2,v1), btab6_(v40+v0,v20,v10,v8,v4,v2,v1),\
121   btab6_(v80+v0,v20,v10,v8,v4,v2,v1), btab6_(v80+v40+v0,v20,v10,v8,v4,v2,v1)
122 
123 /*
124  * byte_reverse_bits[B] = the unsigned char B with the order of bits reversed.
125  */
126 intern_const unsigned char byte_reverse_bits[256];
127 
128 /*
129  * byte_right_mask[N] = a unsigned char with N trailing 1s, 0 <= N <= 8.
130  */
131 intern_const unsigned char byte_right_mask[9];
132 
133 /*
134  * byte_count_bits[B] = the number of 1-bits in a unsigned char with value B.
135  */
136 intern_const unsigned char byte_count_bits[256];
137 
138 /*
139  * byte_bit_run_length_N[B], for 0 <= N <= 7, gives the length of the
140  * run of 1-bits starting at bit N in a unsigned char with value B,
141  * numbering the bits in the unsigned char as 01234567.  If the run includes
142  * the low-order bit (i.e., might be continued into a following unsigned char),
143  * the run length is increased by 8.
144  */
145 intern_const unsigned char
146     byte_bit_run_length_0[256], byte_bit_run_length_1[256],
147     byte_bit_run_length_2[256], byte_bit_run_length_3[256],
148     byte_bit_run_length_4[256], byte_bit_run_length_5[256],
149     byte_bit_run_length_6[256], byte_bit_run_length_7[256];
150 
151 /*
152  * byte_bit_run_length[N] points to byte_bit_run_length_N.
153  * byte_bit_run_length_neg[N] = byte_bit_run_length[-N & 7].
154  */
155 intern_const unsigned char *const byte_bit_run_length[8];
156 intern_const unsigned char *const byte_bit_run_length_neg[8];
157 
158 /*
159  * byte_acegbdfh_to_abcdefgh[acegbdfh] = abcdefgh, where the letters
160  * denote the individual bits of the unsigned char.
161  */
162 intern_const unsigned char byte_acegbdfh_to_abcdefgh[256];
163 
164 #endif /* gsbittab_INCLUDED */
165 
166 /* end of former gsbittab.h */
167 
168 /* shc.h */
169 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
170 /* Common definitions for filters using Huffman coding */
171 
172 #ifndef shc_INCLUDED
173 #  define shc_INCLUDED
174 
175 /* #include "gsbittab.h" */
176 
177 #define hc_bits_size (SIZEOF_INT * 8)
178 #define s_hce_init_inline(ss)\
179   ((ss)->bits = 0, (ss)->bits_left = hc_bits_size)
180 #define s_hcd_init_inline(ss)\
181   ((ss)->bits = 0, (ss)->bits_left = 0)
182 
183 /*
184  * These definitions are valid for code lengths up to 16 bits
185  * and non-negative decoded values up to 15 bits.
186  *
187  * We define 3 different representations of the code: encoding tables,
188  * decoding tables, and a definition table which can be generated easily
189  * from frequency information and which in turn can easily generate
190  * the encoding and decoding tables.
191  *
192  * The definition table has two parts: a list of the number of i-bit
193  * codes for each i >= 1, and the decoded values corresponding to
194  * the code values in increasing lexicographic order (which will also
195  * normally be decreasing code frequency).  Calling these two lists
196  * L[1..M] and V[0..N-1] respectively, we have the following invariants:
197  *      - 1 <= M <= max_hc_length, N >= 2.
198  *      - L[0] = 0.
199  *      - for i=1..M, L[i] >= 0.
200  *      - sum(i=1..M: L[i]) = N.
201  *      - sum(i=1..M: L[i] * 2^-i) = 1.
202  *      - V[0..N-1] are a permutation of the integers 0..N-1.
203  */
204 #define max_hc_length 16
205 typedef struct hc_definition_s {
206     unsigned short *counts;		/* [0..M] */
207     unsigned int num_counts;		/* M */
208     unsigned short *values;		/* [0..N-1] */
209     unsigned int num_values;		/* N */
210 } hc_definition;
211 
212 /**** pts ****/
213 struct stream_hc_state_s;
214 /* definition moved to pts_fax.h */
215 
216 /* ------ Encoding tables ------ */
217 
218 /* Define the structure for the encoding tables. */
219 typedef struct hce_code_s {
220     unsigned short code;
221     unsigned short code_length;
222 } hce_code;
223 
224 #define hce_entry(c, len) { c, len }
225 
226 typedef struct hce_table_s {
227     unsigned int count;
228     hce_code *codes;
229 } hce_table;
230 
231 #define hce_bits_available(n)\
232   (ss->bits_left >= (n) || wlimit - q > ((n) - ss->bits_left - 1) >> 3)
233 
234 /* ------ Encoding utilities ------ */
235 
236 /*
237  * Put a code on the output.  The client is responsible for ensuring
238  * that q does not exceed pw->limit.
239  */
240 
241 #ifdef DEBUG
242 #  define hc_print_value(code, clen)\
243     (gs_debug_c('W') ?\
244      (dlprintf2("[W]0x%x,%d\n", code, clen), 0) : 0)
245 #  define hc_print_value_then(code, clen) hc_print_value(code, clen),
246 #else
247 #  define hc_print_value(code, clen) 0
248 #  define hc_print_value_then(code, clen)	/* */
249 #endif
250 #define hc_print_code(rp) hc_print_value((rp)->code, (rp)->code_length)
251 
252 /* Declare variables that hold the encoder state. */
253 #define hce_declare_state\
254 	register unsigned int bits;\
255 	register int bits_left
256 
257 /* Load the state from the stream. */
258 /* Free variables: ss, bits, bits_left. */
259 #define hce_load_state()\
260 	bits = ss->bits, bits_left = ss->bits_left
261 
262 /* Store the state back in the stream. */
263 /* Free variables: ss, bits, bits_left. */
264 #define hce_store_state()\
265 	ss->bits = bits, ss->bits_left = bits_left
266 
267 /* Put a code on the stream. */
268 intern void hc_put_code_proc _((bool, unsigned char *, unsigned int));
269 
270 #define hc_put_value(ss, q, code, clen)\
271   (hc_print_value_then(code, clen)\
272    ((bits_left -= (clen)) >= 0 ?\
273     (bits += (code) << bits_left) :\
274     (hc_put_code_proc((ss)->FirstBitLowOrder,\
275 		      q += hc_bits_size >> 3,\
276 		      (bits + ((code) >> -bits_left))),\
277      bits = (code) << (bits_left += hc_bits_size))))
278 #define hc_put_code(ss, q, cp)\
279   hc_put_value(ss, q, (cp)->code, (cp)->code_length)
280 
281 /*
282  * Force out the final bits to the output.
283  * Note that this does a store_state, but not a load_state.
284  */
285 intern unsigned char *hc_put_last_bits_proc _((struct stream_hc_state_s *, unsigned char *, unsigned int, int));
286 
287 #define hc_put_last_bits(ss, q)\
288   hc_put_last_bits_proc(ss, q, bits, bits_left)
289 
290 /* ------ Decoding tables ------ */
291 
292 /*
293  * Define the structure for the decoding tables.
294  * First-level nodes are either leaves, which have
295  *      value = decoded value
296  *      code_length <= initial_bits
297  * or non-leaves, which have
298  *      value = the index of a sub-table
299  *      code_length = initial_bits + the number of additional dispatch bits
300  * Second-level nodes are always leaves, with
301  *      code_length = the actual number of bits in the code - initial_bits.
302  */
303 
304 typedef struct hcd_code_s {
305     short value;
306     unsigned short code_length;
307 } hcd_code;
308 
309 typedef struct hcd_table_s {
310     unsigned int count;
311     unsigned int initial_bits;
312     hcd_code *codes;
313 } hcd_table;
314 
315 /* Declare variables that hold the decoder state. */
316 #define hcd_declare_state\
317 	register const unsigned char *p;\
318 	const unsigned char *rlimit;\
319 	unsigned int bits;\
320 	int bits_left
321 
322 /* Load the state from the stream. */
323 /* Free variables: pr, ss, p, rlimit, bits, bits_left. */
324 #define hcd_load_state()\
325 	p = pr->ptr,\
326 	rlimit = pr->limit,\
327 	bits = ss->bits,\
328 	bits_left = ss->bits_left
329 
330 /* Store the state back in the stream. */
331 /* Put back any complete bytes into the input buffer. */
332 /* Free variables: pr, ss, p, bits, bits_left. */
333 #define hcd_store_state()\
334 	pr->ptr = p -= (bits_left >> 3),\
335 	ss->bits = bits >>= (bits_left & ~7),\
336 	ss->bits_left = bits_left &= 7
337 
338 /* Macros to get blocks of bits from the input stream. */
339 /* Invariants: 0 <= bits_left <= bits_size; */
340 /* bits [bits_left-1..0] contain valid data. */
341 
342 #define hcd_bits_available(n)\
343   (bits_left >= (n) || rlimit - p > ((n) - bits_left - 1) >> 3)
344 /* For hcd_ensure_bits, n must not be greater than 8. */
345 #define HCD_ENSURE_BITS_ELSE(n)\
346   if (bits_left >= n)\
347     DO_NOTHING;\
348   else HCD_MORE_BITS_ELSE
349 #define hcd_ensure_bits(n, outl)\
350   BEGIN HCD_ENSURE_BITS_ELSE(n) goto outl; END
351 
352 /* Load more bits into the buffer. */
353 #define HCD_MORE_BITS_1_ELSE\
354   if (p < rlimit) {\
355     int c = *++p;\
356 \
357     if (ss->FirstBitLowOrder)\
358       c = byte_reverse_bits[c];\
359     bits = (bits << 8) + c, bits_left += 8;\
360   } else
361 #if hc_bits_size == 16
362 #  define HCD_MORE_BITS_ELSE HCD_MORE_BITS_1_ELSE
363 #else /* hc_bits_size >= 32 */
364 #  define HCD_MORE_BITS_ELSE\
365   if (rlimit - p >= 3) {\
366     if (ss->FirstBitLowOrder)\
367       bits = (bits << 24) + ((unsigned int)byte_reverse_bits[p[1]] << 16) + ((unsigned int)byte_reverse_bits[p[2]] << 8) + byte_reverse_bits[p[3]];\
368     else\
369       bits = (bits << 24) + ((unsigned int)p[1] << 16) + ((unsigned int)p[2] << 8) + p[3];\
370     bits_left += 24, p += 3;\
371   } else HCD_MORE_BITS_1_ELSE
372 #endif
373 #define hcd_more_bits(outl)\
374   BEGIN HCD_MORE_BITS_ELSE goto outl; END
375 
376 #define hcd_peek_bits(n) ((bits >> (bits_left - (n))) & ((1 << (n)) - 1))
377 
378 /* hcd_peek_var_bits requires bits_left <= 8. */
379 #define hcd_peek_var_bits(n)\
380   ((bits >> (bits_left - (n))) & byte_right_mask[n])
381 
382 /* hcd_peek_bits_left requires bits_left <= 8. */
383 #define hcd_peek_bits_left()\
384   (bits & byte_right_mask[bits_left])
385 
386 #define hcd_skip_bits(n) (bits_left -= (n))
387 
388 #endif /* shc_INCLUDED */
389 
390 /* end of former shc.h */
391 
392 /* scf.h */
393 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
394 /* Common definitions for CCITTFax encoding and decoding filters */
395 
396 #ifndef scf_INCLUDED
397 #  define scf_INCLUDED
398 
399 /* #include "shc.h" */
400 
401 /*
402  * The CCITT Group 3 (T.4) and Group 4 (T.6) fax specifications map
403  * run lengths to Huffman codes.  White and black have different mappings.
404  * If the run length is 64 or greater, two or more codes are needed:
405  *      - One or more 'make-up' codes for 2560 pixels;
406  *      - A 'make-up' code that encodes the multiple of 64;
407  *      - A 'termination' code for the remainder.
408  * For runs of 63 or less, only the 'termination' code is needed.
409  */
410 
411 /* ------ Encoding tables ------ */
412 
413 /*
414  * The maximum possible length of a scan line is determined by the
415  * requirement that 3 runs have to fit into the stream buffer.
416  * A run of length N requires approximately ceil(N / 2560) makeup codes,
417  * hence 1.5 * ceil(N / 2560) bytes.  Taking the largest safe stream
418  * buffer size as 32K, we arrive at the following maximum width:
419  */
420 #if SIZEOF_INT > 2
421 #  define cfe_max_width (2560 * 32000 * 2 / 3)
422 #else
423 #  define cfe_max_width (max_int - 40)	/* avoid overflows */
424 #endif
425 /* The +5 in cfe_max_code_bytes is a little conservative. */
426 #define cfe_max_code_bytes(width) ((width) / 2560 * 3 / 2 + 5)
427 
428 typedef hce_code cfe_run;
429 
430 /* Codes common to 1-D and 2-D encoding. */
431 /* The decoding algorithms know that EOL is 0....01. */
432 #define run_eol_code_length 12
433 #define run_eol_code_value 1
434 intern_const cfe_run cf_run_eol;
435 typedef struct cf_runs_s {
436     cfe_run termination[64];
437     cfe_run make_up[41];
438 } cf_runs;
439 intern_const cf_runs
440       cf_white_runs, cf_black_runs;
441 intern_const cfe_run cf_uncompressed[6];
442 intern_const cfe_run cf_uncompressed_exit[10];	/* indexed by 2 x length of */
443 
444 			/* white run + (1 if next run black, 0 if white) */
445 /* 1-D encoding. */
446 intern_const cfe_run cf1_run_uncompressed;
447 
448 /* 2-D encoding. */
449 intern_const cfe_run cf2_run_pass;
450 
451 #define cf2_run_pass_length 4
452 #define cf2_run_pass_value 0x1
453 #define cf2_run_vertical_offset 3
454 intern_const cfe_run cf2_run_vertical[7];	/* indexed by b1 - a1 + offset */
455 intern_const cfe_run cf2_run_horizontal;
456 
457 #define cf2_run_horizontal_value 1
458 #define cf2_run_horizontal_length 3
459 intern_const cfe_run cf2_run_uncompressed;
460 
461 /* 2-D Group 3 encoding. */
462 intern_const cfe_run cf2_run_eol_1d;
463 intern_const cfe_run cf2_run_eol_2d;
464 
465 /* ------ Decoding tables ------ */
466 
467 typedef hcd_code cfd_node;
468 
469 #define run_length value
470 
471 /*
472  * The value in the decoding tables is either a white or black run length,
473  * or a (negative) exceptional value.
474  */
475 #define run_error (-1)
476 #define run_zeros (-2)	/* EOL follows, possibly with more padding first */
477 #define run_uncompressed (-3)
478 /* 2-D codes */
479 #define run2_pass (-4)
480 #define run2_horizontal (-5)
481 
482 #define cfd_white_initial_bits 8
483 #define cfd_white_min_bits 4	/* shortest white run */
484 /* intern_const cfd_node cf_white_decode[]; */
485 
486 #define cfd_black_initial_bits 7
487 #define cfd_black_min_bits 2	/* shortest black run */
488 /* intern_const cfd_node cf_black_decode[]; */
489 
490 #define cfd_2d_initial_bits 7
491 #define cfd_2d_min_bits 4	/* shortest non-H/V 2-D run */
492 /* intern_const cfd_node cf_2d_decode[]; */
493 
494 #define cfd_uncompressed_initial_bits 6		/* must be 6 */
495 /* intern_const cfd_node cf_uncompressed_decode[]; */
496 
497 /* ------ Run detection macros ------ */
498 
499 /*
500  * For the run detection macros:
501  *   white_byte is 0 or 0xff for BlackIs1 or !BlackIs1 respectively;
502  *   data holds p[-1], inverted if !BlackIs1;
503  *   count is the number of valid bits remaining in the scan line.
504  */
505 
506 /* Aliases for bit processing tables. */
507 #define cf_byte_run_length byte_bit_run_length_neg
508 #define cf_byte_run_length_0 byte_bit_run_length_0
509 
510 /* Skip over white pixels to find the next black pixel in the input. */
511 /* Store the run length in rlen, and update data, p, and count. */
512 /* There are many more white pixels in typical input than black pixels, */
513 /* and the runs of white pixels tend to be much longer, so we use */
514 /* substantially different loops for the two cases. */
515 
516 #define skip_white_pixels(data, p, count, white_byte, rlen)\
517 BEGIN\
518     rlen = cf_byte_run_length[count & 7][data ^ 0xff];\
519     if ( rlen >= 8 ) {		/* run extends past unsigned char boundary */\
520 	if ( white_byte == 0 ) {\
521 	    if ( p[0] ) { data = p[0]; p += 1; rlen -= 8; }\
522 	    else if ( p[1] ) { data = p[1]; p += 2; }\
523 	    else {\
524 		while ( !(p[2] | p[3] | p[4] | p[5]) )\
525 		    p += 4, rlen += 32;\
526 		if ( p[2] ) {\
527 		    data = p[2]; p += 3; rlen += 8;\
528 		} else if ( p[3] ) {\
529 		    data = p[3]; p += 4; rlen += 16;\
530 		} else if ( p[4] ) {\
531 		    data = p[4]; p += 5; rlen += 24;\
532 		} else /* p[5] */ {\
533 		    data = p[5]; p += 6; rlen += 32;\
534 		}\
535 	    }\
536 	} else {\
537 	    if ( p[0] != 0xff ) { data = (unsigned char)~p[0]; p += 1; rlen -= 8; }\
538 	    else if ( p[1] != 0xff ) { data = (unsigned char)~p[1]; p += 2; }\
539 	    else {\
540 		while ( (p[2] & p[3] & p[4] & p[5]) == 0xff )\
541 		    p += 4, rlen += 32;\
542 		if ( p[2] != 0xff ) {\
543 		    data = (unsigned char)~p[2]; p += 3; rlen += 8;\
544 		} else if ( p[3] != 0xff ) {\
545 		    data = (unsigned char)~p[3]; p += 4; rlen += 16;\
546 		} else if ( p[4] != 0xff ) {\
547 		    data = (unsigned char)~p[4]; p += 5; rlen += 24;\
548 		} else /* p[5] != 0xff */ {\
549 		    data = (unsigned char)~p[5]; p += 6; rlen += 32;\
550 		}\
551 	    }\
552 	}\
553 	rlen += cf_byte_run_length_0[data ^ 0xff];\
554     }\
555     count -= rlen;\
556 END
557 
558 /* Skip over black pixels to find the next white pixel in the input. */
559 /* Store the run length in rlen, and update data, p, and count. */
560 
561 #define skip_black_pixels(data, p, count, white_byte, rlen)\
562 BEGIN\
563     rlen = cf_byte_run_length[count & 7][data];\
564     if ( rlen >= 8 ) {\
565 	if ( white_byte == 0 )\
566 	    for ( ; ; p += 4, rlen += 32 ) {\
567 		if ( p[0] != 0xff ) { data = p[0]; p += 1; rlen -= 8; break; }\
568 		if ( p[1] != 0xff ) { data = p[1]; p += 2; break; }\
569 		if ( p[2] != 0xff ) { data = p[2]; p += 3; rlen += 8; break; }\
570 		if ( p[3] != 0xff ) { data = p[3]; p += 4; rlen += 16; break; }\
571 	    }\
572 	else\
573 	    for ( ; ; p += 4, rlen += 32 ) {\
574 		if ( p[0] ) { data = (unsigned char)~p[0]; p += 1; rlen -= 8; break; }\
575 		if ( p[1] ) { data = (unsigned char)~p[1]; p += 2; break; }\
576 		if ( p[2] ) { data = (unsigned char)~p[2]; p += 3; rlen += 8; break; }\
577 		if ( p[3] ) { data = (unsigned char)~p[3]; p += 4; rlen += 16; break; }\
578 	    }\
579 	rlen += cf_byte_run_length_0[data];\
580     }\
581     count -= rlen;\
582 END
583 
584 #endif /* scf_INCLUDED */
585 
586 /* end of former scf.h */
587 
588 #if USE_BUILTIN_FAXE
589 #if OBJDEP
590 #  warning PROVIDES: pts_faxe
591 #endif
592 /* scfe.c */
593 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
594 /* CCITTFax encoding filter */
595 /* #include "config2.h" */
596 /* #include "scf.h" */
597 /* #include "scfx.h" */
598 
599 /* ------ Macros and support routines ------ */
600 
601 /* Statistics */
602 
603 #ifdef DEBUG
604 
605 typedef struct stats_runs_s {
606     unsigned long termination[64];
607     unsigned long make_up[41];
608 } stats_runs_t;
609 static stats_runs_t stats_white_runs, stats_black_runs;
610 
611 #define COUNT_RUN(tab, i) (tab)[i]++;
612 
613 static void
print_run_stats(const stats_runs_t * stats)614 print_run_stats(const stats_runs_t * stats)
615 {
616     int i;
617     unsigned long total;
618 
619     for (i = 0, total = 0; i < 41; i++)
620 	dprintf1(" %lu", stats->make_up[i]),
621 	    total += stats->make_up[i];
622     dprintf1(" total=%lu\n\t", total);
623     for (i = 0, total = 0; i < 64; i++)
624 	dprintf1(" %lu", stats->termination[i]),
625 	    total += stats->termination[i];
626     dprintf1(" total=%lu\n", total);
627 }
628 
629 #else /* !DEBUG */
630 
631 #define COUNT_RUN(cnt, i) DO_NOTHING
632 
633 #endif /* DEBUG */
634 
635 /* Put a run onto the output stream. */
636 /* Free variables: q, bits, bits_left. */
637 
638 #define CF_PUT_RUN(ss, lenv, rt, stats)\
639 BEGIN\
640     cfe_run rr;\
641 \
642     if ( lenv >= 64 ) {\
643 	hce_store_state();\
644 	q = cf_put_long_run(ss, q, lenv, &rt);\
645 	hce_load_state();\
646 	lenv &= 63;\
647     }\
648     rr = rt.termination[lenv];\
649     COUNT_RUN(stats.termination, lenv);\
650     hc_put_value(ss, q, rr.code, rr.code_length);\
651 END
652 
653 static unsigned char *
cf_put_long_run(stream_CFE_state * ss,unsigned char * q,int lenv,const cf_runs * prt)654 cf_put_long_run(stream_CFE_state * ss, unsigned char * q, int lenv, const cf_runs * prt)
655 {
656     hce_declare_state;
657     cfe_run rr;
658 
659 #ifdef DEBUG
660     stats_runs_t *pstats =
661     (prt == &cf_white_runs ? &stats_white_runs : &stats_black_runs);
662 
663 #endif
664 
665     hce_load_state();
666     while (lenv >= 2560 + 64) {
667 	rr = prt->make_up[40];
668 	COUNT_RUN(pstats->make_up, 40);
669 	hc_put_value(ss, q, rr.code, rr.code_length);
670 	lenv -= 2560;
671     }
672     rr = prt->make_up[lenv >> 6];
673     COUNT_RUN(pstats->make_up, lenv >> 6);
674     hc_put_value(ss, q, rr.code, rr.code_length);
675     hce_store_state();
676     return q;
677 }
678 
679 #define CF_PUT_WHITE_RUN(ss, lenv)\
680   CF_PUT_RUN(ss, lenv, cf_white_runs, stats_white_runs)
681 
682 #define CF_PUT_BLACK_RUN(ss, lenv)\
683   CF_PUT_RUN(ss, lenv, cf_black_runs, stats_black_runs)
684 
685 /* ------ CCITTFaxEncode ------ */
686 
687 /* private_st_CFE_state(); */
688 
689 static void s_CFE_release _((stream_state *));
690 
691 /* Set default parameter values. */
692 static void
s_CFE_set_defaults(register stream_state * st)693 s_CFE_set_defaults(register stream_state * st)
694 {
695     stream_CFE_state *const ss = (stream_CFE_state *) st;
696 
697     s_CFE_set_defaults_inline(ss);
698 }
699 
700 /* Initialize CCITTFaxEncode filter */
701 static int
s_CFE_init(register stream_state * st)702 s_CFE_init(register stream_state * st)
703 {
704     stream_CFE_state *const ss = (stream_CFE_state *) st;
705     int columns = ss->Columns;
706 
707     /*
708      * The worst case for encoding is alternating white and black pixels.
709      * For 1-D encoding, the worst case is 9 bits per 2 pixels; for 2-D
710      * (horizontal), 12 bits per 2 pixels.  To fill out a scan line,
711      * we may add up to 6 12-bit EOL codes.
712      */
713     /**** pts: added UL ****/
714     int code_bytes =
715     ((columns * (ss->K == 0 ? 9UL : 12UL)) >> 4) + 20;	/* add slop */
716     int raster = ss->raster =
717 	ROUND_UP((columns + 7) >> 3, ss->DecodedByteAlign);
718 
719     s_hce_init_inline(ss);
720     ss->lbuf = ss->lprev = ss->lcode = 0;	/* in case we have to release */
721     if (columns > cfe_max_width)
722 	return PTSFAX_ERRC;
723 /****** WRONG ******/
724     /* Because skip_white_pixels can look as many as 4 bytes ahead, */
725     /* we need to allow 4 extra bytes at the end of the row buffers. */
726     ss->lbuf = (unsigned char*)gs_alloc_bytes(st->memory, raster + 4, "CFE lbuf");
727     ss->lcode = (unsigned char*)gs_alloc_bytes(st->memory, code_bytes, "CFE lcode");
728     if (ss->lbuf == 0 || ss->lcode == 0) {
729 	s_CFE_release(st);
730 	return PTSFAX_ERRC;
731 /****** WRONG ******/
732     }
733     if (ss->K != 0) {
734 	ss->lprev = (unsigned char*)gs_alloc_bytes(st->memory, raster + 4, "CFE lprev");
735 	if (ss->lprev == 0) {
736 	    s_CFE_release(st);
737 	    return PTSFAX_ERRC;
738 /****** WRONG ******/
739 	}
740 	/* Clear the initial reference line for 2-D encoding. */
741 	/* Make sure it is terminated properly. */
742 	ss->memset_(ss->lprev, (ss->BlackIs1 ? 0 : 0xff), raster);
743 	if (columns & 7)
744 	    ss->lprev[raster - 1] ^= 0x80 >> (columns & 7);
745 	else
746 	    ss->lprev[raster] = ~ss->lprev[0];
747     }
748     ss->read_count = raster;
749     ss->write_count = 0;
750     ss->k_left = (ss->K > 0 ? 1 : ss->K);
751     ss->max_code_bytes = code_bytes;
752     return 0;
753 }
754 
755 /* Release the filter. */
756 static void
s_CFE_release(stream_state * st)757 s_CFE_release(stream_state * st)
758 {
759     stream_CFE_state *const ss = (stream_CFE_state *) st;
760 
761     gs_free_object(st->memory, ss->lprev, "CFE lprev(close)");
762     gs_free_object(st->memory, ss->lcode, "CFE lcode(close)");
763     gs_free_object(st->memory, ss->lbuf, "CFE lbuf(close)");
764 }
765 
766 /* Flush the buffer */
767 static void cf_encode_1d _((stream_CFE_state *, const unsigned char *,
768 			     stream_cursor_write *));
769 static void cf_encode_2d _((stream_CFE_state *, const unsigned char *,
770 			     stream_cursor_write *, const unsigned char *));
771 static int
s_CFE_process(stream_state * st,stream_cursor_read * pr,stream_cursor_write * pw,bool last)772 s_CFE_process(stream_state * st, stream_cursor_read * pr,
773 	      stream_cursor_write * pw, bool last)
774 {
775     stream_CFE_state *const ss = (stream_CFE_state *) st;
776     const unsigned char *rlimit = pr->limit;
777     unsigned char *wlimit = pw->limit;
778     int raster = ss->raster;
779     unsigned char end_mask = 1 << (-ss->Columns & 7);
780     int status = 0;
781 
782     for (;;) {
783 	stream_cursor_write w;
784 
785 	if_debug2('w', "[w]CFE: read_count = %d, write_count=%d,\n",
786 		  ss->read_count, ss->write_count);
787 	if_debug6('w', "    pr = 0x%lx(%d)0x%lx, pw = 0x%lx(%d)0x%lx\n",
788 		  (unsigned long) pr->ptr, (int)(rlimit - pr->ptr), (unsigned long) rlimit,
789 		  (unsigned long) pw->ptr, (int)(wlimit - pw->ptr), (unsigned long) wlimit);
790 	if (ss->write_count) {
791 	    /* Copy more of an encoded line to the caller. */
792 	    int wcount = wlimit - pw->ptr;
793 	    int ccount = min(wcount, ss->write_count);
794 
795 	    ss->memcpy_(pw->ptr + 1, ss->lcode + ss->code_bytes - ss->write_count,
796 		   ccount);
797 	    pw->ptr += ccount;
798 	    if ((ss->write_count -= ccount) > 0) {
799 		status = 1;
800 		break;
801 	    }
802 	}
803 	if (ss->read_count) {
804 	    /* Copy more of an unencoded line from the caller. */
805 	    int rcount = rlimit - pr->ptr;
806 	    int ccount = min(rcount, ss->read_count);
807 
808 	    if (rcount == 0 && last)
809 		break;
810 	    ss->memcpy_(ss->lbuf + raster - ss->read_count,
811 		   pr->ptr + 1, ccount);
812 	    pr->ptr += ccount;
813 	    if ((ss->read_count -= ccount) != 0)
814 		break;
815 	}
816 	/*
817 	 * We have a full scan line in lbuf.  Ensure that it ends with
818 	 * two polarity changes.
819 	 */
820 	{
821 	    unsigned char *end = ss->lbuf + raster - 1;
822 	    unsigned char end_bit = *end & end_mask;
823 	    unsigned char not_bit = end_bit ^ end_mask;
824 
825 	    *end &= -end_mask;
826 	    if (end_mask == 1)
827 		end[1] = (end_bit ? 0x40 : 0x80);
828 	    else if (end_mask == 2)
829 		*end |= not_bit >> 1, end[1] = end_bit << 7;
830 	    else
831 		*end |= (not_bit >> 1) | (end_bit >> 2);
832 	}
833 	/*
834 	 * Write the output directly to the caller's buffer if it's large
835 	 * enough, otherwise to our own buffer.
836 	 */
837 	if (wlimit - pw->ptr >= ss->max_code_bytes) {
838 	    w = *pw;
839 	} else {
840 	    w.ptr = ss->lcode - 1;
841 	    w.limit = w.ptr + ss->max_code_bytes;
842 	}
843 #ifdef DEBUG
844 	if (ss->K > 0) {
845 	    if_debug1('w', "[w]new row, k_left=%d\n",
846 		      ss->k_left);
847 	} else {
848 	    if_debug0('w', "[w]new row\n");
849 	}
850 #endif
851 	/*
852 	 * Write an EOL (actually a "beginning of line") if requested.
853 	 */
854 	if (ss->EndOfLine) {
855 	    const cfe_run *rp =
856 	    (ss->K <= 0 ? &cf_run_eol :
857 	     ss->k_left > 1 ? &cf2_run_eol_2d :
858 	     &cf2_run_eol_1d);
859 	    cfe_run run;
860 
861 	    hce_declare_state;
862 
863 	    hce_load_state();
864 	    if (ss->EncodedByteAlign) {
865 		run = *rp;
866 		/* Pad the run on the left */
867 		/* so it winds up unsigned char-aligned. */
868 		run.code_length +=
869 		    (bits_left - run_eol_code_length) & 7;
870 		if (run.code_length > 16)	/* <= 23 */
871 		    bits_left -= run.code_length & 7,
872 			run.code_length = 16;
873 		rp = &run;
874 	    }
875 	    hc_put_code(ss, w.ptr, rp);
876 	    hce_store_state();
877 	} else if (ss->EncodedByteAlign)
878 	    ss->bits_left &= ~7;
879 	/* Encode the line. */
880 	if (ss->K == 0)
881 	    cf_encode_1d(ss, ss->lbuf, &w);	/* pure 1-D */
882 	else if (ss->K < 0)
883 	    cf_encode_2d(ss, ss->lbuf, &w, ss->lprev);	/* pure 2-D */
884 	else if (--(ss->k_left))	/* mixed, use 2-D */
885 	    cf_encode_2d(ss, ss->lbuf, &w, ss->lprev);
886 	else {			/* mixed, use 1-D */
887 	    cf_encode_1d(ss, ss->lbuf, &w);
888 	    ss->k_left = ss->K;
889 	}
890 	/*
891 	 * If we didn't write directly to the client's buffer, schedule
892 	 * the output data to be written.
893 	 */
894 	if (w.limit == wlimit)
895 	    pw->ptr = w.ptr;
896 	else
897 	    ss->write_count = ss->code_bytes = w.ptr - (ss->lcode - 1);
898 	if (ss->K != 0) {
899 	    /* In 2-D modes, swap the current and previous scan lines. */
900 	    unsigned char *temp = ss->lbuf;
901 
902 	    ss->lbuf = ss->lprev;
903 	    ss->lprev = temp;
904 	}
905 	/* Note that the input buffer needs refilling. */
906 	ss->read_count = raster;
907     }
908     /*
909      * When we exit from the loop, we know that write_count = 0, and
910      * there is no line waiting to be processed in the input buffer.
911      */
912     if (last && status == 0) {
913 	const cfe_run *rp =
914 	(ss->K > 0 ? &cf2_run_eol_1d : &cf_run_eol);
915 	int i = (!ss->EndOfBlock ? 0 : ss->K < 0 ? 2 : 6);
916 	unsigned int bits_to_write =
917 	hc_bits_size - ss->bits_left + i * rp->code_length;
918 	unsigned char *q = pw->ptr;
919 
920 	hce_declare_state;
921 
922 	if (0U+(wlimit - q) < (bits_to_write + 7) >> 3) { /* PTS_UNSIGNED */
923 	    status = 1;
924 	    goto out;
925 	}
926 	hce_load_state();
927 	if (ss->EncodedByteAlign)
928 	    bits_left &= ~7;
929 	while (--i >= 0)
930 	    hc_put_code(ss, q, rp);
931 	/* Force out the last unsigned char or bytes. */
932 	pw->ptr = hc_put_last_bits((stream_hc_state *) ss, q);
933     }
934   out:
935     if_debug9('w', "[w]CFE exit %d: read_count = %d, write_count = %d,\n     pr = 0x%lx(%d)0x%lx; pw = 0x%lx(%d)0x%lx\n",
936 	      status, ss->read_count, ss->write_count,
937 	      (unsigned long) pr->ptr, (int)(rlimit - pr->ptr), (unsigned long) rlimit,
938 	      (unsigned long) pw->ptr, (int)(wlimit - pw->ptr), (unsigned long) wlimit);
939 #ifdef DEBUG
940     if (pr->ptr > rlimit || pw->ptr > wlimit) {
941 	lprintf("Pointer overrun!\n");
942 	status = PTSFAX_ERRC;
943     }
944     if (gs_debug_c('w') && status == 1) {
945 	dlputs("[w]white runs:");
946 	print_run_stats(&stats_white_runs);
947 	dlputs("[w]black runs:");
948 	print_run_stats(&stats_black_runs);
949     }
950 #endif
951     return status;
952 }
953 
954 /* Encode a 1-D scan line. */
955 static void
cf_encode_1d(stream_CFE_state * ss,const unsigned char * lbuf,stream_cursor_write * pw)956 cf_encode_1d(stream_CFE_state * ss, const unsigned char * lbuf, stream_cursor_write * pw)
957 {
958     unsigned int count = ss->raster << 3;
959     unsigned char *q = pw->ptr;
960     int end_count = -ss->Columns & 7;
961     int rlen;
962 
963     hce_declare_state;
964     const unsigned char *p = lbuf;
965     unsigned char invert = (ss->BlackIs1 ? 0 : 0xff);
966 
967     /* Invariant: data = p[-1] ^ invert. */
968     unsigned int data = *p++ ^ invert;
969 
970     hce_load_state();
971     while (count != 0U+end_count) { /* PTS_UNSIGNED */
972 	/* Parse a white run. */
973 	skip_white_pixels(data, p, count, invert, rlen);
974 	CF_PUT_WHITE_RUN(ss, rlen);
975 	if (count == 0U+end_count) /* PTS_UNSIGNED */
976 	    break;
977 	/* Parse a black run. */
978 	skip_black_pixels(data, p, count, invert, rlen);
979 	CF_PUT_BLACK_RUN(ss, rlen);
980     }
981     hce_store_state();
982     pw->ptr = q;
983 }
984 
985 /* Encode a 2-D scan line. */
986 static void
cf_encode_2d(stream_CFE_state * ss,const unsigned char * lbuf,stream_cursor_write * pw,const unsigned char * lprev)987 cf_encode_2d(stream_CFE_state * ss, const unsigned char * lbuf, stream_cursor_write * pw,
988 	     const unsigned char * lprev)
989 {
990     unsigned char invert_white = (ss->BlackIs1 ? 0 : 0xff);
991     unsigned char invert = invert_white;
992     unsigned int count = ss->raster << 3;
993     int end_count = -ss->Columns & 7;
994     const unsigned char *p = lbuf;
995     unsigned char *q = pw->ptr;
996     unsigned int data = *p++ ^ invert;
997 
998     hce_declare_state;
999     /*
1000      * In order to handle the nominal 'changing white' at the beginning of
1001      * each scan line, we need to suppress the test for an initial black bit
1002      * in the reference line when we are at the very beginning of the scan
1003      * line.  To avoid an extra test, we use two different mask tables.
1004      */
1005     static const unsigned char initial_count_bit[8] =
1006     {
1007 	0, 1, 2, 4, 8, 0x10, 0x20, 0x40
1008     };
1009     static const unsigned char further_count_bit[8] =
1010     {
1011 	0x80, 1, 2, 4, 8, 0x10, 0x20, 0x40
1012     };
1013     const unsigned char *count_bit = initial_count_bit;
1014 
1015     hce_load_state();
1016     while (count != 0U+end_count) { /* PTS_UNSIGNED */
1017 	/*
1018 	 * If invert == invert_white, white and black have their
1019 	 * correct meanings; if invert == ~invert_white,
1020 	 * black and white are interchanged.
1021 	 */
1022 	unsigned int a0 = count;
1023 	unsigned int a1;
1024 
1025 #define b1 (a1 - diff)		/* only for printing */
1026 	int diff;
1027 	unsigned int prev_count = count;
1028 	const unsigned char *prev_p = p - lbuf + lprev;
1029 	unsigned char prev_data = prev_p[-1] ^ invert;
1030 	int rlen;
1031 
1032 	/* Find the a1 and b1 transitions. */
1033 	skip_white_pixels(data, p, count, invert, rlen);
1034 	a1 = count;
1035 	if ((prev_data & count_bit[prev_count & 7])) {
1036 	    /* Look for changing white first. */
1037 	    skip_black_pixels(prev_data, prev_p, prev_count, invert, rlen);
1038 	}
1039 	count_bit = further_count_bit;	/* no longer at beginning */
1040       pass:
1041 	if (prev_count != 0U+end_count) /* PTS_UNSIGNED */
1042 	    skip_white_pixels(prev_data, prev_p, prev_count, invert, rlen);
1043 	diff = a1 - prev_count;	/* i.e., logical b1 - a1 */
1044 	/* In all the comparisons below, remember that count */
1045 	/* runs downward, not upward, so the comparisons are */
1046 	/* reversed. */
1047 	if (diff <= -2) {
1048 	    /* Could be a pass mode.  Find b2. */
1049 	    if (prev_count != 0U+ end_count) /* PTS_UNSIGNED */
1050 		skip_black_pixels(prev_data, prev_p,
1051 				  prev_count, invert, rlen);
1052 	    if (prev_count > a1) {
1053 		/* Use pass mode. */
1054 		if_debug4('W', "[W]pass: count = %d, a1 = %d, b1 = %d, new count = %d\n",
1055 			  a0, a1, b1, prev_count);
1056 		hc_put_value(ss, q, cf2_run_pass_value, cf2_run_pass_length);
1057 		a0 = prev_count;
1058 		goto pass;
1059 	    }
1060 	}
1061 	/* Check for vertical coding. */
1062 	if (diff <= 3 && diff >= -3) {
1063 	    /* Use vertical coding. */
1064 	    const cfe_run *cp = &cf2_run_vertical[diff + 3];
1065 
1066 	    if_debug5('W', "[W]vertical %d: count = %d, a1 = %d, b1 = %d, new count = %d\n",
1067 		      diff, a0, a1, b1, count);
1068 	    hc_put_code(ss, q, cp);
1069 	    invert = ~invert;	/* a1 polarity changes */
1070 	    data ^= 0xff;
1071 	    continue;
1072 	}
1073 	/* No luck, use horizontal coding. */
1074 	if (count != 0U+end_count) /* PTS_UNSIGNED */
1075 	    skip_black_pixels(data, p, count, invert, rlen);	/* find a2 */
1076 	hc_put_value(ss, q, cf2_run_horizontal_value,
1077 		     cf2_run_horizontal_length);
1078 	a0 -= a1;
1079 	a1 -= count;
1080 	if (invert == invert_white) {
1081 	    if_debug3('W', "[W]horizontal: white = %d, black = %d, new count = %d\n",
1082 		      a0, a1, count);
1083 	    CF_PUT_WHITE_RUN(ss, a0);
1084 	    CF_PUT_BLACK_RUN(ss, a1);
1085 	} else {
1086 	    if_debug3('W', "[W]horizontal: black = %d, white = %d, new count = %d\n",
1087 		      a0, a1, count);
1088 	    CF_PUT_BLACK_RUN(ss, a0);
1089 	    CF_PUT_WHITE_RUN(ss, a1);
1090 #undef b1
1091 	}
1092     }
1093     hce_store_state();
1094     pw->ptr = q;
1095 }
1096 
1097 /* Stream template */
1098 const stream_template s_CFE_template = {
1099     /*0, &st_CFE_state*/ s_CFE_init, s_CFE_process, 1, 1,
1100     s_CFE_release, s_CFE_set_defaults, 0
1101 };
1102 /* end of former scfe.c */
1103 
1104 /* scfetab.c */
1105 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
1106 /* Tables for CCITTFaxEncode filter */
1107 /* #include "scommon.h" */		/* for scf.h */
1108 /* #include "scf.h" */
1109 
1110 /* We make this a separate file so that it can be used by */
1111 /* the program that generates the tables for the CCITTFaxDecode filter. */
1112 
1113 /* ------ Run encoding tables ------ */
1114 
1115 /* Abbreviate hce_entry to make the file smaller. */
1116 #define RUN(c,len) hce_entry(c,len)
1117 
1118 /* Define the end-of-line code. */
1119 /* Code in scfd.c and scfdgen.c knows that the run value is 1. */
1120 impl_const cfe_run cf_run_eol =
1121 RUN(run_eol_code_value, run_eol_code_length);
1122 
1123 /* Define the 1-D code that signals uncompressed data. */
1124 impl_const cfe_run cf1_run_uncompressed =
1125 RUN(0xf, 12);
1126 
1127 /* Define the 2-D run codes. */
1128 impl_const cfe_run cf2_run_pass =
1129 RUN(cf2_run_pass_value, cf2_run_pass_length);
1130 impl_const cfe_run cf2_run_vertical[7] =
1131 {
1132     RUN(0x3, 7),
1133     RUN(0x3, 6),
1134     RUN(0x3, 3),
1135     RUN(0x1, 1),
1136     RUN(0x2, 3),
1137     RUN(0x2, 6),
1138     RUN(0x2, 7)
1139 };
1140 impl_const cfe_run cf2_run_horizontal =
1141 RUN(cf2_run_horizontal_value, cf2_run_horizontal_length);
1142 impl_const cfe_run cf2_run_uncompressed =
1143 RUN(0xf, 10);
1144 
1145 /* EOL codes for Group 3 2-D. */
1146 /* Code in scfd.c knows that these are 0...01x. */
1147 impl_const cfe_run cf2_run_eol_1d =
1148 RUN((run_eol_code_value << 1) + 1, run_eol_code_length + 1);
1149 impl_const cfe_run cf2_run_eol_2d =
1150 RUN((run_eol_code_value << 1) + 0, run_eol_code_length + 1);
1151 
1152 /* White run codes. */
1153 impl_const cf_runs cf_white_runs =
1154 {
1155     {				/* Termination codes */
1156 	RUN(0x35, 8), RUN(0x7, 6), RUN(0x7, 4), RUN(0x8, 4),
1157 	RUN(0xb, 4), RUN(0xc, 4), RUN(0xe, 4), RUN(0xf, 4),
1158 	RUN(0x13, 5), RUN(0x14, 5), RUN(0x7, 5), RUN(0x8, 5),
1159 	RUN(0x8, 6), RUN(0x3, 6), RUN(0x34, 6), RUN(0x35, 6),
1160 	RUN(0x2a, 6), RUN(0x2b, 6), RUN(0x27, 7), RUN(0xc, 7),
1161 	RUN(0x8, 7), RUN(0x17, 7), RUN(0x3, 7), RUN(0x4, 7),
1162 	RUN(0x28, 7), RUN(0x2b, 7), RUN(0x13, 7), RUN(0x24, 7),
1163 	RUN(0x18, 7), RUN(0x2, 8), RUN(0x3, 8), RUN(0x1a, 8),
1164 	RUN(0x1b, 8), RUN(0x12, 8), RUN(0x13, 8), RUN(0x14, 8),
1165 	RUN(0x15, 8), RUN(0x16, 8), RUN(0x17, 8), RUN(0x28, 8),
1166 	RUN(0x29, 8), RUN(0x2a, 8), RUN(0x2b, 8), RUN(0x2c, 8),
1167 	RUN(0x2d, 8), RUN(0x4, 8), RUN(0x5, 8), RUN(0xa, 8),
1168 	RUN(0xb, 8), RUN(0x52, 8), RUN(0x53, 8), RUN(0x54, 8),
1169 	RUN(0x55, 8), RUN(0x24, 8), RUN(0x25, 8), RUN(0x58, 8),
1170 	RUN(0x59, 8), RUN(0x5a, 8), RUN(0x5b, 8), RUN(0x4a, 8),
1171 	RUN(0x4b, 8), RUN(0x32, 8), RUN(0x33, 8), RUN(0x34, 8)
1172     },
1173     {				/* Make-up codes */
1174 	RUN(0, 0) /* dummy */ , RUN(0x1b, 5), RUN(0x12, 5), RUN(0x17, 6),
1175 	RUN(0x37, 7), RUN(0x36, 8), RUN(0x37, 8), RUN(0x64, 8),
1176 	RUN(0x65, 8), RUN(0x68, 8), RUN(0x67, 8), RUN(0xcc, 9),
1177 	RUN(0xcd, 9), RUN(0xd2, 9), RUN(0xd3, 9), RUN(0xd4, 9),
1178 	RUN(0xd5, 9), RUN(0xd6, 9), RUN(0xd7, 9), RUN(0xd8, 9),
1179 	RUN(0xd9, 9), RUN(0xda, 9), RUN(0xdb, 9), RUN(0x98, 9),
1180 	RUN(0x99, 9), RUN(0x9a, 9), RUN(0x18, 6), RUN(0x9b, 9),
1181 	RUN(0x8, 11), RUN(0xc, 11), RUN(0xd, 11), RUN(0x12, 12),
1182 	RUN(0x13, 12), RUN(0x14, 12), RUN(0x15, 12), RUN(0x16, 12),
1183 	RUN(0x17, 12), RUN(0x1c, 12), RUN(0x1d, 12), RUN(0x1e, 12),
1184 	RUN(0x1f, 12)
1185     }
1186 };
1187 
1188 /* Black run codes. */
1189 impl_const cf_runs cf_black_runs =
1190 {
1191     {				/* Termination codes */
1192 	RUN(0x37, 10), RUN(0x2, 3), RUN(0x3, 2), RUN(0x2, 2),
1193 	RUN(0x3, 3), RUN(0x3, 4), RUN(0x2, 4), RUN(0x3, 5),
1194 	RUN(0x5, 6), RUN(0x4, 6), RUN(0x4, 7), RUN(0x5, 7),
1195 	RUN(0x7, 7), RUN(0x4, 8), RUN(0x7, 8), RUN(0x18, 9),
1196 	RUN(0x17, 10), RUN(0x18, 10), RUN(0x8, 10), RUN(0x67, 11),
1197 	RUN(0x68, 11), RUN(0x6c, 11), RUN(0x37, 11), RUN(0x28, 11),
1198 	RUN(0x17, 11), RUN(0x18, 11), RUN(0xca, 12), RUN(0xcb, 12),
1199 	RUN(0xcc, 12), RUN(0xcd, 12), RUN(0x68, 12), RUN(0x69, 12),
1200 	RUN(0x6a, 12), RUN(0x6b, 12), RUN(0xd2, 12), RUN(0xd3, 12),
1201 	RUN(0xd4, 12), RUN(0xd5, 12), RUN(0xd6, 12), RUN(0xd7, 12),
1202 	RUN(0x6c, 12), RUN(0x6d, 12), RUN(0xda, 12), RUN(0xdb, 12),
1203 	RUN(0x54, 12), RUN(0x55, 12), RUN(0x56, 12), RUN(0x57, 12),
1204 	RUN(0x64, 12), RUN(0x65, 12), RUN(0x52, 12), RUN(0x53, 12),
1205 	RUN(0x24, 12), RUN(0x37, 12), RUN(0x38, 12), RUN(0x27, 12),
1206 	RUN(0x28, 12), RUN(0x58, 12), RUN(0x59, 12), RUN(0x2b, 12),
1207 	RUN(0x2c, 12), RUN(0x5a, 12), RUN(0x66, 12), RUN(0x67, 12)
1208     },
1209     {				/* Make-up codes. */
1210 	RUN(0, 0) /* dummy */ , RUN(0xf, 10), RUN(0xc8, 12), RUN(0xc9, 12),
1211 	RUN(0x5b, 12), RUN(0x33, 12), RUN(0x34, 12), RUN(0x35, 12),
1212 	RUN(0x6c, 13), RUN(0x6d, 13), RUN(0x4a, 13), RUN(0x4b, 13),
1213 	RUN(0x4c, 13), RUN(0x4d, 13), RUN(0x72, 13), RUN(0x73, 13),
1214 	RUN(0x74, 13), RUN(0x75, 13), RUN(0x76, 13), RUN(0x77, 13),
1215 	RUN(0x52, 13), RUN(0x53, 13), RUN(0x54, 13), RUN(0x55, 13),
1216 	RUN(0x5a, 13), RUN(0x5b, 13), RUN(0x64, 13), RUN(0x65, 13),
1217 	RUN(0x8, 11), RUN(0xc, 11), RUN(0xd, 11), RUN(0x12, 12),
1218 	RUN(0x13, 12), RUN(0x14, 12), RUN(0x15, 12), RUN(0x16, 12),
1219 	RUN(0x17, 12), RUN(0x1c, 12), RUN(0x1d, 12), RUN(0x1e, 12),
1220 	RUN(0x1f, 12)
1221     }
1222 };
1223 
1224 /* Uncompressed codes. */
1225 impl_const cfe_run cf_uncompressed[6] =
1226 {
1227     RUN(1, 1),
1228     RUN(1, 2),
1229     RUN(1, 3),
1230     RUN(1, 4),
1231     RUN(1, 5),
1232     RUN(1, 6)
1233 };
1234 
1235 /* Uncompressed exit codes. */
1236 impl_const cfe_run cf_uncompressed_exit[10] =
1237 {
1238     RUN(2, 8), RUN(3, 8),
1239     RUN(2, 9), RUN(3, 9),
1240     RUN(2, 10), RUN(3, 10),
1241     RUN(2, 11), RUN(3, 11),
1242     RUN(2, 12), RUN(3, 12)
1243 };
1244 
1245 #if 0
1246 /* Some C compilers insist on having executable code in every file.... */
1247 void scfetab_dummy _((void));	/* for picky compilers */
1248 void
1249 scfetab_dummy(void)
1250 {
1251 }
1252 #endif
1253 
1254 /* end of former scfetab.c */
1255 
1256 #endif /* USE_BUILTIN_FAXE */
1257 
1258 #if USE_BUILTIN_FAXD
1259 #if OBJDEP
1260 #  warning PROVIDES: pts_faxd
1261 #endif
1262 /* scfdtab.c; must be before scfd.c */
1263 /* #include "scommon.h" */		/* for scf.h */
1264 /* #include "scf.h" */
1265 
1266 /* White decoding table. */
1267 impl_const cfd_node cf_white_decode[] = {
1268 	{ 256, 12 },
1269 	{ 272, 12 },
1270 	{ 29, 8 },
1271 	{ 30, 8 },
1272 	{ 45, 8 },
1273 	{ 46, 8 },
1274 	{ 22, 7 },
1275 	{ 22, 7 },
1276 	{ 23, 7 },
1277 	{ 23, 7 },
1278 	{ 47, 8 },
1279 	{ 48, 8 },
1280 	{ 13, 6 },
1281 	{ 13, 6 },
1282 	{ 13, 6 },
1283 	{ 13, 6 },
1284 	{ 20, 7 },
1285 	{ 20, 7 },
1286 	{ 33, 8 },
1287 	{ 34, 8 },
1288 	{ 35, 8 },
1289 	{ 36, 8 },
1290 	{ 37, 8 },
1291 	{ 38, 8 },
1292 	{ 19, 7 },
1293 	{ 19, 7 },
1294 	{ 31, 8 },
1295 	{ 32, 8 },
1296 	{ 1, 6 },
1297 	{ 1, 6 },
1298 	{ 1, 6 },
1299 	{ 1, 6 },
1300 	{ 12, 6 },
1301 	{ 12, 6 },
1302 	{ 12, 6 },
1303 	{ 12, 6 },
1304 	{ 53, 8 },
1305 	{ 54, 8 },
1306 	{ 26, 7 },
1307 	{ 26, 7 },
1308 	{ 39, 8 },
1309 	{ 40, 8 },
1310 	{ 41, 8 },
1311 	{ 42, 8 },
1312 	{ 43, 8 },
1313 	{ 44, 8 },
1314 	{ 21, 7 },
1315 	{ 21, 7 },
1316 	{ 28, 7 },
1317 	{ 28, 7 },
1318 	{ 61, 8 },
1319 	{ 62, 8 },
1320 	{ 63, 8 },
1321 	{ 0, 8 },
1322 	{ 320, 8 },
1323 	{ 384, 8 },
1324 	{ 10, 5 },
1325 	{ 10, 5 },
1326 	{ 10, 5 },
1327 	{ 10, 5 },
1328 	{ 10, 5 },
1329 	{ 10, 5 },
1330 	{ 10, 5 },
1331 	{ 10, 5 },
1332 	{ 11, 5 },
1333 	{ 11, 5 },
1334 	{ 11, 5 },
1335 	{ 11, 5 },
1336 	{ 11, 5 },
1337 	{ 11, 5 },
1338 	{ 11, 5 },
1339 	{ 11, 5 },
1340 	{ 27, 7 },
1341 	{ 27, 7 },
1342 	{ 59, 8 },
1343 	{ 60, 8 },
1344 	{ 288, 9 },
1345 	{ 290, 9 },
1346 	{ 18, 7 },
1347 	{ 18, 7 },
1348 	{ 24, 7 },
1349 	{ 24, 7 },
1350 	{ 49, 8 },
1351 	{ 50, 8 },
1352 	{ 51, 8 },
1353 	{ 52, 8 },
1354 	{ 25, 7 },
1355 	{ 25, 7 },
1356 	{ 55, 8 },
1357 	{ 56, 8 },
1358 	{ 57, 8 },
1359 	{ 58, 8 },
1360 	{ 192, 6 },
1361 	{ 192, 6 },
1362 	{ 192, 6 },
1363 	{ 192, 6 },
1364 	{ 1664, 6 },
1365 	{ 1664, 6 },
1366 	{ 1664, 6 },
1367 	{ 1664, 6 },
1368 	{ 448, 8 },
1369 	{ 512, 8 },
1370 	{ 292, 9 },
1371 	{ 640, 8 },
1372 	{ 576, 8 },
1373 	{ 294, 9 },
1374 	{ 296, 9 },
1375 	{ 298, 9 },
1376 	{ 300, 9 },
1377 	{ 302, 9 },
1378 	{ 256, 7 },
1379 	{ 256, 7 },
1380 	{ 2, 4 },
1381 	{ 2, 4 },
1382 	{ 2, 4 },
1383 	{ 2, 4 },
1384 	{ 2, 4 },
1385 	{ 2, 4 },
1386 	{ 2, 4 },
1387 	{ 2, 4 },
1388 	{ 2, 4 },
1389 	{ 2, 4 },
1390 	{ 2, 4 },
1391 	{ 2, 4 },
1392 	{ 2, 4 },
1393 	{ 2, 4 },
1394 	{ 2, 4 },
1395 	{ 2, 4 },
1396 	{ 3, 4 },
1397 	{ 3, 4 },
1398 	{ 3, 4 },
1399 	{ 3, 4 },
1400 	{ 3, 4 },
1401 	{ 3, 4 },
1402 	{ 3, 4 },
1403 	{ 3, 4 },
1404 	{ 3, 4 },
1405 	{ 3, 4 },
1406 	{ 3, 4 },
1407 	{ 3, 4 },
1408 	{ 3, 4 },
1409 	{ 3, 4 },
1410 	{ 3, 4 },
1411 	{ 3, 4 },
1412 	{ 128, 5 },
1413 	{ 128, 5 },
1414 	{ 128, 5 },
1415 	{ 128, 5 },
1416 	{ 128, 5 },
1417 	{ 128, 5 },
1418 	{ 128, 5 },
1419 	{ 128, 5 },
1420 	{ 8, 5 },
1421 	{ 8, 5 },
1422 	{ 8, 5 },
1423 	{ 8, 5 },
1424 	{ 8, 5 },
1425 	{ 8, 5 },
1426 	{ 8, 5 },
1427 	{ 8, 5 },
1428 	{ 9, 5 },
1429 	{ 9, 5 },
1430 	{ 9, 5 },
1431 	{ 9, 5 },
1432 	{ 9, 5 },
1433 	{ 9, 5 },
1434 	{ 9, 5 },
1435 	{ 9, 5 },
1436 	{ 16, 6 },
1437 	{ 16, 6 },
1438 	{ 16, 6 },
1439 	{ 16, 6 },
1440 	{ 17, 6 },
1441 	{ 17, 6 },
1442 	{ 17, 6 },
1443 	{ 17, 6 },
1444 	{ 4, 4 },
1445 	{ 4, 4 },
1446 	{ 4, 4 },
1447 	{ 4, 4 },
1448 	{ 4, 4 },
1449 	{ 4, 4 },
1450 	{ 4, 4 },
1451 	{ 4, 4 },
1452 	{ 4, 4 },
1453 	{ 4, 4 },
1454 	{ 4, 4 },
1455 	{ 4, 4 },
1456 	{ 4, 4 },
1457 	{ 4, 4 },
1458 	{ 4, 4 },
1459 	{ 4, 4 },
1460 	{ 5, 4 },
1461 	{ 5, 4 },
1462 	{ 5, 4 },
1463 	{ 5, 4 },
1464 	{ 5, 4 },
1465 	{ 5, 4 },
1466 	{ 5, 4 },
1467 	{ 5, 4 },
1468 	{ 5, 4 },
1469 	{ 5, 4 },
1470 	{ 5, 4 },
1471 	{ 5, 4 },
1472 	{ 5, 4 },
1473 	{ 5, 4 },
1474 	{ 5, 4 },
1475 	{ 5, 4 },
1476 	{ 14, 6 },
1477 	{ 14, 6 },
1478 	{ 14, 6 },
1479 	{ 14, 6 },
1480 	{ 15, 6 },
1481 	{ 15, 6 },
1482 	{ 15, 6 },
1483 	{ 15, 6 },
1484 	{ 64, 5 },
1485 	{ 64, 5 },
1486 	{ 64, 5 },
1487 	{ 64, 5 },
1488 	{ 64, 5 },
1489 	{ 64, 5 },
1490 	{ 64, 5 },
1491 	{ 64, 5 },
1492 	{ 6, 4 },
1493 	{ 6, 4 },
1494 	{ 6, 4 },
1495 	{ 6, 4 },
1496 	{ 6, 4 },
1497 	{ 6, 4 },
1498 	{ 6, 4 },
1499 	{ 6, 4 },
1500 	{ 6, 4 },
1501 	{ 6, 4 },
1502 	{ 6, 4 },
1503 	{ 6, 4 },
1504 	{ 6, 4 },
1505 	{ 6, 4 },
1506 	{ 6, 4 },
1507 	{ 6, 4 },
1508 	{ 7, 4 },
1509 	{ 7, 4 },
1510 	{ 7, 4 },
1511 	{ 7, 4 },
1512 	{ 7, 4 },
1513 	{ 7, 4 },
1514 	{ 7, 4 },
1515 	{ 7, 4 },
1516 	{ 7, 4 },
1517 	{ 7, 4 },
1518 	{ 7, 4 },
1519 	{ 7, 4 },
1520 	{ 7, 4 },
1521 	{ 7, 4 },
1522 	{ 7, 4 },
1523 	{ 7, 4 },
1524 	{ -2, 3 },
1525 	{ -2, 3 },
1526 	{ -1, 0 },
1527 	{ -1, 0 },
1528 	{ -1, 0 },
1529 	{ -1, 0 },
1530 	{ -1, 0 },
1531 	{ -1, 0 },
1532 	{ -1, 0 },
1533 	{ -1, 0 },
1534 	{ -1, 0 },
1535 	{ -1, 0 },
1536 	{ -1, 0 },
1537 	{ -1, 0 },
1538 	{ -1, 0 },
1539 	{ -3, 4 },
1540 	{ 1792, 3 },
1541 	{ 1792, 3 },
1542 	{ 1984, 4 },
1543 	{ 2048, 4 },
1544 	{ 2112, 4 },
1545 	{ 2176, 4 },
1546 	{ 2240, 4 },
1547 	{ 2304, 4 },
1548 	{ 1856, 3 },
1549 	{ 1856, 3 },
1550 	{ 1920, 3 },
1551 	{ 1920, 3 },
1552 	{ 2368, 4 },
1553 	{ 2432, 4 },
1554 	{ 2496, 4 },
1555 	{ 2560, 4 },
1556 	{ 1472, 1 },
1557 	{ 1536, 1 },
1558 	{ 1600, 1 },
1559 	{ 1728, 1 },
1560 	{ 704, 1 },
1561 	{ 768, 1 },
1562 	{ 832, 1 },
1563 	{ 896, 1 },
1564 	{ 960, 1 },
1565 	{ 1024, 1 },
1566 	{ 1088, 1 },
1567 	{ 1152, 1 },
1568 	{ 1216, 1 },
1569 	{ 1280, 1 },
1570 	{ 1344, 1 },
1571 	{ 1408, 1 }
1572 };
1573 
1574 /* Black decoding table. */
1575 impl_const cfd_node cf_black_decode[] = {
1576 	{ 128, 12 },
1577 	{ 160, 13 },
1578 	{ 224, 12 },
1579 	{ 256, 12 },
1580 	{ 10, 7 },
1581 	{ 11, 7 },
1582 	{ 288, 12 },
1583 	{ 12, 7 },
1584 	{ 9, 6 },
1585 	{ 9, 6 },
1586 	{ 8, 6 },
1587 	{ 8, 6 },
1588 	{ 7, 5 },
1589 	{ 7, 5 },
1590 	{ 7, 5 },
1591 	{ 7, 5 },
1592 	{ 6, 4 },
1593 	{ 6, 4 },
1594 	{ 6, 4 },
1595 	{ 6, 4 },
1596 	{ 6, 4 },
1597 	{ 6, 4 },
1598 	{ 6, 4 },
1599 	{ 6, 4 },
1600 	{ 5, 4 },
1601 	{ 5, 4 },
1602 	{ 5, 4 },
1603 	{ 5, 4 },
1604 	{ 5, 4 },
1605 	{ 5, 4 },
1606 	{ 5, 4 },
1607 	{ 5, 4 },
1608 	{ 1, 3 },
1609 	{ 1, 3 },
1610 	{ 1, 3 },
1611 	{ 1, 3 },
1612 	{ 1, 3 },
1613 	{ 1, 3 },
1614 	{ 1, 3 },
1615 	{ 1, 3 },
1616 	{ 1, 3 },
1617 	{ 1, 3 },
1618 	{ 1, 3 },
1619 	{ 1, 3 },
1620 	{ 1, 3 },
1621 	{ 1, 3 },
1622 	{ 1, 3 },
1623 	{ 1, 3 },
1624 	{ 4, 3 },
1625 	{ 4, 3 },
1626 	{ 4, 3 },
1627 	{ 4, 3 },
1628 	{ 4, 3 },
1629 	{ 4, 3 },
1630 	{ 4, 3 },
1631 	{ 4, 3 },
1632 	{ 4, 3 },
1633 	{ 4, 3 },
1634 	{ 4, 3 },
1635 	{ 4, 3 },
1636 	{ 4, 3 },
1637 	{ 4, 3 },
1638 	{ 4, 3 },
1639 	{ 4, 3 },
1640 	{ 3, 2 },
1641 	{ 3, 2 },
1642 	{ 3, 2 },
1643 	{ 3, 2 },
1644 	{ 3, 2 },
1645 	{ 3, 2 },
1646 	{ 3, 2 },
1647 	{ 3, 2 },
1648 	{ 3, 2 },
1649 	{ 3, 2 },
1650 	{ 3, 2 },
1651 	{ 3, 2 },
1652 	{ 3, 2 },
1653 	{ 3, 2 },
1654 	{ 3, 2 },
1655 	{ 3, 2 },
1656 	{ 3, 2 },
1657 	{ 3, 2 },
1658 	{ 3, 2 },
1659 	{ 3, 2 },
1660 	{ 3, 2 },
1661 	{ 3, 2 },
1662 	{ 3, 2 },
1663 	{ 3, 2 },
1664 	{ 3, 2 },
1665 	{ 3, 2 },
1666 	{ 3, 2 },
1667 	{ 3, 2 },
1668 	{ 3, 2 },
1669 	{ 3, 2 },
1670 	{ 3, 2 },
1671 	{ 3, 2 },
1672 	{ 2, 2 },
1673 	{ 2, 2 },
1674 	{ 2, 2 },
1675 	{ 2, 2 },
1676 	{ 2, 2 },
1677 	{ 2, 2 },
1678 	{ 2, 2 },
1679 	{ 2, 2 },
1680 	{ 2, 2 },
1681 	{ 2, 2 },
1682 	{ 2, 2 },
1683 	{ 2, 2 },
1684 	{ 2, 2 },
1685 	{ 2, 2 },
1686 	{ 2, 2 },
1687 	{ 2, 2 },
1688 	{ 2, 2 },
1689 	{ 2, 2 },
1690 	{ 2, 2 },
1691 	{ 2, 2 },
1692 	{ 2, 2 },
1693 	{ 2, 2 },
1694 	{ 2, 2 },
1695 	{ 2, 2 },
1696 	{ 2, 2 },
1697 	{ 2, 2 },
1698 	{ 2, 2 },
1699 	{ 2, 2 },
1700 	{ 2, 2 },
1701 	{ 2, 2 },
1702 	{ 2, 2 },
1703 	{ 2, 2 },
1704 	{ -2, 4 },
1705 	{ -2, 4 },
1706 	{ -1, 0 },
1707 	{ -1, 0 },
1708 	{ -1, 0 },
1709 	{ -1, 0 },
1710 	{ -1, 0 },
1711 	{ -1, 0 },
1712 	{ -1, 0 },
1713 	{ -1, 0 },
1714 	{ -1, 0 },
1715 	{ -1, 0 },
1716 	{ -1, 0 },
1717 	{ -1, 0 },
1718 	{ -1, 0 },
1719 	{ -3, 5 },
1720 	{ 1792, 4 },
1721 	{ 1792, 4 },
1722 	{ 1984, 5 },
1723 	{ 2048, 5 },
1724 	{ 2112, 5 },
1725 	{ 2176, 5 },
1726 	{ 2240, 5 },
1727 	{ 2304, 5 },
1728 	{ 1856, 4 },
1729 	{ 1856, 4 },
1730 	{ 1920, 4 },
1731 	{ 1920, 4 },
1732 	{ 2368, 5 },
1733 	{ 2432, 5 },
1734 	{ 2496, 5 },
1735 	{ 2560, 5 },
1736 	{ 18, 3 },
1737 	{ 18, 3 },
1738 	{ 18, 3 },
1739 	{ 18, 3 },
1740 	{ 18, 3 },
1741 	{ 18, 3 },
1742 	{ 18, 3 },
1743 	{ 18, 3 },
1744 	{ 52, 5 },
1745 	{ 52, 5 },
1746 	{ 640, 6 },
1747 	{ 704, 6 },
1748 	{ 768, 6 },
1749 	{ 832, 6 },
1750 	{ 55, 5 },
1751 	{ 55, 5 },
1752 	{ 56, 5 },
1753 	{ 56, 5 },
1754 	{ 1280, 6 },
1755 	{ 1344, 6 },
1756 	{ 1408, 6 },
1757 	{ 1472, 6 },
1758 	{ 59, 5 },
1759 	{ 59, 5 },
1760 	{ 60, 5 },
1761 	{ 60, 5 },
1762 	{ 1536, 6 },
1763 	{ 1600, 6 },
1764 	{ 24, 4 },
1765 	{ 24, 4 },
1766 	{ 24, 4 },
1767 	{ 24, 4 },
1768 	{ 25, 4 },
1769 	{ 25, 4 },
1770 	{ 25, 4 },
1771 	{ 25, 4 },
1772 	{ 1664, 6 },
1773 	{ 1728, 6 },
1774 	{ 320, 5 },
1775 	{ 320, 5 },
1776 	{ 384, 5 },
1777 	{ 384, 5 },
1778 	{ 448, 5 },
1779 	{ 448, 5 },
1780 	{ 512, 6 },
1781 	{ 576, 6 },
1782 	{ 53, 5 },
1783 	{ 53, 5 },
1784 	{ 54, 5 },
1785 	{ 54, 5 },
1786 	{ 896, 6 },
1787 	{ 960, 6 },
1788 	{ 1024, 6 },
1789 	{ 1088, 6 },
1790 	{ 1152, 6 },
1791 	{ 1216, 6 },
1792 	{ 64, 3 },
1793 	{ 64, 3 },
1794 	{ 64, 3 },
1795 	{ 64, 3 },
1796 	{ 64, 3 },
1797 	{ 64, 3 },
1798 	{ 64, 3 },
1799 	{ 64, 3 },
1800 	{ 13, 1 },
1801 	{ 13, 1 },
1802 	{ 13, 1 },
1803 	{ 13, 1 },
1804 	{ 13, 1 },
1805 	{ 13, 1 },
1806 	{ 13, 1 },
1807 	{ 13, 1 },
1808 	{ 13, 1 },
1809 	{ 13, 1 },
1810 	{ 13, 1 },
1811 	{ 13, 1 },
1812 	{ 13, 1 },
1813 	{ 13, 1 },
1814 	{ 13, 1 },
1815 	{ 13, 1 },
1816 	{ 23, 4 },
1817 	{ 23, 4 },
1818 	{ 50, 5 },
1819 	{ 51, 5 },
1820 	{ 44, 5 },
1821 	{ 45, 5 },
1822 	{ 46, 5 },
1823 	{ 47, 5 },
1824 	{ 57, 5 },
1825 	{ 58, 5 },
1826 	{ 61, 5 },
1827 	{ 256, 5 },
1828 	{ 16, 3 },
1829 	{ 16, 3 },
1830 	{ 16, 3 },
1831 	{ 16, 3 },
1832 	{ 17, 3 },
1833 	{ 17, 3 },
1834 	{ 17, 3 },
1835 	{ 17, 3 },
1836 	{ 48, 5 },
1837 	{ 49, 5 },
1838 	{ 62, 5 },
1839 	{ 63, 5 },
1840 	{ 30, 5 },
1841 	{ 31, 5 },
1842 	{ 32, 5 },
1843 	{ 33, 5 },
1844 	{ 40, 5 },
1845 	{ 41, 5 },
1846 	{ 22, 4 },
1847 	{ 22, 4 },
1848 	{ 14, 1 },
1849 	{ 14, 1 },
1850 	{ 14, 1 },
1851 	{ 14, 1 },
1852 	{ 14, 1 },
1853 	{ 14, 1 },
1854 	{ 14, 1 },
1855 	{ 14, 1 },
1856 	{ 14, 1 },
1857 	{ 14, 1 },
1858 	{ 14, 1 },
1859 	{ 14, 1 },
1860 	{ 14, 1 },
1861 	{ 14, 1 },
1862 	{ 14, 1 },
1863 	{ 14, 1 },
1864 	{ 15, 2 },
1865 	{ 15, 2 },
1866 	{ 15, 2 },
1867 	{ 15, 2 },
1868 	{ 15, 2 },
1869 	{ 15, 2 },
1870 	{ 15, 2 },
1871 	{ 15, 2 },
1872 	{ 128, 5 },
1873 	{ 192, 5 },
1874 	{ 26, 5 },
1875 	{ 27, 5 },
1876 	{ 28, 5 },
1877 	{ 29, 5 },
1878 	{ 19, 4 },
1879 	{ 19, 4 },
1880 	{ 20, 4 },
1881 	{ 20, 4 },
1882 	{ 34, 5 },
1883 	{ 35, 5 },
1884 	{ 36, 5 },
1885 	{ 37, 5 },
1886 	{ 38, 5 },
1887 	{ 39, 5 },
1888 	{ 21, 4 },
1889 	{ 21, 4 },
1890 	{ 42, 5 },
1891 	{ 43, 5 },
1892 	{ 0, 3 },
1893 	{ 0, 3 },
1894 	{ 0, 3 },
1895 	{ 0, 3 }
1896 };
1897 
1898 /* 2-D decoding table. */
1899 impl_const cfd_node cf_2d_decode[] = {
1900 	{ 128, 11 },
1901 	{ 144, 10 },
1902 	{ 6, 7 },
1903 	{ 0, 7 },
1904 	{ 5, 6 },
1905 	{ 5, 6 },
1906 	{ 1, 6 },
1907 	{ 1, 6 },
1908 	{ -4, 4 },
1909 	{ -4, 4 },
1910 	{ -4, 4 },
1911 	{ -4, 4 },
1912 	{ -4, 4 },
1913 	{ -4, 4 },
1914 	{ -4, 4 },
1915 	{ -4, 4 },
1916 	{ -5, 3 },
1917 	{ -5, 3 },
1918 	{ -5, 3 },
1919 	{ -5, 3 },
1920 	{ -5, 3 },
1921 	{ -5, 3 },
1922 	{ -5, 3 },
1923 	{ -5, 3 },
1924 	{ -5, 3 },
1925 	{ -5, 3 },
1926 	{ -5, 3 },
1927 	{ -5, 3 },
1928 	{ -5, 3 },
1929 	{ -5, 3 },
1930 	{ -5, 3 },
1931 	{ -5, 3 },
1932 	{ 4, 3 },
1933 	{ 4, 3 },
1934 	{ 4, 3 },
1935 	{ 4, 3 },
1936 	{ 4, 3 },
1937 	{ 4, 3 },
1938 	{ 4, 3 },
1939 	{ 4, 3 },
1940 	{ 4, 3 },
1941 	{ 4, 3 },
1942 	{ 4, 3 },
1943 	{ 4, 3 },
1944 	{ 4, 3 },
1945 	{ 4, 3 },
1946 	{ 4, 3 },
1947 	{ 4, 3 },
1948 	{ 2, 3 },
1949 	{ 2, 3 },
1950 	{ 2, 3 },
1951 	{ 2, 3 },
1952 	{ 2, 3 },
1953 	{ 2, 3 },
1954 	{ 2, 3 },
1955 	{ 2, 3 },
1956 	{ 2, 3 },
1957 	{ 2, 3 },
1958 	{ 2, 3 },
1959 	{ 2, 3 },
1960 	{ 2, 3 },
1961 	{ 2, 3 },
1962 	{ 2, 3 },
1963 	{ 2, 3 },
1964 	{ 3, 1 },
1965 	{ 3, 1 },
1966 	{ 3, 1 },
1967 	{ 3, 1 },
1968 	{ 3, 1 },
1969 	{ 3, 1 },
1970 	{ 3, 1 },
1971 	{ 3, 1 },
1972 	{ 3, 1 },
1973 	{ 3, 1 },
1974 	{ 3, 1 },
1975 	{ 3, 1 },
1976 	{ 3, 1 },
1977 	{ 3, 1 },
1978 	{ 3, 1 },
1979 	{ 3, 1 },
1980 	{ 3, 1 },
1981 	{ 3, 1 },
1982 	{ 3, 1 },
1983 	{ 3, 1 },
1984 	{ 3, 1 },
1985 	{ 3, 1 },
1986 	{ 3, 1 },
1987 	{ 3, 1 },
1988 	{ 3, 1 },
1989 	{ 3, 1 },
1990 	{ 3, 1 },
1991 	{ 3, 1 },
1992 	{ 3, 1 },
1993 	{ 3, 1 },
1994 	{ 3, 1 },
1995 	{ 3, 1 },
1996 	{ 3, 1 },
1997 	{ 3, 1 },
1998 	{ 3, 1 },
1999 	{ 3, 1 },
2000 	{ 3, 1 },
2001 	{ 3, 1 },
2002 	{ 3, 1 },
2003 	{ 3, 1 },
2004 	{ 3, 1 },
2005 	{ 3, 1 },
2006 	{ 3, 1 },
2007 	{ 3, 1 },
2008 	{ 3, 1 },
2009 	{ 3, 1 },
2010 	{ 3, 1 },
2011 	{ 3, 1 },
2012 	{ 3, 1 },
2013 	{ 3, 1 },
2014 	{ 3, 1 },
2015 	{ 3, 1 },
2016 	{ 3, 1 },
2017 	{ 3, 1 },
2018 	{ 3, 1 },
2019 	{ 3, 1 },
2020 	{ 3, 1 },
2021 	{ 3, 1 },
2022 	{ 3, 1 },
2023 	{ 3, 1 },
2024 	{ 3, 1 },
2025 	{ 3, 1 },
2026 	{ 3, 1 },
2027 	{ 3, 1 },
2028 	{ -2, 4 },
2029 	{ -1, 0 },
2030 	{ -1, 0 },
2031 	{ -1, 0 },
2032 	{ -1, 0 },
2033 	{ -1, 0 },
2034 	{ -1, 0 },
2035 	{ -1, 0 },
2036 	{ -1, 0 },
2037 	{ -1, 0 },
2038 	{ -1, 0 },
2039 	{ -1, 0 },
2040 	{ -1, 0 },
2041 	{ -1, 0 },
2042 	{ -1, 0 },
2043 	{ -1, 0 },
2044 	{ -1, 0 },
2045 	{ -1, 0 },
2046 	{ -1, 0 },
2047 	{ -1, 0 },
2048 	{ -1, 0 },
2049 	{ -1, 0 },
2050 	{ -1, 0 },
2051 	{ -3, 3 }
2052 };
2053 
2054 /* Uncompresssed decoding table. */
2055 impl_const cfd_node cf_uncompressed_decode[] = {
2056 	{ 64, 12 },
2057 	{ 5, 6 },
2058 	{ 4, 5 },
2059 	{ 4, 5 },
2060 	{ 3, 4 },
2061 	{ 3, 4 },
2062 	{ 3, 4 },
2063 	{ 3, 4 },
2064 	{ 2, 3 },
2065 	{ 2, 3 },
2066 	{ 2, 3 },
2067 	{ 2, 3 },
2068 	{ 2, 3 },
2069 	{ 2, 3 },
2070 	{ 2, 3 },
2071 	{ 2, 3 },
2072 	{ 1, 2 },
2073 	{ 1, 2 },
2074 	{ 1, 2 },
2075 	{ 1, 2 },
2076 	{ 1, 2 },
2077 	{ 1, 2 },
2078 	{ 1, 2 },
2079 	{ 1, 2 },
2080 	{ 1, 2 },
2081 	{ 1, 2 },
2082 	{ 1, 2 },
2083 	{ 1, 2 },
2084 	{ 1, 2 },
2085 	{ 1, 2 },
2086 	{ 1, 2 },
2087 	{ 1, 2 },
2088 	{ 0, 1 },
2089 	{ 0, 1 },
2090 	{ 0, 1 },
2091 	{ 0, 1 },
2092 	{ 0, 1 },
2093 	{ 0, 1 },
2094 	{ 0, 1 },
2095 	{ 0, 1 },
2096 	{ 0, 1 },
2097 	{ 0, 1 },
2098 	{ 0, 1 },
2099 	{ 0, 1 },
2100 	{ 0, 1 },
2101 	{ 0, 1 },
2102 	{ 0, 1 },
2103 	{ 0, 1 },
2104 	{ 0, 1 },
2105 	{ 0, 1 },
2106 	{ 0, 1 },
2107 	{ 0, 1 },
2108 	{ 0, 1 },
2109 	{ 0, 1 },
2110 	{ 0, 1 },
2111 	{ 0, 1 },
2112 	{ 0, 1 },
2113 	{ 0, 1 },
2114 	{ 0, 1 },
2115 	{ 0, 1 },
2116 	{ 0, 1 },
2117 	{ 0, 1 },
2118 	{ 0, 1 },
2119 	{ 0, 1 },
2120 	{ -1, 0 },
2121 	{ -1, 0 },
2122 	{ 8, 6 },
2123 	{ 9, 6 },
2124 	{ 6, 5 },
2125 	{ 6, 5 },
2126 	{ 7, 5 },
2127 	{ 7, 5 },
2128 	{ 4, 4 },
2129 	{ 4, 4 },
2130 	{ 4, 4 },
2131 	{ 4, 4 },
2132 	{ 5, 4 },
2133 	{ 5, 4 },
2134 	{ 5, 4 },
2135 	{ 5, 4 },
2136 	{ 2, 3 },
2137 	{ 2, 3 },
2138 	{ 2, 3 },
2139 	{ 2, 3 },
2140 	{ 2, 3 },
2141 	{ 2, 3 },
2142 	{ 2, 3 },
2143 	{ 2, 3 },
2144 	{ 3, 3 },
2145 	{ 3, 3 },
2146 	{ 3, 3 },
2147 	{ 3, 3 },
2148 	{ 3, 3 },
2149 	{ 3, 3 },
2150 	{ 3, 3 },
2151 	{ 3, 3 },
2152 	{ 0, 2 },
2153 	{ 0, 2 },
2154 	{ 0, 2 },
2155 	{ 0, 2 },
2156 	{ 0, 2 },
2157 	{ 0, 2 },
2158 	{ 0, 2 },
2159 	{ 0, 2 },
2160 	{ 0, 2 },
2161 	{ 0, 2 },
2162 	{ 0, 2 },
2163 	{ 0, 2 },
2164 	{ 0, 2 },
2165 	{ 0, 2 },
2166 	{ 0, 2 },
2167 	{ 0, 2 },
2168 	{ 1, 2 },
2169 	{ 1, 2 },
2170 	{ 1, 2 },
2171 	{ 1, 2 },
2172 	{ 1, 2 },
2173 	{ 1, 2 },
2174 	{ 1, 2 },
2175 	{ 1, 2 },
2176 	{ 1, 2 },
2177 	{ 1, 2 },
2178 	{ 1, 2 },
2179 	{ 1, 2 },
2180 	{ 1, 2 },
2181 	{ 1, 2 },
2182 	{ 1, 2 },
2183 	{ 1, 2 }
2184 };
2185 
2186 #if 0
2187 /* Dummy executable code to pacify compilers. */
2188 void scfdtab_dummy _((void));
2189 void
2190 scfdtab_dummy()
2191 {
2192 }
2193 #endif
2194 /* end of former scfdtab.c */
2195 /* scfd.c */
2196 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
2197 /* CCITTFax decoding filter */
2198 /* #include "gstypes.h" */
2199 /* #include "scf.h" */
2200 /* #include "scfx.h" */
2201 
2202 /* Utility procedures */
2203 int stream_move _((stream_cursor_read *, stream_cursor_write *));	/* in stream.c */
2204 
2205 /* ------ CCITTFaxDecode ------ */
2206 
2207 /* private_st_CFD_state(); */
2208 
2209 /* Set default parameter values. */
2210 static void
s_CFD_set_defaults(register stream_state * st)2211 s_CFD_set_defaults(register stream_state * st)
2212 {
2213     stream_CFD_state *const ss = (stream_CFD_state *) st;
2214 
2215     s_CFD_set_defaults_inline(ss);
2216 }
2217 
2218 /* Initialize CCITTFaxDecode filter */
2219 static int
s_CFD_init(stream_state * st)2220 s_CFD_init(stream_state * st)
2221 {
2222     stream_CFD_state *const ss = (stream_CFD_state *) st;
2223     int raster = ss->raster =
2224 	ROUND_UP((ss->Columns + 7) >> 3, ss->DecodedByteAlign);
2225     unsigned char white = (ss->BlackIs1 ? 0 : 0xff);
2226 
2227     s_hcd_init_inline(ss);
2228     /* Because skip_white_pixels can look as many as 4 bytes ahead, */
2229     /* we need to allow 4 extra bytes at the end of the row buffers. */
2230     ss->lbuf = (unsigned char*)gs_alloc_bytes(st->memory, raster + 4, "CFD lbuf");
2231     ss->lprev = 0;
2232     if (ss->lbuf == 0)
2233 	return PTSFAX_ERRC;
2234 /****** WRONG ******/
2235     if (ss->K != 0) {
2236 	ss->lprev = (unsigned char*)gs_alloc_bytes(st->memory, raster + 4, "CFD lprev");
2237 	if (ss->lprev == 0)
2238 	    return PTSFAX_ERRC;
2239 /****** WRONG ******/
2240 	/* Clear the initial reference line for 2-D encoding. */
2241 	ss->memset_(ss->lbuf, white, raster);
2242 	/* Ensure that the scan of the reference line will stop. */
2243 	ss->lbuf[raster] = 0xa0;
2244     }
2245     ss->k_left = min(ss->K, 0);
2246     ss->run_color = 0;
2247     ss->damaged_rows = 0;
2248     ss->skipping_damage = false;
2249     ss->cbit = 0;
2250     ss->uncomp_run = 0;
2251     ss->rows_left = (ss->Rows <= 0 || ss->EndOfBlock ? -1 : ss->Rows + 1);
2252     ss->rpos = ss->wpos = raster - 1;
2253     ss->eol_count = 0;
2254     ss->invert = white;
2255     ss->min_left = 1;
2256     return 0;
2257 }
2258 
2259 /* Release the filter. */
2260 static void
s_CFD_release(stream_state * st)2261 s_CFD_release(stream_state * st)
2262 {
2263     stream_CFD_state *const ss = (stream_CFD_state *) st;
2264 
2265     gs_free_object(st->memory, ss->lprev, "CFD lprev(close)");
2266     gs_free_object(st->memory, ss->lbuf, "CFD lbuf(close)");
2267 }
2268 
2269 /* Declare the variables that hold the state. */
2270 #define cfd_declare_state\
2271 	hcd_declare_state;\
2272 	register unsigned char *q;\
2273 	int qbit
2274 /* Load the state from the stream. */
2275 #define cfd_load_state()\
2276 	hcd_load_state(),\
2277 	q = ss->lbuf + ss->wpos, qbit = ss->cbit
2278 /* Store the state back in the stream. */
2279 #define cfd_store_state()\
2280 	hcd_store_state(),\
2281 	ss->wpos = q - ss->lbuf, ss->cbit = qbit
2282 
2283 /* Macros to get blocks of bits from the input stream. */
2284 /* Invariants: 0 <= bits_left <= bits_size; */
2285 /* bits [bits_left-1..0] contain valid data. */
2286 
2287 #define avail_bits(n) hcd_bits_available(n)
2288 #define ensure_bits(n, outl) hcd_ensure_bits(n, outl)
2289 #define peek_bits(n) hcd_peek_bits(n)
2290 #define peek_var_bits(n) hcd_peek_var_bits(n)
2291 #define skip_bits(n) hcd_skip_bits(n)
2292 
2293 /* Get a run from the stream. */
2294 #ifdef DEBUG
2295 #  define IF_DEBUG(expr) expr
2296 #else
2297 #  define IF_DEBUG(expr) DO_NOTHING
2298 #endif
2299 #define get_run(decode, initial_bits, min_bits, runlen, str, locl, outl)\
2300     BEGIN\
2301 	const cfd_node *np;\
2302 	int clen;\
2303 \
2304 	HCD_ENSURE_BITS_ELSE(initial_bits) {\
2305 	    /* We might still have enough bits for the specific code. */\
2306 	    if (bits_left < min_bits) goto outl;\
2307 	    np = &decode[hcd_peek_bits_left() << (initial_bits - bits_left)];\
2308 	    if ((clen = np->code_length) > bits_left) goto outl;\
2309 	    goto locl;\
2310 	}\
2311 	np = &decode[peek_bits(initial_bits)];\
2312 	if ((clen = np->code_length) > initial_bits) {\
2313 		IF_DEBUG(unsigned int init_bits = peek_bits(initial_bits));\
2314 		if (!avail_bits(clen)) goto outl;\
2315 		clen -= initial_bits;\
2316 		skip_bits(initial_bits);\
2317 		ensure_bits(clen, outl);		/* can't goto outl */\
2318 		np = &decode[np->run_length + peek_var_bits(clen)];\
2319 		if_debug4('W', "%s xcode=0x%x,%d rlen=%d\n", str,\
2320 			  (init_bits << np->code_length) +\
2321 			    peek_var_bits(np->code_length),\
2322 			  initial_bits + np->code_length,\
2323 			  np->run_length);\
2324 		skip_bits(np->code_length);\
2325 	} else {\
2326     locl:	if_debug4('W', "%s code=0x%x,%d rlen=%d\n", str,\
2327 			  peek_var_bits(clen), clen, np->run_length);\
2328 		skip_bits(clen);\
2329 	}\
2330 	runlen = np->run_length;\
2331     END
2332 
2333 /* Skip data bits for a white run. */
2334 /* rlen is either less than 64, or a multiple of 64. */
2335 #define skip_data(rlen, makeup_label)\
2336 	if ( (qbit -= rlen) < 0 )\
2337 	{	q -= qbit >> 3, qbit &= 7;\
2338 		if ( rlen >= 64 ) goto makeup_label;\
2339 	}
2340 
2341 /* Invert data bits for a black run. */
2342 /* If rlen >= 64, execute makeup_action: this is to handle */
2343 /* makeup codes efficiently, since these are always a multiple of 64. */
2344 #define invert_data(rlen, black_byte, makeup_action, d)\
2345 	if ( rlen > qbit )\
2346 	{	*q++ ^= (1 << qbit) - 1;\
2347 		rlen -= qbit;\
2348 		switch ( rlen >> 3 )\
2349 		{\
2350 		case 7:		/* original rlen possibly >= 64 */\
2351 			if ( rlen + qbit >= 64 ) goto d;\
2352 			*q++ = black_byte;\
2353 		case 6: *q++ = black_byte;\
2354 		case 5: *q++ = black_byte;\
2355 		case 4: *q++ = black_byte;\
2356 		case 3: *q++ = black_byte;\
2357 		case 2: *q++ = black_byte;\
2358 		case 1: *q = black_byte;\
2359 			rlen &= 7;\
2360 			if ( !rlen ) { qbit = 0; break; }\
2361 			q++;\
2362 		case 0:			/* know rlen != 0 */\
2363 			qbit = 8 - rlen;\
2364 			*q ^= 0xff << qbit;\
2365 			break;\
2366 		default:	/* original rlen >= 64 */\
2367 d:			ss->memset_(q, black_byte, rlen >> 3);\
2368 			q += rlen >> 3;\
2369 			rlen &= 7;\
2370 			if ( !rlen ) qbit = 0, q--;\
2371 			else qbit = 8 - rlen, *q ^= 0xff << qbit;\
2372 			makeup_action;\
2373 		}\
2374 	}\
2375 	else\
2376 		qbit -= rlen,\
2377 		*q ^= ((1 << rlen) - 1) << qbit
2378 
2379 /* Buffer refill for CCITTFaxDecode filter */
2380 static int cf_decode_eol _((stream_CFD_state *, stream_cursor_read *));
2381 static int cf_decode_1d _((stream_CFD_state *, stream_cursor_read *));
2382 static int cf_decode_2d _((stream_CFD_state *, stream_cursor_read *));
2383 static int cf_decode_uncompressed _((stream_CFD_state *, stream_cursor_read *));
2384 static int
s_CFD_process(stream_state * st,stream_cursor_read * pr,stream_cursor_write * pw,bool last)2385 s_CFD_process(stream_state * st, stream_cursor_read * pr,
2386 	      stream_cursor_write * pw, bool last)
2387 {
2388     stream_CFD_state *const ss = (stream_CFD_state *) st;
2389     int wstop = ss->raster - 1;
2390     int eol_count = ss->eol_count;
2391     int k_left = ss->k_left;
2392     int rows_left = ss->rows_left;
2393     int status = 0;
2394 
2395     (void)last; /**** pts ****/
2396 
2397 #ifdef DEBUG
2398     const unsigned char *rstart = pr->ptr;
2399     const unsigned char *wstart = pw->ptr;
2400 
2401 #endif
2402 
2403   top:
2404 #ifdef DEBUG
2405     {
2406 	hcd_declare_state;
2407 	hcd_load_state();
2408 	if_debug8('w', "\
2409 [w]CFD_process top: eol_count=%d, k_left=%d, rows_left=%d\n\
2410     bits=0x%lx, bits_left=%d, read %u, wrote %u%s\n",
2411 		  eol_count, k_left, rows_left,
2412 		  (unsigned long) bits, bits_left,
2413 		  (unsigned int) (p - rstart), (unsigned int) (pw->ptr - wstart),
2414 		  (ss->skipping_damage ? ", skipping damage" : ""));
2415     }
2416 #endif
2417     if (ss->skipping_damage) {	/* Skip until we reach an EOL. */
2418 	hcd_declare_state;
2419 	int skip;
2420 
2421 	status = 0;
2422 	do {
2423 	    switch ((skip = cf_decode_eol(ss, pr))) {
2424 		default:	/* not EOL */
2425 		    hcd_load_state();
2426 		    skip_bits(-skip);
2427 		    hcd_store_state();
2428 		    continue;
2429 		case 0:	/* need more input */
2430 		    goto out;
2431 		case 1:	/* EOL */
2432 		    {		/* Back up over the EOL. */
2433 			hcd_load_state();
2434 			bits_left += run_eol_code_length;
2435 			hcd_store_state();
2436 		    }
2437 		    ss->skipping_damage = false;
2438 	    }
2439 	}
2440 	while (ss->skipping_damage);
2441 	ss->damaged_rows++;
2442     }
2443     /*
2444      * Check for a completed input scan line.  This isn't quite as
2445      * simple as it seems, because we could have run out of input data
2446      * between a makeup code and a 0-length termination code, or in a
2447      * 2-D line before a final horizontal code with a 0-length second
2448      * run.  There's probably a way to think about this situation that
2449      * doesn't require a special check, but I haven't found it yet.
2450      */
2451     if (ss->wpos == wstop && ss->cbit <= (-ss->Columns & 7) &&
2452 	(k_left == 0 ? !(ss->run_color & ~1) : ss->run_color == 0)
2453 	) {			/* Check for completed data to be copied to the client. */
2454 	/* (We could avoid the extra copy step for 1-D, but */
2455 	/* it's simpler not to, and it doesn't cost much.) */
2456 	if (ss->rpos < ss->wpos) {
2457 	    stream_cursor_read cr;
2458 
2459 	    cr.ptr = ss->lbuf + ss->rpos;
2460 	    cr.limit = ss->lbuf + ss->wpos;
2461 	    status = stream_move(&cr, pw);
2462 	    ss->rpos = cr.ptr - ss->lbuf;
2463 	    if (status)
2464 		goto out;
2465 	}
2466 	if (rows_left > 0 && --rows_left == 0) {
2467 	    status = EOFC;
2468 	    goto out;
2469 	}
2470 	if (ss->K != 0) {
2471 	    unsigned char *prev_bits = ss->lprev;
2472 
2473 	    ss->lprev = ss->lbuf;
2474 	    ss->lbuf = prev_bits;
2475 	    if (ss->K > 0)
2476 		k_left = (k_left == 0 ? ss->K : k_left) - 1;
2477 	}
2478 	ss->rpos = ss->wpos = -1;
2479 	ss->eol_count = eol_count = 0;
2480 	ss->cbit = 0;
2481 	ss->invert = (ss->BlackIs1 ? 0 : 0xff);
2482 	ss->memset_(ss->lbuf, ss->invert, wstop + 1);
2483 	ss->run_color = 0;
2484 	/*
2485 	 * If EndOfLine is true, we want to include the unsigned char padding
2486 	 * in the string of initial zeros in the EOL.  If EndOfLine
2487 	 * is false, we aren't sure what we should do....
2488 	 */
2489 	if (ss->EncodedByteAlign & !ss->EndOfLine)
2490 	    ss->bits_left &= ~7;
2491     }
2492     /* If we're between scan lines, scan for EOLs. */
2493     if (ss->wpos < 0) {
2494 	while ((status = cf_decode_eol(ss, pr)) > 0) {
2495 	    if_debug0('w', "[w]EOL\n");
2496 	    /* If we are in a Group 3 mixed regime, */
2497 	    /* check the next bit for 1- vs. 2-D. */
2498 	    if (ss->K > 0) {
2499 		hcd_declare_state;
2500 		hcd_load_state();
2501 		ensure_bits(1, out);	/* can't fail */
2502 		k_left = (peek_bits(1) ? 0 : 1);
2503 		skip_bits(1);
2504 		hcd_store_state();
2505 	    }
2506 	    ++eol_count;
2507 	    /*
2508 	     * According to Adobe, the decoder should always check for
2509 	     * the EOD sequence, regardless of EndOfBlock: the Red Book's
2510 	     * documentation of EndOfBlock is wrong.
2511 	     */
2512 	    if (eol_count == (ss->K < 0 ? 2 : 6)) {
2513 		status = EOFC;
2514 		goto out;
2515 	    }
2516 	}
2517 	if (status == 0)	/* input empty while scanning EOLs */
2518 	    goto out;
2519 	switch (eol_count) {
2520 	    case 0:
2521 		if (ss->EndOfLine) {	/* EOL is required, but none is present. */
2522 		    status = PTSFAX_ERRC;
2523 		    goto check;
2524 		}
2525 	    case 1:
2526 		break;
2527 	    default:
2528 		status = PTSFAX_ERRC;
2529 		goto check;
2530 	}
2531     }
2532     /* Now decode actual data. */
2533     if (k_left < 0) {
2534 	if_debug0('w', "[w2]new row\n");
2535 	status = cf_decode_2d(ss, pr);
2536     } else if (k_left == 0) {
2537 	if_debug0('w', "[w1]new row\n");
2538 	status = cf_decode_1d(ss, pr);
2539     } else {
2540 	if_debug1('w', "[w1]new 2-D row, %d left\n", k_left);
2541 	status = cf_decode_2d(ss, pr);
2542     }
2543     if_debug3('w', "[w]CFD status = %d, wpos = %d, cbit = %d\n",
2544 	      status, ss->wpos, ss->cbit);
2545   check:switch (status) {
2546 	case 1:		/* output full */
2547 	    goto top;
2548 	case PTSFAX_ERRC:
2549 	    /* Check for special handling of damaged rows. */
2550 	    if (ss->damaged_rows >= ss->DamagedRowsBeforeError ||
2551 		!(ss->EndOfLine && ss->K >= 0)
2552 		)
2553 		break;
2554 	    /* Substitute undamaged data if appropriate. */
2555 /****** NOT IMPLEMENTED YET ******/
2556 	    {
2557 		ss->wpos = wstop;
2558 		ss->cbit = -ss->Columns & 7;
2559 		ss->run_color = 0;
2560 	    }
2561 	    ss->skipping_damage = true;
2562 	    goto top;
2563 	default:
2564 	    ss->damaged_rows = 0;	/* finished a good row */
2565     }
2566   out:ss->k_left = k_left;
2567     ss->rows_left = rows_left;
2568     ss->eol_count = eol_count;
2569     return status;
2570 }
2571 
2572 /*
2573  * Decode a leading EOL, if any.
2574  * If an EOL is present, skip over it and return 1;
2575  * if no EOL is present, read no input and return -N, where N is the
2576  * number of initial bits that can be skipped in the search for an EOL;
2577  * if more input is needed, return 0.
2578  * Note that if we detected an EOL, we know that we can back up over it;
2579  * if we detected an N-bit non-EOL, we know that at least N bits of data
2580  * are available in the buffer.
2581  */
2582 static int
cf_decode_eol(stream_CFD_state * ss,stream_cursor_read * pr)2583 cf_decode_eol(stream_CFD_state * ss, stream_cursor_read * pr)
2584 {
2585     hcd_declare_state;
2586     int zeros;
2587     int look_ahead;
2588 
2589     hcd_load_state();
2590     for (zeros = 0; zeros < run_eol_code_length - 1; zeros++) {
2591 	ensure_bits(1, out);
2592 	if (peek_bits(1))
2593 	    return -(zeros + 1);
2594 	skip_bits(1);
2595     }
2596     /* We definitely have an EOL.  Skip further zero bits. */
2597     look_ahead = (ss->K > 0 ? 2 : 1);
2598     for (;;) {
2599 	ensure_bits(look_ahead, back);
2600 	if (peek_bits(1))
2601 	    break;
2602 	skip_bits(1);
2603     }
2604     skip_bits(1);
2605     hcd_store_state();
2606     return 1;
2607   back:			/*
2608 				 * We ran out of data while skipping zeros.
2609 				 * We know we are at a unsigned char boundary, and have just skipped
2610 				 * at least run_eol_code_length - 1 zeros.  However,
2611 				 * bits_left may be 1 if look_ahead == 2.
2612 				 */
2613     bits &= (1 << bits_left) - 1;
2614     bits_left += run_eol_code_length - 1;
2615     hcd_store_state();
2616   out:return 0;
2617 }
2618 
2619 /* Decode a 1-D scan line. */
2620 static int
cf_decode_1d(stream_CFD_state * ss,stream_cursor_read * pr)2621 cf_decode_1d(stream_CFD_state * ss, stream_cursor_read * pr)
2622 {
2623     cfd_declare_state;
2624     unsigned char black_byte = (ss->BlackIs1 ? 0xff : 0);
2625     int end_bit = -ss->Columns & 7;
2626     unsigned char *stop = ss->lbuf - 1 + ss->raster;
2627     int run_color = ss->run_color;
2628     int status;
2629     int bcnt;
2630 
2631     cfd_load_state();
2632     if_debug1('w', "[w1]entry run_color = %d\n", ss->run_color);
2633     if (ss->run_color > 0)
2634 	goto db;
2635     else
2636 	goto dw;
2637 #define q_at_stop() (q >= stop && (qbit <= end_bit || q > stop))
2638   top:run_color = 0;
2639     if (q_at_stop())
2640 	goto done;
2641   dw:				/* Decode a white run. */
2642     get_run(cf_white_decode, cfd_white_initial_bits, cfd_white_min_bits,
2643 	    bcnt, "[w1]white", dwl, out0);
2644     if (bcnt < 0) {		/* exceptional situation */
2645 	switch (bcnt) {
2646 	    case run_uncompressed:	/* Uncompressed data. */
2647 		cfd_store_state();
2648 		bcnt = cf_decode_uncompressed(ss, pr);
2649 		if (bcnt < 0)
2650 		    return bcnt;
2651 		cfd_load_state();
2652 		if (bcnt)
2653 		    goto db;
2654 		else
2655 		    goto dw;
2656 		/*case run_error: */
2657 		/*case run_zeros: *//* Premature end-of-line. */
2658 	    default:
2659 		status = PTSFAX_ERRC;
2660 		goto out;
2661 	}
2662     }
2663     skip_data(bcnt, dwx);
2664     if (q_at_stop()) {
2665 	run_color = 0;		/* not inside a run */
2666 	goto done;
2667     }
2668     run_color = 1;
2669   db:				/* Decode a black run. */
2670     get_run(cf_black_decode, cfd_black_initial_bits, cfd_black_min_bits,
2671 	    bcnt, "[w1]black", dbl, out1);
2672     if (bcnt < 0) {		/* All exceptional codes are invalid here. */
2673 /****** WRONG, uncompressed IS ALLOWED ******/
2674 	status = PTSFAX_ERRC;
2675 	goto out;
2676     }
2677     /* Invert bits designated by black run. */
2678     invert_data(bcnt, black_byte, goto dbx, idb);
2679     goto top;
2680   dwx:				/* If we run out of data after a makeup code, */
2681     /* note that we are still processing a white run. */
2682     run_color = -1;
2683     goto dw;
2684   dbx:				/* If we run out of data after a makeup code, */
2685     /* note that we are still processing a black run. */
2686     run_color = 2;
2687     goto db;
2688   done:if (q > stop || qbit < end_bit)
2689 	status = PTSFAX_ERRC;
2690     else
2691 	status = 1;
2692   out:cfd_store_state();
2693     ss->run_color = run_color;
2694     if_debug1('w', "[w1]exit run_color = %d\n", run_color);
2695     return status;
2696   out0:			/* We already set run_color to 0 or -1. */
2697     status = 0;
2698     goto out;
2699   out1:			/* We already set run_color to 1 or 2. */
2700     status = 0;
2701     goto out;
2702 }
2703 
2704 /* Decode a 2-D scan line. */
2705 static int
cf_decode_2d(stream_CFD_state * ss,stream_cursor_read * pr)2706 cf_decode_2d(stream_CFD_state * ss, stream_cursor_read * pr)
2707 {
2708     cfd_declare_state;
2709     unsigned char invert_white = (ss->BlackIs1 ? 0 : 0xff);
2710     unsigned char black_byte = ~invert_white;
2711     unsigned char invert = ss->invert;
2712     int end_count = -ss->Columns & 7;
2713     unsigned int raster = ss->raster;
2714     unsigned char *q0 = ss->lbuf;
2715     unsigned char *prev_q01 = ss->lprev + 1;
2716     unsigned char *endptr = q0 - 1 + raster;
2717     int init_count = raster << 3;
2718     register int count;
2719     int rlen;
2720     int status;
2721 
2722     cfd_load_state();
2723     count = ((endptr - q) << 3) + qbit;
2724     endptr[1] = 0xa0;		/* a unsigned char with some 0s and some 1s, */
2725     /* to ensure run scan will stop */
2726     if_debug1('W', "[w2]raster=%d\n", raster);
2727     switch (ss->run_color) {
2728 	case -2:
2729 	    ss->run_color = 0;
2730 	    goto hww;
2731 	case -1:
2732 	    ss->run_color = 0;
2733 	    goto hbw;
2734 	case 1:
2735 	    ss->run_color = 0;
2736 	    goto hwb;
2737 	case 2:
2738 	    ss->run_color = 0;
2739 	    goto hbb;
2740 	    /*case 0: */
2741     }
2742   top:if (count <= end_count) {
2743 	status = (count < end_count ? PTSFAX_ERRC : 1);
2744 	goto out;
2745     }
2746     /* If invert == invert_white, white and black have their */
2747     /* correct meanings; if invert == ~invert_white, */
2748     /* black and white are interchanged. */
2749     if_debug1('W', "[w2]%4d:\n", count);
2750 #ifdef DEBUG
2751     /* Check the invariant between q, qbit, and count. */
2752     {
2753 	int pcount = (endptr - q) * 8 + qbit;
2754 
2755 	if (pcount != count)
2756 	    dlprintf2("[w2]Error: count=%d pcount=%d\n",
2757 		      count, pcount);
2758     }
2759 #endif
2760     /*
2761      * We could just use get_run here, but we can do better.  However,
2762      * we must be careful to handle the case where the very last codes
2763      * in the input stream are 1-bit "vertical 0" codes: we can't just
2764      * use ensure_bits(3, ...) and go to get more data if it fails.
2765      */
2766     ensure_bits(3, out3);
2767 #define vertical_0 (countof(cf2_run_vertical) / 2)
2768     switch (peek_bits(3)) {
2769 	default /*4..7*/ :	/* vertical(0) */
2770 v0:	    skip_bits(1);
2771 	    rlen = vertical_0;
2772 	    break;
2773 	case 2:		/* vertical(+1) */
2774 	    skip_bits(3);
2775 	    rlen = vertical_0 + 1;
2776 	    break;
2777 	case 3:		/* vertical(-1) */
2778 	    skip_bits(3);
2779 	    rlen = vertical_0 - 1;
2780 	    break;
2781 	case 1:		/* horizontal */
2782 	    skip_bits(3);
2783 	    if (invert == invert_white)
2784 		goto hww;
2785 	    else
2786 		goto hbb;
2787 	case 0:		/* everything else */
2788 	    get_run(cf_2d_decode, cfd_2d_initial_bits, cfd_2d_min_bits,
2789 		    rlen, "[w2]", d2l, out0);
2790 	    /* rlen may be run2_pass, run_uncompressed, or */
2791 	    /* 0..countof(cf2_run_vertical)-1. */
2792 	    if (rlen < 0)
2793 		switch (rlen) {
2794 		    case run2_pass:
2795 			break;
2796 		    case run_uncompressed:
2797 			{
2798 			    int which;
2799 
2800 			    cfd_store_state();
2801 			    which = cf_decode_uncompressed(ss, pr);
2802 			    if (which < 0) {
2803 				status = which;
2804 				goto out;
2805 			    }
2806 			    cfd_load_state();
2807 /****** ADJUST count ******/
2808 			    invert = (which ? ~invert_white : invert_white);
2809 			}
2810 			goto top;
2811 		    default:	/* run_error, run_zeros */
2812 			status = PTSFAX_ERRC;
2813 			goto out;
2814 		}
2815     }
2816     /* Interpreting the run requires scanning the */
2817     /* previous ('reference') line. */
2818     {
2819 	int prev_count = count;
2820 	unsigned char prev_data;
2821 	int dlen;
2822 	static const unsigned char count_bit[8] =
2823 	{0x80, 1, 2, 4, 8, 0x10, 0x20, 0x40};
2824 	unsigned char *prev_q = prev_q01 + (q - q0);
2825 	int plen;
2826 
2827 	if (!(count & 7))
2828 	    prev_q++;		/* because of skip macros */
2829 	prev_data = prev_q[-1] ^ invert;
2830 	/* Find the b1 transition. */
2831 	if ((prev_data & count_bit[prev_count & 7]) &&
2832 	    (prev_count < init_count || invert != invert_white)
2833 	    ) {			/* Look for changing white first. */
2834 	    if_debug1('W', " data=0x%x", prev_data);
2835 	    skip_black_pixels(prev_data, prev_q,
2836 			      prev_count, invert, plen);
2837 	    if (prev_count < end_count)		/* overshot */
2838 		prev_count = end_count;
2839 	    if_debug1('W', " b1 other=%d", prev_count);
2840 	}
2841 	if (prev_count != end_count) {
2842 	    if_debug1('W', " data=0x%x", prev_data);
2843 	    skip_white_pixels(prev_data, prev_q,
2844 			      prev_count, invert, plen);
2845 	    if (prev_count < end_count)		/* overshot */
2846 		prev_count = end_count;
2847 	    if_debug1('W', " b1 same=%d", prev_count);
2848 	}
2849 	/* b1 = prev_count; */
2850 	if (rlen == run2_pass) {	/* Pass mode.  Find b2. */
2851 	    if (prev_count != end_count) {
2852 		if_debug1('W', " data=0x%x", prev_data);
2853 		skip_black_pixels(prev_data, prev_q,
2854 				  prev_count, invert, plen);
2855 		if (prev_count < end_count)	/* overshot */
2856 		    prev_count = end_count;
2857 	    }
2858 	    /* b2 = prev_count; */
2859 	    if_debug2('W', " b2=%d, pass %d\n",
2860 		      prev_count, count - prev_count);
2861 	} else {		/* Vertical coding. */
2862 	    /* Remember that count counts *down*. */
2863 	    prev_count += rlen - vertical_0;	/* a1 */
2864 	    if_debug2('W', " vertical %d -> %d\n",
2865 		      rlen - vertical_0, prev_count);
2866 	}
2867 	/* Now either invert or skip from count */
2868 	/* to prev_count, and reset count. */
2869 	if (invert == invert_white) {	/* Skip data bits. */
2870 	    q = endptr - (prev_count >> 3);
2871 	    qbit = prev_count & 7;
2872 	} else {		/* Invert data bits. */
2873 	    dlen = count - prev_count;
2874 	    invert_data(dlen, black_byte, DO_NOTHING, idd);
2875 	}
2876 	count = prev_count;
2877 	if (rlen >= 0)		/* vertical mode */
2878 	    invert = ~invert;	/* polarity changes */
2879     }
2880     goto top;
2881  out3:
2882     if (bits_left > 0 && peek_bits(1)) {
2883 	/* This is a 1-bit "vertical 0" code, which we can still process. */
2884 	goto v0;
2885     }
2886     /* falls through */
2887   out0:status = 0;
2888     /* falls through */
2889   out:cfd_store_state();
2890     ss->invert = invert;
2891     return status;
2892     /*
2893      * We handle horizontal decoding here, so that we can
2894      * branch back into it if we run out of input data.
2895      */
2896     /* White, then black. */
2897   hww:get_run(cf_white_decode, cfd_white_initial_bits, cfd_white_min_bits,
2898 	      rlen, " white", wwl, outww);
2899     if ((count -= rlen) < end_count) {
2900 	status = PTSFAX_ERRC;
2901 	goto out;
2902     }
2903     skip_data(rlen, hww);
2904     /* Handle the second half of a white-black horizontal code. */
2905   hwb:get_run(cf_black_decode, cfd_black_initial_bits, cfd_black_min_bits,
2906 	      rlen, " black", wbl, outwb);
2907     if ((count -= rlen) < end_count) {
2908 	status = PTSFAX_ERRC;
2909 	goto out;
2910     }
2911     invert_data(rlen, black_byte, goto hwb, ihwb);
2912     goto top;
2913   outww:ss->run_color = -2;
2914     goto out0;
2915   outwb:ss->run_color = 1;
2916     goto out0;
2917     /* Black, then white. */
2918   hbb:get_run(cf_black_decode, cfd_black_initial_bits, cfd_black_min_bits,
2919 	      rlen, " black", bbl, outbb);
2920     if ((count -= rlen) < end_count) {
2921 	status = PTSFAX_ERRC;
2922 	goto out;
2923     }
2924     invert_data(rlen, black_byte, goto hbb, ihbb);
2925     /* Handle the second half of a black-white horizontal code. */
2926   hbw:get_run(cf_white_decode, cfd_white_initial_bits, cfd_white_min_bits,
2927 	      rlen, " white", bwl, outbw);
2928     if ((count -= rlen) < end_count) {
2929 	status = PTSFAX_ERRC;
2930 	goto out;
2931     }
2932     skip_data(rlen, hbw);
2933     goto top;
2934   outbb:ss->run_color = 2;
2935     goto out0;
2936   outbw:ss->run_color = -1;
2937     goto out0;
2938 }
2939 
2940 #if 1				/*************** */
2941 static int
cf_decode_uncompressed(stream_CFD_state * ss,stream_cursor_read * pr)2942 cf_decode_uncompressed(stream_CFD_state * ss, stream_cursor_read * pr)
2943 {
2944     (void)ss;
2945     (void)pr;
2946     /**** pts ****/
2947     return PTSFAX_ERRC;
2948 }
2949 #else /*************** */
2950 
2951 /* Decode uncompressed data. */
2952 /* (Not tested: no sample data available!) */
2953 /****** DOESN'T CHECK FOR OVERFLOWING SCAN LINE ******/
2954 static int
cf_decode_uncompressed(stream * s)2955 cf_decode_uncompressed(stream * s)
2956 {
2957     cfd_declare_state;
2958     const cfd_node *np;
2959     int clen, rlen;
2960 
2961     cfd_load_state();
2962     while (1) {
2963 	ensure_bits(cfd_uncompressed_initial_bits, NOOUT);
2964 	np = &cf_uncompressed_decode[peek_bits(cfd_uncompressed_initial_bits)];
2965 	clen = np->code_length;
2966 	rlen = np->run_length;
2967 	if (clen > cfd_uncompressed_initial_bits) {	/* Must be an exit code. */
2968 	    break;
2969 	}
2970 	if (rlen == cfd_uncompressed_initial_bits) {	/* Longest representable white run */
2971 	    if_debug1('W', "[wu]%d\n", rlen);
2972 	    if ((qbit -= cfd_uncompressed_initial_bits) < 0)
2973 		qbit += 8, q++;
2974 	} else {
2975 	    if_debug1('W', "[wu]%d+1\n", rlen);
2976 	    if (qbit -= rlen < 0)
2977 		qbit += 8, q++;
2978 	    *q ^= 1 << qbit;
2979 	}
2980 	skip_bits(clen);
2981     }
2982     clen -= cfd_uncompressed_initial_bits;
2983     skip_bits(cfd_uncompressed_initial_bits);
2984     ensure_bits(clen, NOOUT);
2985     np = &cf_uncompressed_decode[rlen + peek_var_bits(clen)];
2986     rlen = np->run_length;
2987     skip_bits(np->code_length);
2988     if_debug1('w', "[wu]exit %d\n", rlen);
2989     if (rlen >= 0) {		/* Valid exit code, rlen = 2 * run length + next polarity */
2990 	if ((qbit -= rlen >> 1) < 0)
2991 	    qbit += 8, q++;
2992 	rlen &= 1;
2993     }
2994   out:
2995 /******* WRONG ******/
2996     cfd_store_state();
2997     return rlen;
2998 }
2999 
3000 #endif /*************** */
3001 
3002 /* Move as much data as possible from one buffer to another. */
3003 /* Return 0 if the input became empty, 1 if the output became full. */
3004 int
stream_move(stream_cursor_read * pr,stream_cursor_write * pw)3005 stream_move(stream_cursor_read * pr, stream_cursor_write * pw)
3006 {
3007     unsigned int rcount = pr->limit - pr->ptr;
3008     unsigned int wcount = pw->limit - pw->ptr;
3009     unsigned int count;
3010     int status;
3011 
3012     if (rcount <= wcount)
3013         count = rcount, status = 0;
3014     else
3015         count = wcount, status = 1;
3016 
3017     /**** pts ****/ /* Dat: memcpy should be enough instead of memmove */
3018     #if 0
3019       memmove(pw->ptr + 1, pr->ptr + 1, count);
3020       pr->ptr += count;
3021       pw->ptr += count;
3022     #else
3023       while (count--!=0) *++pw->ptr=*++pr->ptr;
3024     #endif
3025     return status;
3026 }
3027 
3028 #if 1 /**** pts ****/
3029 /* Stream template */
3030 const stream_template s_CFD_template =
3031 {/*0, &st_CFD_state*/ s_CFD_init, s_CFD_process, 1, 1, s_CFD_release,
3032  s_CFD_set_defaults, 0
3033 };
3034 #endif
3035 
3036 /* end of former scfd.c */
3037 
3038 
3039 #endif /* USE_BUILTIN_FAXD */
3040 
3041 #if USE_BUILTIN_FAXE || USE_BUILTIN_FAXD
3042 /* shc.c */
3043 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
3044 /* Support code for shc.h */
3045 /* #include "scommon.h" */
3046 /* #include "shc.h" */
3047 /* #include "scfx.h" */ /**** pts ****/ /* struct stream_hc_state_s */
3048 
3049 /* ------ Encoding ------ */
3050 
3051 /* Empty the 1-word buffer onto the output stream. */
3052 /* q has already been incremented. */
3053 static void
hc_put_code_proc(bool reverse_bits,unsigned char * q,unsigned int cw)3054 hc_put_code_proc(bool reverse_bits, unsigned char * q, unsigned int cw)
3055 {
3056 #define cb(n) ((unsigned char)(cw >> (n * 8)))
3057     if (reverse_bits) {
3058 #if hc_bits_size > 16
3059 	q[-3] = byte_reverse_bits[cb(3)];
3060 	q[-2] = byte_reverse_bits[cb(2)];
3061 #endif
3062 	q[-1] = byte_reverse_bits[cb(1)];
3063 	q[0] = byte_reverse_bits[cb(0)];
3064     } else {
3065 #if hc_bits_size > 16
3066 	q[-3] = cb(3);
3067 	q[-2] = cb(2);
3068 #endif
3069 	q[-1] = cb(1);
3070 	q[0] = cb(0);
3071     }
3072 #undef cb
3073 }
3074 
3075 /* Put out any final bytes. */
3076 /* Note that this does a store_state, but not a load_state. */
3077 static unsigned char *
hc_put_last_bits_proc(stream_hc_state * ss,unsigned char * q,unsigned int bits,int bits_left)3078 hc_put_last_bits_proc(stream_hc_state * ss, unsigned char * q, unsigned int bits, int bits_left)
3079 {
3080     while (bits_left < hc_bits_size) {
3081 	unsigned char c = (unsigned char) (bits >> (hc_bits_size - 8));
3082 
3083 	if (ss->FirstBitLowOrder)
3084 	    c = byte_reverse_bits[c];
3085 	*++q = c;
3086 	bits <<= 8;
3087 	bits_left += 8;
3088     }
3089     ss->bits = bits;
3090     ss->bits_left = bits_left;
3091     return q;
3092 }
3093 
3094 /* end of former shc.c */
3095 
3096 
3097 /* gsbittab.c */
3098 /*$Id: pts_fax.c,v 1.3 2005/02/21 13:09:56 pts Exp $ */
3099 /* Tables for bit operations */
3100 /* #include "gstypes.h" */
3101 /* #include "gsbittab.h" */
3102 
3103 /* ---------------- Byte processing tables ---------------- */
3104 
3105 /*
3106  * byte_reverse_bits[B] = the unsigned char B with the order of bits reversed.
3107  */
3108 impl_const unsigned char byte_reverse_bits[256] = {
3109     bit_table_8(0, 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80)
3110 };
3111 
3112 /*
3113  * byte_right_mask[N] = a unsigned char with N trailing 1s, 0 <= N <= 8.
3114  */
3115 impl_const unsigned char byte_right_mask[9] = {
3116     0, 1, 3, 7, 0xf, 0x1f, 0x3f, 0x7f, 0xff
3117 };
3118 
3119 /*
3120  * byte_count_bits[B] = the number of 1-bits in a unsigned char with value B.
3121  */
3122 impl_const unsigned char byte_count_bits[256] = {
3123     bit_table_8(0, 1, 1, 1, 1, 1, 1, 1, 1)
3124 };
3125 
3126 /* ---------------- Scanning tables ---------------- */
3127 
3128 /*
3129  * byte_bit_run_length_N[B], for 0 <= N <= 7, gives the length of the
3130  * run of 1-bits starting at bit N in a unsigned char with value B,
3131  * numbering the bits in the unsigned char as 01234567.  If the run includes
3132  * the low-order bit (i.e., might be continued into a following unsigned char),
3133  * the run length is increased by 8.
3134  */
3135 
3136 #define t8(n) n,n,n,n,n+1,n+1,n+2,n+11
3137 #define r8(n) n,n,n,n,n,n,n,n
3138 #define r16(n) r8(n),r8(n)
3139 #define r32(n) r16(n),r16(n)
3140 #define r64(n) r32(n),r32(n)
3141 #define r128(n) r64(n),r64(n)
3142 impl_const unsigned char byte_bit_run_length_0[256] = {
3143     r128(0), r64(1), r32(2), r16(3), r8(4), t8(5)
3144 };
3145 impl_const unsigned char byte_bit_run_length_1[256] = {
3146     r64(0), r32(1), r16(2), r8(3), t8(4),
3147     r64(0), r32(1), r16(2), r8(3), t8(4)
3148 };
3149 impl_const unsigned char byte_bit_run_length_2[256] = {
3150     r32(0), r16(1), r8(2), t8(3),
3151     r32(0), r16(1), r8(2), t8(3),
3152     r32(0), r16(1), r8(2), t8(3),
3153     r32(0), r16(1), r8(2), t8(3)
3154 };
3155 impl_const unsigned char byte_bit_run_length_3[256] = {
3156     r16(0), r8(1), t8(2), r16(0), r8(1), t8(2),
3157     r16(0), r8(1), t8(2), r16(0), r8(1), t8(2),
3158     r16(0), r8(1), t8(2), r16(0), r8(1), t8(2),
3159     r16(0), r8(1), t8(2), r16(0), r8(1), t8(2)
3160 };
3161 impl_const unsigned char byte_bit_run_length_4[256] = {
3162     r8(0), t8(1), r8(0), t8(1), r8(0), t8(1), r8(0), t8(1),
3163     r8(0), t8(1), r8(0), t8(1), r8(0), t8(1), r8(0), t8(1),
3164     r8(0), t8(1), r8(0), t8(1), r8(0), t8(1), r8(0), t8(1),
3165     r8(0), t8(1), r8(0), t8(1), r8(0), t8(1), r8(0), t8(1),
3166 };
3167 
3168 #define rr8(a,b,c,d,e,f,g,h)\
3169   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h,\
3170   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h,\
3171   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h,\
3172   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h,\
3173   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h,\
3174   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h,\
3175   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h,\
3176   a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h, a,b,c,d,e,f,g,h
3177 impl_const unsigned char byte_bit_run_length_5[256] = {
3178     rr8(0, 0, 0, 0, 1, 1, 2, 11)
3179 };
3180 impl_const unsigned char byte_bit_run_length_6[256] = {
3181     rr8(0, 0, 1, 10, 0, 0, 1, 10)
3182 };
3183 impl_const unsigned char byte_bit_run_length_7[256] = {
3184     rr8(0, 9, 0, 9, 0, 9, 0, 9)
3185 };
3186 
3187 /* Pointer tables indexed by bit number. */
3188 
3189 impl_const unsigned char *const byte_bit_run_length[8] = {
3190     byte_bit_run_length_0, byte_bit_run_length_1,
3191     byte_bit_run_length_2, byte_bit_run_length_3,
3192     byte_bit_run_length_4, byte_bit_run_length_5,
3193     byte_bit_run_length_6, byte_bit_run_length_7
3194 };
3195 impl_const unsigned char *const byte_bit_run_length_neg[8] = {
3196     byte_bit_run_length_0, byte_bit_run_length_7,
3197     byte_bit_run_length_6, byte_bit_run_length_5,
3198     byte_bit_run_length_4, byte_bit_run_length_3,
3199     byte_bit_run_length_2, byte_bit_run_length_1
3200 };
3201 
3202 /*
3203  * byte_acegbdfh_to_abcdefgh[acegbdfh] = abcdefgh, where the letters
3204  * denote the individual bits of the unsigned char.
3205  */
3206 impl_const unsigned char byte_acegbdfh_to_abcdefgh[256] = {
3207     bit_table_8(0, 0x80, 0x20, 0x08, 0x02, 0x40, 0x10, 0x04, 0x01)
3208 };
3209 
3210 #if 0
3211 /* Some C compilers insist on having executable code in every file.... */
3212 void gsbittab_dummy _((void));	/* for picky compilers */
3213 void
3214 gsbittab_dummy(void)
3215 {
3216 }
3217 #endif
3218 
3219 /* end of former gsbittab.c */
3220 #endif /* USE_BUILTIN_FAXE || USE_BUILTIN_FAXD */
3221 
3222 /* end of pts_fax.c */
3223