| 1 |
/* |
| 2 |
* Copyright (c) 2017, Planet Labs |
| 3 |
* Author: <even.rouault at spatialys.com> |
| 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 ZSTD_SUPPORT |
| 27 |
/* |
| 28 |
* TIFF Library. |
| 29 |
* |
| 30 |
* ZSTD Compression Support |
| 31 |
* |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "tif_predict.h" |
| 35 |
#include "zstd.h" |
| 36 |
|
| 37 |
#include <stdio.h> |
| 38 |
|
| 39 |
/* |
| 40 |
* State block for each open TIFF file using ZSTD compression/decompression. |
| 41 |
*/ |
| 42 |
typedef struct |
| 43 |
{ |
| 44 |
TIFFPredictorState predict; |
| 45 |
ZSTD_DStream *dstream; |
| 46 |
ZSTD_CStream *cstream; |
| 47 |
int compression_level; /* compression level */ |
| 48 |
ZSTD_outBuffer out_buffer; |
| 49 |
int state; /* state flags */ |
| 50 |
#define LSTATE_INIT_DECODE 0x01 |
| 51 |
#define LSTATE_INIT_ENCODE 0x02 |
| 52 |
|
| 53 |
TIFFVGetMethod vgetparent; /* super-class method */ |
| 54 |
TIFFVSetMethod vsetparent; /* super-class method */ |
| 55 |
} ZSTDState; |
| 56 |
|
| 57 |
#define GetZSTDState(tif) ((ZSTDState *)(tif)->tif_data) |
| 58 |
#define ZSTDDecoderState(tif) GetZSTDState(tif) |
| 59 |
#define ZSTDEncoderState(tif) GetZSTDState(tif) |
| 60 |
|
| 61 |
static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s); |
| 62 |
static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s); |
| 63 |
|
| 64 |
static int ZSTDFixupTags(TIFF *tif) |
| 65 |
{ |
| 66 |
(void)tif; |
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
static int ZSTDSetupDecode(TIFF *tif) |
| 71 |
{ |
| 72 |
ZSTDState *sp = ZSTDDecoderState(tif); |
| 73 |
|
| 74 |
assert(sp != NULL); |
| 75 |
|
| 76 |
/* if we were last encoding, terminate this mode */ |
| 77 |
if (sp->state & LSTATE_INIT_ENCODE) |
| 78 |
{ |
| 79 |
ZSTD_freeCStream(sp->cstream); |
| 80 |
sp->cstream = NULL; |
| 81 |
sp->state = 0; |
| 82 |
} |
| 83 |
|
| 84 |
sp->state |= LSTATE_INIT_DECODE; |
| 85 |
return 1; |
| 86 |
} |
| 87 |
|
| 88 |
/* |
| 89 |
* Setup state for decoding a strip. |
| 90 |
*/ |
| 91 |
static int ZSTDPreDecode(TIFF *tif, uint16_t s) |
| 92 |
{ |
| 93 |
static const char module[] = "ZSTDPreDecode"; |
| 94 |
ZSTDState *sp = ZSTDDecoderState(tif); |
| 95 |
size_t zstd_ret; |
| 96 |
|
| 97 |
(void)s; |
| 98 |
assert(sp != NULL); |
| 99 |
|
| 100 |
if ((sp->state & LSTATE_INIT_DECODE) == 0) |
| 101 |
tif->tif_setupdecode(tif); |
| 102 |
|
| 103 |
if (sp->dstream == NULL) |
| 104 |
{ |
| 105 |
sp->dstream = ZSTD_createDStream(); |
| 106 |
if (sp->dstream == NULL) |
| 107 |
{ |
| 108 |
TIFFErrorExtR(tif, module, "Cannot allocate decompression stream"); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
zstd_ret = ZSTD_initDStream(sp->dstream); |
| 114 |
if (ZSTD_isError(zstd_ret)) |
| 115 |
{ |
| 116 |
TIFFErrorExtR(tif, module, "Error in ZSTD_initDStream(): %s", |
| 117 |
ZSTD_getErrorName(zstd_ret)); |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s) |
| 125 |
{ |
| 126 |
static const char module[] = "ZSTDDecode"; |
| 127 |
ZSTDState *sp = ZSTDDecoderState(tif); |
| 128 |
ZSTD_inBuffer in_buffer; |
| 129 |
ZSTD_outBuffer out_buffer; |
| 130 |
size_t zstd_ret; |
| 131 |
|
| 132 |
(void)s; |
| 133 |
assert(sp != NULL); |
| 134 |
assert(sp->state == LSTATE_INIT_DECODE); |
| 135 |
|
| 136 |
in_buffer.src = tif->tif_rawcp; |
| 137 |
in_buffer.size = (size_t)tif->tif_rawcc; |
| 138 |
in_buffer.pos = 0; |
| 139 |
|
| 140 |
out_buffer.dst = op; |
| 141 |
out_buffer.size = (size_t)occ; |
| 142 |
out_buffer.pos = 0; |
| 143 |
|
| 144 |
do |
| 145 |
{ |
| 146 |
zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer, &in_buffer); |
| 147 |
if (ZSTD_isError(zstd_ret)) |
| 148 |
{ |
| 149 |
memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos); |
| 150 |
TIFFErrorExtR(tif, module, "Error in ZSTD_decompressStream(): %s", |
| 151 |
ZSTD_getErrorName(zstd_ret)); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
} while (zstd_ret != 0 && in_buffer.pos < in_buffer.size && |
| 155 |
out_buffer.pos < out_buffer.size); |
| 156 |
|
| 157 |
if (out_buffer.pos < (size_t)occ) |
| 158 |
{ |
| 159 |
memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos); |
| 160 |
TIFFErrorExtR(tif, module, |
| 161 |
"Not enough data at scanline %lu (short %lu bytes)", |
| 162 |
(unsigned long)tif->tif_row, |
| 163 |
(unsigned long)((size_t)occ - out_buffer.pos)); |
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
tif->tif_rawcp += in_buffer.pos; |
| 168 |
tif->tif_rawcc -= in_buffer.pos; |
| 169 |
|
| 170 |
return 1; |
| 171 |
} |
| 172 |
|
| 173 |
static int ZSTDSetupEncode(TIFF *tif) |
| 174 |
{ |
| 175 |
ZSTDState *sp = ZSTDEncoderState(tif); |
| 176 |
|
| 177 |
assert(sp != NULL); |
| 178 |
if (sp->state & LSTATE_INIT_DECODE) |
| 179 |
{ |
| 180 |
ZSTD_freeDStream(sp->dstream); |
| 181 |
sp->dstream = NULL; |
| 182 |
sp->state = 0; |
| 183 |
} |
| 184 |
|
| 185 |
sp->state |= LSTATE_INIT_ENCODE; |
| 186 |
return 1; |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
* Reset encoding state at the start of a strip. |
| 191 |
*/ |
| 192 |
static int ZSTDPreEncode(TIFF *tif, uint16_t s) |
| 193 |
{ |
| 194 |
static const char module[] = "ZSTDPreEncode"; |
| 195 |
ZSTDState *sp = ZSTDEncoderState(tif); |
| 196 |
size_t zstd_ret; |
| 197 |
|
| 198 |
(void)s; |
| 199 |
assert(sp != NULL); |
| 200 |
if (sp->state != LSTATE_INIT_ENCODE) |
| 201 |
tif->tif_setupencode(tif); |
| 202 |
|
| 203 |
if (sp->cstream == NULL) |
| 204 |
{ |
| 205 |
sp->cstream = ZSTD_createCStream(); |
| 206 |
if (sp->cstream == NULL) |
| 207 |
{ |
| 208 |
TIFFErrorExtR(tif, module, "Cannot allocate compression stream"); |
| 209 |
return 0; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level); |
| 214 |
if (ZSTD_isError(zstd_ret)) |
| 215 |
{ |
| 216 |
TIFFErrorExtR(tif, module, "Error in ZSTD_initCStream(): %s", |
| 217 |
ZSTD_getErrorName(zstd_ret)); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
sp->out_buffer.dst = tif->tif_rawdata; |
| 222 |
sp->out_buffer.size = (size_t)tif->tif_rawdatasize; |
| 223 |
sp->out_buffer.pos = 0; |
| 224 |
|
| 225 |
return 1; |
| 226 |
} |
| 227 |
|
| 228 |
/* |
| 229 |
* Encode a chunk of pixels. |
| 230 |
*/ |
| 231 |
static int ZSTDEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s) |
| 232 |
{ |
| 233 |
static const char module[] = "ZSTDEncode"; |
| 234 |
ZSTDState *sp = ZSTDEncoderState(tif); |
| 235 |
ZSTD_inBuffer in_buffer; |
| 236 |
size_t zstd_ret; |
| 237 |
|
| 238 |
assert(sp != NULL); |
| 239 |
assert(sp->state == LSTATE_INIT_ENCODE); |
| 240 |
|
| 241 |
(void)s; |
| 242 |
|
| 243 |
in_buffer.src = bp; |
| 244 |
in_buffer.size = (size_t)cc; |
| 245 |
in_buffer.pos = 0; |
| 246 |
|
| 247 |
do |
| 248 |
{ |
| 249 |
zstd_ret = |
| 250 |
ZSTD_compressStream(sp->cstream, &sp->out_buffer, &in_buffer); |
| 251 |
if (ZSTD_isError(zstd_ret)) |
| 252 |
{ |
| 253 |
TIFFErrorExtR(tif, module, "Error in ZSTD_compressStream(): %s", |
| 254 |
ZSTD_getErrorName(zstd_ret)); |
| 255 |
return 0; |
| 256 |
} |
| 257 |
if (sp->out_buffer.pos == sp->out_buffer.size) |
| 258 |
{ |
| 259 |
tif->tif_rawcc = tif->tif_rawdatasize; |
| 260 |
if (!TIFFFlushData1(tif)) |
| 261 |
return 0; |
| 262 |
sp->out_buffer.dst = tif->tif_rawcp; |
| 263 |
sp->out_buffer.pos = 0; |
| 264 |
} |
| 265 |
} while (in_buffer.pos < in_buffer.size); |
| 266 |
|
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* Finish off an encoded strip by flushing it. |
| 272 |
*/ |
| 273 |
static int ZSTDPostEncode(TIFF *tif) |
| 274 |
{ |
| 275 |
static const char module[] = "ZSTDPostEncode"; |
| 276 |
ZSTDState *sp = ZSTDEncoderState(tif); |
| 277 |
size_t zstd_ret; |
| 278 |
|
| 279 |
do |
| 280 |
{ |
| 281 |
zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer); |
| 282 |
if (ZSTD_isError(zstd_ret)) |
| 283 |
{ |
| 284 |
TIFFErrorExtR(tif, module, "Error in ZSTD_endStream(): %s", |
| 285 |
ZSTD_getErrorName(zstd_ret)); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
if (sp->out_buffer.pos > 0) |
| 289 |
{ |
| 290 |
tif->tif_rawcc = sp->out_buffer.pos; |
| 291 |
if (!TIFFFlushData1(tif)) |
| 292 |
return 0; |
| 293 |
sp->out_buffer.dst = tif->tif_rawcp; |
| 294 |
sp->out_buffer.pos = 0; |
| 295 |
} |
| 296 |
} while (zstd_ret != 0); |
| 297 |
return 1; |
| 298 |
} |
| 299 |
|
| 300 |
static void ZSTDCleanup(TIFF *tif) |
| 301 |
{ |
| 302 |
ZSTDState *sp = GetZSTDState(tif); |
| 303 |
|
| 304 |
assert(sp != 0); |
| 305 |
|
| 306 |
(void)TIFFPredictorCleanup(tif); |
| 307 |
|
| 308 |
tif->tif_tagmethods.vgetfield = sp->vgetparent; |
| 309 |
tif->tif_tagmethods.vsetfield = sp->vsetparent; |
| 310 |
|
| 311 |
if (sp->dstream) |
| 312 |
{ |
| 313 |
ZSTD_freeDStream(sp->dstream); |
| 314 |
sp->dstream = NULL; |
| 315 |
} |
| 316 |
if (sp->cstream) |
| 317 |
{ |
| 318 |
ZSTD_freeCStream(sp->cstream); |
| 319 |
sp->cstream = NULL; |
| 320 |
} |
| 321 |
_TIFFfreeExt(tif, sp); |
| 322 |
tif->tif_data = NULL; |
| 323 |
|
| 324 |
_TIFFSetDefaultCompressionState(tif); |
| 325 |
} |
| 326 |
|
| 327 |
static int ZSTDVSetField(TIFF *tif, uint32_t tag, va_list ap) |
| 328 |
{ |
| 329 |
static const char module[] = "ZSTDVSetField"; |
| 330 |
ZSTDState *sp = GetZSTDState(tif); |
| 331 |
|
| 332 |
switch (tag) |
| 333 |
{ |
| 334 |
case TIFFTAG_ZSTD_LEVEL: |
| 335 |
sp->compression_level = (int)va_arg(ap, int); |
| 336 |
if (sp->compression_level <= 0 || |
| 337 |
sp->compression_level > ZSTD_maxCLevel()) |
| 338 |
{ |
| 339 |
TIFFWarningExtR(tif, module, |
| 340 |
"ZSTD_LEVEL should be between 1 and %d", |
| 341 |
ZSTD_maxCLevel()); |
| 342 |
} |
| 343 |
return 1; |
| 344 |
default: |
| 345 |
return (*sp->vsetparent)(tif, tag, ap); |
| 346 |
} |
| 347 |
/*NOTREACHED*/ |
| 348 |
} |
| 349 |
|
| 350 |
static int ZSTDVGetField(TIFF *tif, uint32_t tag, va_list ap) |
| 351 |
{ |
| 352 |
ZSTDState *sp = GetZSTDState(tif); |
| 353 |
|
| 354 |
switch (tag) |
| 355 |
{ |
| 356 |
case TIFFTAG_ZSTD_LEVEL: |
| 357 |
*va_arg(ap, int *) = sp->compression_level; |
| 358 |
break; |
| 359 |
default: |
| 360 |
return (*sp->vgetparent)(tif, tag, ap); |
| 361 |
} |
| 362 |
return 1; |
| 363 |
} |
| 364 |
|
| 365 |
static const TIFFField ZSTDFields[] = { |
| 366 |
{TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, |
| 367 |
TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "ZSTD compression_level", |
| 368 |
NULL}, |
| 369 |
}; |
| 370 |
|
| 371 |
int TIFFInitZSTD(TIFF *tif, int scheme) |
| 372 |
{ |
| 373 |
static const char module[] = "TIFFInitZSTD"; |
| 374 |
ZSTDState *sp; |
| 375 |
|
| 376 |
(void)scheme; |
| 377 |
assert(scheme == COMPRESSION_ZSTD); |
| 378 |
|
| 379 |
/* |
| 380 |
* Merge codec-specific tag information. |
| 381 |
*/ |
| 382 |
if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields))) |
| 383 |
{ |
| 384 |
TIFFErrorExtR(tif, module, "Merging ZSTD codec-specific tags failed"); |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
/* |
| 389 |
* Allocate state block so tag methods have storage to record values. |
| 390 |
*/ |
| 391 |
tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(ZSTDState)); |
| 392 |
if (tif->tif_data == NULL) |
| 393 |
goto bad; |
| 394 |
sp = GetZSTDState(tif); |
| 395 |
|
| 396 |
/* |
| 397 |
* Override parent get/set field methods. |
| 398 |
*/ |
| 399 |
sp->vgetparent = tif->tif_tagmethods.vgetfield; |
| 400 |
tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */ |
| 401 |
sp->vsetparent = tif->tif_tagmethods.vsetfield; |
| 402 |
tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */ |
| 403 |
|
| 404 |
/* Default values for codec-specific fields */ |
| 405 |
sp->compression_level = 9; /* default comp. level */ |
| 406 |
sp->state = 0; |
| 407 |
sp->dstream = 0; |
| 408 |
sp->cstream = 0; |
| 409 |
sp->out_buffer.dst = NULL; |
| 410 |
sp->out_buffer.size = 0; |
| 411 |
sp->out_buffer.pos = 0; |
| 412 |
|
| 413 |
/* |
| 414 |
* Install codec methods. |
| 415 |
*/ |
| 416 |
tif->tif_fixuptags = ZSTDFixupTags; |
| 417 |
tif->tif_setupdecode = ZSTDSetupDecode; |
| 418 |
tif->tif_predecode = ZSTDPreDecode; |
| 419 |
tif->tif_decoderow = ZSTDDecode; |
| 420 |
tif->tif_decodestrip = ZSTDDecode; |
| 421 |
tif->tif_decodetile = ZSTDDecode; |
| 422 |
tif->tif_setupencode = ZSTDSetupEncode; |
| 423 |
tif->tif_preencode = ZSTDPreEncode; |
| 424 |
tif->tif_postencode = ZSTDPostEncode; |
| 425 |
tif->tif_encoderow = ZSTDEncode; |
| 426 |
tif->tif_encodestrip = ZSTDEncode; |
| 427 |
tif->tif_encodetile = ZSTDEncode; |
| 428 |
tif->tif_cleanup = ZSTDCleanup; |
| 429 |
/* |
| 430 |
* Setup predictor setup. |
| 431 |
*/ |
| 432 |
(void)TIFFPredictorInit(tif); |
| 433 |
return 1; |
| 434 |
bad: |
| 435 |
TIFFErrorExtR(tif, module, "No space for ZSTD state block"); |
| 436 |
return 0; |
| 437 |
} |
| 438 |
#endif /* ZSTD_SUPPORT */ |