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