Coverage Report

Created: 2026-09-26 08:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/libtiff/tif_packbits.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
#include "tiffiop.h"
26
#ifdef PACKBITS_SUPPORT
27
/*
28
 * TIFF Library.
29
 *
30
 * PackBits Compression Algorithm Support
31
 */
32
#include <stdio.h>
33
34
#ifndef PACKBITS_READ_ONLY
35
36
static int PackBitsPreEncode(TIFF *tif, uint16_t s)
37
0
{
38
0
    (void)s;
39
40
0
    tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(tmsize_t));
41
0
    if (tif->tif_data == NULL)
42
0
        return (0);
43
    /*
44
     * Calculate the scanline/tile-width size in bytes.
45
     */
46
0
    if (isTiled(tif))
47
0
        *(tmsize_t *)tif->tif_data = TIFFTileRowSize(tif);
48
0
    else
49
0
        *(tmsize_t *)tif->tif_data = TIFFScanlineSize(tif);
50
0
    return (1);
51
0
}
52
53
static int PackBitsPostEncode(TIFF *tif)
54
0
{
55
0
    if (tif->tif_data)
56
0
        _TIFFfreeExt(tif, tif->tif_data);
57
0
    return (1);
58
0
}
59
60
/*
61
 * Encode a run of pixels.
62
 */
63
static int PackBitsEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
64
0
{
65
0
    unsigned char *bp = (unsigned char *)buf;
66
0
    uint8_t *op;
67
0
    uint8_t *ep;
68
0
    uint8_t *lastliteral;
69
0
    long n, slop;
70
0
    int b;
71
0
    enum
72
0
    {
73
0
        BASE,
74
0
        LITERAL,
75
0
        RUN,
76
0
        LITERAL_RUN
77
0
    } state;
78
79
0
    (void)s;
80
0
    op = tif->tif_rawcp;
81
0
    ep = tif->tif_rawdata + tif->tif_rawdatasize;
82
0
    state = BASE;
83
0
    lastliteral = NULL;
84
0
    while (cc > 0)
85
0
    {
86
        /*
87
         * Find the longest string of identical bytes.
88
         */
89
0
        b = *bp++;
90
0
        cc--;
91
0
        n = 1;
92
0
        for (; cc > 0 && b == *bp; cc--, bp++)
93
0
            n++;
94
0
    again:
95
0
        if (op + 2 >= ep)
96
0
        { /* insure space for new data */
97
            /*
98
             * Be careful about writing the last
99
             * literal.  Must write up to that point
100
             * and then copy the remainder to the
101
             * front of the buffer.
102
             */
103
0
            if (state == LITERAL || state == LITERAL_RUN)
104
0
            {
105
0
                slop = (long)(op - lastliteral);
106
0
                tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
107
0
                if (!TIFFFlushData1(tif))
108
0
                    return (0);
109
0
                op = tif->tif_rawcp;
110
0
                while (slop-- > 0)
111
0
                    *op++ = *lastliteral++;
112
0
                lastliteral = tif->tif_rawcp;
113
0
            }
114
0
            else
115
0
            {
116
0
                tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
117
0
                if (!TIFFFlushData1(tif))
118
0
                    return (0);
119
0
                op = tif->tif_rawcp;
120
0
            }
121
0
        }
122
0
        switch (state)
123
0
        {
124
0
            case BASE: /* initial state, set run/literal */
125
0
                if (n > 1)
126
0
                {
127
0
                    state = RUN;
128
0
                    if (n > 128)
129
0
                    {
130
0
                        *op++ = (uint8_t)-127;
131
0
                        *op++ = (uint8_t)b;
132
0
                        n -= 128;
133
0
                        goto again;
134
0
                    }
135
0
                    *op++ = (uint8_t)(-(n - 1));
136
0
                    *op++ = (uint8_t)b;
137
0
                }
138
0
                else
139
0
                {
140
0
                    lastliteral = op;
141
0
                    *op++ = 0;
142
0
                    *op++ = (uint8_t)b;
143
0
                    state = LITERAL;
144
0
                }
145
0
                break;
146
0
            case LITERAL: /* last object was literal string */
147
0
                if (n > 1)
148
0
                {
149
0
                    state = LITERAL_RUN;
150
0
                    if (n > 128)
151
0
                    {
152
0
                        *op++ = (uint8_t)-127;
153
0
                        *op++ = (uint8_t)b;
154
0
                        n -= 128;
155
0
                        goto again;
156
0
                    }
157
0
                    *op++ = (uint8_t)(-(n - 1)); /* encode run */
158
0
                    *op++ = (uint8_t)b;
159
0
                }
160
0
                else
161
0
                { /* extend literal */
162
0
                    if (++(*lastliteral) == 127)
163
0
                        state = BASE;
164
0
                    *op++ = (uint8_t)b;
165
0
                }
166
0
                break;
167
0
            case RUN: /* last object was run */
168
0
                if (n > 1)
169
0
                {
170
0
                    if (n > 128)
171
0
                    {
172
0
                        *op++ = (uint8_t)-127;
173
0
                        *op++ = (uint8_t)b;
174
0
                        n -= 128;
175
0
                        goto again;
176
0
                    }
177
0
                    *op++ = (uint8_t)(-(n - 1));
178
0
                    *op++ = (uint8_t)b;
179
0
                }
180
0
                else
181
0
                {
182
0
                    lastliteral = op;
183
0
                    *op++ = 0;
184
0
                    *op++ = (uint8_t)b;
185
0
                    state = LITERAL;
186
0
                }
187
0
                break;
188
0
            case LITERAL_RUN: /* literal followed by a run */
189
                /*
190
                 * Check to see if previous run should
191
                 * be converted to a literal, in which
192
                 * case we convert literal-run-literal
193
                 * to a single literal.
194
                 */
195
0
                if (n == 1 && op[-2] == (uint8_t)-1 && *lastliteral < 126)
196
0
                {
197
0
                    state =
198
0
                        (((*lastliteral) = (uint8_t)(*lastliteral + 2)) == 127
199
0
                             ? BASE
200
0
                             : LITERAL);
201
0
                    op[-2] = op[-1]; /* replicate */
202
0
                }
203
0
                else
204
0
                    state = RUN;
205
0
                goto again;
206
0
            default:
207
0
                break;
208
0
        }
209
0
    }
210
0
    tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
211
0
    tif->tif_rawcp = op;
212
0
    return (1);
213
0
}
214
215
/*
216
 * Encode a rectangular chunk of pixels.  We break it up
217
 * into row-sized pieces to insure that encoded runs do
218
 * not span rows.  Otherwise, there can be problems with
219
 * the decoder if data is read, for example, by scanlines
220
 * when it was encoded by strips.
221
 */
