Coverage Report

Created: 2025-11-16 07:22

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
26.0k
#define LSTATE_INIT_DECODE 0x01
51
30.6k
#define LSTATE_INIT_ENCODE 0x02
52
53
    TIFFVGetMethod vgetparent; /* super-class method */
54
    TIFFVSetMethod vsetparent; /* super-class method */
55
} ZSTDState;
56
57
11.2M
#define GetZSTDState(tif) ((ZSTDState *)(tif)->tif_data)
58
1.45M
#define ZSTDDecoderState(tif) GetZSTDState(tif)
59
9.40M
#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
10.2k
{
66
10.2k
    (void)tif;
67
10.2k
    return 1;
68
10.2k
}
69
70
static int ZSTDSetupDecode(TIFF *tif)
71
10.1k
{
72
10.1k
    ZSTDState *sp = ZSTDDecoderState(tif);
73
74
10.1k
    assert(sp != NULL);
75
76
    /* if we were last encoding, terminate this mode */
77
10.1k
    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
10.1k
    sp->state |= LSTATE_INIT_DECODE;
85
10.1k
    return 1;
86
10.1k
}
87
88
/*
89
 * Setup state for decoding a strip.
90
 */
91
static int ZSTDPreDecode(TIFF *tif, uint16_t s)
92
10.3k
{
93
10.3k
    static const char module[] = "ZSTDPreDecode";
94
10.3k
    ZSTDState *sp = ZSTDDecoderState(tif);
95
10.3k
    size_t zstd_ret;
96
97
10.3k
    (void)s;
98
10.3k
    assert(sp != NULL);
99
100
10.3k
    if ((sp->state & LSTATE_INIT_DECODE) == 0)
101
0
        tif->tif_setupdecode(tif);
102
103
10.3k
    if (sp->dstream == NULL)
104
10.1k
    {
105
10.1k
        sp->dstream = ZSTD_createDStream();
106
10.1k
        if (sp->dstream == NULL)
107
0
        {
108
0
            TIFFErrorExtR(tif, module, "Cannot allocate decompression stream");
109
0
            return 0;
110
0
        }
111
10.1k
    }
112
113
10.3k
    zstd_ret = ZSTD_initDStream(sp->dstream);
114
10.3k
    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
10.3k
    return 1;
122
10.3k
}
123
124
static int ZSTDDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
125
1.43M
{
126
1.43M
    static const char module[] = "ZSTDDecode";
127
1.43M
    ZSTDState *sp = ZSTDDecoderState(tif);
128
1.43M
    ZSTD_inBuffer in_buffer;
129
1.43M
    ZSTD_outBuffer out_buffer;
130
1.43M
    size_t zstd_ret;
131
132
1.43M
    (void)s;
133
1.43M
    assert(sp != NULL);
134
1.43M
    assert(sp->state == LSTATE_INIT_DECODE);
135
136
1.43M
    in_buffer.src = tif->tif_rawcp;
137
1.43M
    in_buffer.size = (size_t)tif->tif_rawcc;
138
1.43M
    in_buffer.pos = 0;
139
140
1.43M
    out_buffer.dst = op;
141
1.43M
    out_buffer.size = (size_t)occ;
142
1.43M
    out_buffer.pos = 0;
143
144
1.43M
    do
145
1.43M
    {
146
1.43M
        zstd_ret = ZSTD_decompressStream(sp->dstream, &out_buffer, &in_buffer);
147
1.43M
        if (ZSTD_isError(zstd_ret))
148
6.78k
        {
149
6.78k
            memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
150
6.78k
            TIFFErrorExtR(tif, module, "Error in ZSTD_decompressStream(): %s",
151
6.78k
                          ZSTD_getErrorName(zstd_ret));
152
6.78k
            return 0;
153
6.78k
        }
154
1.43M
    } while (zstd_ret != 0 && in_buffer.pos < in_buffer.size &&
155
1.40M
             out_buffer.pos < out_buffer.size);
156
157
1.42M
    if (out_buffer.pos < (size_t)occ)
158
541
    {
159
541
        memset(op + out_buffer.pos, 0, out_buffer.size - out_buffer.pos);
160
541
        TIFFErrorExtR(tif, module,
161
541
                      "Not enough data at scanline %lu (short %lu bytes)",
162
541
                      (unsigned long)tif->tif_row,
163
541
                      (unsigned long)((size_t)occ - out_buffer.pos));
164
541
        return 0;
165
541
    }
166
167
1.42M
    tif->tif_rawcp += in_buffer.pos;
168
1.42M
    tif->tif_rawcc -= in_buffer.pos;
169
170
1.42M
    return 1;
171
1.42M
}
172
173
static int ZSTDSetupEncode(TIFF *tif)
174
5.54k
{
175
5.54k
    ZSTDState *sp = ZSTDEncoderState(tif);
176
177
5.54k
    assert(sp != NULL);
178
5.54k
    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
5.54k
    sp->state |= LSTATE_INIT_ENCODE;
186
5.54k
    return 1;
187
5.54k
}
188
189
/*
190
 * Reset encoding state at the start of a strip.
191
 */
