/src/freeimage-svn/FreeImage/trunk/Source/LibTIFF4/tif_fax3.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright (c) 1990-1997 Sam Leffler  | 
3  |  |  * Copyright (c) 1991-1997 Silicon Graphics, Inc.  | 
4  |  |  *  | 
5  |  |  * Permission to use, copy, modify, distribute, and sell this software and  | 
6  |  |  * its documentation for any purpose is hereby granted without fee, provided  | 
7  |  |  * that (i) the above copyright notices and this permission notice appear in  | 
8  |  |  * all copies of the software and related documentation, and (ii) the names of  | 
9  |  |  * Sam Leffler and Silicon Graphics may not be used in any advertising or  | 
10  |  |  * publicity relating to the software without the specific, prior written  | 
11  |  |  * permission of Sam Leffler and Silicon Graphics.  | 
12  |  |  *  | 
13  |  |  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,  | 
14  |  |  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY  | 
15  |  |  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  | 
16  |  |  *  | 
17  |  |  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR  | 
18  |  |  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,  | 
19  |  |  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  | 
20  |  |  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF  | 
21  |  |  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE  | 
22  |  |  * OF THIS SOFTWARE.  | 
23  |  |  */  | 
24  |  |  | 
25  |  | #include "tiffiop.h"  | 
26  |  | #ifdef CCITT_SUPPORT  | 
27  |  | /*  | 
28  |  |  * TIFF Library.  | 
29  |  |  *  | 
30  |  |  * CCITT Group 3 (T.4) and Group 4 (T.6) Compression Support.  | 
31  |  |  *  | 
32  |  |  * This file contains support for decoding and encoding TIFF  | 
33  |  |  * compression algorithms 2, 3, 4, and 32771.  | 
34  |  |  *  | 
35  |  |  * Decoder support is derived, with permission, from the code  | 
36  |  |  * in Frank Cringle's viewfax program;  | 
37  |  |  *      Copyright (C) 1990, 1995  Frank D. Cringle.  | 
38  |  |  */  | 
39  |  | #include "tif_fax3.h"  | 
40  |  | #define G3CODES  | 
41  |  | #include "t4.h"  | 
42  |  | #include <stdio.h>  | 
43  |  |  | 
44  |  | #ifndef EOF_REACHED_COUNT_THRESHOLD  | 
45  |  | /* Arbitrary threshold to avoid corrupted single-strip files with extremely  | 
46  |  |  * large imageheight to cause apparently endless looping, such as in  | 
47  |  |  * https://gitlab.com/libtiff/libtiff/-/issues/583  | 
48  |  |  */  | 
49  | 0  | #define EOF_REACHED_COUNT_THRESHOLD 8192  | 
50  |  | #endif  | 
51  |  |  | 
52  |  | /*  | 
53  |  |  * Compression+decompression state blocks are  | 
54  |  |  * derived from this ``base state'' block.  | 
55  |  |  */  | 
56  |  | typedef struct  | 
57  |  | { | 
58  |  |     int rw_mode;        /* O_RDONLY for decode, else encode */  | 
59  |  |     int mode;           /* operating mode */  | 
60  |  |     tmsize_t rowbytes;  /* bytes in a decoded scanline */  | 
61  |  |     uint32_t rowpixels; /* pixels in a scanline */  | 
62  |  |  | 
63  |  |     uint16_t cleanfaxdata; /* CleanFaxData tag */  | 
64  |  |     uint32_t badfaxrun;    /* BadFaxRun tag */  | 
65  |  |     uint32_t badfaxlines;  /* BadFaxLines tag */  | 
66  |  |     uint32_t groupoptions; /* Group 3/4 options tag */  | 
67  |  |  | 
68  |  |     TIFFVGetMethod vgetparent; /* super-class method */  | 
69  |  |     TIFFVSetMethod vsetparent; /* super-class method */  | 
70  |  |     TIFFPrintMethod printdir;  /* super-class method */  | 
71  |  | } Fax3BaseState;  | 
72  | 0  | #define Fax3State(tif) ((Fax3BaseState *)(tif)->tif_data)  | 
73  |  |  | 
74  |  | typedef enum  | 
75  |  | { | 
76  |  |     G3_1D,  | 
77  |  |     G3_2D  | 
78  |  | } Ttag;  | 
79  |  | typedef struct  | 
80  |  | { | 
81  |  |     Fax3BaseState b;  | 
82  |  |  | 
83  |  |     /* Decoder state info */  | 
84  |  |     const unsigned char *bitmap; /* bit reversal table */  | 
85  |  |     uint32_t data;               /* current i/o byte/word */  | 
86  |  |     int bit;                     /* current i/o bit in byte */  | 
87  |  |     int EOLcnt;                  /* count of EOL codes recognized */  | 
88  |  |     int eofReachedCount;         /* number of times decode has been called with  | 
89  |  |                                     EOF already reached */  | 
90  |  |     TIFFFaxFillFunc fill;        /* fill routine */  | 
91  |  |     uint32_t *runs;              /* b&w runs for current/previous row */  | 
92  |  |     uint32_t nruns;              /* size of the refruns / curruns arrays */  | 
93  |  |     uint32_t *refruns;           /* runs for reference line */  | 
94  |  |     uint32_t *curruns;           /* runs for current line */  | 
95  |  |  | 
96  |  |     /* Encoder state info */  | 
97  |  |     Ttag tag;               /* encoding state */  | 
98  |  |     unsigned char *refline; /* reference line for 2d decoding */  | 
99  |  |     int k;                  /* #rows left that can be 2d encoded */  | 
100  |  |     int maxk;               /* max #rows that can be 2d encoded */  | 
101  |  |  | 
102  |  |     int line;  | 
103  |  | } Fax3CodecState;  | 
104  | 0  | #define DecoderState(tif) ((Fax3CodecState *)Fax3State(tif))  | 
105  | 0  | #define EncoderState(tif) ((Fax3CodecState *)Fax3State(tif))  | 
106  |  |  | 
107  | 0  | #define is2DEncoding(sp) (sp->b.groupoptions & GROUP3OPT_2DENCODING)  | 
108  | 0  | #define isAligned(p, t) ((((size_t)(p)) & (sizeof(t) - 1)) == 0)  | 
109  |  |  | 
110  |  | /*  | 
111  |  |  * Group 3 and Group 4 Decoding.  | 
112  |  |  */  | 
113  |  |  | 
114  |  | /*  | 
115  |  |  * These macros glue the TIFF library state to  | 
116  |  |  * the state expected by Frank's decoder.  | 
117  |  |  */  | 
118  |  | #define DECLARE_STATE(tif, sp, mod)                                            \  | 
119  | 0  |     static const char module[] = mod;                                          \  | 
120  | 0  |     Fax3CodecState *sp = DecoderState(tif);                                    \  | 
121  | 0  |     int a0;                                   /* reference element */          \  | 
122  | 0  |     int lastx = sp->b.rowpixels;              /* last element in row */        \  | 
123  | 0  |     uint32_t BitAcc;                          /* bit accumulator */            \  | 
124  | 0  |     int BitsAvail;                            /* # valid bits in BitAcc */     \  | 
125  | 0  |     int RunLength;                            /* length of current run */      \  | 
126  | 0  |     unsigned char *cp;                        /* next byte of input data */    \  | 
127  | 0  |     unsigned char *ep;                        /* end of input data */          \  | 
128  | 0  |     uint32_t *pa;                             /* place to stuff next run */    \  | 
129  | 0  |     uint32_t *thisrun;                        /* current row's run array */    \  | 
130  | 0  |     int EOLcnt;                               /* # EOL codes recognized */     \  | 
131  | 0  |     const unsigned char *bitmap = sp->bitmap; /* input data bit reverser */    \  | 
132  | 0  |     const TIFFFaxTabEnt *TabEnt  | 
133  |  |  | 
134  |  | #define DECLARE_STATE_2D(tif, sp, mod)                                         \  | 
135  | 0  |     DECLARE_STATE(tif, sp, mod);                                               \  | 
136  | 0  |     int b1; /* next change on prev line */                                     \  | 
137  | 0  |     uint32_t                                                                   \  | 
138  | 0  |         *pb /* next run in reference line */ /*                                \  | 
139  |  |                                               * Load any state that may be     \  | 
140  |  |                                               * changed during decoding.       \  | 
141  |  |                                               */  | 
142  |  | #define CACHE_STATE(tif, sp)                                                   \  | 
143  | 0  |     do                                                                         \  | 
144  | 0  |     {                                                                          \ | 
145  | 0  |         BitAcc = sp->data;                                                     \  | 
146  | 0  |         BitsAvail = sp->bit;                                                   \  | 
147  | 0  |         EOLcnt = sp->EOLcnt;                                                   \  | 
148  | 0  |         cp = (unsigned char *)tif->tif_rawcp;                                  \  | 
149  | 0  |         ep = cp + tif->tif_rawcc;                                              \  | 
150  | 0  |     } while (0)  | 
151  |  | /*  | 
152  |  |  * Save state possibly changed during decoding.  | 
153  |  |  */  | 
154  |  | #define UNCACHE_STATE(tif, sp)                                                 \  | 
155  | 0  |     do                                                                         \  | 
156  | 0  |     {                                                                          \ | 
157  | 0  |         sp->bit = BitsAvail;                                                   \  | 
158  | 0  |         sp->data = BitAcc;                                                     \  | 
159  | 0  |         sp->EOLcnt = EOLcnt;                                                   \  | 
160  | 0  |         tif->tif_rawcc -= (tmsize_t)((uint8_t *)cp - tif->tif_rawcp);          \  | 
161  | 0  |         tif->tif_rawcp = (uint8_t *)cp;                                        \  | 
162  | 0  |     } while (0)  | 
163  |  |  | 
164  |  | /*  | 
165  |  |  * Setup state for decoding a strip.  | 
166  |  |  */  | 
167  |  | static int Fax3PreDecode(TIFF *tif, uint16_t s)  | 
168  | 0  | { | 
169  | 0  |     Fax3CodecState *sp = DecoderState(tif);  | 
170  |  | 
  | 
171  | 0  |     (void)s;  | 
172  | 0  |     assert(sp != NULL);  | 
173  | 0  |     sp->bit = 0; /* force initial read */  | 
174  | 0  |     sp->data = 0;  | 
175  | 0  |     sp->EOLcnt = 0; /* force initial scan for EOL */  | 
176  | 0  |     sp->eofReachedCount = 0;  | 
177  |  |     /*  | 
178  |  |      * Decoder assumes lsb-to-msb bit order.  Note that we select  | 
179  |  |      * this here rather than in Fax3SetupState so that viewers can  | 
180  |  |      * hold the image open, fiddle with the FillOrder tag value,  | 
181  |  |      * and then re-decode the image.  Otherwise they'd need to close  | 
182  |  |      * and open the image to get the state reset.  | 
183  |  |      */  | 
184  | 0  |     sp->bitmap =  | 
185  | 0  |         TIFFGetBitRevTable(tif->tif_dir.td_fillorder != FILLORDER_LSB2MSB);  | 
186  | 0  |     sp->curruns = sp->runs;  | 
187  | 0  |     if (sp->refruns)  | 
188  | 0  |     { /* init reference line to white */ | 
189  | 0  |         sp->refruns = sp->runs + sp->nruns;  | 
190  | 0  |         sp->refruns[0] = (uint32_t)sp->b.rowpixels;  | 
191  | 0  |         sp->refruns[1] = 0;  | 
192  | 0  |     }  | 
193  | 0  |     sp->line = 0;  | 
194  | 0  |     return (1);  | 
195  | 0  | }  | 
196  |  |  | 
197  |  | /*  | 
198  |  |  * Routine for handling various errors/conditions.  | 
199  |  |  * Note how they are "glued into the decoder" by  | 
200  |  |  * overriding the definitions used by the decoder.  | 
201  |  |  */  | 
202  |  |  | 
203  |  | static void Fax3Unexpected(const char *module, TIFF *tif, uint32_t line,  | 
204  |  |                            uint32_t a0)  | 
205  | 0  | { | 
206  | 0  |     TIFFErrorExtR(tif, module,  | 
207  | 0  |                   "Bad code word at line %" PRIu32 " of %s %" PRIu32  | 
208  | 0  |                   " (x %" PRIu32 ")",  | 
209  | 0  |                   line, isTiled(tif) ? "tile" : "strip",  | 
210  | 0  |                   (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0);  | 
211  | 0  | }  | 
212  | 0  | #define unexpected(table, a0) Fax3Unexpected(module, tif, sp->line, a0)  | 
213  |  |  | 
214  |  | static void Fax3Extension(const char *module, TIFF *tif, uint32_t line,  | 
215  |  |                           uint32_t a0)  | 
216  | 0  | { | 
217  | 0  |     TIFFErrorExtR(tif, module,  | 
218  | 0  |                   "Uncompressed data (not supported) at line %" PRIu32  | 
219  | 0  |                   " of %s %" PRIu32 " (x %" PRIu32 ")",  | 
220  | 0  |                   line, isTiled(tif) ? "tile" : "strip",  | 
221  | 0  |                   (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0);  | 
222  | 0  | }  | 
223  | 0  | #define extension(a0) Fax3Extension(module, tif, sp->line, a0)  | 
224  |  |  | 
225  |  | static void Fax3BadLength(const char *module, TIFF *tif, uint32_t line,  | 
226  |  |                           uint32_t a0, uint32_t lastx)  | 
227  | 0  | { | 
228  | 0  |     TIFFWarningExtR(tif, module,  | 
229  | 0  |                     "%s at line %" PRIu32 " of %s %" PRIu32 " (got %" PRIu32  | 
230  | 0  |                     ", expected %" PRIu32 ")",  | 
231  | 0  |                     a0 < lastx ? "Premature EOL" : "Line length mismatch", line,  | 
232  | 0  |                     isTiled(tif) ? "tile" : "strip",  | 
233  | 0  |                     (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0,  | 
234  | 0  |                     lastx);  | 
235  | 0  | }  | 
236  | 0  | #define badlength(a0, lastx) Fax3BadLength(module, tif, sp->line, a0, lastx)  | 
237  |  |  | 
238  |  | static void Fax3PrematureEOF(const char *module, TIFF *tif, uint32_t line,  | 
239  |  |                              uint32_t a0)  | 
240  | 0  | { | 
241  | 0  |     TIFFWarningExtR(tif, module,  | 
242  | 0  |                     "Premature EOF at line %" PRIu32 " of %s %" PRIu32  | 
243  | 0  |                     " (x %" PRIu32 ")",  | 
244  | 0  |                     line, isTiled(tif) ? "tile" : "strip",  | 
245  | 0  |                     (isTiled(tif) ? tif->tif_curtile : tif->tif_curstrip), a0);  | 
246  | 0  | }  | 
247  |  | #define prematureEOF(a0)                                                       \  | 
248  | 0  |     do                                                                         \  | 
249  | 0  |     {                                                                          \ | 
250  | 0  |         Fax3PrematureEOF(module, tif, sp->line, a0);                           \  | 
251  | 0  |         ++sp->eofReachedCount;                                                 \  | 
252  | 0  |     } while (0)  | 
253  |  |  | 
254  |  | #define Nop  | 
255  |  |  | 
256  |  | /**  | 
257  |  |  * Decode the requested amount of G3 1D-encoded data.  | 
258  |  |  * @param buf destination buffer  | 
259  |  |  * @param occ available bytes in destination buffer  | 
260  |  |  * @param s number of planes (ignored)  | 
261  |  |  * @returns 1 for success, -1 in case of error  | 
262  |  |  */  | 
263  |  | static int Fax3Decode1D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)  | 
264  | 0  | { | 
265  | 0  |     DECLARE_STATE(tif, sp, "Fax3Decode1D");  | 
266  | 0  |     (void)s;  | 
267  | 0  |     if (occ % sp->b.rowbytes)  | 
268  | 0  |     { | 
269  | 0  |         TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");  | 
270  | 0  |         return (-1);  | 
271  | 0  |     }  | 
272  | 0  |     if (sp->eofReachedCount >= EOF_REACHED_COUNT_THRESHOLD)  | 
273  | 0  |     { | 
274  | 0  |         TIFFErrorExtR(  | 
275  | 0  |             tif, module,  | 
276  | 0  |             "End of file has already been reached %d times within that strip",  | 
277  | 0  |             sp->eofReachedCount);  | 
278  | 0  |         return (-1);  | 
279  | 0  |     }  | 
280  | 0  |     CACHE_STATE(tif, sp);  | 
281  | 0  |     thisrun = sp->curruns;  | 
282  | 0  |     while (occ > 0)  | 
283  | 0  |     { | 
284  | 0  |         a0 = 0;  | 
285  | 0  |         RunLength = 0;  | 
286  | 0  |         pa = thisrun;  | 
287  |  | #ifdef FAX3_DEBUG  | 
288  |  |         printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); | 
289  |  |         printf("-------------------- %" PRIu32 "\n", tif->tif_row); | 
290  |  |         fflush(stdout);  | 
291  |  | #endif  | 
292  | 0  |         SYNC_EOL(EOF1D);  | 
293  | 0  |         EXPAND1D(EOF1Da);  | 
294  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
295  | 0  |         buf += sp->b.rowbytes;  | 
296  | 0  |         occ -= sp->b.rowbytes;  | 
297  | 0  |         sp->line++;  | 
298  | 0  |         continue;  | 
299  | 0  |     EOF1D: /* premature EOF */  | 
300  | 0  |         CLEANUP_RUNS();  | 
301  | 0  |     EOF1Da: /* premature EOF */  | 
302  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
303  | 0  |         UNCACHE_STATE(tif, sp);  | 
304  | 0  |         return (-1);  | 
305  | 0  |     }  | 
306  | 0  |     UNCACHE_STATE(tif, sp);  | 
307  | 0  |     return (1);  | 
308  | 0  | }  | 
309  |  |  | 
310  |  | #define SWAP(t, a, b)                                                          \  | 
311  | 0  |     {                                                                          \ | 
312  | 0  |         t x;                                                                   \  | 
313  | 0  |         x = (a);                                                               \  | 
314  | 0  |         (a) = (b);                                                             \  | 
315  | 0  |         (b) = x;                                                               \  | 
316  | 0  |     }  | 
317  |  | /*  | 
318  |  |  * Decode the requested amount of G3 2D-encoded data.  | 
319  |  |  */  | 
320  |  | static int Fax3Decode2D(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)  | 
321  | 0  | { | 
322  | 0  |     DECLARE_STATE_2D(tif, sp, "Fax3Decode2D");  | 
323  | 0  |     int is1D; /* current line is 1d/2d-encoded */  | 
324  | 0  |     (void)s;  | 
325  | 0  |     if (occ % sp->b.rowbytes)  | 
326  | 0  |     { | 
327  | 0  |         TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");  | 
328  | 0  |         return (-1);  | 
329  | 0  |     }  | 
330  | 0  |     if (sp->eofReachedCount >= EOF_REACHED_COUNT_THRESHOLD)  | 
331  | 0  |     { | 
332  | 0  |         TIFFErrorExtR(  | 
333  | 0  |             tif, module,  | 
334  | 0  |             "End of file has already been reached %d times within that strip",  | 
335  | 0  |             sp->eofReachedCount);  | 
336  | 0  |         return (-1);  | 
337  | 0  |     }  | 
338  | 0  |     CACHE_STATE(tif, sp);  | 
339  | 0  |     while (occ > 0)  | 
340  | 0  |     { | 
341  | 0  |         a0 = 0;  | 
342  | 0  |         RunLength = 0;  | 
343  | 0  |         pa = thisrun = sp->curruns;  | 
344  |  | #ifdef FAX3_DEBUG  | 
345  |  |         printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d EOLcnt = %d", BitAcc, | 
346  |  |                BitsAvail, EOLcnt);  | 
347  |  | #endif  | 
348  | 0  |         SYNC_EOL(EOF2D);  | 
349  | 0  |         NeedBits8(1, EOF2D);  | 
350  | 0  |         is1D = GetBits(1); /* 1D/2D-encoding tag bit */  | 
351  | 0  |         ClrBits(1);  | 
352  |  | #ifdef FAX3_DEBUG  | 
353  |  |         printf(" %s\n-------------------- %" PRIu32 "\n", is1D ? "1D" : "2D", | 
354  |  |                tif->tif_row);  | 
355  |  |         fflush(stdout);  | 
356  |  | #endif  | 
357  | 0  |         pb = sp->refruns;  | 
358  | 0  |         b1 = *pb++;  | 
359  | 0  |         if (is1D)  | 
360  | 0  |             EXPAND1D(EOF2Da);  | 
361  | 0  |         else  | 
362  | 0  |             EXPAND2D(EOF2Da);  | 
363  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
364  | 0  |         if (pa < thisrun + sp->nruns)  | 
365  | 0  |         { | 
366  | 0  |             SETVALUE(0); /* imaginary change for reference */  | 
367  | 0  |         }  | 
368  | 0  |         SWAP(uint32_t *, sp->curruns, sp->refruns);  | 
369  | 0  |         buf += sp->b.rowbytes;  | 
370  | 0  |         occ -= sp->b.rowbytes;  | 
371  | 0  |         sp->line++;  | 
372  | 0  |         continue;  | 
373  | 0  |     EOF2D: /* premature EOF */  | 
374  | 0  |         CLEANUP_RUNS();  | 
375  | 0  |     EOF2Da: /* premature EOF */  | 
376  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
377  | 0  |         UNCACHE_STATE(tif, sp);  | 
378  | 0  |         return (-1);  | 
379  | 0  |     }  | 
380  | 0  |     UNCACHE_STATE(tif, sp);  | 
381  | 0  |     return (1);  | 
382  | 0  | }  | 
383  |  | #undef SWAP  | 
384  |  |  | 
385  |  | #define FILL(n, cp)                                                            \  | 
386  | 0  |     for (int32_t ifill = 0; ifill < (n); ++ifill)                              \  | 
387  | 0  |     {                                                                          \ | 
388  | 0  |         (cp)[ifill] = 0xff;                                                    \  | 
389  | 0  |     }                                                                          \  | 
390  | 0  |     (cp) += (n);  | 
391  |  |  | 
392  |  | #define ZERO(n, cp)                                                            \  | 
393  | 0  |     for (int32_t izero = 0; izero < (n); ++izero)                              \  | 
394  | 0  |     {                                                                          \ | 
395  | 0  |         (cp)[izero] = 0;                                                       \  | 
396  | 0  |     }                                                                          \  | 
397  | 0  |     (cp) += (n);  | 
398  |  |  | 
399  |  | /*  | 
400  |  |  * Bit-fill a row according to the white/black  | 
401  |  |  * runs generated during G3/G4 decoding.  | 
402  |  |  */  | 
403  |  | void _TIFFFax3fillruns(unsigned char *buf, uint32_t *runs, uint32_t *erun,  | 
404  |  |                        uint32_t lastx)  | 
405  | 0  | { | 
406  | 0  |     static const unsigned char _fillmasks[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, | 
407  | 0  |                                                0xf8, 0xfc, 0xfe, 0xff};  | 
408  | 0  |     unsigned char *cp;  | 
409  | 0  |     uint32_t x, bx, run;  | 
410  | 0  |     int32_t n, nw;  | 
411  | 0  |     int64_t *lp;  | 
412  |  | 
  | 
413  | 0  |     if ((erun - runs) & 1)  | 
414  | 0  |         *erun++ = 0;  | 
415  | 0  |     x = 0;  | 
416  | 0  |     for (; runs < erun; runs += 2)  | 
417  | 0  |     { | 
418  | 0  |         run = runs[0];  | 
419  | 0  |         if (x + run > lastx || run > lastx)  | 
420  | 0  |             run = runs[0] = (uint32_t)(lastx - x);  | 
421  | 0  |         if (run)  | 
422  | 0  |         { | 
423  | 0  |             cp = buf + (x >> 3);  | 
424  | 0  |             bx = x & 7;  | 
425  | 0  |             if (run > 8 - bx)  | 
426  | 0  |             { | 
427  | 0  |                 if (bx)  | 
428  | 0  |                 { /* align to byte boundary */ | 
429  | 0  |                     *cp++ &= 0xff << (8 - bx);  | 
430  | 0  |                     run -= 8 - bx;  | 
431  | 0  |                 }  | 
432  | 0  |                 if ((n = run >> 3) != 0)  | 
433  | 0  |                 { /* multiple bytes to fill */ | 
434  | 0  |                     if ((n / sizeof(int64_t)) > 1)  | 
435  | 0  |                     { | 
436  |  |                         /*  | 
437  |  |                          * Align to int64_tword boundary and fill.  | 
438  |  |                          */  | 
439  | 0  |                         for (; n && !isAligned(cp, int64_t); n--)  | 
440  | 0  |                             *cp++ = 0x00;  | 
441  | 0  |                         lp = (int64_t *)cp;  | 
442  | 0  |                         nw = (int32_t)(n / sizeof(int64_t));  | 
443  | 0  |                         n -= nw * sizeof(int64_t);  | 
444  | 0  |                         do  | 
445  | 0  |                         { | 
446  | 0  |                             *lp++ = 0L;  | 
447  | 0  |                         } while (--nw);  | 
448  | 0  |                         cp = (unsigned char *)lp;  | 
449  | 0  |                     }  | 
450  | 0  |                     ZERO(n, cp);  | 
451  | 0  |                     run &= 7;  | 
452  | 0  |                 }  | 
453  | 0  |                 if (run)  | 
454  | 0  |                     cp[0] &= 0xff >> run;  | 
455  | 0  |             }  | 
456  | 0  |             else  | 
457  | 0  |                 cp[0] &= ~(_fillmasks[run] >> bx);  | 
458  | 0  |             x += runs[0];  | 
459  | 0  |         }  | 
460  | 0  |         run = runs[1];  | 
461  | 0  |         if (x + run > lastx || run > lastx)  | 
462  | 0  |             run = runs[1] = lastx - x;  | 
463  | 0  |         if (run)  | 
464  | 0  |         { | 
465  | 0  |             cp = buf + (x >> 3);  | 
466  | 0  |             bx = x & 7;  | 
467  | 0  |             if (run > 8 - bx)  | 
468  | 0  |             { | 
469  | 0  |                 if (bx)  | 
470  | 0  |                 { /* align to byte boundary */ | 
471  | 0  |                     *cp++ |= 0xff >> bx;  | 
472  | 0  |                     run -= 8 - bx;  | 
473  | 0  |                 }  | 
474  | 0  |                 if ((n = run >> 3) != 0)  | 
475  | 0  |                 { /* multiple bytes to fill */ | 
476  | 0  |                     if ((n / sizeof(int64_t)) > 1)  | 
477  | 0  |                     { | 
478  |  |                         /*  | 
479  |  |                          * Align to int64_t boundary and fill.  | 
480  |  |                          */  | 
481  | 0  |                         for (; n && !isAligned(cp, int64_t); n--)  | 
482  | 0  |                             *cp++ = 0xff;  | 
483  | 0  |                         lp = (int64_t *)cp;  | 
484  | 0  |                         nw = (int32_t)(n / sizeof(int64_t));  | 
485  | 0  |                         n -= nw * sizeof(int64_t);  | 
486  | 0  |                         do  | 
487  | 0  |                         { | 
488  | 0  |                             *lp++ = -1L;  | 
489  | 0  |                         } while (--nw);  | 
490  | 0  |                         cp = (unsigned char *)lp;  | 
491  | 0  |                     }  | 
492  | 0  |                     FILL(n, cp);  | 
493  | 0  |                     run &= 7;  | 
494  | 0  |                 }  | 
495  |  |                 /* Explicit 0xff masking to make icc -check=conversions happy */  | 
496  | 0  |                 if (run)  | 
497  | 0  |                     cp[0] = (unsigned char)((cp[0] | (0xff00 >> run)) & 0xff);  | 
498  | 0  |             }  | 
499  | 0  |             else  | 
500  | 0  |                 cp[0] |= _fillmasks[run] >> bx;  | 
501  | 0  |             x += runs[1];  | 
502  | 0  |         }  | 
503  | 0  |     }  | 
504  | 0  |     assert(x == lastx);  | 
505  | 0  | }  | 
506  |  | #undef ZERO  | 
507  |  | #undef FILL  | 
508  |  |  | 
509  |  | static int Fax3FixupTags(TIFF *tif)  | 
510  | 0  | { | 
511  | 0  |     (void)tif;  | 
512  | 0  |     return (1);  | 
513  | 0  | }  | 
514  |  |  | 
515  |  | /*  | 
516  |  |  * Setup G3/G4-related compression/decompression state  | 
517  |  |  * before data is processed.  This routine is called once  | 
518  |  |  * per image -- it sets up different state based on whether  | 
519  |  |  * or not decoding or encoding is being done and whether  | 
520  |  |  * 1D- or 2D-encoded data is involved.  | 
521  |  |  */  | 
522  |  | static int Fax3SetupState(TIFF *tif)  | 
523  | 0  | { | 
524  | 0  |     static const char module[] = "Fax3SetupState";  | 
525  | 0  |     TIFFDirectory *td = &tif->tif_dir;  | 
526  | 0  |     Fax3BaseState *sp = Fax3State(tif);  | 
527  | 0  |     int needsRefLine;  | 
528  | 0  |     Fax3CodecState *dsp = (Fax3CodecState *)Fax3State(tif);  | 
529  | 0  |     tmsize_t rowbytes;  | 
530  | 0  |     uint32_t rowpixels;  | 
531  |  | 
  | 
532  | 0  |     if (td->td_bitspersample != 1)  | 
533  | 0  |     { | 
534  | 0  |         TIFFErrorExtR(tif, module,  | 
535  | 0  |                       "Bits/sample must be 1 for Group 3/4 encoding/decoding");  | 
536  | 0  |         return (0);  | 
537  | 0  |     }  | 
538  |  |     /*  | 
539  |  |      * Calculate the scanline/tile widths.  | 
540  |  |      */  | 
541  | 0  |     if (isTiled(tif))  | 
542  | 0  |     { | 
543  | 0  |         rowbytes = TIFFTileRowSize(tif);  | 
544  | 0  |         rowpixels = td->td_tilewidth;  | 
545  | 0  |     }  | 
546  | 0  |     else  | 
547  | 0  |     { | 
548  | 0  |         rowbytes = TIFFScanlineSize(tif);  | 
549  | 0  |         rowpixels = td->td_imagewidth;  | 
550  | 0  |     }  | 
551  | 0  |     if ((int64_t)rowbytes < ((int64_t)rowpixels + 7) / 8)  | 
552  | 0  |     { | 
553  | 0  |         TIFFErrorExtR(tif, module,  | 
554  | 0  |                       "Inconsistent number of bytes per row : rowbytes=%" PRId64  | 
555  | 0  |                       " rowpixels=%" PRIu32,  | 
556  | 0  |                       (int64_t)rowbytes, rowpixels);  | 
557  | 0  |         return (0);  | 
558  | 0  |     }  | 
559  | 0  |     sp->rowbytes = rowbytes;  | 
560  | 0  |     sp->rowpixels = rowpixels;  | 
561  |  |     /*  | 
562  |  |      * Allocate any additional space required for decoding/encoding.  | 
563  |  |      */  | 
564  | 0  |     needsRefLine = ((sp->groupoptions & GROUP3OPT_2DENCODING) ||  | 
565  | 0  |                     td->td_compression == COMPRESSION_CCITTFAX4);  | 
566  |  |  | 
567  |  |     /*  | 
568  |  |       Assure that allocation computations do not overflow.  | 
569  |  |  | 
570  |  |       TIFFroundup and TIFFSafeMultiply return zero on integer overflow  | 
571  |  |     */  | 
572  | 0  |     dsp->runs = (uint32_t *)NULL;  | 
573  | 0  |     dsp->nruns = TIFFroundup_32(rowpixels + 1, 32);  | 
574  | 0  |     if (needsRefLine)  | 
575  | 0  |     { | 
576  | 0  |         dsp->nruns = TIFFSafeMultiply(uint32_t, dsp->nruns, 2);  | 
577  | 0  |     }  | 
578  | 0  |     if ((dsp->nruns == 0) || (TIFFSafeMultiply(uint32_t, dsp->nruns, 2) == 0))  | 
579  | 0  |     { | 
580  | 0  |         TIFFErrorExtR(tif, tif->tif_name,  | 
581  | 0  |                       "Row pixels integer overflow (rowpixels %" PRIu32 ")",  | 
582  | 0  |                       rowpixels);  | 
583  | 0  |         return (0);  | 
584  | 0  |     }  | 
585  | 0  |     dsp->runs = (uint32_t *)_TIFFCheckMalloc(  | 
586  | 0  |         tif, TIFFSafeMultiply(uint32_t, dsp->nruns, 2), sizeof(uint32_t),  | 
587  | 0  |         "for Group 3/4 run arrays");  | 
588  | 0  |     if (dsp->runs == NULL)  | 
589  | 0  |         return (0);  | 
590  | 0  |     memset(dsp->runs, 0,  | 
591  | 0  |            TIFFSafeMultiply(uint32_t, dsp->nruns, 2) * sizeof(uint32_t));  | 
592  | 0  |     dsp->curruns = dsp->runs;  | 
593  | 0  |     if (needsRefLine)  | 
594  | 0  |         dsp->refruns = dsp->runs + dsp->nruns;  | 
595  | 0  |     else  | 
596  | 0  |         dsp->refruns = NULL;  | 
597  | 0  |     if (td->td_compression == COMPRESSION_CCITTFAX3 && is2DEncoding(dsp))  | 
598  | 0  |     { /* NB: default is 1D routine */ | 
599  | 0  |         tif->tif_decoderow = Fax3Decode2D;  | 
600  | 0  |         tif->tif_decodestrip = Fax3Decode2D;  | 
601  | 0  |         tif->tif_decodetile = Fax3Decode2D;  | 
602  | 0  |     }  | 
603  |  | 
  | 
604  | 0  |     if (needsRefLine)  | 
605  | 0  |     { /* 2d encoding */ | 
606  | 0  |         Fax3CodecState *esp = EncoderState(tif);  | 
607  |  |         /*  | 
608  |  |          * 2d encoding requires a scanline  | 
609  |  |          * buffer for the ``reference line''; the  | 
610  |  |          * scanline against which delta encoding  | 
611  |  |          * is referenced.  The reference line must  | 
612  |  |          * be initialized to be ``white'' (done elsewhere).  | 
613  |  |          */  | 
614  | 0  |         esp->refline = (unsigned char *)_TIFFmallocExt(tif, rowbytes);  | 
615  | 0  |         if (esp->refline == NULL)  | 
616  | 0  |         { | 
617  | 0  |             TIFFErrorExtR(tif, module, "No space for Group 3/4 reference line");  | 
618  | 0  |             return (0);  | 
619  | 0  |         }  | 
620  | 0  |     }  | 
621  | 0  |     else /* 1d encoding */  | 
622  | 0  |         EncoderState(tif)->refline = NULL;  | 
623  |  |  | 
624  | 0  |     return (1);  | 
625  | 0  | }  | 
626  |  |  | 
627  |  | /*  | 
628  |  |  * CCITT Group 3 FAX Encoding.  | 
629  |  |  */  | 
630  |  |  | 
631  |  | #define Fax3FlushBits(tif, sp)                                                 \  | 
632  | 0  |     {                                                                          \ | 
633  | 0  |         if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)                        \  | 
634  | 0  |         {                                                                      \ | 
635  | 0  |             if (!TIFFFlushData1(tif))                                          \  | 
636  | 0  |                 return 0;                                                      \  | 
637  | 0  |         }                                                                      \  | 
638  | 0  |         *(tif)->tif_rawcp++ = (uint8_t)(sp)->data;                             \  | 
639  | 0  |         (tif)->tif_rawcc++;                                                    \  | 
640  | 0  |         (sp)->data = 0, (sp)->bit = 8;                                         \  | 
641  | 0  |     }  | 
642  |  | #define _FlushBits(tif)                                                        \  | 
643  | 0  |     {                                                                          \ | 
644  | 0  |         if ((tif)->tif_rawcc >= (tif)->tif_rawdatasize)                        \  | 
645  | 0  |         {                                                                      \ | 
646  | 0  |             if (!TIFFFlushData1(tif))                                          \  | 
647  | 0  |                 return 0;                                                      \  | 
648  | 0  |         }                                                                      \  | 
649  | 0  |         *(tif)->tif_rawcp++ = (uint8_t)data;                                   \  | 
650  | 0  |         (tif)->tif_rawcc++;                                                    \  | 
651  | 0  |         data = 0, bit = 8;                                                     \  | 
652  | 0  |     }  | 
653  |  | static const int _msbmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, | 
654  |  |                                 0x1f, 0x3f, 0x7f, 0xff};  | 
655  |  | #define _PutBits(tif, bits, length)                                            \  | 
656  | 0  |     {                                                                          \ | 
657  | 0  |         while (length > bit)                                                   \  | 
658  | 0  |         {                                                                      \ | 
659  | 0  |             data |= bits >> (length - bit);                                    \  | 
660  | 0  |             length -= bit;                                                     \  | 
661  | 0  |             _FlushBits(tif);                                                   \  | 
662  | 0  |         }                                                                      \  | 
663  | 0  |         assert(length < 9);                                                    \  | 
664  | 0  |         data |= (bits & _msbmask[length]) << (bit - length);                   \  | 
665  | 0  |         bit -= length;                                                         \  | 
666  | 0  |         if (bit == 0)                                                          \  | 
667  | 0  |             _FlushBits(tif);                                                   \  | 
668  | 0  |     }  | 
669  |  |  | 
670  |  | /*  | 
671  |  |  * Write a variable-length bit-value to  | 
672  |  |  * the output stream.  Values are  | 
673  |  |  * assumed to be at most 16 bits.  | 
674  |  |  */  | 
675  |  | static int Fax3PutBits(TIFF *tif, unsigned int bits, unsigned int length)  | 
676  | 0  | { | 
677  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
678  | 0  |     unsigned int bit = sp->bit;  | 
679  | 0  |     int data = sp->data;  | 
680  |  | 
  | 
681  | 0  |     _PutBits(tif, bits, length);  | 
682  |  | 
  | 
683  | 0  |     sp->data = data;  | 
684  | 0  |     sp->bit = bit;  | 
685  | 0  |     return 1;  | 
686  | 0  | }  | 
687  |  |  | 
688  |  | /*  | 
689  |  |  * Write a code to the output stream.  | 
690  |  |  */  | 
691  | 0  | #define putcode(tif, te) Fax3PutBits(tif, (te)->code, (te)->length)  | 
692  |  |  | 
693  |  | #ifdef FAX3_DEBUG  | 
694  |  | #define DEBUG_COLOR(w) (tab == TIFFFaxWhiteCodes ? w "W" : w "B")  | 
695  |  | #define DEBUG_PRINT(what, len)                                                 \  | 
696  |  |     {                                                                          \ | 
697  |  |         int t;                                                                 \  | 
698  |  |         printf("%08" PRIX32 "/%-2d: %s%5d\t", data, bit, DEBUG_COLOR(what),    \ | 
699  |  |                len);                                                           \  | 
700  |  |         for (t = length - 1; t >= 0; t--)                                      \  | 
701  |  |             putchar(code & (1 << t) ? '1' : '0');                              \  | 
702  |  |         putchar('\n');                                                         \ | 
703  |  |     }  | 
704  |  | #endif  | 
705  |  |  | 
706  |  | /*  | 
707  |  |  * Write the sequence of codes that describes  | 
708  |  |  * the specified span of zero's or one's.  The  | 
709  |  |  * appropriate table that holds the make-up and  | 
710  |  |  * terminating codes is supplied.  | 
711  |  |  */  | 
712  |  | static int putspan(TIFF *tif, int32_t span, const tableentry *tab)  | 
713  | 0  | { | 
714  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
715  | 0  |     unsigned int bit = sp->bit;  | 
716  | 0  |     int data = sp->data;  | 
717  | 0  |     unsigned int code, length;  | 
718  |  | 
  | 
719  | 0  |     while (span >= 2624)  | 
720  | 0  |     { | 
721  | 0  |         const tableentry *te = &tab[63 + (2560 >> 6)];  | 
722  | 0  |         code = te->code;  | 
723  | 0  |         length = te->length;  | 
724  |  | #ifdef FAX3_DEBUG  | 
725  |  |         DEBUG_PRINT("MakeUp", te->runlen); | 
726  |  | #endif  | 
727  | 0  |         _PutBits(tif, code, length);  | 
728  | 0  |         span -= te->runlen;  | 
729  | 0  |     }  | 
730  | 0  |     if (span >= 64)  | 
731  | 0  |     { | 
732  | 0  |         const tableentry *te = &tab[63 + (span >> 6)];  | 
733  | 0  |         assert(te->runlen == 64 * (span >> 6));  | 
734  | 0  |         code = te->code;  | 
735  | 0  |         length = te->length;  | 
736  |  | #ifdef FAX3_DEBUG  | 
737  |  |         DEBUG_PRINT("MakeUp", te->runlen); | 
738  |  | #endif  | 
739  | 0  |         _PutBits(tif, code, length);  | 
740  | 0  |         span -= te->runlen;  | 
741  | 0  |     }  | 
742  | 0  |     code = tab[span].code;  | 
743  | 0  |     length = tab[span].length;  | 
744  |  | #ifdef FAX3_DEBUG  | 
745  |  |     DEBUG_PRINT("  Term", tab[span].runlen); | 
746  |  | #endif  | 
747  | 0  |     _PutBits(tif, code, length);  | 
748  |  | 
  | 
749  | 0  |     sp->data = data;  | 
750  | 0  |     sp->bit = bit;  | 
751  |  | 
  | 
752  | 0  |     return 1;  | 
753  | 0  | }  | 
754  |  |  | 
755  |  | /*  | 
756  |  |  * Write an EOL code to the output stream.  The zero-fill  | 
757  |  |  * logic for byte-aligning encoded scanlines is handled  | 
758  |  |  * here.  We also handle writing the tag bit for the next  | 
759  |  |  * scanline when doing 2d encoding.  | 
760  |  |  */  | 
761  |  | static int Fax3PutEOL(TIFF *tif)  | 
762  | 0  | { | 
763  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
764  | 0  |     unsigned int bit = sp->bit;  | 
765  | 0  |     int data = sp->data;  | 
766  | 0  |     unsigned int code, length, tparm;  | 
767  |  | 
  | 
768  | 0  |     if (sp->b.groupoptions & GROUP3OPT_FILLBITS)  | 
769  | 0  |     { | 
770  |  |         /*  | 
771  |  |          * Force bit alignment so EOL will terminate on  | 
772  |  |          * a byte boundary.  That is, force the bit alignment  | 
773  |  |          * to 16-12 = 4 before putting out the EOL code.  | 
774  |  |          */  | 
775  | 0  |         int align = 8 - 4;  | 
776  | 0  |         if (align != sp->bit)  | 
777  | 0  |         { | 
778  | 0  |             if (align > sp->bit)  | 
779  | 0  |                 align = sp->bit + (8 - align);  | 
780  | 0  |             else  | 
781  | 0  |                 align = sp->bit - align;  | 
782  | 0  |             tparm = align;  | 
783  | 0  |             _PutBits(tif, 0, tparm);  | 
784  | 0  |         }  | 
785  | 0  |     }  | 
786  | 0  |     code = EOL;  | 
787  | 0  |     length = 12;  | 
788  | 0  |     if (is2DEncoding(sp))  | 
789  | 0  |     { | 
790  | 0  |         code = (code << 1) | (sp->tag == G3_1D);  | 
791  | 0  |         length++;  | 
792  | 0  |     }  | 
793  | 0  |     _PutBits(tif, code, length);  | 
794  |  | 
  | 
795  | 0  |     sp->data = data;  | 
796  | 0  |     sp->bit = bit;  | 
797  |  | 
  | 
798  | 0  |     return 1;  | 
799  | 0  | }  | 
800  |  |  | 
801  |  | /*  | 
802  |  |  * Reset encoding state at the start of a strip.  | 
803  |  |  */  | 
804  |  | static int Fax3PreEncode(TIFF *tif, uint16_t s)  | 
805  | 0  | { | 
806  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
807  |  | 
  | 
808  | 0  |     (void)s;  | 
809  | 0  |     assert(sp != NULL);  | 
810  | 0  |     sp->bit = 8;  | 
811  | 0  |     sp->data = 0;  | 
812  | 0  |     sp->tag = G3_1D;  | 
813  |  |     /*  | 
814  |  |      * This is necessary for Group 4; otherwise it isn't  | 
815  |  |      * needed because the first scanline of each strip ends  | 
816  |  |      * up being copied into the refline.  | 
817  |  |      */  | 
818  | 0  |     if (sp->refline)  | 
819  | 0  |         _TIFFmemset(sp->refline, 0x00, sp->b.rowbytes);  | 
820  | 0  |     if (is2DEncoding(sp))  | 
821  | 0  |     { | 
822  | 0  |         float res = tif->tif_dir.td_yresolution;  | 
823  |  |         /*  | 
824  |  |          * The CCITT spec says that when doing 2d encoding, you  | 
825  |  |          * should only do it on K consecutive scanlines, where K  | 
826  |  |          * depends on the resolution of the image being encoded  | 
827  |  |          * (2 for <= 200 lpi, 4 for > 200 lpi).  Since the directory  | 
828  |  |          * code initializes td_yresolution to 0, this code will  | 
829  |  |          * select a K of 2 unless the YResolution tag is set  | 
830  |  |          * appropriately.  (Note also that we fudge a little here  | 
831  |  |          * and use 150 lpi to avoid problems with units conversion.)  | 
832  |  |          */  | 
833  | 0  |         if (tif->tif_dir.td_resolutionunit == RESUNIT_CENTIMETER)  | 
834  | 0  |             res *= 2.54f; /* convert to inches */  | 
835  | 0  |         sp->maxk = (res > 150 ? 4 : 2);  | 
836  | 0  |         sp->k = sp->maxk - 1;  | 
837  | 0  |     }  | 
838  | 0  |     else  | 
839  | 0  |         sp->k = sp->maxk = 0;  | 
840  | 0  |     sp->line = 0;  | 
841  | 0  |     return (1);  | 
842  | 0  | }  | 
843  |  |  | 
844  |  | static const unsigned char zeroruns[256] = { | 
845  |  |     8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0f */  | 
846  |  |     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1f */  | 
847  |  |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2f */  | 
848  |  |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3f */  | 
849  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */  | 
850  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5f */  | 
851  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */  | 
852  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7f */  | 
853  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8f */  | 
854  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9f */  | 
855  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xa0 - 0xaf */  | 
856  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xb0 - 0xbf */  | 
857  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xc0 - 0xcf */  | 
858  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xd0 - 0xdf */  | 
859  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xe0 - 0xef */  | 
860  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xf0 - 0xff */  | 
861  |  | };  | 
862  |  | static const unsigned char oneruns[256] = { | 
863  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 - 0x0f */  | 
864  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */  | 
865  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 - 0x2f */  | 
866  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */  | 
867  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */  | 
868  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */  | 
869  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6f */  | 
870  |  |     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7f */  | 
871  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x80 - 0x8f */  | 
872  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x90 - 0x9f */  | 
873  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xa0 - 0xaf */  | 
874  |  |     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0xb0 - 0xbf */  | 
875  |  |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xc0 - 0xcf */  | 
876  |  |     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0xd0 - 0xdf */  | 
877  |  |     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0xe0 - 0xef */  | 
878  |  |     4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8, /* 0xf0 - 0xff */  | 
879  |  | };  | 
880  |  |  | 
881  |  | /*  | 
882  |  |  * Find a span of ones or zeros using the supplied  | 
883  |  |  * table.  The ``base'' of the bit string is supplied  | 
884  |  |  * along with the start+end bit indices.  | 
885  |  |  */  | 
886  |  | static inline int32_t find0span(unsigned char *bp, int32_t bs, int32_t be)  | 
887  | 0  | { | 
888  | 0  |     int32_t bits = be - bs;  | 
889  | 0  |     int32_t n, span;  | 
890  |  | 
  | 
891  | 0  |     bp += bs >> 3;  | 
892  |  |     /*  | 
893  |  |      * Check partial byte on lhs.  | 
894  |  |      */  | 
895  | 0  |     if (bits > 0 && (n = (bs & 7)) != 0)  | 
896  | 0  |     { | 
897  | 0  |         span = zeroruns[(*bp << n) & 0xff];  | 
898  | 0  |         if (span > 8 - n) /* table value too generous */  | 
899  | 0  |             span = 8 - n;  | 
900  | 0  |         if (span > bits) /* constrain span to bit range */  | 
901  | 0  |             span = bits;  | 
902  | 0  |         if (n + span < 8) /* doesn't extend to edge of byte */  | 
903  | 0  |             return (span);  | 
904  | 0  |         bits -= span;  | 
905  | 0  |         bp++;  | 
906  | 0  |     }  | 
907  | 0  |     else  | 
908  | 0  |         span = 0;  | 
909  | 0  |     if (bits >= (int32_t)(2 * 8 * sizeof(int64_t)))  | 
910  | 0  |     { | 
911  | 0  |         int64_t *lp;  | 
912  |  |         /*  | 
913  |  |          * Align to int64_t boundary and check int64_t words.  | 
914  |  |          */  | 
915  | 0  |         while (!isAligned(bp, int64_t))  | 
916  | 0  |         { | 
917  | 0  |             if (*bp != 0x00)  | 
918  | 0  |                 return (span + zeroruns[*bp]);  | 
919  | 0  |             span += 8;  | 
920  | 0  |             bits -= 8;  | 
921  | 0  |             bp++;  | 
922  | 0  |         }  | 
923  | 0  |         lp = (int64_t *)bp;  | 
924  | 0  |         while ((bits >= (int32_t)(8 * sizeof(int64_t))) && (0 == *lp))  | 
925  | 0  |         { | 
926  | 0  |             span += 8 * sizeof(int64_t);  | 
927  | 0  |             bits -= 8 * sizeof(int64_t);  | 
928  | 0  |             lp++;  | 
929  | 0  |         }  | 
930  | 0  |         bp = (unsigned char *)lp;  | 
931  | 0  |     }  | 
932  |  |     /*  | 
933  |  |      * Scan full bytes for all 0's.  | 
934  |  |      */  | 
935  | 0  |     while (bits >= 8)  | 
936  | 0  |     { | 
937  | 0  |         if (*bp != 0x00) /* end of run */  | 
938  | 0  |             return (span + zeroruns[*bp]);  | 
939  | 0  |         span += 8;  | 
940  | 0  |         bits -= 8;  | 
941  | 0  |         bp++;  | 
942  | 0  |     }  | 
943  |  |     /*  | 
944  |  |      * Check partial byte on rhs.  | 
945  |  |      */  | 
946  | 0  |     if (bits > 0)  | 
947  | 0  |     { | 
948  | 0  |         n = zeroruns[*bp];  | 
949  | 0  |         span += (n > bits ? bits : n);  | 
950  | 0  |     }  | 
951  | 0  |     return (span);  | 
952  | 0  | }  | 
953  |  |  | 
954  |  | static inline int32_t find1span(unsigned char *bp, int32_t bs, int32_t be)  | 
955  | 0  | { | 
956  | 0  |     int32_t bits = be - bs;  | 
957  | 0  |     int32_t n, span;  | 
958  |  | 
  | 
959  | 0  |     bp += bs >> 3;  | 
960  |  |     /*  | 
961  |  |      * Check partial byte on lhs.  | 
962  |  |      */  | 
963  | 0  |     if (bits > 0 && (n = (bs & 7)) != 0)  | 
964  | 0  |     { | 
965  | 0  |         span = oneruns[(*bp << n) & 0xff];  | 
966  | 0  |         if (span > 8 - n) /* table value too generous */  | 
967  | 0  |             span = 8 - n;  | 
968  | 0  |         if (span > bits) /* constrain span to bit range */  | 
969  | 0  |             span = bits;  | 
970  | 0  |         if (n + span < 8) /* doesn't extend to edge of byte */  | 
971  | 0  |             return (span);  | 
972  | 0  |         bits -= span;  | 
973  | 0  |         bp++;  | 
974  | 0  |     }  | 
975  | 0  |     else  | 
976  | 0  |         span = 0;  | 
977  | 0  |     if (bits >= (int32_t)(2 * 8 * sizeof(int64_t)))  | 
978  | 0  |     { | 
979  | 0  |         int64_t *lp;  | 
980  |  |         /*  | 
981  |  |          * Align to int64_t boundary and check int64_t words.  | 
982  |  |          */  | 
983  | 0  |         while (!isAligned(bp, int64_t))  | 
984  | 0  |         { | 
985  | 0  |             if (*bp != 0xff)  | 
986  | 0  |                 return (span + oneruns[*bp]);  | 
987  | 0  |             span += 8;  | 
988  | 0  |             bits -= 8;  | 
989  | 0  |             bp++;  | 
990  | 0  |         }  | 
991  | 0  |         lp = (int64_t *)bp;  | 
992  | 0  |         while ((bits >= (int32_t)(8 * sizeof(int64_t))) &&  | 
993  | 0  |                (~((uint64_t)0) == (uint64_t)*lp))  | 
994  | 0  |         { | 
995  | 0  |             span += 8 * sizeof(int64_t);  | 
996  | 0  |             bits -= 8 * sizeof(int64_t);  | 
997  | 0  |             lp++;  | 
998  | 0  |         }  | 
999  | 0  |         bp = (unsigned char *)lp;  | 
1000  | 0  |     }  | 
1001  |  |     /*  | 
1002  |  |      * Scan full bytes for all 1's.  | 
1003  |  |      */  | 
1004  | 0  |     while (bits >= 8)  | 
1005  | 0  |     { | 
1006  | 0  |         if (*bp != 0xff) /* end of run */  | 
1007  | 0  |             return (span + oneruns[*bp]);  | 
1008  | 0  |         span += 8;  | 
1009  | 0  |         bits -= 8;  | 
1010  | 0  |         bp++;  | 
1011  | 0  |     }  | 
1012  |  |     /*  | 
1013  |  |      * Check partial byte on rhs.  | 
1014  |  |      */  | 
1015  | 0  |     if (bits > 0)  | 
1016  | 0  |     { | 
1017  | 0  |         n = oneruns[*bp];  | 
1018  | 0  |         span += (n > bits ? bits : n);  | 
1019  | 0  |     }  | 
1020  | 0  |     return (span);  | 
1021  | 0  | }  | 
1022  |  |  | 
1023  |  | /*  | 
1024  |  |  * Return the offset of the next bit in the range  | 
1025  |  |  * [bs..be] that is different from the specified  | 
1026  |  |  * color.  The end, be, is returned if no such bit  | 
1027  |  |  * exists.  | 
1028  |  |  */  | 
1029  |  | #define finddiff(_cp, _bs, _be, _color)                                        \  | 
1030  | 0  |     (_bs + (_color ? find1span(_cp, _bs, _be) : find0span(_cp, _bs, _be)))  | 
1031  |  | /*  | 
1032  |  |  * Like finddiff, but also check the starting bit  | 
1033  |  |  * against the end in case start > end.  | 
1034  |  |  */  | 
1035  |  | #define finddiff2(_cp, _bs, _be, _color)                                       \  | 
1036  | 0  |     (_bs < _be ? finddiff(_cp, _bs, _be, _color) : _be)  | 
1037  |  |  | 
1038  |  | /*  | 
1039  |  |  * 1d-encode a row of pixels.  The encoding is  | 
1040  |  |  * a sequence of all-white or all-black spans  | 
1041  |  |  * of pixels encoded with Huffman codes.  | 
1042  |  |  */  | 
1043  |  | static int Fax3Encode1DRow(TIFF *tif, unsigned char *bp, uint32_t bits)  | 
1044  | 0  | { | 
1045  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
1046  | 0  |     int32_t span;  | 
1047  | 0  |     uint32_t bs = 0;  | 
1048  |  | 
  | 
1049  | 0  |     for (;;)  | 
1050  | 0  |     { | 
1051  | 0  |         span = find0span(bp, bs, bits); /* white span */  | 
1052  | 0  |         if (!putspan(tif, span, TIFFFaxWhiteCodes))  | 
1053  | 0  |             return 0;  | 
1054  | 0  |         bs += span;  | 
1055  | 0  |         if (bs >= bits)  | 
1056  | 0  |             break;  | 
1057  | 0  |         span = find1span(bp, bs, bits); /* black span */  | 
1058  | 0  |         if (!putspan(tif, span, TIFFFaxBlackCodes))  | 
1059  | 0  |             return 0;  | 
1060  | 0  |         bs += span;  | 
1061  | 0  |         if (bs >= bits)  | 
1062  | 0  |             break;  | 
1063  | 0  |     }  | 
1064  | 0  |     if (sp->b.mode & (FAXMODE_BYTEALIGN | FAXMODE_WORDALIGN))  | 
1065  | 0  |     { | 
1066  | 0  |         if (sp->bit != 8) /* byte-align */  | 
1067  | 0  |             Fax3FlushBits(tif, sp);  | 
1068  | 0  |         if ((sp->b.mode & FAXMODE_WORDALIGN) &&  | 
1069  | 0  |             !isAligned(tif->tif_rawcp, uint16_t))  | 
1070  | 0  |             Fax3FlushBits(tif, sp);  | 
1071  | 0  |     }  | 
1072  | 0  |     return (1);  | 
1073  | 0  | }  | 
1074  |  |  | 
1075  |  | static const tableentry horizcode = {3, 0x1, 0}; /* 001 */ | 
1076  |  | static const tableentry passcode = {4, 0x1, 0};  /* 0001 */ | 
1077  |  | static const tableentry vcodes[7] = { | 
1078  |  |     {7, 0x03, 0}, /* 0000 011 */ | 
1079  |  |     {6, 0x03, 0}, /* 0000 11 */ | 
1080  |  |     {3, 0x03, 0}, /* 011 */ | 
1081  |  |     {1, 0x1, 0},  /* 1 */ | 
1082  |  |     {3, 0x2, 0},  /* 010 */ | 
1083  |  |     {6, 0x02, 0}, /* 0000 10 */ | 
1084  |  |     {7, 0x02, 0}  /* 0000 010 */ | 
1085  |  | };  | 
1086  |  |  | 
1087  |  | /*  | 
1088  |  |  * 2d-encode a row of pixels.  Consult the CCITT  | 
1089  |  |  * documentation for the algorithm.  | 
1090  |  |  */  | 
1091  |  | static int Fax3Encode2DRow(TIFF *tif, unsigned char *bp, unsigned char *rp,  | 
1092  |  |                            uint32_t bits)  | 
1093  | 0  | { | 
1094  | 0  | #define PIXEL(buf, ix) ((((buf)[(ix) >> 3]) >> (7 - ((ix)&7))) & 1)  | 
1095  | 0  |     uint32_t a0 = 0;  | 
1096  | 0  |     uint32_t a1 = (PIXEL(bp, 0) != 0 ? 0 : finddiff(bp, 0, bits, 0));  | 
1097  | 0  |     uint32_t b1 = (PIXEL(rp, 0) != 0 ? 0 : finddiff(rp, 0, bits, 0));  | 
1098  | 0  |     uint32_t a2, b2;  | 
1099  |  | 
  | 
1100  | 0  |     for (;;)  | 
1101  | 0  |     { | 
1102  | 0  |         b2 = finddiff2(rp, b1, bits, PIXEL(rp, b1));  | 
1103  | 0  |         if (b2 >= a1)  | 
1104  | 0  |         { | 
1105  |  |             /* Naive computation triggers  | 
1106  |  |              * -fsanitize=undefined,unsigned-integer-overflow */  | 
1107  |  |             /* although it is correct unless the difference between both is < 31  | 
1108  |  |              * bit */  | 
1109  |  |             /* int32_t d = b1 - a1; */  | 
1110  | 0  |             int32_t d = (b1 >= a1 && b1 - a1 <= 3U)  ? (int32_t)(b1 - a1)  | 
1111  | 0  |                         : (b1 < a1 && a1 - b1 <= 3U) ? -(int32_t)(a1 - b1)  | 
1112  | 0  |                                                      : 0x7FFFFFFF;  | 
1113  | 0  |             if (!(-3 <= d && d <= 3))  | 
1114  | 0  |             { /* horizontal mode */ | 
1115  | 0  |                 a2 = finddiff2(bp, a1, bits, PIXEL(bp, a1));  | 
1116  | 0  |                 if (!putcode(tif, &horizcode))  | 
1117  | 0  |                     return 0;  | 
1118  | 0  |                 if (a0 + a1 == 0 || PIXEL(bp, a0) == 0)  | 
1119  | 0  |                 { | 
1120  | 0  |                     if (!putspan(tif, a1 - a0, TIFFFaxWhiteCodes))  | 
1121  | 0  |                         return 0;  | 
1122  | 0  |                     if (!putspan(tif, a2 - a1, TIFFFaxBlackCodes))  | 
1123  | 0  |                         return 0;  | 
1124  | 0  |                 }  | 
1125  | 0  |                 else  | 
1126  | 0  |                 { | 
1127  | 0  |                     if (!putspan(tif, a1 - a0, TIFFFaxBlackCodes))  | 
1128  | 0  |                         return 0;  | 
1129  | 0  |                     if (!putspan(tif, a2 - a1, TIFFFaxWhiteCodes))  | 
1130  | 0  |                         return 0;  | 
1131  | 0  |                 }  | 
1132  | 0  |                 a0 = a2;  | 
1133  | 0  |             }  | 
1134  | 0  |             else  | 
1135  | 0  |             { /* vertical mode */ | 
1136  | 0  |                 if (!putcode(tif, &vcodes[d + 3]))  | 
1137  | 0  |                     return 0;  | 
1138  | 0  |                 a0 = a1;  | 
1139  | 0  |             }  | 
1140  | 0  |         }  | 
1141  | 0  |         else  | 
1142  | 0  |         { /* pass mode */ | 
1143  | 0  |             if (!putcode(tif, &passcode))  | 
1144  | 0  |                 return 0;  | 
1145  | 0  |             a0 = b2;  | 
1146  | 0  |         }  | 
1147  | 0  |         if (a0 >= bits)  | 
1148  | 0  |             break;  | 
1149  | 0  |         a1 = finddiff(bp, a0, bits, PIXEL(bp, a0));  | 
1150  | 0  |         b1 = finddiff(rp, a0, bits, !PIXEL(bp, a0));  | 
1151  | 0  |         b1 = finddiff(rp, b1, bits, PIXEL(bp, a0));  | 
1152  | 0  |     }  | 
1153  | 0  |     return (1);  | 
1154  | 0  | #undef PIXEL  | 
1155  | 0  | }  | 
1156  |  |  | 
1157  |  | /*  | 
1158  |  |  * Encode a buffer of pixels.  | 
1159  |  |  */  | 
1160  |  | static int Fax3Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)  | 
1161  | 0  | { | 
1162  | 0  |     static const char module[] = "Fax3Encode";  | 
1163  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
1164  | 0  |     (void)s;  | 
1165  | 0  |     if (cc % sp->b.rowbytes)  | 
1166  | 0  |     { | 
1167  | 0  |         TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written");  | 
1168  | 0  |         return (0);  | 
1169  | 0  |     }  | 
1170  | 0  |     while (cc > 0)  | 
1171  | 0  |     { | 
1172  | 0  |         if ((sp->b.mode & FAXMODE_NOEOL) == 0)  | 
1173  | 0  |         { | 
1174  | 0  |             if (!Fax3PutEOL(tif))  | 
1175  | 0  |                 return 0;  | 
1176  | 0  |         }  | 
1177  | 0  |         if (is2DEncoding(sp))  | 
1178  | 0  |         { | 
1179  | 0  |             if (sp->tag == G3_1D)  | 
1180  | 0  |             { | 
1181  | 0  |                 if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))  | 
1182  | 0  |                     return (0);  | 
1183  | 0  |                 sp->tag = G3_2D;  | 
1184  | 0  |             }  | 
1185  | 0  |             else  | 
1186  | 0  |             { | 
1187  | 0  |                 if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))  | 
1188  | 0  |                     return (0);  | 
1189  | 0  |                 sp->k--;  | 
1190  | 0  |             }  | 
1191  | 0  |             if (sp->k == 0)  | 
1192  | 0  |             { | 
1193  | 0  |                 sp->tag = G3_1D;  | 
1194  | 0  |                 sp->k = sp->maxk - 1;  | 
1195  | 0  |             }  | 
1196  | 0  |             else  | 
1197  | 0  |                 _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);  | 
1198  | 0  |         }  | 
1199  | 0  |         else  | 
1200  | 0  |         { | 
1201  | 0  |             if (!Fax3Encode1DRow(tif, bp, sp->b.rowpixels))  | 
1202  | 0  |                 return (0);  | 
1203  | 0  |         }  | 
1204  | 0  |         bp += sp->b.rowbytes;  | 
1205  | 0  |         cc -= sp->b.rowbytes;  | 
1206  | 0  |     }  | 
1207  | 0  |     return (1);  | 
1208  | 0  | }  | 
1209  |  |  | 
1210  |  | static int Fax3PostEncode(TIFF *tif)  | 
1211  | 0  | { | 
1212  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
1213  |  | 
  | 
1214  | 0  |     if (sp->bit != 8)  | 
1215  | 0  |         Fax3FlushBits(tif, sp);  | 
1216  | 0  |     return (1);  | 
1217  | 0  | }  | 
1218  |  |  | 
1219  |  | static int _Fax3Close(TIFF *tif)  | 
1220  | 0  | { | 
1221  | 0  |     if ((Fax3State(tif)->mode & FAXMODE_NORTC) == 0 && tif->tif_rawcp)  | 
1222  | 0  |     { | 
1223  | 0  |         Fax3CodecState *sp = EncoderState(tif);  | 
1224  | 0  |         unsigned int code = EOL;  | 
1225  | 0  |         unsigned int length = 12;  | 
1226  | 0  |         int i;  | 
1227  |  | 
  | 
1228  | 0  |         if (is2DEncoding(sp))  | 
1229  | 0  |         { | 
1230  | 0  |             code = (code << 1) | (sp->tag == G3_1D);  | 
1231  | 0  |             length++;  | 
1232  | 0  |         }  | 
1233  | 0  |         for (i = 0; i < 6; i++)  | 
1234  | 0  |             Fax3PutBits(tif, code, length);  | 
1235  | 0  |         Fax3FlushBits(tif, sp);  | 
1236  | 0  |     }  | 
1237  | 0  |     return 1;  | 
1238  | 0  | }  | 
1239  |  |  | 
1240  | 0  | static void Fax3Close(TIFF *tif) { _Fax3Close(tif); } | 
1241  |  |  | 
1242  |  | static void Fax3Cleanup(TIFF *tif)  | 
1243  | 0  | { | 
1244  | 0  |     Fax3CodecState *sp = DecoderState(tif);  | 
1245  |  | 
  | 
1246  | 0  |     assert(sp != 0);  | 
1247  |  | 
  | 
1248  | 0  |     tif->tif_tagmethods.vgetfield = sp->b.vgetparent;  | 
1249  | 0  |     tif->tif_tagmethods.vsetfield = sp->b.vsetparent;  | 
1250  | 0  |     tif->tif_tagmethods.printdir = sp->b.printdir;  | 
1251  |  | 
  | 
1252  | 0  |     if (sp->runs)  | 
1253  | 0  |         _TIFFfreeExt(tif, sp->runs);  | 
1254  | 0  |     if (sp->refline)  | 
1255  | 0  |         _TIFFfreeExt(tif, sp->refline);  | 
1256  |  | 
  | 
1257  | 0  |     _TIFFfreeExt(tif, tif->tif_data);  | 
1258  | 0  |     tif->tif_data = NULL;  | 
1259  |  | 
  | 
1260  | 0  |     _TIFFSetDefaultCompressionState(tif);  | 
1261  | 0  | }  | 
1262  |  |  | 
1263  |  | #define FIELD_BADFAXLINES (FIELD_CODEC + 0)  | 
1264  |  | #define FIELD_CLEANFAXDATA (FIELD_CODEC + 1)  | 
1265  |  | #define FIELD_BADFAXRUN (FIELD_CODEC + 2)  | 
1266  |  |  | 
1267  |  | #define FIELD_OPTIONS (FIELD_CODEC + 7)  | 
1268  |  |  | 
1269  |  | static const TIFFField faxFields[] = { | 
1270  |  |     {TIFFTAG_FAXMODE, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, | 
1271  |  |      FIELD_PSEUDO, FALSE, FALSE, "FaxMode", NULL},  | 
1272  |  |     {TIFFTAG_FAXFILLFUNC, 0, 0, TIFF_ANY, 0, TIFF_SETGET_OTHER, | 
1273  |  |      TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "FaxFillFunc", NULL},  | 
1274  |  |     {TIFFTAG_BADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, | 
1275  |  |      TIFF_SETGET_UINT32, FIELD_BADFAXLINES, TRUE, FALSE, "BadFaxLines", NULL},  | 
1276  |  |     {TIFFTAG_CLEANFAXDATA, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, | 
1277  |  |      TIFF_SETGET_UINT16, FIELD_CLEANFAXDATA, TRUE, FALSE, "CleanFaxData", NULL},  | 
1278  |  |     {TIFFTAG_CONSECUTIVEBADFAXLINES, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, | 
1279  |  |      TIFF_SETGET_UINT32, FIELD_BADFAXRUN, TRUE, FALSE, "ConsecutiveBadFaxLines",  | 
1280  |  |      NULL}};  | 
1281  |  | static const TIFFField fax3Fields[] = { | 
1282  |  |     {TIFFTAG_GROUP3OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, | 
1283  |  |      TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group3Options", NULL},  | 
1284  |  | };  | 
1285  |  | static const TIFFField fax4Fields[] = { | 
1286  |  |     {TIFFTAG_GROUP4OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, | 
1287  |  |      TIFF_SETGET_UINT32, FIELD_OPTIONS, FALSE, FALSE, "Group4Options", NULL},  | 
1288  |  | };  | 
1289  |  |  | 
1290  |  | static int Fax3VSetField(TIFF *tif, uint32_t tag, va_list ap)  | 
1291  | 0  | { | 
1292  | 0  |     Fax3BaseState *sp = Fax3State(tif);  | 
1293  | 0  |     const TIFFField *fip;  | 
1294  |  | 
  | 
1295  | 0  |     assert(sp != 0);  | 
1296  | 0  |     assert(sp->vsetparent != 0);  | 
1297  |  | 
  | 
1298  | 0  |     switch (tag)  | 
1299  | 0  |     { | 
1300  | 0  |         case TIFFTAG_FAXMODE:  | 
1301  | 0  |             sp->mode = (int)va_arg(ap, int);  | 
1302  | 0  |             return 1; /* NB: pseudo tag */  | 
1303  | 0  |         case TIFFTAG_FAXFILLFUNC:  | 
1304  | 0  |             DecoderState(tif)->fill = va_arg(ap, TIFFFaxFillFunc);  | 
1305  | 0  |             return 1; /* NB: pseudo tag */  | 
1306  | 0  |         case TIFFTAG_GROUP3OPTIONS:  | 
1307  |  |             /* XXX: avoid reading options if compression mismatches. */  | 
1308  | 0  |             if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX3)  | 
1309  | 0  |                 sp->groupoptions = (uint32_t)va_arg(ap, uint32_t);  | 
1310  | 0  |             break;  | 
1311  | 0  |         case TIFFTAG_GROUP4OPTIONS:  | 
1312  |  |             /* XXX: avoid reading options if compression mismatches. */  | 
1313  | 0  |             if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)  | 
1314  | 0  |                 sp->groupoptions = (uint32_t)va_arg(ap, uint32_t);  | 
1315  | 0  |             break;  | 
1316  | 0  |         case TIFFTAG_BADFAXLINES:  | 
1317  | 0  |             sp->badfaxlines = (uint32_t)va_arg(ap, uint32_t);  | 
1318  | 0  |             break;  | 
1319  | 0  |         case TIFFTAG_CLEANFAXDATA:  | 
1320  | 0  |             sp->cleanfaxdata = (uint16_t)va_arg(ap, uint16_vap);  | 
1321  | 0  |             break;  | 
1322  | 0  |         case TIFFTAG_CONSECUTIVEBADFAXLINES:  | 
1323  | 0  |             sp->badfaxrun = (uint32_t)va_arg(ap, uint32_t);  | 
1324  | 0  |             break;  | 
1325  | 0  |         default:  | 
1326  | 0  |             return (*sp->vsetparent)(tif, tag, ap);  | 
1327  | 0  |     }  | 
1328  |  |  | 
1329  | 0  |     if ((fip = TIFFFieldWithTag(tif, tag)) != NULL)  | 
1330  | 0  |         TIFFSetFieldBit(tif, fip->field_bit);  | 
1331  | 0  |     else  | 
1332  | 0  |         return 0;  | 
1333  |  |  | 
1334  | 0  |     tif->tif_flags |= TIFF_DIRTYDIRECT;  | 
1335  | 0  |     return 1;  | 
1336  | 0  | }  | 
1337  |  |  | 
1338  |  | static int Fax3VGetField(TIFF *tif, uint32_t tag, va_list ap)  | 
1339  | 0  | { | 
1340  | 0  |     Fax3BaseState *sp = Fax3State(tif);  | 
1341  |  | 
  | 
1342  | 0  |     assert(sp != 0);  | 
1343  |  | 
  | 
1344  | 0  |     switch (tag)  | 
1345  | 0  |     { | 
1346  | 0  |         case TIFFTAG_FAXMODE:  | 
1347  | 0  |             *va_arg(ap, int *) = sp->mode;  | 
1348  | 0  |             break;  | 
1349  | 0  |         case TIFFTAG_FAXFILLFUNC:  | 
1350  | 0  |             *va_arg(ap, TIFFFaxFillFunc *) = DecoderState(tif)->fill;  | 
1351  | 0  |             break;  | 
1352  | 0  |         case TIFFTAG_GROUP3OPTIONS:  | 
1353  | 0  |         case TIFFTAG_GROUP4OPTIONS:  | 
1354  | 0  |             *va_arg(ap, uint32_t *) = sp->groupoptions;  | 
1355  | 0  |             break;  | 
1356  | 0  |         case TIFFTAG_BADFAXLINES:  | 
1357  | 0  |             *va_arg(ap, uint32_t *) = sp->badfaxlines;  | 
1358  | 0  |             break;  | 
1359  | 0  |         case TIFFTAG_CLEANFAXDATA:  | 
1360  | 0  |             *va_arg(ap, uint16_t *) = sp->cleanfaxdata;  | 
1361  | 0  |             break;  | 
1362  | 0  |         case TIFFTAG_CONSECUTIVEBADFAXLINES:  | 
1363  | 0  |             *va_arg(ap, uint32_t *) = sp->badfaxrun;  | 
1364  | 0  |             break;  | 
1365  | 0  |         default:  | 
1366  | 0  |             return (*sp->vgetparent)(tif, tag, ap);  | 
1367  | 0  |     }  | 
1368  | 0  |     return (1);  | 
1369  | 0  | }  | 
1370  |  |  | 
1371  |  | static void Fax3PrintDir(TIFF *tif, FILE *fd, long flags)  | 
1372  | 0  | { | 
1373  | 0  |     Fax3BaseState *sp = Fax3State(tif);  | 
1374  |  | 
  | 
1375  | 0  |     assert(sp != 0);  | 
1376  |  | 
  | 
1377  | 0  |     (void)flags;  | 
1378  | 0  |     if (TIFFFieldSet(tif, FIELD_OPTIONS))  | 
1379  | 0  |     { | 
1380  | 0  |         const char *sep = " ";  | 
1381  | 0  |         if (tif->tif_dir.td_compression == COMPRESSION_CCITTFAX4)  | 
1382  | 0  |         { | 
1383  | 0  |             fprintf(fd, "  Group 4 Options:");  | 
1384  | 0  |             if (sp->groupoptions & GROUP4OPT_UNCOMPRESSED)  | 
1385  | 0  |                 fprintf(fd, "%suncompressed data", sep);  | 
1386  | 0  |         }  | 
1387  | 0  |         else  | 
1388  | 0  |         { | 
1389  |  | 
  | 
1390  | 0  |             fprintf(fd, "  Group 3 Options:");  | 
1391  | 0  |             if (sp->groupoptions & GROUP3OPT_2DENCODING)  | 
1392  | 0  |             { | 
1393  | 0  |                 fprintf(fd, "%s2-d encoding", sep);  | 
1394  | 0  |                 sep = "+";  | 
1395  | 0  |             }  | 
1396  | 0  |             if (sp->groupoptions & GROUP3OPT_FILLBITS)  | 
1397  | 0  |             { | 
1398  | 0  |                 fprintf(fd, "%sEOL padding", sep);  | 
1399  | 0  |                 sep = "+";  | 
1400  | 0  |             }  | 
1401  | 0  |             if (sp->groupoptions & GROUP3OPT_UNCOMPRESSED)  | 
1402  | 0  |                 fprintf(fd, "%suncompressed data", sep);  | 
1403  | 0  |         }  | 
1404  | 0  |         fprintf(fd, " (%" PRIu32 " = 0x%" PRIx32 ")\n", sp->groupoptions,  | 
1405  | 0  |                 sp->groupoptions);  | 
1406  | 0  |     }  | 
1407  | 0  |     if (TIFFFieldSet(tif, FIELD_CLEANFAXDATA))  | 
1408  | 0  |     { | 
1409  | 0  |         fprintf(fd, "  Fax Data:");  | 
1410  | 0  |         switch (sp->cleanfaxdata)  | 
1411  | 0  |         { | 
1412  | 0  |             case CLEANFAXDATA_CLEAN:  | 
1413  | 0  |                 fprintf(fd, " clean");  | 
1414  | 0  |                 break;  | 
1415  | 0  |             case CLEANFAXDATA_REGENERATED:  | 
1416  | 0  |                 fprintf(fd, " receiver regenerated");  | 
1417  | 0  |                 break;  | 
1418  | 0  |             case CLEANFAXDATA_UNCLEAN:  | 
1419  | 0  |                 fprintf(fd, " uncorrected errors");  | 
1420  | 0  |                 break;  | 
1421  | 0  |         }  | 
1422  | 0  |         fprintf(fd, " (%" PRIu16 " = 0x%" PRIx16 ")\n", sp->cleanfaxdata,  | 
1423  | 0  |                 sp->cleanfaxdata);  | 
1424  | 0  |     }  | 
1425  | 0  |     if (TIFFFieldSet(tif, FIELD_BADFAXLINES))  | 
1426  | 0  |         fprintf(fd, "  Bad Fax Lines: %" PRIu32 "\n", sp->badfaxlines);  | 
1427  | 0  |     if (TIFFFieldSet(tif, FIELD_BADFAXRUN))  | 
1428  | 0  |         fprintf(fd, "  Consecutive Bad Fax Lines: %" PRIu32 "\n",  | 
1429  | 0  |                 sp->badfaxrun);  | 
1430  | 0  |     if (sp->printdir)  | 
1431  | 0  |         (*sp->printdir)(tif, fd, flags);  | 
1432  | 0  | }  | 
1433  |  |  | 
1434  |  | static int InitCCITTFax3(TIFF *tif)  | 
1435  | 0  | { | 
1436  | 0  |     static const char module[] = "InitCCITTFax3";  | 
1437  | 0  |     Fax3BaseState *sp;  | 
1438  |  |  | 
1439  |  |     /*  | 
1440  |  |      * Merge codec-specific tag information.  | 
1441  |  |      */  | 
1442  | 0  |     if (!_TIFFMergeFields(tif, faxFields, TIFFArrayCount(faxFields)))  | 
1443  | 0  |     { | 
1444  | 0  |         TIFFErrorExtR(tif, "InitCCITTFax3",  | 
1445  | 0  |                       "Merging common CCITT Fax codec-specific tags failed");  | 
1446  | 0  |         return 0;  | 
1447  | 0  |     }  | 
1448  |  |  | 
1449  |  |     /*  | 
1450  |  |      * Allocate state block so tag methods have storage to record values.  | 
1451  |  |      */  | 
1452  | 0  |     tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(Fax3CodecState));  | 
1453  |  | 
  | 