222
static int PackBitsEncodeChunk(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
223
0
{
224
0
    tmsize_t rowsize = *(tmsize_t *)tif->tif_data;
225
226
0
    while (cc > 0)
227
0
    {
228
0
        tmsize_t chunk = rowsize;
229
230
0
        if (cc < chunk)
231
0
            chunk = cc;
232
233
0
        if (PackBitsEncode(tif, bp, chunk, s) < 0)
234
0
            return (-1);
235
0
        bp += chunk;
236
0
        cc -= chunk;
237
0
    }
238
0
    return (1);
239
0
}
240
241
#endif
242
243
static int PackBitsDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
244
383k
{
245
383k
    static const char module[] = "PackBitsDecode";
246
383k
    int8_t *bp;
247
383k
    tmsize_t cc;
248
383k
    long n;
249
383k
    int b;
250
251
383k
    (void)s;
252
383k
    bp = (int8_t *)tif->tif_rawcp;
253
383k
    cc = tif->tif_rawcc;
254
1.82M
    while (cc > 0 && occ > 0)
255
1.44M
    {
256
1.44M
        n = (long)*bp++;
257
1.44M
        cc--;
258
1.44M
        if (n < 0)
259
668k
        {                  /* replicate next byte -n+1 times */
260
668k
            if (n == -128) /* nop */
261
15.1k
                continue;
262
653k
            n = -n + 1;
263
653k
            if (occ < (tmsize_t)n)
264
193k
            {
265
193k
                TIFFWarningExtR(tif, module,
266
193k
                                "Discarding %" TIFF_SSIZE_FORMAT
267
193k
                                " bytes to avoid buffer overrun",
268
193k
                                (tmsize_t)n - occ);
269
193k
                n = (long)occ;
270
193k
            }
271
653k
            if (cc == 0)
272
355
            {
273
355
                TIFFWarningExtR(
274
355
                    tif, module,
275
355
                    "Terminating PackBitsDecode due to lack of data.");
276
355
                break;
277
355
            }
278
653k
            occ -= n;
279
653k
            b = *bp++;
280
653k
            cc--;
281
12.0M
            while (n-- > 0)
282
11.3M
                *op++ = (uint8_t)b;
283
653k
        }
284
776k
        else
285
776k
        { /* copy next n+1 bytes literally */
286
776k
            if (occ < (tmsize_t)(n + 1))
287
156k
            {
288
156k
                TIFFWarningExtR(tif, module,
289
156k
                                "Discarding %" TIFF_SSIZE_FORMAT
290
156k
                                " bytes to avoid buffer overrun",
291
156k
                                (tmsize_t)n - occ + 1);
292
156k
                n = (long)occ - 1;
293
156k
            }
294
776k
            if (cc < (tmsize_t)(n + 1))
295
873
            {
296
873
                TIFFWarningExtR(
297
873
                    tif, module,
298
873
                    "Terminating PackBitsDecode due to lack of data.");
299
873
                break;
300
873
            }
301
775k
            _TIFFmemcpy(op, bp, ++n);
302
775k
            op += n;
303
775k
            occ -= n;
304
775k
            bp += n;
305
775k
            cc -= n;
306
775k
        }
307
1.44M
    }
308
383k
    tif->tif_rawcp = (uint8_t *)bp;
309
383k
    tif->tif_rawcc = cc;
310
383k
    if (occ > 0)
311
1.50k
    {
312
1.50k
        memset(op, 0, (size_t)occ);
313
1.50k
        TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
314
1.50k
                      tif->tif_dir.td_row);
315
1.50k
        return (0);
316
1.50k
    }
317
382k
    return (1);
318
383k
}
tif_packbits.c:gdal_PackBitsDecode
Line
Count
Source
244
383k
{
245
383k
    static const char module[] = "PackBitsDecode";
246
383k
    int8_t *bp;
247
383k
    tmsize_t cc;
248
383k
    long n;
249
383k
    int b;
250
251
383k
    (void)s;
252
383k
    bp = (int8_t *)tif->tif_rawcp;
253
383k
    cc = tif->tif_rawcc;
254
1.81M
    while (cc > 0 && occ > 0)
255
1.43M
    {
256
1.43M
        n = (long)*bp++;
257
1.43M
        cc--;
258
1.43M
        if (n < 0)
259
661k
        {                  /* replicate next byte -n+1 times */
260
661k
            if (n == -128) /* nop */
261
15.0k
                continue;
262
646k
            n = -n + 1;
263
646k
            if (occ < (tmsize_t)n)
264
193k
            {
265
193k
                TIFFWarningExtR(tif, module,
266
193k
                                "Discarding %" TIFF_SSIZE_FORMAT
267
193k
                                " bytes to avoid buffer overrun",
268
193k
                                (tmsize_t)n - occ);
269
193k
                n = (long)occ;
270
193k
            }
271
646k
            if (cc == 0)
272
355
            {
273
355
                TIFFWarningExtR(
274
355
                    tif, module,
275
355
                    "Terminating PackBitsDecode due to lack of data.");
276
355
                break;
277
355
            }
278
646k
            occ -= n;
279
646k
            b = *bp++;
280
646k
            cc--;
281
11.2M
            while (n-- > 0)
282
10.6M
                *op++ = (uint8_t)b;
283
646k
        }
284
773k
        else
285
773k
        { /* copy next n+1 bytes literally */
286
773k
            if (occ < (tmsize_t)(n + 1))
287
156k
            {
288
156k
                TIFFWarningExtR(tif, module,
289
156k
                                "Discarding %" TIFF_SSIZE_FORMAT
290
156k
                                " bytes to avoid buffer overrun",
291
156k
                                (tmsize_t)n - occ + 1);
292
156k
                n = (long)occ - 1;
293
156k
            }
294
773k
            if (cc < (tmsize_t)(n + 1))
295
857
            {
296
857
                TIFFWarningExtR(
297
857
                    tif, module,
298
857
                    "Terminating PackBitsDecode due to lack of data.");
299
857
                break;
300
857
            }
301
772k
            _TIFFmemcpy(op, bp, ++n);
302
772k
            op += n;
303
772k
            occ -= n;
304
772k
            bp += n;
305
772k
            cc -= n;
306
772k
        }
307
1.43M
    }
308
383k
    tif->tif_rawcp = (uint8_t *)bp;
309
383k
    tif->tif_rawcc = cc;
310
383k
    if (occ > 0)
311
1.48k
    {
312
1.48k
        memset(op, 0, (size_t)occ);
313
1.48k
        TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
314
1.48k
                      tif->tif_dir.td_row);
315
1.48k
        return (0);
316
1.48k
    }