192
static int ZSTDPreEncode(TIFF *tif, uint16_t s)
193
14.9k
{
194
14.9k
    static const char module[] = "ZSTDPreEncode";
195
14.9k
    ZSTDState *sp = ZSTDEncoderState(tif);
196
14.9k
    size_t zstd_ret;
197
198
14.9k
    (void)s;
199
14.9k
    assert(sp != NULL);
200
14.9k
    if (sp->state != LSTATE_INIT_ENCODE)
201
0
        tif->tif_setupencode(tif);
202
203
14.9k
    if (sp->cstream == NULL)
204
5.54k
    {
205
5.54k
        sp->cstream = ZSTD_createCStream();
206
5.54k
        if (sp->cstream == NULL)
207
0
        {
208
0
            TIFFErrorExtR(tif, module, "Cannot allocate compression stream");
209
0
            return 0;
210
0
        }
211
5.54k
    }
212
213
14.9k
    zstd_ret = ZSTD_initCStream(sp->cstream, sp->compression_level);
214
14.9k
    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
14.9k
    sp->out_buffer.dst = tif->tif_rawdata;
222
14.9k
    sp->out_buffer.size = (size_t)tif->tif_rawdatasize;
223
14.9k
    sp->out_buffer.pos = 0;
224
225
14.9k
    return 1;
226
14.9k
}
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
9.37M
{
233
9.37M
    static const char module[] = "ZSTDEncode";
234
9.37M
    ZSTDState *sp = ZSTDEncoderState(tif);
235
9.37M
    ZSTD_inBuffer in_buffer;
236
9.37M
    size_t zstd_ret;
237
238
9.37M
    assert(sp != NULL);
239
9.37M
    assert(sp->state == LSTATE_INIT_ENCODE);
240
241
9.37M
    (void)s;
242
243
9.37M
    in_buffer.src = bp;
244
9.37M
    in_buffer.size = (size_t)cc;
245
9.37M
    in_buffer.pos = 0;
246
247
9.37M
    do
248
9.37M
    {
249
9.37M
        zstd_ret =
250
9.37M
            ZSTD_compressStream(sp->cstream, &sp->out_buffer, &in_buffer);
251
9.37M
        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
9.37M
        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
9.37M
    } while (in_buffer.pos < in_buffer.size);
266
267
9.37M
    return 1;
268
9.37M
}
269
270
/*
271
 * Finish off an encoded strip by flushing it.
272
 */