1454  | 0  |     if (tif->tif_data == NULL)  | 
1455  | 0  |     { | 
1456  | 0  |         TIFFErrorExtR(tif, module, "No space for state block");  | 
1457  | 0  |         return (0);  | 
1458  | 0  |     }  | 
1459  | 0  |     _TIFFmemset(tif->tif_data, 0, sizeof(Fax3CodecState));  | 
1460  |  | 
  | 
1461  | 0  |     sp = Fax3State(tif);  | 
1462  | 0  |     sp->rw_mode = tif->tif_mode;  | 
1463  |  |  | 
1464  |  |     /*  | 
1465  |  |      * Override parent get/set field methods.  | 
1466  |  |      */  | 
1467  | 0  |     sp->vgetparent = tif->tif_tagmethods.vgetfield;  | 
1468  | 0  |     tif->tif_tagmethods.vgetfield = Fax3VGetField; /* hook for codec tags */  | 
1469  | 0  |     sp->vsetparent = tif->tif_tagmethods.vsetfield;  | 
1470  | 0  |     tif->tif_tagmethods.vsetfield = Fax3VSetField; /* hook for codec tags */  | 
1471  | 0  |     sp->printdir = tif->tif_tagmethods.printdir;  | 
1472  | 0  |     tif->tif_tagmethods.printdir = Fax3PrintDir; /* hook for codec tags */  | 
1473  | 0  |     sp->groupoptions = 0;  | 
1474  |  | 
  | 
