Coverage Report

Created: 2026-09-14 07:37

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