317
382k
    return (1);
318
383k
}
libertiffdataset.cpp:PackBitsDecode(tiff*, unsigned char*, long, unsigned short)
Line
Count
Source
244
24
{
245
24
    static const char module[] = "PackBitsDecode";
246
24
    int8_t *bp;
247
24
    tmsize_t cc;
248
24
    long n;
249
24
    int b;
250
251
24
    (void)s;
252
24
    bp = (int8_t *)tif->tif_rawcp;
253
24
    cc = tif->tif_rawcc;
254
9.97k
    while (cc > 0 && occ > 0)
255
9.96k
    {
256
9.96k
        n = (long)*bp++;
257
9.96k
        cc--;
258
9.96k
        if (n < 0)
259
6.58k
        {                  /* replicate next byte -n+1 times */
260
6.58k
            if (n == -128) /* nop */
261
9
                continue;
262
6.57k
            n = -n + 1;
263
6.57k
            if (occ < (tmsize_t)n)
264
1
            {
265
1
                TIFFWarningExtR(tif, module,
266
1
                                "Discarding %" TIFF_SSIZE_FORMAT
267
1
                                " bytes to avoid buffer overrun",
268
1
                                (tmsize_t)n - occ);
269
1
                n = (long)occ;
270
1
            }
271
6.57k
            if (cc == 0)
272
0
            {
273
0
                TIFFWarningExtR(
274
0
                    tif, module,
275
0
                    "Terminating PackBitsDecode due to lack of data.");
276
0
                break;
277
0
            }
278
6.57k
            occ -= n;
279
6.57k
            b = *bp++;
280
6.57k
            cc--;
281
760k
            while (n-- > 0)
282
753k
                *op++ = (uint8_t)b;
283
6.57k
        }
284
3.38k
        else
285
3.38k
        { /* copy next n+1 bytes literally */
286
3.38k
            if (occ < (tmsize_t)(n + 1))
287
2
            {
288
2
                TIFFWarningExtR(tif, module,
289
2
                                "Discarding %" TIFF_SSIZE_FORMAT
290
2
                                " bytes to avoid buffer overrun",
291
2
                                (tmsize_t)n - occ + 1);
292
2
                n = (long)occ - 1;
293
2
            }
294
3.38k
            if (cc < (tmsize_t)(n + 1))
295
16
            {
296
16
                TIFFWarningExtR(
297
16
                    tif, module,
298
16
                    "Terminating PackBitsDecode due to lack of data.");
299
16
                break;
300
16
            }
301
3.36k
            _TIFFmemcpy(op, bp, ++n);
302
3.36k
            op += n;
303
3.36k
            occ -= n;
304
3.36k
            bp += n;
305
3.36k
            cc -= n;
306
3.36k
        }
307
9.96k
    }
308
24
    tif->tif_rawcp = (uint8_t *)bp;
309
24
    tif->tif_rawcc = cc;
310
24
    if (occ > 0)
311
21
    {
312
21
        memset(op, 0, (size_t)occ);
313
21
        TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
314
21
                      tif->tif_dir.td_row);
315
21
        return (0);
316
21
    }