1475  | 0  |     if (sp->rw_mode == O_RDONLY) /* FIXME: improve for in place update */  | 
1476  | 0  |         tif->tif_flags |= TIFF_NOBITREV; /* decoder does bit reversal */  | 
1477  | 0  |     DecoderState(tif)->runs = NULL;  | 
1478  | 0  |     TIFFSetField(tif, TIFFTAG_FAXFILLFUNC, _TIFFFax3fillruns);  | 
1479  | 0  |     EncoderState(tif)->refline = NULL;  | 
1480  |  |  | 
1481  |  |     /*  | 
1482  |  |      * Install codec methods.  | 
1483  |  |      */  | 
1484  | 0  |     tif->tif_fixuptags = Fax3FixupTags;  | 
1485  | 0  |     tif->tif_setupdecode = Fax3SetupState;  | 
1486  | 0  |     tif->tif_predecode = Fax3PreDecode;  | 
1487  | 0  |     tif->tif_decoderow = Fax3Decode1D;  | 
1488  | 0  |     tif->tif_decodestrip = Fax3Decode1D;  | 
1489  | 0  |     tif->tif_decodetile = Fax3Decode1D;  | 
1490  | 0  |     tif->tif_setupencode = Fax3SetupState;  | 
1491  | 0  |     tif->tif_preencode = Fax3PreEncode;  | 
1492  | 0  |     tif->tif_postencode = Fax3PostEncode;  | 
1493  | 0  |     tif->tif_encoderow = Fax3Encode;  | 
1494  | 0  |     tif->tif_encodestrip = Fax3Encode;  | 
1495  | 0  |     tif->tif_encodetile = Fax3Encode;  | 
1496  | 0  |     tif->tif_close = Fax3Close;  | 
1497  | 0  |     tif->tif_cleanup = Fax3Cleanup;  | 
1498  |  | 
  | 
