Coverage Report

Created: 2026-03-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/libtiff/tif_compress.c
Line
Count
Source
1
/*
2
 * Copyright (c) 1988-1997 Sam Leffler
3
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4
 *
5
 * Permission to use, copy, modify, distribute, and sell this software and
6
 * its documentation for any purpose is hereby granted without fee, provided
7
 * that (i) the above copyright notices and this permission notice appear in
8
 * all copies of the software and related documentation, and (ii) the names of
9
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10
 * publicity relating to the software without the specific, prior written
11
 * permission of Sam Leffler and Silicon Graphics.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22
 * OF THIS SOFTWARE.
23
 */
24
25
/*
26
 * TIFF Library
27
 *
28
 * Compression Scheme Configuration Support.
29
 */
30
#include "tiffiop.h"
31
32
static int TIFFNoEncode(TIFF *tif, const char *method)
33
0
{
34
0
    const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression);
35
36
0
    if (c)
37
0
    {
38
0
        TIFFErrorExtR(tif, tif->tif_name, "%s %s encoding is not implemented",
39
0
                      c->name, method);
40
0
    }
41
0
    else
42
0
    {
43
0
        TIFFErrorExtR(tif, tif->tif_name,
44
0
                      "Compression scheme %" PRIu16
45
0
                      " %s encoding is not implemented",
46
0
                      tif->tif_dir.td_compression, method);
47
0
    }
48
0
    return (-1);
