| 1 |
/* |
| 2 |
* Copyright (c) 1988-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 4 |
* Copyright (c) 2022 Even Rouault |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 7 |
* its documentation for any purpose is hereby granted without fee, provided |
| 8 |
* that (i) the above copyright notices and this permission notice appear in |
| 9 |
* all copies of the software and related documentation, and (ii) the names of |
| 10 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 11 |
* publicity relating to the software without the specific, prior written |
| 12 |
* permission of Sam Leffler and Silicon Graphics. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 16 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 17 |
* |
| 18 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 19 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 20 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 21 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 22 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 23 |
* OF THIS SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "tiffiop.h" |
| 27 |
#ifdef LZW_SUPPORT |
| 28 |
/* |
| 29 |
* TIFF Library. |
| 30 |
* Rev 5.0 Lempel-Ziv & Welch Compression Support |
| 31 |
* |
| 32 |
* This code is derived from the compress program whose code is |
| 33 |
* derived from software contributed to Berkeley by James A. Woods, |
| 34 |
* derived from original work by Spencer Thomas and Joseph Orost. |
| 35 |
* |
| 36 |
* The original Berkeley copyright notice appears below in its entirety. |
| 37 |
*/ |
| 38 |
#include "tif_predict.h" |
| 39 |
|
| 40 |
#include <stdbool.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <stdlib.h> |
| 43 |
|
| 44 |
/* Select the plausible largest natural integer type for the architecture */ |
| 45 |
#define SIZEOF_WORDTYPE SIZEOF_SIZE_T |
| 46 |
typedef size_t WordType; |
| 47 |
|
| 48 |
/* |
| 49 |
* NB: The 5.0 spec describes a different algorithm than Aldus |
| 50 |
* implements. Specifically, Aldus does code length transitions |
| 51 |
* one code earlier than should be done (for real LZW). |
| 52 |
* Earlier versions of this library implemented the correct |
| 53 |
* LZW algorithm, but emitted codes in a bit order opposite |
| 54 |
* to the TIFF spec. Thus, to maintain compatibility w/ Aldus |
| 55 |
* we interpret MSB-LSB ordered codes to be images written w/ |
| 56 |
* old versions of this library, but otherwise adhere to the |
| 57 |
* Aldus "off by one" algorithm. |
| 58 |
* |
| 59 |
* Future revisions to the TIFF spec are expected to "clarify this issue". |
| 60 |
*/ |
| 61 |
#define LZW_COMPAT /* include backwards compatibility code */ |
| 62 |
|
| 63 |
#define MAXCODE(n) ((1L << (n)) - 1) |
| 64 |
/* |
| 65 |
* The TIFF spec specifies that encoded bit |
| 66 |
* strings range from 9 to 12 bits. |
| 67 |
*/ |
| 68 |
#define BITS_MIN 9 /* start with 9 bits */ |
| 69 |
#define BITS_MAX 12 /* max of 12 bit strings */ |
| 70 |
/* predefined codes */ |
| 71 |
#define CODE_CLEAR 256 /* code to clear string table */ |
| 72 |
#define CODE_EOI 257 /* end-of-information code */ |
| 73 |
#define CODE_FIRST 258 /* first free code entry */ |
| 74 |
#define CODE_MAX MAXCODE(BITS_MAX) |
| 75 |
#define HSIZE 9001L /* 91% occupancy */ |
| 76 |
#define HSHIFT (13 - 8) |
| 77 |
#ifdef LZW_COMPAT |
| 78 |
/* NB: +1024 is for compatibility with old files */ |
| 79 |
#define CSIZE (MAXCODE(BITS_MAX) + 1024L) |
| 80 |
#else |
| 81 |
#define CSIZE (MAXCODE(BITS_MAX) + 1L) |
| 82 |
#endif |
| 83 |
|
| 84 |
/* |
| 85 |
* State block for each open TIFF file using LZW |
| 86 |
* compression/decompression. Note that the predictor |
| 87 |
* state block must be first in this data structure. |
| 88 |
*/ |
| 89 |
typedef struct |
| 90 |
{ |
| 91 |
TIFFPredictorState predict; /* predictor super class */ |
| 92 |
|
| 93 |
unsigned short nbits; /* # of bits/code */ |
| 94 |
unsigned short maxcode; /* maximum code for lzw_nbits */ |
| 95 |
unsigned short free_ent; /* next free entry in hash table */ |
| 96 |
WordType nextdata; /* next bits of i/o */ |
| 97 |
long nextbits; /* # of valid bits in lzw_nextdata */ |
| 98 |
|
| 99 |
int rw_mode; /* preserve rw_mode from init */ |
| 100 |
} LZWBaseState; |
| 101 |
|
| 102 |
#define lzw_nbits base.nbits |
| 103 |
#define lzw_maxcode base.maxcode |
| 104 |
#define lzw_free_ent base.free_ent |
| 105 |
#define lzw_nextdata base.nextdata |
| 106 |
#define lzw_nextbits base.nextbits |
| 107 |
|
| 108 |
/* |
| 109 |
* Encoding-specific state. |
| 110 |
*/ |
| 111 |
typedef uint16_t hcode_t; /* codes fit in 16 bits */ |
| 112 |
typedef struct |
| 113 |
{ |
| 114 |
long hash; |
| 115 |
hcode_t code; |
| 116 |
} hash_t; |
| 117 |
|
| 118 |
/* |
| 119 |
* Decoding-specific state. |
| 120 |
*/ |
| 121 |
typedef struct code_ent |
| 122 |
{ |
| 123 |
struct code_ent *next; |
| 124 |
unsigned short length; /* string len, including this token */ |
| 125 |
/* firstchar should be placed immediately before value in this structure */ |
| 126 |
unsigned char firstchar; /* first token of string */ |
| 127 |
unsigned char value; /* data value */ |
| 128 |
bool repeated; |
| 129 |
} code_t; |
| 130 |
|
| 131 |
typedef int (*decodeFunc)(TIFF *, uint8_t *, tmsize_t, uint16_t); |
| 132 |
|
| 133 |
typedef struct |
| 134 |
{ |
| 135 |
LZWBaseState base; |
| 136 |
|
| 137 |
/* Decoding specific data */ |
| 138 |
long dec_nbitsmask; /* lzw_nbits 1 bits, right adjusted */ |
| 139 |
tmsize_t dec_restart; /* restart count */ |
| 140 |
uint64_t dec_bitsleft; /* available bits in raw data */ |
| 141 |
tmsize_t old_tif_rawcc; /* value of tif_rawcc at the end of the previous |
| 142 |
TIFLZWDecode() call */ |
| 143 |
decodeFunc dec_decode; /* regular or backwards compatible */ |
| 144 |
code_t *dec_codep; /* current recognized code */ |
| 145 |
code_t *dec_oldcodep; /* previously recognized code */ |
| 146 |
code_t *dec_free_entp; /* next free entry */ |
| 147 |
code_t *dec_maxcodep; /* max available entry */ |
| 148 |
code_t *dec_codetab; /* kept separate for small machines */ |
| 149 |
int read_error; /* whether a read error has occurred, and which should cause |
| 150 |
further reads in the same strip/tile to be aborted */ |
| 151 |
|
| 152 |
/* Encoding specific data */ |
| 153 |
int enc_oldcode; /* last code encountered */ |
| 154 |
tmsize_t enc_checkpoint; /* point at which to clear table */ |
| 155 |
#define CHECK_GAP 10000 /* enc_ratio check interval */ |
| 156 |
tmsize_t enc_ratio; /* current compression ratio */ |
| 157 |
tmsize_t enc_incount; /* (input) data bytes encoded */ |
| 158 |
tmsize_t enc_outcount; /* encoded (output) bytes */ |
| 159 |
uint8_t *enc_rawlimit; /* bound on tif_rawdata buffer */ |
| 160 |
hash_t *enc_hashtab; /* kept separate for small machines */ |
| 161 |
} LZWCodecState; |
| 162 |
|
| 163 |
#define LZWState(tif) ((LZWBaseState *)(tif)->tif_data) |
| 164 |
#define LZWDecoderState(tif) ((LZWCodecState *)LZWState(tif)) |
| 165 |
#define LZWEncoderState(tif) ((LZWCodecState *)LZWState(tif)) |
| 166 |
|
| 167 |
static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s); |
| 168 |
#ifdef LZW_COMPAT |
| 169 |
static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s); |
| 170 |
#endif |
| 171 |
static void cl_hash(LZWCodecState *); |
| 172 |
|
| 173 |
/* |
| 174 |
* LZW Decoder. |
| 175 |
*/ |
| 176 |
|
| 177 |
static int LZWFixupTags(TIFF *tif) |
| 178 |
{ |
| 179 |
(void)tif; |
| 180 |
return (1); |
| 181 |
} |
| 182 |
|
| 183 |
static int LZWSetupDecode(TIFF *tif) |
| 184 |
{ |
| 185 |
static const char module[] = "LZWSetupDecode"; |
| 186 |
LZWCodecState *sp = LZWDecoderState(tif); |
| 187 |
int code; |
| 188 |
|
| 189 |
if (sp == NULL) |
| 190 |
{ |
| 191 |
/* |
| 192 |
* Allocate state block so tag methods have storage to record |
| 193 |
* values. |
| 194 |
*/ |
| 195 |
tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState)); |
| 196 |
if (tif->tif_data == NULL) |
| 197 |
{ |
| 198 |
TIFFErrorExtR(tif, module, "No space for LZW state block"); |
| 199 |
return (0); |
| 200 |
} |
| 201 |
|
| 202 |
sp = LZWDecoderState(tif); |
| 203 |
sp->dec_codetab = NULL; |
| 204 |
sp->dec_decode = NULL; |
| 205 |
|
| 206 |
/* |
| 207 |
* Setup predictor setup. |
| 208 |
*/ |
| 209 |
(void)TIFFPredictorInit(tif); |
| 210 |
} |
| 211 |
|
| 212 |
if (sp->dec_codetab == NULL) |
| 213 |
{ |
| 214 |
sp->dec_codetab = (code_t *)_TIFFmallocExt(tif, CSIZE * sizeof(code_t)); |
| 215 |
if (sp->dec_codetab == NULL) |
| 216 |
{ |
| 217 |
TIFFErrorExtR(tif, module, "No space for LZW code table"); |
| 218 |
return (0); |
| 219 |
} |
| 220 |
/* |
| 221 |
* Pre-load the table. |
| 222 |
*/ |
| 223 |
code = 255; |
| 224 |
do |
| 225 |
{ |
| 226 |
sp->dec_codetab[code].firstchar = (unsigned char)code; |
| 227 |
sp->dec_codetab[code].value = (unsigned char)code; |
| 228 |
sp->dec_codetab[code].repeated = true; |
| 229 |
sp->dec_codetab[code].length = 1; |
| 230 |
sp->dec_codetab[code].next = NULL; |
| 231 |
} while (code--); |
| 232 |
/* |
| 233 |
* Zero-out the unused entries */ |
| 234 |
/* Silence false positive */ |
| 235 |
/* coverity[overrun-buffer-arg] */ |
| 236 |
memset(&sp->dec_codetab[CODE_CLEAR], 0, |
| 237 |
(CODE_FIRST - CODE_CLEAR) * sizeof(code_t)); |
| 238 |
} |
| 239 |
return (1); |
| 240 |
} |
| 241 |
|
| 242 |
/* |
| 243 |
* Setup state for decoding a strip. |
| 244 |
*/ |
| 245 |
static int LZWPreDecode(TIFF *tif, uint16_t s) |
| 246 |
{ |
| 247 |
static const char module[] = "LZWPreDecode"; |
| 248 |
LZWCodecState *sp = LZWDecoderState(tif); |
| 249 |
|
| 250 |
(void)s; |
| 251 |
assert(sp != NULL); |
| 252 |
if (sp->dec_codetab == NULL) |
| 253 |
{ |
| 254 |
tif->tif_setupdecode(tif); |
| 255 |
if (sp->dec_codetab == NULL) |
| 256 |
return (0); |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* Check for old bit-reversed codes. |
| 261 |
*/ |
| 262 |
if (tif->tif_rawcc >= 2 && tif->tif_rawdata[0] == 0 && |
| 263 |
(tif->tif_rawdata[1] & 0x1)) |
| 264 |
{ |
| 265 |
#ifdef LZW_COMPAT |
| 266 |
if (!sp->dec_decode) |
| 267 |
{ |
| 268 |
TIFFWarningExtR(tif, module, "Old-style LZW codes, convert file"); |
| 269 |
/* |
| 270 |
* Override default decoding methods with |
| 271 |
* ones that deal with the old coding. |
| 272 |
* Otherwise the predictor versions set |
| 273 |
* above will call the compatibility routines |
| 274 |
* through the dec_decode method. |
| 275 |
*/ |
| 276 |
tif->tif_decoderow = LZWDecodeCompat; |
| 277 |
tif->tif_decodestrip = LZWDecodeCompat; |
| 278 |
tif->tif_decodetile = LZWDecodeCompat; |
| 279 |
/* |
| 280 |
* If doing horizontal differencing, must |
| 281 |
* re-setup the predictor logic since we |
| 282 |
* switched the basic decoder methods... |
| 283 |
*/ |
| 284 |
(*tif->tif_setupdecode)(tif); |
| 285 |
sp->dec_decode = LZWDecodeCompat; |
| 286 |
} |
| 287 |
sp->lzw_maxcode = MAXCODE(BITS_MIN); |
| 288 |
#else /* !LZW_COMPAT */ |
| 289 |
if (!sp->dec_decode) |
| 290 |
{ |
| 291 |
TIFFErrorExtR(tif, module, "Old-style LZW codes not supported"); |
| 292 |
sp->dec_decode = LZWDecode; |
| 293 |
} |
| 294 |
return (0); |
| 295 |
#endif /* !LZW_COMPAT */ |
| 296 |
} |
| 297 |
else |
| 298 |
{ |
| 299 |
sp->lzw_maxcode = MAXCODE(BITS_MIN) - 1; |
| 300 |
sp->dec_decode = LZWDecode; |
| 301 |
} |
| 302 |
sp->lzw_nbits = BITS_MIN; |
| 303 |
sp->lzw_nextbits = 0; |
| 304 |
sp->lzw_nextdata = 0; |
| 305 |
|
| 306 |
sp->dec_restart = 0; |
| 307 |
sp->dec_nbitsmask = MAXCODE(BITS_MIN); |
| 308 |
sp->dec_bitsleft = 0; |
| 309 |
sp->old_tif_rawcc = 0; |
| 310 |
sp->dec_free_entp = sp->dec_codetab - 1; // + CODE_FIRST; |
| 311 |
/* |
| 312 |
* Zero entries that are not yet filled in. We do |
| 313 |
* this to guard against bogus input data that causes |
| 314 |
* us to index into undefined entries. If you can |
| 315 |
* come up with a way to safely bounds-check input codes |
| 316 |
* while decoding then you can remove this operation. |
| 317 |
*/ |
| 318 |
sp->dec_oldcodep = &sp->dec_codetab[0]; |
| 319 |
sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask - 1]; |
| 320 |
sp->read_error = 0; |
| 321 |
return (1); |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* Decode a "hunk of data". |
| 326 |
*/ |
| 327 |
|
| 328 |
/* Get the next 32 or 64-bit from the input data */ |
| 329 |
#ifdef WORDS_BIGENDIAN |
| 330 |
#define GetNextData(nextdata, bp) memcpy(&nextdata, bp, sizeof(nextdata)) |
| 331 |
#elif SIZEOF_WORDTYPE == 8 |
| 332 |
#if defined(_M_X64) |
| 333 |
#define GetNextData(nextdata, bp) nextdata = _byteswap_uint64(*(uint64_t *)(bp)) |
| 334 |
#elif defined(__GNUC__) |
| 335 |
#define GetNextData(nextdata, bp) \ |
| 336 |
memcpy(&nextdata, bp, sizeof(nextdata)); \ |
| 337 |
nextdata = __builtin_bswap64(nextdata) |
| 338 |
#else |
| 339 |
#define GetNextData(nextdata, bp) \ |
| 340 |
nextdata = (((uint64_t)bp[0]) << 56) | (((uint64_t)bp[1]) << 48) | \ |
| 341 |
(((uint64_t)bp[2]) << 40) | (((uint64_t)bp[3]) << 32) | \ |
| 342 |
(((uint64_t)bp[4]) << 24) | (((uint64_t)bp[5]) << 16) | \ |
| 343 |
(((uint64_t)bp[6]) << 8) | (((uint64_t)bp[7])) |
| 344 |
#endif |
| 345 |
#elif SIZEOF_WORDTYPE == 4 |
| 346 |
#if defined(_M_X86) |
| 347 |
#define GetNextData(nextdata, bp) \ |
| 348 |
nextdata = _byteswap_ulong(*(unsigned long *)(bp)) |
| 349 |
#elif defined(__GNUC__) |
| 350 |
#define GetNextData(nextdata, bp) \ |
| 351 |
memcpy(&nextdata, bp, sizeof(nextdata)); \ |
| 352 |
nextdata = __builtin_bswap32(nextdata) |
| 353 |
#else |
| 354 |
#define GetNextData(nextdata, bp) \ |
| 355 |
nextdata = (((uint32_t)bp[0]) << 24) | (((uint32_t)bp[1]) << 16) | \ |
| 356 |
(((uint32_t)bp[2]) << 8) | (((uint32_t)bp[3])) |
| 357 |
#endif |
| 358 |
#else |
| 359 |
#error "Unhandled SIZEOF_WORDTYPE" |
| 360 |
#endif |
| 361 |
|
| 362 |
#define GetNextCodeLZW() \ |
| 363 |
do \ |
| 364 |
{ \ |
| 365 |
nextbits -= nbits; \ |
| 366 |
if (nextbits < 0) \ |
| 367 |
{ \ |
| 368 |
if (dec_bitsleft >= 8 * SIZEOF_WORDTYPE) \ |
| 369 |
{ \ |
| 370 |
unsigned codetmp = (unsigned)(nextdata << (-nextbits)); \ |
| 371 |
GetNextData(nextdata, bp); \ |
| 372 |
bp += SIZEOF_WORDTYPE; \ |
| 373 |
nextbits += 8 * SIZEOF_WORDTYPE; \ |
| 374 |
dec_bitsleft -= 8 * SIZEOF_WORDTYPE; \ |
| 375 |
code = (WordType)((codetmp | (nextdata >> nextbits)) & \ |
| 376 |
nbitsmask); \ |
| 377 |
break; \ |
| 378 |
} \ |
| 379 |
else \ |
| 380 |
{ \ |
| 381 |
if (dec_bitsleft < 8) \ |
| 382 |
{ \ |
| 383 |
goto no_eoi; \ |
| 384 |
} \ |
| 385 |
nextdata = (nextdata << 8) | *(bp)++; \ |
| 386 |
nextbits += 8; \ |
| 387 |
dec_bitsleft -= 8; \ |
| 388 |
if (nextbits < 0) \ |
| 389 |
{ \ |
| 390 |
if (dec_bitsleft < 8) \ |
| 391 |
{ \ |
| 392 |
goto no_eoi; \ |
| 393 |
} \ |
| 394 |
nextdata = (nextdata << 8) | *(bp)++; \ |
| 395 |
nextbits += 8; \ |
| 396 |
dec_bitsleft -= 8; \ |
| 397 |
} \ |
| 398 |
} \ |
| 399 |
} \ |
| 400 |
code = (WordType)((nextdata >> nextbits) & nbitsmask); \ |
| 401 |
} while (0) |
| 402 |
|
| 403 |
static int LZWDecode(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s) |
| 404 |
{ |
| 405 |
static const char module[] = "LZWDecode"; |
| 406 |
LZWCodecState *sp = LZWDecoderState(tif); |
| 407 |
uint8_t *op = (uint8_t *)op0; |
| 408 |
tmsize_t occ = occ0; |
| 409 |
uint8_t *bp; |
| 410 |
long nbits, nextbits, nbitsmask; |
| 411 |
WordType nextdata; |
| 412 |
code_t *free_entp, *maxcodep, *oldcodep; |
| 413 |
|
| 414 |
(void)s; |
| 415 |
assert(sp != NULL); |
| 416 |
assert(sp->dec_codetab != NULL); |
| 417 |
|
| 418 |
if (sp->read_error) |
| 419 |
{ |
| 420 |
memset(op, 0, (size_t)occ); |
| 421 |
TIFFErrorExtR(tif, module, |
| 422 |
"LZWDecode: Scanline %" PRIu32 " cannot be read due to " |
| 423 |
"previous error", |
| 424 |
tif->tif_row); |
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
/* |
| 429 |
* Restart interrupted output operation. |
| 430 |
*/ |
| 431 |
if (sp->dec_restart) |
| 432 |
{ |
| 433 |
tmsize_t residue; |
| 434 |
|
| 435 |
code_t *codep = sp->dec_codep; |
| 436 |
residue = codep->length - sp->dec_restart; |
| 437 |
if (residue > occ) |
| 438 |
{ |
| 439 |
/* |
| 440 |
* Residue from previous decode is sufficient |
| 441 |
* to satisfy decode request. Skip to the |
| 442 |
* start of the decoded string, place decoded |
| 443 |
* values in the output buffer, and return. |
| 444 |
*/ |
| 445 |
sp->dec_restart += occ; |
| 446 |
do |
| 447 |
{ |
| 448 |
codep = codep->next; |
| 449 |
} while (--residue > occ && codep); |
| 450 |
if (codep) |
| 451 |
{ |
| 452 |
uint8_t *tp = op + occ; |
| 453 |
do |
| 454 |
{ |
| 455 |
*--tp = codep->value; |
| 456 |
codep = codep->next; |
| 457 |
} while (--occ && codep); |
| 458 |
} |
| 459 |
return (1); |
| 460 |
} |
| 461 |
/* |
| 462 |
* Residue satisfies only part of the decode request. |
| 463 |
*/ |
| 464 |
op += residue; |
| 465 |
occ -= residue; |
| 466 |
uint8_t *tp = op; |
| 467 |
do |
| 468 |
{ |
| 469 |
*--tp = codep->value; |
| 470 |
codep = codep->next; |
| 471 |
} while (--residue && codep); |
| 472 |
sp->dec_restart = 0; |
| 473 |
} |
| 474 |
|
| 475 |
bp = (uint8_t *)tif->tif_rawcp; |
| 476 |
sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3); |
| 477 |
uint64_t dec_bitsleft = sp->dec_bitsleft; |
| 478 |
nbits = sp->lzw_nbits; |
| 479 |
nextdata = sp->lzw_nextdata; |
| 480 |
nextbits = sp->lzw_nextbits; |
| 481 |
nbitsmask = sp->dec_nbitsmask; |
| 482 |
oldcodep = sp->dec_oldcodep; |
| 483 |
free_entp = sp->dec_free_entp; |
| 484 |
maxcodep = sp->dec_maxcodep; |
| 485 |
code_t *const dec_codetab = sp->dec_codetab; |
| 486 |
code_t *codep; |
| 487 |
|
| 488 |
if (occ == 0) |
| 489 |
{ |
| 490 |
goto after_loop; |
| 491 |
} |
| 492 |
|
| 493 |
begin: |
| 494 |
{ |
| 495 |
WordType code; |
| 496 |
GetNextCodeLZW(); |
| 497 |
codep = dec_codetab + code; |
| 498 |
if (code >= CODE_FIRST) |
| 499 |
goto code_above_or_equal_to_258; |
| 500 |
if (code < 256) |
| 501 |
goto code_below_256; |
| 502 |
if (code == CODE_EOI) |
| 503 |
goto after_loop; |
| 504 |
goto code_clear; |
| 505 |
|
| 506 |
code_below_256: |
| 507 |
{ |
| 508 |
if (codep > free_entp) |
| 509 |
goto error_code; |
| 510 |
free_entp->next = oldcodep; |
| 511 |
free_entp->firstchar = oldcodep->firstchar; |
| 512 |
free_entp->length = oldcodep->length + 1; |
| 513 |
free_entp->value = (uint8_t)code; |
| 514 |
free_entp->repeated = |
| 515 |
(bool)(oldcodep->repeated & (oldcodep->value == code)); |
| 516 |
if (++free_entp > maxcodep) |
| 517 |
{ |
| 518 |
if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */ |
| 519 |
nbits = BITS_MAX; |
| 520 |
nbitsmask = MAXCODE(nbits); |
| 521 |
maxcodep = dec_codetab + nbitsmask - 1; |
| 522 |
if (free_entp >= &dec_codetab[CSIZE]) |
| 523 |
{ |
| 524 |
/* At that point, the next valid states are either EOI or a */ |
| 525 |
/* CODE_CLEAR. If a regular code is read, at the next */ |
| 526 |
/* attempt at registering a new entry, we will error out */ |
| 527 |
/* due to setting free_entp before any valid code */ |
| 528 |
free_entp = dec_codetab - 1; |
| 529 |
} |
| 530 |
} |
| 531 |
oldcodep = codep; |
| 532 |
*op++ = (uint8_t)code; |
| 533 |
occ--; |
| 534 |
if (occ == 0) |
| 535 |
goto after_loop; |
| 536 |
goto begin; |
| 537 |
} |
| 538 |
|
| 539 |
code_above_or_equal_to_258: |
| 540 |
{ |
| 541 |
/* |
| 542 |
* Add the new entry to the code table. |
| 543 |
*/ |
| 544 |
|
| 545 |
if (codep >= free_entp) |
| 546 |
{ |
| 547 |
if (codep != free_entp) |
| 548 |
goto error_code; |
| 549 |
free_entp->value = oldcodep->firstchar; |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
free_entp->value = codep->firstchar; |
| 554 |
} |
| 555 |
free_entp->repeated = |
| 556 |
(bool)(oldcodep->repeated & (oldcodep->value == free_entp->value)); |
| 557 |
free_entp->next = oldcodep; |
| 558 |
|
| 559 |
free_entp->firstchar = oldcodep->firstchar; |
| 560 |
free_entp->length = oldcodep->length + 1; |
| 561 |
if (++free_entp > maxcodep) |
| 562 |
{ |
| 563 |
if (++nbits > BITS_MAX) /* should not happen for a conformant encoder */ |
| 564 |
nbits = BITS_MAX; |
| 565 |
nbitsmask = MAXCODE(nbits); |
| 566 |
maxcodep = dec_codetab + nbitsmask - 1; |
| 567 |
if (free_entp >= &dec_codetab[CSIZE]) |
| 568 |
{ |
| 569 |
/* At that point, the next valid states are either EOI or a */ |
| 570 |
/* CODE_CLEAR. If a regular code is read, at the next */ |
| 571 |
/* attempt at registering a new entry, we will error out */ |
| 572 |
/* due to setting free_entp before any valid code */ |
| 573 |
free_entp = dec_codetab - 1; |
| 574 |
} |
| 575 |
} |
| 576 |
oldcodep = codep; |
| 577 |
|
| 578 |
/* |
| 579 |
* Code maps to a string, copy string |
| 580 |
* value to output (written in reverse). |
| 581 |
*/ |
| 582 |
/* tiny bit faster on x86_64 to store in unsigned short than int */ |
| 583 |
unsigned short len = codep->length; |
| 584 |
|
| 585 |
if (len < 3) /* equivalent to len == 2 given all other conditions */ |
| 586 |
{ |
| 587 |
if (occ <= 2) |
| 588 |
{ |
| 589 |
if (occ == 2) |
| 590 |
{ |
| 591 |
memcpy(op, &(codep->firstchar), 2); |
| 592 |
op += 2; |
| 593 |
occ -= 2; |
| 594 |
goto after_loop; |
| 595 |
} |
| 596 |
goto too_short_buffer; |
| 597 |
} |
| 598 |
|
| 599 |
memcpy(op, &(codep->firstchar), 2); |
| 600 |
op += 2; |
| 601 |
occ -= 2; |
| 602 |
goto begin; /* we can save the comparison occ > 0 */ |
| 603 |
} |
| 604 |
|
| 605 |
if (len == 3) |
| 606 |
{ |
| 607 |
if (occ <= 3) |
| 608 |
{ |
| 609 |
if (occ == 3) |
| 610 |
{ |
| 611 |
op[0] = codep->firstchar; |
| 612 |
op[1] = codep->next->value; |
| 613 |
op[2] = codep->value; |
| 614 |
op += 3; |
| 615 |
occ -= 3; |
| 616 |
goto after_loop; |
| 617 |
} |
| 618 |
goto too_short_buffer; |
| 619 |
} |
| 620 |
|
| 621 |
op[0] = codep->firstchar; |
| 622 |
op[1] = codep->next->value; |
| 623 |
op[2] = codep->value; |
| 624 |
op += 3; |
| 625 |
occ -= 3; |
| 626 |
goto begin; /* we can save the comparison occ > 0 */ |
| 627 |
} |
| 628 |
|
| 629 |
if (len > occ) |
| 630 |
{ |
| 631 |
goto too_short_buffer; |
| 632 |
} |
| 633 |
|
| 634 |
if (codep->repeated) |
| 635 |
{ |
| 636 |
memset(op, codep->value, len); |
| 637 |
op += len; |
| 638 |
occ -= len; |
| 639 |
if (occ == 0) |
| 640 |
goto after_loop; |
| 641 |
goto begin; |
| 642 |
} |
| 643 |
|
| 644 |
uint8_t *tp = op + len; |
| 645 |
|
| 646 |
assert(len >= 4); |
| 647 |
|
| 648 |
*--tp = codep->value; |
| 649 |
codep = codep->next; |
| 650 |
*--tp = codep->value; |
| 651 |
codep = codep->next; |
| 652 |
*--tp = codep->value; |
| 653 |
codep = codep->next; |
| 654 |
*--tp = codep->value; |
| 655 |
if (tp > op) |
| 656 |
{ |
| 657 |
do |
| 658 |
{ |
| 659 |
codep = codep->next; |
| 660 |
*--tp = codep->value; |
| 661 |
} while (tp > op); |
| 662 |
} |
| 663 |
|
| 664 |
assert(occ >= len); |
| 665 |
op += len; |
| 666 |
occ -= len; |
| 667 |
if (occ == 0) |
| 668 |
goto after_loop; |
| 669 |
goto begin; |
| 670 |
} |
| 671 |
|
| 672 |
code_clear: |
| 673 |
{ |
| 674 |
free_entp = dec_codetab + CODE_FIRST; |
| 675 |
nbits = BITS_MIN; |
| 676 |
nbitsmask = MAXCODE(BITS_MIN); |
| 677 |
maxcodep = dec_codetab + nbitsmask - 1; |
| 678 |
do |
| 679 |
{ |
| 680 |
GetNextCodeLZW(); |
| 681 |
} while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */ |
| 682 |
if (code == CODE_EOI) |
| 683 |
goto after_loop; |
| 684 |
if (code > CODE_EOI) |
| 685 |
{ |
| 686 |
goto error_code; |
| 687 |
} |
| 688 |
*op++ = (uint8_t)code; |
| 689 |
occ--; |
| 690 |
oldcodep = dec_codetab + code; |
| 691 |
if (occ == 0) |
| 692 |
goto after_loop; |
| 693 |
goto begin; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
too_short_buffer: |
| 698 |
{ |
| 699 |
/* |
| 700 |
* String is too long for decode buffer, |
| 701 |
* locate portion that will fit, copy to |
| 702 |
* the decode buffer, and setup restart |
| 703 |
* logic for the next decoding call. |
| 704 |
*/ |
| 705 |
sp->dec_codep = codep; |
| 706 |
do |
| 707 |
{ |
| 708 |
codep = codep->next; |
| 709 |
} while (codep->length > occ); |
| 710 |
|
| 711 |
sp->dec_restart = occ; |
| 712 |
uint8_t *tp = op + occ; |
| 713 |
do |
| 714 |
{ |
| 715 |
*--tp = codep->value; |
| 716 |
codep = codep->next; |
| 717 |
} while (--occ); |
| 718 |
} |
| 719 |
|
| 720 |
after_loop: |
| 721 |
tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp); |
| 722 |
tif->tif_rawcp = (uint8_t *)bp; |
| 723 |
sp->old_tif_rawcc = tif->tif_rawcc; |
| 724 |
sp->dec_bitsleft = dec_bitsleft; |
| 725 |
sp->lzw_nbits = (unsigned short)nbits; |
| 726 |
sp->lzw_nextdata = nextdata; |
| 727 |
sp->lzw_nextbits = nextbits; |
| 728 |
sp->dec_nbitsmask = nbitsmask; |
| 729 |
sp->dec_oldcodep = oldcodep; |
| 730 |
sp->dec_free_entp = free_entp; |
| 731 |
sp->dec_maxcodep = maxcodep; |
| 732 |
|
| 733 |
if (occ > 0) |
| 734 |
{ |
| 735 |
memset(op, 0, (size_t)occ); |
| 736 |
TIFFErrorExtR(tif, module, |
| 737 |
"Not enough data at scanline %" PRIu32 " (short %" PRIu64 |
| 738 |
" bytes)", |
| 739 |
tif->tif_row, (uint64_t)occ); |
| 740 |
return (0); |
| 741 |
} |
| 742 |
return (1); |
| 743 |
|
| 744 |
no_eoi: |
| 745 |
memset(op, 0, (size_t)occ); |
| 746 |
sp->read_error = 1; |
| 747 |
TIFFErrorExtR(tif, module, |
| 748 |
"LZWDecode: Strip %" PRIu32 " not terminated with EOI code", |
| 749 |
tif->tif_curstrip); |
| 750 |
return 0; |
| 751 |
error_code: |
| 752 |
memset(op, 0, (size_t)occ); |
| 753 |
sp->read_error = 1; |
| 754 |
TIFFErrorExtR(tif, tif->tif_name, "Using code not yet in table"); |
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
#ifdef LZW_COMPAT |
| 759 |
|
| 760 |
/* |
| 761 |
* This check shouldn't be necessary because each |
| 762 |
* strip is suppose to be terminated with CODE_EOI. |
| 763 |
*/ |
| 764 |
#define NextCode(_tif, _sp, _bp, _code, _get, dec_bitsleft) \ |
| 765 |
{ \ |
| 766 |
if (dec_bitsleft < (uint64_t)nbits) \ |
| 767 |
{ \ |
| 768 |
TIFFWarningExtR(_tif, module, \ |
| 769 |
"LZWDecode: Strip %" PRIu32 \ |
| 770 |
" not terminated with EOI code", \ |
| 771 |
_tif->tif_curstrip); \ |
| 772 |
_code = CODE_EOI; \ |
| 773 |
} \ |
| 774 |
else \ |
| 775 |
{ \ |
| 776 |
_get(_sp, _bp, _code); \ |
| 777 |
dec_bitsleft -= nbits; \ |
| 778 |
} \ |
| 779 |
} |
| 780 |
|
| 781 |
/* |
| 782 |
* Decode a "hunk of data" for old images. |
| 783 |
*/ |
| 784 |
#define GetNextCodeCompat(sp, bp, code) \ |
| 785 |
{ \ |
| 786 |
nextdata |= (unsigned long)*(bp)++ << nextbits; \ |
| 787 |
nextbits += 8; \ |
| 788 |
if (nextbits < nbits) \ |
| 789 |
{ \ |
| 790 |
nextdata |= (unsigned long)*(bp)++ << nextbits; \ |
| 791 |
nextbits += 8; \ |
| 792 |
} \ |
| 793 |
code = (hcode_t)(nextdata & nbitsmask); \ |
| 794 |
nextdata >>= nbits; \ |
| 795 |
nextbits -= nbits; \ |
| 796 |
} |
| 797 |
|
| 798 |
static int LZWDecodeCompat(TIFF *tif, uint8_t *op0, tmsize_t occ0, uint16_t s) |
| 799 |
{ |
| 800 |
static const char module[] = "LZWDecodeCompat"; |
| 801 |
LZWCodecState *sp = LZWDecoderState(tif); |
| 802 |
uint8_t *op = (uint8_t *)op0; |
| 803 |
tmsize_t occ = occ0; |
| 804 |
uint8_t *tp; |
| 805 |
uint8_t *bp; |
| 806 |
int code, nbits; |
| 807 |
int len; |
| 808 |
long nextbits, nbitsmask; |
| 809 |
WordType nextdata; |
| 810 |
code_t *codep, *free_entp, *maxcodep, *oldcodep; |
| 811 |
|
| 812 |
(void)s; |
| 813 |
assert(sp != NULL); |
| 814 |
|
| 815 |
/* |
| 816 |
* Restart interrupted output operation. |
| 817 |
*/ |
| 818 |
if (sp->dec_restart) |
| 819 |
{ |
| 820 |
tmsize_t residue; |
| 821 |
|
| 822 |
codep = sp->dec_codep; |
| 823 |
residue = codep->length - sp->dec_restart; |
| 824 |
if (residue > occ) |
| 825 |
{ |
| 826 |
/* |
| 827 |
* Residue from previous decode is sufficient |
| 828 |
* to satisfy decode request. Skip to the |
| 829 |
* start of the decoded string, place decoded |
| 830 |
* values in the output buffer, and return. |
| 831 |
*/ |
| 832 |
sp->dec_restart += occ; |
| 833 |
do |
| 834 |
{ |
| 835 |
codep = codep->next; |
| 836 |
} while (--residue > occ); |
| 837 |
tp = op + occ; |
| 838 |
do |
| 839 |
{ |
| 840 |
*--tp = codep->value; |
| 841 |
codep = codep->next; |
| 842 |
} while (--occ); |
| 843 |
return (1); |
| 844 |
} |
| 845 |
/* |
| 846 |
* Residue satisfies only part of the decode request. |
| 847 |
*/ |
| 848 |
op += residue; |
| 849 |
occ -= residue; |
| 850 |
tp = op; |
| 851 |
do |
| 852 |
{ |
| 853 |
*--tp = codep->value; |
| 854 |
codep = codep->next; |
| 855 |
} while (--residue); |
| 856 |
sp->dec_restart = 0; |
| 857 |
} |
| 858 |
|
| 859 |
bp = (uint8_t *)tif->tif_rawcp; |
| 860 |
|
| 861 |
sp->dec_bitsleft += (((uint64_t)tif->tif_rawcc - sp->old_tif_rawcc) << 3); |
| 862 |
uint64_t dec_bitsleft = sp->dec_bitsleft; |
| 863 |
|
| 864 |
nbits = sp->lzw_nbits; |
| 865 |
nextdata = sp->lzw_nextdata; |
| 866 |
nextbits = sp->lzw_nextbits; |
| 867 |
nbitsmask = sp->dec_nbitsmask; |
| 868 |
oldcodep = sp->dec_oldcodep; |
| 869 |
free_entp = sp->dec_free_entp; |
| 870 |
maxcodep = sp->dec_maxcodep; |
| 871 |
|
| 872 |
while (occ > 0) |
| 873 |
{ |
| 874 |
NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft); |
| 875 |
if (code == CODE_EOI) |
| 876 |
break; |
| 877 |
if (code == CODE_CLEAR) |
| 878 |
{ |
| 879 |
do |
| 880 |
{ |
| 881 |
free_entp = sp->dec_codetab + CODE_FIRST; |
| 882 |
_TIFFmemset(free_entp, 0, |
| 883 |
(CSIZE - CODE_FIRST) * sizeof(code_t)); |
| 884 |
nbits = BITS_MIN; |
| 885 |
nbitsmask = MAXCODE(BITS_MIN); |
| 886 |
maxcodep = sp->dec_codetab + nbitsmask; |
| 887 |
NextCode(tif, sp, bp, code, GetNextCodeCompat, dec_bitsleft); |
| 888 |
} while (code == CODE_CLEAR); /* consecutive CODE_CLEAR codes */ |
| 889 |
if (code == CODE_EOI) |
| 890 |
break; |
| 891 |
if (code > CODE_CLEAR) |
| 892 |
{ |
| 893 |
TIFFErrorExtR( |
| 894 |
tif, tif->tif_name, |
| 895 |
"LZWDecode: Corrupted LZW table at scanline %" PRIu32, |
| 896 |
tif->tif_row); |
| 897 |
return (0); |
| 898 |
} |
| 899 |
*op++ = (uint8_t)code; |
| 900 |
occ--; |
| 901 |
oldcodep = sp->dec_codetab + code; |
| 902 |
continue; |
| 903 |
} |
| 904 |
codep = sp->dec_codetab + code; |
| 905 |
|
| 906 |
/* |
| 907 |
* Add the new entry to the code table. |
| 908 |
*/ |
| 909 |
if (free_entp < &sp->dec_codetab[0] || |
| 910 |
free_entp >= &sp->dec_codetab[CSIZE]) |
| 911 |
{ |
| 912 |
TIFFErrorExtR(tif, module, |
| 913 |
"Corrupted LZW table at scanline %" PRIu32, |
| 914 |
tif->tif_row); |
| 915 |
return (0); |
| 916 |
} |
| 917 |
|
| 918 |
free_entp->next = oldcodep; |
| 919 |
if (free_entp->next < &sp->dec_codetab[0] || |
| 920 |
free_entp->next >= &sp->dec_codetab[CSIZE]) |
| 921 |
{ |
| 922 |
TIFFErrorExtR(tif, module, |
| 923 |
"Corrupted LZW table at scanline %" PRIu32, |
| 924 |
tif->tif_row); |
| 925 |
return (0); |
| 926 |
} |
| 927 |
free_entp->firstchar = free_entp->next->firstchar; |
| 928 |
free_entp->length = free_entp->next->length + 1; |
| 929 |
free_entp->value = |
| 930 |
(codep < free_entp) ? codep->firstchar : free_entp->firstchar; |
| 931 |
if (++free_entp > maxcodep) |
| 932 |
{ |
| 933 |
if (++nbits > BITS_MAX) /* should not happen */ |
| 934 |
nbits = BITS_MAX; |
| 935 |
nbitsmask = MAXCODE(nbits); |
| 936 |
maxcodep = sp->dec_codetab + nbitsmask; |
| 937 |
} |
| 938 |
oldcodep = codep; |
| 939 |
if (code >= 256) |
| 940 |
{ |
| 941 |
/* |
| 942 |
* Code maps to a string, copy string |
| 943 |
* value to output (written in reverse). |
| 944 |
*/ |
| 945 |
if (codep->length == 0) |
| 946 |
{ |
| 947 |
TIFFErrorExtR( |
| 948 |
tif, module, |
| 949 |
"Wrong length of decoded " |
| 950 |
"string: data probably corrupted at scanline %" PRIu32, |
| 951 |
tif->tif_row); |
| 952 |
return (0); |
| 953 |
} |
| 954 |
if (codep->length > occ) |
| 955 |
{ |
| 956 |
/* |
| 957 |
* String is too long for decode buffer, |
| 958 |
* locate portion that will fit, copy to |
| 959 |
* the decode buffer, and setup restart |
| 960 |
* logic for the next decoding call. |
| 961 |
*/ |
| 962 |
sp->dec_codep = codep; |
| 963 |
do |
| 964 |
{ |
| 965 |
codep = codep->next; |
| 966 |
} while (codep->length > occ); |
| 967 |
sp->dec_restart = occ; |
| 968 |
tp = op + occ; |
| 969 |
do |
| 970 |
{ |
| 971 |
*--tp = codep->value; |
| 972 |
codep = codep->next; |
| 973 |
} while (--occ); |
| 974 |
break; |
| 975 |
} |
| 976 |
len = codep->length; |
| 977 |
tp = op + len; |
| 978 |
do |
| 979 |
{ |
| 980 |
*--tp = codep->value; |
| 981 |
codep = codep->next; |
| 982 |
} while (codep && tp > op); |
| 983 |
assert(occ >= len); |
| 984 |
op += len; |
| 985 |
occ -= len; |
| 986 |
} |
| 987 |
else |
| 988 |
{ |
| 989 |
*op++ = (uint8_t)code; |
| 990 |
occ--; |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
tif->tif_rawcc -= (tmsize_t)((uint8_t *)bp - tif->tif_rawcp); |
| 995 |
tif->tif_rawcp = (uint8_t *)bp; |
| 996 |
|
| 997 |
sp->old_tif_rawcc = tif->tif_rawcc; |
| 998 |
sp->dec_bitsleft = dec_bitsleft; |
| 999 |
|
| 1000 |
sp->lzw_nbits = (unsigned short)nbits; |
| 1001 |
sp->lzw_nextdata = nextdata; |
| 1002 |
sp->lzw_nextbits = nextbits; |
| 1003 |
sp->dec_nbitsmask = nbitsmask; |
| 1004 |
sp->dec_oldcodep = oldcodep; |
| 1005 |
sp->dec_free_entp = free_entp; |
| 1006 |
sp->dec_maxcodep = maxcodep; |
| 1007 |
|
| 1008 |
if (occ > 0) |
| 1009 |
{ |
| 1010 |
TIFFErrorExtR(tif, module, |
| 1011 |
"Not enough data at scanline %" PRIu32 " (short %" PRIu64 |
| 1012 |
" bytes)", |
| 1013 |
tif->tif_row, (uint64_t)occ); |
| 1014 |
return (0); |
| 1015 |
} |
| 1016 |
return (1); |
| 1017 |
} |
| 1018 |
#endif /* LZW_COMPAT */ |
| 1019 |
|
| 1020 |
/* |
| 1021 |
* LZW Encoding. |
| 1022 |
*/ |
| 1023 |
|
| 1024 |
static int LZWSetupEncode(TIFF *tif) |
| 1025 |
{ |
| 1026 |
static const char module[] = "LZWSetupEncode"; |
| 1027 |
LZWCodecState *sp = LZWEncoderState(tif); |
| 1028 |
|
| 1029 |
assert(sp != NULL); |
| 1030 |
sp->enc_hashtab = (hash_t *)_TIFFmallocExt(tif, HSIZE * sizeof(hash_t)); |
| 1031 |
if (sp->enc_hashtab == NULL) |
| 1032 |
{ |
| 1033 |
TIFFErrorExtR(tif, module, "No space for LZW hash table"); |
| 1034 |
return (0); |
| 1035 |
} |
| 1036 |
return (1); |
| 1037 |
} |
| 1038 |
|
| 1039 |
/* |
| 1040 |
* Reset encoding state at the start of a strip. |
| 1041 |
*/ |
| 1042 |
static int LZWPreEncode(TIFF *tif, uint16_t s) |
| 1043 |
{ |
| 1044 |
LZWCodecState *sp = LZWEncoderState(tif); |
| 1045 |
|
| 1046 |
(void)s; |
| 1047 |
assert(sp != NULL); |
| 1048 |
|
| 1049 |
if (sp->enc_hashtab == NULL) |
| 1050 |
{ |
| 1051 |
tif->tif_setupencode(tif); |
| 1052 |
} |
| 1053 |
|
| 1054 |
sp->lzw_nbits = BITS_MIN; |
| 1055 |
sp->lzw_maxcode = MAXCODE(BITS_MIN); |
| 1056 |
sp->lzw_free_ent = CODE_FIRST; |
| 1057 |
sp->lzw_nextbits = 0; |
| 1058 |
sp->lzw_nextdata = 0; |
| 1059 |
sp->enc_checkpoint = CHECK_GAP; |
| 1060 |
sp->enc_ratio = 0; |
| 1061 |
sp->enc_incount = 0; |
| 1062 |
sp->enc_outcount = 0; |
| 1063 |
/* |
| 1064 |
* The 4 here insures there is space for 2 max-sized |
| 1065 |
* codes in LZWEncode and LZWPostDecode. |
| 1066 |
*/ |
| 1067 |
sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize - 1 - 4; |
| 1068 |
cl_hash(sp); /* clear hash table */ |
| 1069 |
sp->enc_oldcode = (hcode_t)-1; /* generates CODE_CLEAR in LZWEncode */ |
| 1070 |
return (1); |
| 1071 |
} |
| 1072 |
|
| 1073 |
#define CALCRATIO(sp, rat) \ |
| 1074 |
{ \ |
| 1075 |
if (incount > 0x007fffff) \ |
| 1076 |
{ /* NB: shift will overflow */ \ |
| 1077 |
rat = outcount >> 8; \ |
| 1078 |
rat = (rat == 0 ? 0x7fffffff : incount / rat); \ |
| 1079 |
} \ |
| 1080 |
else \ |
| 1081 |
rat = (incount << 8) / outcount; \ |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* Explicit 0xff masking to make icc -check=conversions happy */ |
| 1085 |
#define PutNextCode(op, c) \ |
| 1086 |
{ \ |
| 1087 |
nextdata = (nextdata << nbits) | c; \ |
| 1088 |
nextbits += nbits; \ |
| 1089 |
*op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \ |
| 1090 |
nextbits -= 8; \ |
| 1091 |
if (nextbits >= 8) \ |
| 1092 |
{ \ |
| 1093 |
*op++ = (unsigned char)((nextdata >> (nextbits - 8)) & 0xff); \ |
| 1094 |
nextbits -= 8; \ |
| 1095 |
} \ |
| 1096 |
outcount += nbits; \ |
| 1097 |
} |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* Encode a chunk of pixels. |
| 1101 |
* |
| 1102 |
* Uses an open addressing double hashing (no chaining) on the |
| 1103 |
* prefix code/next character combination. We do a variant of |
| 1104 |
* Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's |
| 1105 |
* relatively-prime secondary probe. Here, the modular division |
| 1106 |
* first probe is gives way to a faster exclusive-or manipulation. |
| 1107 |
* Also do block compression with an adaptive reset, whereby the |
| 1108 |
* code table is cleared when the compression ratio decreases, |
| 1109 |
* but after the table fills. The variable-length output codes |
| 1110 |
* are re-sized at this point, and a CODE_CLEAR is generated |
| 1111 |
* for the decoder. |
| 1112 |
*/ |
| 1113 |
static int LZWEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
| 1114 |
{ |
| 1115 |
register LZWCodecState *sp = LZWEncoderState(tif); |
| 1116 |
register long fcode; |
| 1117 |
register hash_t *hp; |
| 1118 |
register int h, c; |
| 1119 |
hcode_t ent; |
| 1120 |
long disp; |
| 1121 |
tmsize_t incount, outcount, checkpoint; |
| 1122 |
WordType nextdata; |
| 1123 |
long nextbits; |
| 1124 |
int free_ent, maxcode, nbits; |
| 1125 |
uint8_t *op; |
| 1126 |
uint8_t *limit; |
| 1127 |
|
| 1128 |
(void)s; |
| 1129 |
if (sp == NULL) |
| 1130 |
return (0); |
| 1131 |
|
| 1132 |
assert(sp->enc_hashtab != NULL); |
| 1133 |
|
| 1134 |
/* |
| 1135 |
* Load local state. |
| 1136 |
*/ |
| 1137 |
incount = sp->enc_incount; |
| 1138 |
outcount = sp->enc_outcount; |
| 1139 |
checkpoint = sp->enc_checkpoint; |
| 1140 |
nextdata = sp->lzw_nextdata; |
| 1141 |
nextbits = sp->lzw_nextbits; |
| 1142 |
free_ent = sp->lzw_free_ent; |
| 1143 |
maxcode = sp->lzw_maxcode; |
| 1144 |
nbits = sp->lzw_nbits; |
| 1145 |
op = tif->tif_rawcp; |
| 1146 |
limit = sp->enc_rawlimit; |
| 1147 |
ent = (hcode_t)sp->enc_oldcode; |
| 1148 |
|
| 1149 |
if (ent == (hcode_t)-1 && cc > 0) |
| 1150 |
{ |
| 1151 |
/* |
| 1152 |
* NB: This is safe because it can only happen |
| 1153 |
* at the start of a strip where we know there |
| 1154 |
* is space in the data buffer. |
| 1155 |
*/ |
| 1156 |
PutNextCode(op, CODE_CLEAR); |
| 1157 |
ent = *bp++; |
| 1158 |
cc--; |
| 1159 |
incount++; |
| 1160 |
} |
| 1161 |
while (cc > 0) |
| 1162 |
{ |
| 1163 |
c = *bp++; |
| 1164 |
cc--; |
| 1165 |
incount++; |
| 1166 |
fcode = ((long)c << BITS_MAX) + ent; |
| 1167 |
h = (c << HSHIFT) ^ ent; /* xor hashing */ |
| 1168 |
#ifdef _WINDOWS |
| 1169 |
/* |
| 1170 |
* Check hash index for an overflow. |
| 1171 |
*/ |
| 1172 |
if (h >= HSIZE) |
| 1173 |
h -= HSIZE; |
| 1174 |
#endif |
| 1175 |
hp = &sp->enc_hashtab[h]; |
| 1176 |
if (hp->hash == fcode) |
| 1177 |
{ |
| 1178 |
ent = hp->code; |
| 1179 |
continue; |
| 1180 |
} |
| 1181 |
if (hp->hash >= 0) |
| 1182 |
{ |
| 1183 |
/* |
| 1184 |
* Primary hash failed, check secondary hash. |
| 1185 |
*/ |
| 1186 |
disp = HSIZE - h; |
| 1187 |
if (h == 0) |
| 1188 |
disp = 1; |
| 1189 |
do |
| 1190 |
{ |
| 1191 |
/* |
| 1192 |
* Avoid pointer arithmetic because of |
| 1193 |
* wraparound problems with segments. |
| 1194 |
*/ |
| 1195 |
if ((h -= disp) < 0) |
| 1196 |
h += HSIZE; |
| 1197 |
hp = &sp->enc_hashtab[h]; |
| 1198 |
if (hp->hash == fcode) |
| 1199 |
{ |
| 1200 |
ent = hp->code; |
| 1201 |
goto hit; |
| 1202 |
} |
| 1203 |
} while (hp->hash >= 0); |
| 1204 |
} |
| 1205 |
/* |
| 1206 |
* New entry, emit code and add to table. |
| 1207 |
*/ |
| 1208 |
/* |
| 1209 |
* Verify there is space in the buffer for the code |
| 1210 |
* and any potential Clear code that might be emitted |
| 1211 |
* below. The value of limit is setup so that there |
| 1212 |
* are at least 4 bytes free--room for 2 codes. |
| 1213 |
*/ |
| 1214 |
if (op > limit) |
| 1215 |
{ |
| 1216 |
tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); |
| 1217 |
if (!TIFFFlushData1(tif)) |
| 1218 |
return 0; |
| 1219 |
op = tif->tif_rawdata; |
| 1220 |
} |
| 1221 |
PutNextCode(op, ent); |
| 1222 |
ent = (hcode_t)c; |
| 1223 |
hp->code = (hcode_t)(free_ent++); |
| 1224 |
hp->hash = fcode; |
| 1225 |
if (free_ent == CODE_MAX - 1) |
| 1226 |
{ |
| 1227 |
/* table is full, emit clear code and reset */ |
| 1228 |
cl_hash(sp); |
| 1229 |
sp->enc_ratio = 0; |
| 1230 |
incount = 0; |
| 1231 |
outcount = 0; |
| 1232 |
free_ent = CODE_FIRST; |
| 1233 |
PutNextCode(op, CODE_CLEAR); |
| 1234 |
nbits = BITS_MIN; |
| 1235 |
maxcode = MAXCODE(BITS_MIN); |
| 1236 |
} |
| 1237 |
else |
| 1238 |
{ |
| 1239 |
/* |
| 1240 |
* If the next entry is going to be too big for |
| 1241 |
* the code size, then increase it, if possible. |
| 1242 |
*/ |
| 1243 |
if (free_ent > maxcode) |
| 1244 |
{ |
| 1245 |
nbits++; |
| 1246 |
assert(nbits <= BITS_MAX); |
| 1247 |
maxcode = (int)MAXCODE(nbits); |
| 1248 |
} |
| 1249 |
else if (incount >= checkpoint) |
| 1250 |
{ |
| 1251 |
tmsize_t rat; |
| 1252 |
/* |
| 1253 |
* Check compression ratio and, if things seem |
| 1254 |
* to be slipping, clear the hash table and |
| 1255 |
* reset state. The compression ratio is a |
| 1256 |
* 24+8-bit fractional number. |
| 1257 |
*/ |
| 1258 |
checkpoint = incount + CHECK_GAP; |
| 1259 |
CALCRATIO(sp, rat); |
| 1260 |
if (rat <= sp->enc_ratio) |
| 1261 |
{ |
| 1262 |
cl_hash(sp); |
| 1263 |
sp->enc_ratio = 0; |
| 1264 |
incount = 0; |
| 1265 |
outcount = 0; |
| 1266 |
free_ent = CODE_FIRST; |
| 1267 |
PutNextCode(op, CODE_CLEAR); |
| 1268 |
nbits = BITS_MIN; |
| 1269 |
maxcode = MAXCODE(BITS_MIN); |
| 1270 |
} |
| 1271 |
else |
| 1272 |
sp->enc_ratio = rat; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
hit:; |
| 1276 |
} |
| 1277 |
|
| 1278 |
/* |
| 1279 |
* Restore global state. |
| 1280 |
*/ |
| 1281 |
sp->enc_incount = incount; |
| 1282 |
sp->enc_outcount = outcount; |
| 1283 |
sp->enc_checkpoint = checkpoint; |
| 1284 |
sp->enc_oldcode = ent; |
| 1285 |
sp->lzw_nextdata = nextdata; |
| 1286 |
sp->lzw_nextbits = nextbits; |
| 1287 |
sp->lzw_free_ent = (unsigned short)free_ent; |
| 1288 |
sp->lzw_maxcode = (unsigned short)maxcode; |
| 1289 |
sp->lzw_nbits = (unsigned short)nbits; |
| 1290 |
tif->tif_rawcp = op; |
| 1291 |
return (1); |
| 1292 |
} |
| 1293 |
|
| 1294 |
/* |
| 1295 |
* Finish off an encoded strip by flushing the last |
| 1296 |
* string and tacking on an End Of Information code. |
| 1297 |
*/ |
| 1298 |
static int LZWPostEncode(TIFF *tif) |
| 1299 |
{ |
| 1300 |
register LZWCodecState *sp = LZWEncoderState(tif); |
| 1301 |
uint8_t *op = tif->tif_rawcp; |
| 1302 |
long nextbits = sp->lzw_nextbits; |
| 1303 |
WordType nextdata = sp->lzw_nextdata; |
| 1304 |
tmsize_t outcount = sp->enc_outcount; |
| 1305 |
int nbits = sp->lzw_nbits; |
| 1306 |
|
| 1307 |
if (op > sp->enc_rawlimit) |
| 1308 |
{ |
| 1309 |
tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); |
| 1310 |
if (!TIFFFlushData1(tif)) |
| 1311 |
return 0; |
| 1312 |
op = tif->tif_rawdata; |
| 1313 |
} |
| 1314 |
if (sp->enc_oldcode != (hcode_t)-1) |
| 1315 |
{ |
| 1316 |
int free_ent = sp->lzw_free_ent; |
| 1317 |
|
| 1318 |
PutNextCode(op, sp->enc_oldcode); |
| 1319 |
sp->enc_oldcode = (hcode_t)-1; |
| 1320 |
free_ent++; |
| 1321 |
|
| 1322 |
if (free_ent == CODE_MAX - 1) |
| 1323 |
{ |
| 1324 |
/* table is full, emit clear code and reset */ |
| 1325 |
outcount = 0; |
| 1326 |
PutNextCode(op, CODE_CLEAR); |
| 1327 |
nbits = BITS_MIN; |
| 1328 |
} |
| 1329 |
else |
| 1330 |
{ |
| 1331 |
/* |
| 1332 |
* If the next entry is going to be too big for |
| 1333 |
* the code size, then increase it, if possible. |
| 1334 |
*/ |
| 1335 |
if (free_ent > sp->lzw_maxcode) |
| 1336 |
{ |
| 1337 |
nbits++; |
| 1338 |
assert(nbits <= BITS_MAX); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
} |
| 1342 |
PutNextCode(op, CODE_EOI); |
| 1343 |
/* Explicit 0xff masking to make icc -check=conversions happy */ |
| 1344 |
if (nextbits > 0) |
| 1345 |
*op++ = (unsigned char)((nextdata << (8 - nextbits)) & 0xff); |
| 1346 |
tif->tif_rawcc = (tmsize_t)(op - tif->tif_rawdata); |
| 1347 |
(void)outcount; |
| 1348 |
return (1); |
| 1349 |
} |
| 1350 |
|
| 1351 |
/* |
| 1352 |
* Reset encoding hash table. |
| 1353 |
*/ |
| 1354 |
static void cl_hash(LZWCodecState *sp) |
| 1355 |
{ |
| 1356 |
register hash_t *hp = &sp->enc_hashtab[HSIZE - 1]; |
| 1357 |
register long i = HSIZE - 8; |
| 1358 |
|
| 1359 |
do |
| 1360 |
{ |
| 1361 |
i -= 8; |
| 1362 |
hp[-7].hash = -1; |
| 1363 |
hp[-6].hash = -1; |
| 1364 |
hp[-5].hash = -1; |
| 1365 |
hp[-4].hash = -1; |
| 1366 |
hp[-3].hash = -1; |
| 1367 |
hp[-2].hash = -1; |
| 1368 |
hp[-1].hash = -1; |
| 1369 |
hp[0].hash = -1; |
| 1370 |
hp -= 8; |
| 1371 |
} while (i >= 0); |
| 1372 |
for (i += 8; i > 0; i--, hp--) |
| 1373 |
hp->hash = -1; |
| 1374 |
} |
| 1375 |
|
| 1376 |
static void LZWCleanup(TIFF *tif) |
| 1377 |
{ |
| 1378 |
(void)TIFFPredictorCleanup(tif); |
| 1379 |
|
| 1380 |
assert(tif->tif_data != 0); |
| 1381 |
|
| 1382 |
if (LZWDecoderState(tif)->dec_codetab) |
| 1383 |
_TIFFfreeExt(tif, LZWDecoderState(tif)->dec_codetab); |
| 1384 |
|
| 1385 |
if (LZWEncoderState(tif)->enc_hashtab) |
| 1386 |
_TIFFfreeExt(tif, LZWEncoderState(tif)->enc_hashtab); |
| 1387 |
|
| 1388 |
_TIFFfreeExt(tif, tif->tif_data); |
| 1389 |
tif->tif_data = NULL; |
| 1390 |
|
| 1391 |
_TIFFSetDefaultCompressionState(tif); |
| 1392 |
} |
| 1393 |
|
| 1394 |
int TIFFInitLZW(TIFF *tif, int scheme) |
| 1395 |
{ |
| 1396 |
static const char module[] = "TIFFInitLZW"; |
| 1397 |
(void)scheme; |
| 1398 |
assert(scheme == COMPRESSION_LZW); |
| 1399 |
/* |
| 1400 |
* Allocate state block so tag methods have storage to record values. |
| 1401 |
*/ |
| 1402 |
tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LZWCodecState)); |
| 1403 |
if (tif->tif_data == NULL) |
| 1404 |
goto bad; |
| 1405 |
LZWDecoderState(tif)->dec_codetab = NULL; |
| 1406 |
LZWDecoderState(tif)->dec_decode = NULL; |
| 1407 |
LZWEncoderState(tif)->enc_hashtab = NULL; |
| 1408 |
LZWState(tif)->rw_mode = tif->tif_mode; |
| 1409 |
|
| 1410 |
/* |
| 1411 |
* Install codec methods. |
| 1412 |
*/ |
| 1413 |
tif->tif_fixuptags = LZWFixupTags; |
| 1414 |
tif->tif_setupdecode = LZWSetupDecode; |
| 1415 |
tif->tif_predecode = LZWPreDecode; |
| 1416 |
tif->tif_decoderow = LZWDecode; |
| 1417 |
tif->tif_decodestrip = LZWDecode; |
| 1418 |
tif->tif_decodetile = LZWDecode; |
| 1419 |
tif->tif_setupencode = LZWSetupEncode; |
| 1420 |
tif->tif_preencode = LZWPreEncode; |
| 1421 |
tif->tif_postencode = LZWPostEncode; |
| 1422 |
tif->tif_encoderow = LZWEncode; |
| 1423 |
tif->tif_encodestrip = LZWEncode; |
| 1424 |
tif->tif_encodetile = LZWEncode; |
| 1425 |
tif->tif_cleanup = LZWCleanup; |
| 1426 |
/* |
| 1427 |
* Setup predictor setup. |
| 1428 |
*/ |
| 1429 |
(void)TIFFPredictorInit(tif); |
| 1430 |
return (1); |
| 1431 |
bad: |
| 1432 |
TIFFErrorExtR(tif, module, "No space for LZW state block"); |
| 1433 |
return (0); |
| 1434 |
} |
| 1435 |
|
| 1436 |
/* |
| 1437 |
* Copyright (c) 1985, 1986 The Regents of the University of California. |
| 1438 |
* All rights reserved. |
| 1439 |
* |
| 1440 |
* This code is derived from software contributed to Berkeley by |
| 1441 |
* James A. Woods, derived from original work by Spencer Thomas |
| 1442 |
* and Joseph Orost. |
| 1443 |
* |
| 1444 |
* Redistribution and use in source and binary forms are permitted |
| 1445 |
* provided that the above copyright notice and this paragraph are |
| 1446 |
* duplicated in all such forms and that any documentation, |
| 1447 |
* advertising materials, and other materials related to such |
| 1448 |
* distribution and use acknowledge that the software was developed |
| 1449 |
* by the University of California, Berkeley. The name of the |
| 1450 |
* University may not be used to endorse or promote products derived |
| 1451 |
* from this software without specific prior written permission. |
| 1452 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 1453 |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 1454 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 1455 |
*/ |
| 1456 |
#endif /* LZW_SUPPORT */ |