1499  | 0  |     return (1);  | 
1500  | 0  | }  | 
1501  |  |  | 
1502  |  | int TIFFInitCCITTFax3(TIFF *tif, int scheme)  | 
1503  | 0  | { | 
1504  | 0  |     (void)scheme;  | 
1505  | 0  |     if (InitCCITTFax3(tif))  | 
1506  | 0  |     { | 
1507  |  |         /*  | 
1508  |  |          * Merge codec-specific tag information.  | 
1509  |  |          */  | 
1510  | 0  |         if (!_TIFFMergeFields(tif, fax3Fields, TIFFArrayCount(fax3Fields)))  | 
1511  | 0  |         { | 
1512  | 0  |             TIFFErrorExtR(tif, "TIFFInitCCITTFax3",  | 
1513  | 0  |                           "Merging CCITT Fax 3 codec-specific tags failed");  | 
1514  | 0  |             return 0;  | 
1515  | 0  |         }  | 
1516  |  |  | 
1517  |  |         /*  | 
1518  |  |          * The default format is Class/F-style w/o RTC.  | 
1519  |  |          */  | 
1520  | 0  |         return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_CLASSF);  | 
1521  | 0  |     }  | 
1522  | 0  |     else  | 
1523  | 0  |         return 01;  | 
1524  | 0  | }  | 
1525  |  |  | 
1526  |  | /*  | 
1527  |  |  * CCITT Group 4 (T.6) Facsimile-compatible  | 
1528  |  |  * Compression Scheme Support.  | 
1529  |  |  */  | 
1530  |  |  | 
1531  |  | #define SWAP(t, a, b)                                                          \  | 
1532  | 0  |     {                                                                          \ | 
1533  | 0  |         t x;                                                                   \  | 
1534  | 0  |         x = (a);                                                               \  | 
1535  | 0  |         (a) = (b);                                                             \  | 
1536  | 0  |         (b) = x;                                                               \  | 
1537  | 0  |     }  | 
1538  |  | /*  | 
1539  |  |  * Decode the requested amount of G4-encoded data.  | 
1540  |  |  */  | 
1541  |  | static int Fax4Decode(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)  | 
1542  | 0  | { | 
1543  | 0  |     DECLARE_STATE_2D(tif, sp, "Fax4Decode");  | 
1544  | 0  |     (void)s;  | 
1545  | 0  |     if (occ % sp->b.rowbytes)  | 
1546  | 0  |     { | 
1547  | 0  |         TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");  | 
1548  | 0  |         return (-1);  | 
1549  | 0  |     }  | 
1550  | 0  |     if (sp->eofReachedCount >= EOF_REACHED_COUNT_THRESHOLD)  | 
1551  | 0  |     { | 
1552  | 0  |         TIFFErrorExtR(  | 
1553  | 0  |             tif, module,  | 
1554  | 0  |             "End of file has already been reached %d times within that strip",  | 
1555  | 0  |             sp->eofReachedCount);  | 
1556  | 0  |         return (-1);  | 
1557  | 0  |     }  | 
1558  | 0  |     CACHE_STATE(tif, sp);  | 
1559  | 0  |     while (occ > 0)  | 
1560  | 0  |     { | 
1561  | 0  |         a0 = 0;  | 
1562  | 0  |         RunLength = 0;  | 
1563  | 0  |         pa = thisrun = sp->curruns;  | 
1564  | 0  |         pb = sp->refruns;  | 
1565  | 0  |         b1 = *pb++;  | 
1566  |  | #ifdef FAX3_DEBUG  | 
1567  |  |         printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); | 
1568  |  |         printf("-------------------- %d\n", tif->tif_row); | 
1569  |  |         fflush(stdout);  | 
1570  |  | #endif  | 
1571  | 0  |         EXPAND2D(EOFG4);  | 
1572  | 0  |         if (EOLcnt)  | 
1573  | 0  |             goto EOFG4;  | 
1574  | 0  |         if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */  | 
1575  | 0  |         { | 
1576  | 0  |             TIFFErrorExtR(tif, module,  | 
1577  | 0  |                           "Buffer overrun detected : %" TIFF_SSIZE_FORMAT  | 
1578  | 0  |                           " bytes available, %d bits needed",  | 
1579  | 0  |                           occ, lastx);  | 
1580  | 0  |             return -1;  | 
1581  | 0  |         }  | 
1582  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
1583  | 0  |         SETVALUE(0); /* imaginary change for reference */  | 
1584  | 0  |         SWAP(uint32_t *, sp->curruns, sp->refruns);  | 
1585  | 0  |         buf += sp->b.rowbytes;  | 
1586  | 0  |         occ -= sp->b.rowbytes;  | 
1587  | 0  |         sp->line++;  | 
1588  | 0  |         continue;  | 
1589  | 0  |     EOFG4:  | 
1590  | 0  |         NeedBits16(13, BADG4);  | 
1591  | 0  |     BADG4:  | 
1592  |  | #ifdef FAX3_DEBUG  | 
1593  |  |         if (GetBits(13) != 0x1001)  | 
1594  |  |             fputs("Bad EOFB\n", stderr); | 
1595  |  | #endif  | 
1596  | 0  |         ClrBits(13);  | 
1597  | 0  |         if (((lastx + 7) >> 3) > (int)occ) /* check for buffer overrun */  | 
1598  | 0  |         { | 
1599  | 0  |             TIFFErrorExtR(tif, module,  | 
1600  | 0  |                           "Buffer overrun detected : %" TIFF_SSIZE_FORMAT  | 
1601  | 0  |                           " bytes available, %d bits needed",  | 
1602  | 0  |                           occ, lastx);  | 
1603  | 0  |             return -1;  | 
1604  | 0  |         }  | 
1605  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
1606  | 0  |         UNCACHE_STATE(tif, sp);  | 
1607  | 0  |         return (sp->line ? 1 : -1); /* don't error on badly-terminated strips */  | 
1608  | 0  |     }  | 
1609  | 0  |     UNCACHE_STATE(tif, sp);  | 
1610  | 0  |     return (1);  | 
1611  | 0  | }  | 
1612  |  | #undef SWAP  | 
1613  |  |  | 
1614  |  | /*  | 
1615  |  |  * Encode the requested amount of data.  | 
1616  |  |  */  | 
1617  |  | static int Fax4Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)  | 
1618  | 0  | { | 
1619  | 0  |     static const char module[] = "Fax4Encode";  | 
1620  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
1621  | 0  |     (void)s;  | 
1622  | 0  |     if (cc % sp->b.rowbytes)  | 
1623  | 0  |     { | 
1624  | 0  |         TIFFErrorExtR(tif, module, "Fractional scanlines cannot be written");  | 
1625  | 0  |         return (0);  | 
1626  | 0  |     }  | 
1627  | 0  |     while (cc > 0)  | 
1628  | 0  |     { | 
1629  | 0  |         if (!Fax3Encode2DRow(tif, bp, sp->refline, sp->b.rowpixels))  | 
1630  | 0  |             return (0);  | 
1631  | 0  |         _TIFFmemcpy(sp->refline, bp, sp->b.rowbytes);  | 
1632  | 0  |         bp += sp->b.rowbytes;  | 
1633  | 0  |         cc -= sp->b.rowbytes;  | 
1634  | 0  |     }  | 
1635  | 0  |     return (1);  | 
1636  | 0  | }  | 
1637  |  |  | 
1638  |  | static int Fax4PostEncode(TIFF *tif)  | 
1639  | 0  | { | 
1640  | 0  |     Fax3CodecState *sp = EncoderState(tif);  | 
1641  |  |  | 
1642  |  |     /* terminate strip w/ EOFB */  | 
1643  | 0  |     Fax3PutBits(tif, EOL, 12);  | 
1644  | 0  |     Fax3PutBits(tif, EOL, 12);  | 
1645  | 0  |     if (sp->bit != 8)  | 
1646  | 0  |         Fax3FlushBits(tif, sp);  | 
1647  | 0  |     return (1);  | 
1648  | 0  | }  | 
1649  |  |  | 
1650  |  | int TIFFInitCCITTFax4(TIFF *tif, int scheme)  | 
1651  | 0  | { | 
1652  | 0  |     (void)scheme;  | 
1653  | 0  |     if (InitCCITTFax3(tif))  | 
1654  | 0  |     { /* reuse G3 support */ | 
1655  |  |         /*  | 
1656  |  |          * Merge codec-specific tag information.  | 
1657  |  |          */  | 
1658  | 0  |         if (!_TIFFMergeFields(tif, fax4Fields, TIFFArrayCount(fax4Fields)))  | 
1659  | 0  |         { | 
1660  | 0  |             TIFFErrorExtR(tif, "TIFFInitCCITTFax4",  | 
1661  | 0  |                           "Merging CCITT Fax 4 codec-specific tags failed");  | 
1662  | 0  |             return 0;  | 
1663  | 0  |         }  | 
1664  |  |  | 
1665  | 0  |         tif->tif_decoderow = Fax4Decode;  | 
1666  | 0  |         tif->tif_decodestrip = Fax4Decode;  | 
1667  | 0  |         tif->tif_decodetile = Fax4Decode;  | 
1668  | 0  |         tif->tif_encoderow = Fax4Encode;  | 
1669  | 0  |         tif->tif_encodestrip = Fax4Encode;  | 
1670  | 0  |         tif->tif_encodetile = Fax4Encode;  | 
1671  | 0  |         tif->tif_postencode = Fax4PostEncode;  | 
1672  |  |         /*  | 
1673  |  |          * Suppress RTC at the end of each strip.  | 
1674  |  |          */  | 
1675  | 0  |         return TIFFSetField(tif, TIFFTAG_FAXMODE, FAXMODE_NORTC);  | 
1676  | 0  |     }  | 
1677  | 0  |     else  | 
1678  | 0  |         return (0);  | 
1679  | 0  | }  | 
1680  |  |  | 
1681  |  | /*  | 
1682  |  |  * CCITT Group 3 1-D Modified Huffman RLE Compression Support.  | 
1683  |  |  * (Compression algorithms 2 and 32771)  | 
1684  |  |  */  | 
1685  |  |  | 
1686  |  | /*  | 
1687  |  |  * Decode the requested amount of RLE-encoded data.  | 
1688  |  |  */  | 
1689  |  | static int Fax3DecodeRLE(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)  | 
1690  | 0  | { | 
1691  | 0  |     DECLARE_STATE(tif, sp, "Fax3DecodeRLE");  | 
1692  | 0  |     int mode = sp->b.mode;  | 
1693  | 0  |     (void)s;  | 
1694  | 0  |     if (occ % sp->b.rowbytes)  | 
1695  | 0  |     { | 
1696  | 0  |         TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");  | 
1697  | 0  |         return (-1);  | 
1698  | 0  |     }  | 
1699  | 0  |     CACHE_STATE(tif, sp);  | 
1700  | 0  |     thisrun = sp->curruns;  | 
1701  | 0  |     while (occ > 0)  | 
1702  | 0  |     { | 
1703  | 0  |         a0 = 0;  | 
1704  | 0  |         RunLength = 0;  | 
1705  | 0  |         pa = thisrun;  | 
1706  |  | #ifdef FAX3_DEBUG  | 
1707  |  |         printf("\nBitAcc=%08" PRIX32 ", BitsAvail = %d\n", BitAcc, BitsAvail); | 
1708  |  |         printf("-------------------- %" PRIu32 "\n", tif->tif_row); | 
1709  |  |         fflush(stdout);  | 
1710  |  | #endif  | 
1711  | 0  |         EXPAND1D(EOFRLE);  | 
1712  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
1713  |  |         /*  | 
1714  |  |          * Cleanup at the end of the row.  | 
1715  |  |          */  | 
1716  | 0  |         if (mode & FAXMODE_BYTEALIGN)  | 
1717  | 0  |         { | 
1718  | 0  |             int n = BitsAvail - (BitsAvail & ~7);  | 
1719  | 0  |             ClrBits(n);  | 
1720  | 0  |         }  | 
1721  | 0  |         else if (mode & FAXMODE_WORDALIGN)  | 
1722  | 0  |         { | 
1723  | 0  |             int n = BitsAvail - (BitsAvail & ~15);  | 
1724  | 0  |             ClrBits(n);  | 
1725  | 0  |             if (BitsAvail == 0 && !isAligned(cp, uint16_t))  | 
1726  | 0  |                 cp++;  | 
1727  | 0  |         }  | 
1728  | 0  |         buf += sp->b.rowbytes;  | 
1729  | 0  |         occ -= sp->b.rowbytes;  | 
1730  | 0  |         sp->line++;  | 
1731  | 0  |         continue;  | 
1732  | 0  |     EOFRLE: /* premature EOF */  | 
1733  | 0  |         (*sp->fill)(buf, thisrun, pa, lastx);  | 
1734  | 0  |         UNCACHE_STATE(tif, sp);  | 
1735  | 0  |         return (-1);  | 
1736  | 0  |     }  | 
1737  | 0  |     UNCACHE_STATE(tif, sp);  | 
1738  | 0  |     return (1);  | 
1739  | 0  | }  | 
1740  |  |  | 
1741  |  | int TIFFInitCCITTRLE(TIFF *tif, int scheme)  | 
1742  | 0  | { | 
1743  | 0  |     (void)scheme;  | 
1744  | 0  |     if (InitCCITTFax3(tif))  | 
1745  | 0  |     { /* reuse G3 support */ | 
1746  | 0  |         tif->tif_decoderow = Fax3DecodeRLE;  | 
1747  | 0  |         tif->tif_decodestrip = Fax3DecodeRLE;  | 
1748  | 0  |         tif->tif_decodetile = Fax3DecodeRLE;  | 
1749  |  |         /*  | 
1750  |  |          * Suppress RTC+EOLs when encoding and byte-align data.  | 
1751  |  |          */  | 
1752  | 0  |         return TIFFSetField(tif, TIFFTAG_FAXMODE,  | 
1753  | 0  |                             FAXMODE_NORTC | FAXMODE_NOEOL | FAXMODE_BYTEALIGN);  | 
1754  | 0  |     }  | 
1755  | 0  |     else  | 
1756  | 0  |         return (0);  | 
1757  | 0  | }  | 
1758  |  |  | 
1759  |  | int TIFFInitCCITTRLEW(TIFF *tif, int scheme)  | 
1760  | 0  | { | 
1761  | 0  |     (void)scheme;  | 
1762  | 0  |     if (InitCCITTFax3(tif))  | 
1763  | 0  |     { /* reuse G3 support */ | 
1764  | 0  |         tif->tif_decoderow = Fax3DecodeRLE;  | 
1765  | 0  |         tif->tif_decodestrip = Fax3DecodeRLE;  | 
1766  | 0  |         tif->tif_decodetile = Fax3DecodeRLE;  | 
1767  |  |         /*  | 
1768  |  |          * Suppress RTC+EOLs when encoding and word-align data.  | 
1769  |  |          */  | 
1770  | 0  |         return TIFFSetField(tif, TIFFTAG_FAXMODE,  | 
1771  | 0  |                             FAXMODE_NORTC | FAXMODE_NOEOL | FAXMODE_WORDALIGN);  | 
1772  | 0  |     }  | 
1773  | 0  |     else  | 
1774  | 0  |         return (0);  | 
1775  | 0  | }  | 
1776  |  | #endif /* CCITT_SUPPORT */  |