49
0
}
50
51
int _TIFFNoRowEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
52
0
{
53
0
    (void)pp;
54
0
    (void)cc;
55
0
    (void)s;
56
0
    return (TIFFNoEncode(tif, "scanline"));
57
0
}
58
59
int _TIFFNoStripEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
60
0
{
61
0
    (void)pp;
62
0
    (void)cc;
63
0
    (void)s;
64
0
    return (TIFFNoEncode(tif, "strip"));
65
0
}
66
67
int _TIFFNoTileEncode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
68
0
{
69
0
    (void)pp;
70
0
    (void)cc;
71
0
    (void)s;
72
0
    return (TIFFNoEncode(tif, "tile"));
73
0
}
74
75
static int TIFFNoDecode(TIFF *tif, const char *method)
76
4
{
77
4
    const TIFFCodec *c = TIFFFindCODEC(tif->tif_dir.td_compression);
78
79
4
    if (c)
80
4
        TIFFErrorExtR(tif, tif->tif_name, "%s %s decoding is not implemented",
81
4
                      c->name, method);
82
0
    else
83
0
        TIFFErrorExtR(tif, tif->tif_name,
84
0
                      "Compression scheme %" PRIu16
85
0
                      " %s decoding is not implemented",
86
0
                      tif->tif_dir.td_compression, method);
87
4
    return (0);
88
4
}
89
90
static int _TIFFNoFixupTags(TIFF *tif)
91
3.44k
{
92
3.44k
    (void)tif;
93
3.44k
    return (1);
94
3.44k
}
95
96
int _TIFFNoRowDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
97
0
{
98
0
    (void)pp;
99
0
    (void)cc;
100
0
    (void)s;
101
0
    return (TIFFNoDecode(tif, "scanline"));
102
0
}
103
104
int _TIFFNoStripDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
105
0
{
106
0
    (void)pp;
107
0
    (void)cc;
108
0
    (void)s;
109
0
    return (TIFFNoDecode(tif, "strip"));
110
0
}
111
112
int _TIFFNoTileDecode(TIFF *tif, uint8_t *pp, tmsize_t cc, uint16_t s)
113
4
{
114
4
    (void)pp;
115
4
    (void)cc;
116
4
    (void)s;
117
4
    return (TIFFNoDecode(tif, "tile"));
118
4
}
119
120
int _TIFFNoSeek(TIFF *tif, uint32_t off)
121
0
{
122
0
    (void)off;
123
0
    TIFFErrorExtR(tif, tif->tif_name,
124
0
                  "Compression algorithm does not support random access");
125
0
    return (0);
126
0
}
127
128
int _TIFFNoPreCode(TIFF *tif, uint16_t s)
129
721k
{
130
721k
    (void)tif;
131
721k
    (void)s;
132
721k
    return (1);
133
721k
}
134
135
static int _TIFFtrue(TIFF *tif)
136
30.8k
{
137
30.8k
    (void)tif;
138
30.8k
    return (1);
139
30.8k
}
140
362k
static void _TIFFvoid(TIFF *tif) { (void)tif; }
141
142
void _TIFFSetDefaultPostDecode(TIFF *tif)
143
610k
{
144
610k
    tif->tif_postdecode = _TIFFNoPostDecode;
145
    /*
146
     * If the data require post-decoding processing to byte-swap
147
     * samples, set it up here.  Note that since tags are required
148
     * to be ordered, compression code can override this behavior
149
     * in the setup method if it wants to roll the post decoding
150
     * work in with its normal work.
151
     */
152
610k
    if (tif->tif_flags & TIFF_SWAB)
153
155k
    {
154
155k
        TIFFDirectory *td = &tif->tif_dir;
155
155k
        if (td->td_bitspersample == 16)
156
1.93k
            tif->tif_postdecode = _TIFFSwab16BitData;
157
153k
        else if (td->td_bitspersample == 24)
158
948
            tif->tif_postdecode = _TIFFSwab24BitData;
159
152k
        else if (td->td_bitspersample == 32)
160
1.32k
            tif->tif_postdecode = _TIFFSwab32BitData;
161
151k
        else if (td->td_bitspersample == 64)
162
690
            tif->tif_postdecode = _TIFFSwab64BitData;
163
150k
        else if (td->td_bitspersample == 128)
164
15
        {
165
            // Used for Complex 64-bit floating point.
166
            // The real and imaginary parts are byte-swapped separately.
167
15
            tif->tif_postdecode = _TIFFSwab64BitData;
168
15
        }
169
155k
    }
170
610k
}
171
172
void _TIFFSetDefaultCompressionState(TIFF *tif)
173
517k
{
174
517k
    tif->tif_fixuptags = _TIFFNoFixupTags;
175
517k
    tif->tif_decodestatus = TRUE;
176
517k
    tif->tif_setupdecode = _TIFFtrue;
177
517k
    tif->tif_predecode = _TIFFNoPreCode;
178
517k
    _TIFFSetDefaultPostDecode(tif);
179
517k
    tif->tif_decoderow = _TIFFNoRowDecode;
180
517k
    tif->tif_decodestrip = _TIFFNoStripDecode;
181
517k
    tif->tif_decodetile = _TIFFNoTileDecode;
182
517k
    tif->tif_encodestatus = TRUE;
183
517k
    tif->tif_setupencode = _TIFFtrue;
184
517k
    tif->tif_preencode = _TIFFNoPreCode;
185
517k
    tif->tif_postencode = _TIFFtrue;
186
517k
    tif->tif_encoderow = _TIFFNoRowEncode;
187
517k
    tif->tif_encodestrip = _TIFFNoStripEncode;
188
517k
    tif->tif_encodetile = _TIFFNoTileEncode;
189
517k
    tif->tif_close = _TIFFvoid;
190
517k
    tif->tif_seek = _TIFFNoSeek;
191
517k
    tif->tif_cleanup = _TIFFvoid;
192
517k
    tif->tif_defstripsize = _TIFFDefaultStripSize;
193
517k
    tif->tif_deftilesize = _TIFFDefaultTileSize;
194
517k
    tif->tif_flags &= ~(TIFF_NOBITREV | TIFF_NOREADRAW);
195
517k
}
196
197
int TIFFSetCompressionScheme(TIFF *tif, int scheme)
198
299k
{
199
299k
    const TIFFCodec *c = TIFFFindCODEC((uint16_t)scheme);
200
201
299k
    _TIFFSetDefaultCompressionState(tif);
202
    /*
203
     * Don't treat an unknown compression scheme as an error.
204
     * This permits applications to open files with data that
205
     * the library does not have builtin support for, but which
206
     * may still be meaningful.
207
     */
208
299k
    return (c ? (*c->init)(tif, scheme) : 1);
209
299k
}
210
211
/*
212
 * Other compression schemes may be registered.  Registered
213
 * schemes can also override the builtin versions provided
214
 * by this library.
215
 */
216
typedef struct _codec
217
{
218
    struct _codec *next;
219
    TIFFCodec *info;
220
} codec_t;
221
static codec_t *registeredCODECS = NULL;
222
223
const TIFFCodec *TIFFFindCODEC(uint16_t scheme)
224
499k
{
225
499k
    const TIFFCodec *c;
226
499k
    codec_t *cd;
227
228
499k
    for (cd = registeredCODECS; cd; cd = cd->next)
229
0
        if (cd->info->scheme == scheme)
230
0
            return ((const TIFFCodec *)cd->info);
231
2.20M
    for (c = _TIFFBuiltinCODECS; c->name; c++)
232
2.20M
        if (c->scheme == scheme)
233
496k
            return (c);
234
3.01k
    return ((const TIFFCodec *)0);
235
499k
}
236
237
TIFFCodec *TIFFRegisterCODEC(uint16_t scheme, const char *name,
238
                             TIFFInitMethod init)