273
static int ZSTDPostEncode(TIFF *tif)
274
14.9k
{
275
14.9k
    static const char module[] = "ZSTDPostEncode";
276
14.9k
    ZSTDState *sp = ZSTDEncoderState(tif);
277
14.9k
    size_t zstd_ret;
278
279
14.9k
    do
280
14.9k
    {
281
14.9k
        zstd_ret = ZSTD_endStream(sp->cstream, &sp->out_buffer);
282
14.9k
        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
14.9k
        if (sp->out_buffer.pos > 0)
289
14.9k
        {
290
14.9k
            tif->tif_rawcc = sp->out_buffer.pos;
291
14.9k
            if (!TIFFFlushData1(tif))
292
5
                return 0;
293
14.9k
            sp->out_buffer.dst = tif->tif_rawcp;
294
14.9k
            sp->out_buffer.pos = 0;
295
14.9k
        }
296
14.9k
    } while (zstd_ret != 0);
297
14.9k
    return 1;
298
14.9k
}
299
300
static void ZSTDCleanup(TIFF *tif)
301
16.0k
{
302
16.0k
    ZSTDState *sp = GetZSTDState(tif);
303
304
16.0k
    assert(sp != 0);
305
306
16.0k
    (void)TIFFPredictorCleanup(tif);
307
308
16.0k
    tif->tif_tagmethods.vgetfield = sp->vgetparent;
309
16.0k
    tif->tif_tagmethods.vsetfield = sp->vsetparent;
310
311
16.0k
    if (sp->dstream)
312
10.1k
    {
313
10.1k
        ZSTD_freeDStream(sp->dstream);
314
10.1k
        sp->dstream = NULL;
315
10.1k
    }
316
16.0k
    if (sp->cstream)
317
5.54k
    {
318
5.54k
        ZSTD_freeCStream(sp->cstream);
319
5.54k
        sp->cstream = NULL;
320
5.54k
    }
321
16.0k
    _TIFFfreeExt(tif, sp);
322
16.0k
    tif->tif_data = NULL;
323
324
16.0k
    _TIFFSetDefaultCompressionState(tif);
325
16.0k
}
326
327
static int ZSTDVSetField(TIFF *tif, uint32_t tag, va_list ap)
328
107k
{
329
107k
    static const char module[] = "ZSTDVSetField";
330
107k
    ZSTDState *sp = GetZSTDState(tif);
331
332
107k
    switch (tag)
333
107k
    {
334
5.54k
        case TIFFTAG_ZSTD_LEVEL:
335
5.54k
            sp->compression_level = (int)va_arg(ap, int);
336
5.54k
            if (sp->compression_level <= 0 ||
337
5.54k
                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
5.54k
            return 1;
344
102k
        default:
345
102k
            return (*sp->vsetparent)(tif, tag, ap);
346
107k
    }
347
    /*NOTREACHED*/
348
107k
}
349
350
static int ZSTDVGetField(TIFF *tif, uint32_t tag, va_list ap)
351
211k
{
352
211k
    ZSTDState *sp = GetZSTDState(tif);
353
354
211k
    switch (tag)
355
211k
    {
356
0
        case TIFFTAG_ZSTD_LEVEL:
357
0
            *va_arg(ap, int *) = sp->compression_level;
358
0
            break;
359
211k
        default:
360
211k
            return (*sp->vgetparent)(tif, tag, ap);
361
211k
    }
362
0
    return 1;
363
211k
}
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
int TIFFInitZSTD(TIFF *tif, int scheme)
371
16.0k
{
372
16.0k
    static const char module[] = "TIFFInitZSTD";
373
16.0k
    ZSTDState *sp;
374
375
16.0k
    (void)scheme;
376
16.0k
    assert(scheme == COMPRESSION_ZSTD);
377
378
    /*
379
     * Merge codec-specific tag information.
380
     */
381
16.0k
    if (!_TIFFMergeFields(tif, ZSTDFields, TIFFArrayCount(ZSTDFields)))
382
0
    {
383
0
        TIFFErrorExtR(tif, module, "Merging ZSTD codec-specific tags failed");
384
0
        return 0;
385
0
    }
386
387
    /*
388
     * Allocate state block so tag methods have storage to record values.
389
     */
390
16.0k
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(ZSTDState));
391
16.0k
    if (tif->tif_data == NULL)
392
0
        goto bad;
393
16.0k
    sp = GetZSTDState(tif);
394
395
    /*
396
     * Override parent get/set field methods.
397
     */
398
16.0k
    sp->vgetparent = tif->tif_tagmethods.vgetfield;
399
16.0k
    tif->tif_tagmethods.vgetfield = ZSTDVGetField; /* hook for codec tags */
400
16.0k
    sp->vsetparent = tif->tif_tagmethods.vsetfield;
401
16.0k
    tif->tif_tagmethods.vsetfield = ZSTDVSetField; /* hook for codec tags */
402
403
    /* Default values for codec-specific fields */
404
16.0k
    sp->compression_level = 9; /* default comp. level */
405
16.0k
    sp->state = 0;
406
16.0k
    sp->dstream = 0;
407
16.0k
    sp->cstream = 0;
408
16.0k
    sp->out_buffer.dst = NULL;
409
16.0k
    sp->out_buffer.size = 0;
410
16.0k
    sp->out_buffer.pos = 0;
411
412
    /*
413
     * Install codec methods.
414
     */
415
16.0k
    tif->tif_fixuptags = ZSTDFixupTags;
416
16.0k
    tif->tif_setupdecode = ZSTDSetupDecode;
417
16.0k
    tif->tif_predecode = ZSTDPreDecode;
418
16.0k
    tif->tif_decoderow = ZSTDDecode;
419
16.0k
    tif->tif_decodestrip = ZSTDDecode;
420
16.0k
    tif->tif_decodetile = ZSTDDecode;
421
16.0k
    tif->tif_setupencode = ZSTDSetupEncode;
422
16.0k
    tif->tif_preencode = ZSTDPreEncode;
423
16.0k
    tif->tif_postencode = ZSTDPostEncode;
424
16.0k
    tif->tif_encoderow = ZSTDEncode;
425
16.0k
    tif->tif_encodestrip = ZSTDEncode;
426
16.0k
    tif->tif_encodetile = ZSTDEncode;
427
16.0k
    tif->tif_cleanup = ZSTDCleanup;
428
    /*
429
     * Setup predictor setup.
430
     */
431
16.0k
    (void)TIFFPredictorInit(tif);
432
16.0k
    return 1;
433
0
bad:
434
0
    TIFFErrorExtR(tif, module, "No space for ZSTD state block");
435
0
    return 0;
436
16.0k
}
437
#endif /* ZSTD_SUPPORT */