317
3
    return (1);
318
24
}
319
320
static uint64_t PackBitsGetMaxCompressionRatio(TIFF *tif)
321
91
{
322
91
    (void)tif;
323
324
    /* See README_for_libtiff_developpers.md for raw data used to estimate
325
     * the maximum compression rate. */
326
327
91
    return 64;
328
91
}
tif_packbits.c:PackBitsGetMaxCompressionRatio
Line
Count
Source
321
91
{
322
91
    (void)tif;
323
324
    /* See README_for_libtiff_developpers.md for raw data used to estimate
325
     * the maximum compression rate. */
326
327
91
    return 64;
328
91
}
Unexecuted instantiation: libertiffdataset.cpp:PackBitsGetMaxCompressionRatio(tiff*)
329
330
int TIFFInitPackBits(TIFF *tif, int scheme)
331
3.32k
{
332
3.32k
    (void)scheme;
333
3.32k
    tif->tif_decoderow = PackBitsDecode;
334
3.32k
    tif->tif_decodestrip = PackBitsDecode;
335
3.32k
    tif->tif_decodetile = PackBitsDecode;
336
#ifndef PACKBITS_READ_ONLY
337
3.31k
    tif->tif_preencode = PackBitsPreEncode;
338
3.31k
    tif->tif_postencode = PackBitsPostEncode;
339
3.31k
    tif->tif_encoderow = PackBitsEncode;
340
3.31k
    tif->tif_encodestrip = PackBitsEncodeChunk;
341
3.31k
    tif->tif_encodetile = PackBitsEncodeChunk;
342
#endif
343
3.32k
    tif->tif_getmaxcompressionratio = PackBitsGetMaxCompressionRatio;
344
345
3.32k
    return (1);
346
3.32k
}
gdal_TIFFInitPackBits
Line
Count
Source
331
3.31k
{
332
3.31k
    (void)scheme;
333
3.31k
    tif->tif_decoderow = PackBitsDecode;
334
3.31k
    tif->tif_decodestrip = PackBitsDecode;
335
3.31k
    tif->tif_decodetile = PackBitsDecode;
336
3.31k
#ifndef PACKBITS_READ_ONLY
337
3.31k
    tif->tif_preencode = PackBitsPreEncode;
338
3.31k
    tif->tif_postencode = PackBitsPostEncode;
339
3.31k
    tif->tif_encoderow = PackBitsEncode;
340
3.31k
    tif->tif_encodestrip = PackBitsEncodeChunk;
341
3.31k
    tif->tif_encodetile = PackBitsEncodeChunk;
342
3.31k
#endif
343
3.31k
    tif->tif_getmaxcompressionratio = PackBitsGetMaxCompressionRatio;
344
345
3.31k
    return (1);
346
3.31k
}
LIBERTIFF_TIFFInitPackBits
Line
Count
Source
331
10
{
332
10
    (void)scheme;
333
10
    tif->tif_decoderow = PackBitsDecode;
334
10
    tif->tif_decodestrip = PackBitsDecode;
335
10
    tif->tif_decodetile = PackBitsDecode;
336
#ifndef PACKBITS_READ_ONLY
337
    tif->tif_preencode = PackBitsPreEncode;
338
    tif->tif_postencode = PackBitsPostEncode;
339
    tif->tif_encoderow = PackBitsEncode;
340
    tif->tif_encodestrip = PackBitsEncodeChunk;
341
    tif->tif_encodetile = PackBitsEncodeChunk;
342
#endif
343
10
    tif->tif_getmaxcompressionratio = PackBitsGetMaxCompressionRatio;
344
345
10
    return (1);
346
10
}
347
#endif /* PACKBITS_SUPPORT */