239
0
{
240
0
    codec_t *cd = (codec_t *)_TIFFmallocExt(
241
0
        NULL,
242
0
        (tmsize_t)(sizeof(codec_t) + sizeof(TIFFCodec) + strlen(name) + 1));
243
244
0
    if (cd != NULL)
245
0
    {
246
0
        char *codec_name;
247
0
        cd->info = (TIFFCodec *)((uint8_t *)cd + sizeof(codec_t));
248
0
        codec_name = (char *)((uint8_t *)cd->info + sizeof(TIFFCodec));
249
0
        strcpy(codec_name, name);
250
0
        cd->info->name = codec_name;
251
0
        cd->info->scheme = scheme;
252
0
        cd->info->init = init;
253
0
        cd->next = registeredCODECS;
254
0
        registeredCODECS = cd;
255
0
    }
256
0
    else
257
0
    {
258
0
        TIFFErrorExt(0, "TIFFRegisterCODEC",
259
0
                     "No space to register compression scheme %s", name);
260
0
        return NULL;
261
0
    }
262
0
    return (cd->info);
263
0
}
264
265
void TIFFUnRegisterCODEC(TIFFCodec *c)
266
0
{
267
0
    codec_t *cd;
268
0
    codec_t **pcd;
269
270
0
    for (pcd = &registeredCODECS; (cd = *pcd) != NULL; pcd = &cd->next)
271
0
        if (cd->info == c)
272
0
        {
273
0
            *pcd = cd->next;
274
0
            _TIFFfreeExt(NULL, cd);
275
0
            return;
276
0
        }
277
0
    TIFFErrorExt(0, "TIFFUnRegisterCODEC",
278
0
                 "Cannot remove compression scheme %s; not registered",
279
0
                 c->name);
280
0
}
281
282
/************************************************************************/
283
/*                      TIFFGetConfiguredCODECs()                       */
284
/************************************************************************/
285
286
/**
287
 * Get list of configured codecs, both built-in and registered by user.
288
 * Caller is responsible to free this structure.
289
 *
290
 * @return returns array of TIFFCodec records (the last record should be NULL)
291
 * or NULL if function failed.
292
 */
293
294
TIFFCodec *TIFFGetConfiguredCODECs(void)
295
0
{
296
0
    int i = 1;
297
0
    codec_t *cd;
298
0
    const TIFFCodec *c;
299
0
    TIFFCodec *codecs = NULL;
300
0
    TIFFCodec *new_codecs;
301
302
0
    for (cd = registeredCODECS; cd; cd = cd->next)
303
0
    {
304
0
        new_codecs =
305
0
            (TIFFCodec *)_TIFFreallocExt(NULL, codecs, i * sizeof(TIFFCodec));
306
0
        if (!new_codecs)
307
0
        {
308
0
            _TIFFfreeExt(NULL, codecs);
309
0
            return NULL;
310
0
        }
311
0
        codecs = new_codecs;
312
0
        _TIFFmemcpy(codecs + i - 1, cd->info, sizeof(TIFFCodec));
313
0
        i++;
314
0
    }
315
0
    for (c = _TIFFBuiltinCODECS; c->name; c++)
316
0
    {
317
0
        if (TIFFIsCODECConfigured(c->scheme))
318
0
        {
319
0
            new_codecs = (TIFFCodec *)_TIFFreallocExt(NULL, codecs,
320
0
                                                      i * sizeof(TIFFCodec));
321
0
            if (!new_codecs)
322
0
            {
323
0
                _TIFFfreeExt(NULL, codecs);
324
0
                return NULL;
325
0
            }
326
0
            codecs = new_codecs;
327
0
            _TIFFmemcpy(codecs + i - 1, (const void *)c, sizeof(TIFFCodec));
328
0
            i++;
329
0
        }
330
0
    }
331
332
0
    new_codecs =
333
0
        (TIFFCodec *)_TIFFreallocExt(NULL, codecs, i * sizeof(TIFFCodec));
334
0
    if (!new_codecs)
335
0
    {
336
0
        _TIFFfreeExt(NULL, codecs);
337
0
        return NULL;
338
0
    }
339
0
    codecs = new_codecs;
340
0
    _TIFFmemset(codecs + i - 1, 0, sizeof(TIFFCodec));
341
342
0
    return codecs;
343
0
}