Coverage Report

Created: 2025-06-22 06:59

/src/gdal/frmts/gtiff/libtiff/tif_packbits.c
Line
Count
Source (jump to first uncovered line)
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 = (((*lastliteral) += 2) == 127 ? BASE : LITERAL);
198
0
                    op[-2] = op[-1]; /* replicate */
199
0
                }
200
0
                else
201
0
                    state = RUN;
202
0
                goto again;
203
0
        }
204
0
    }
205
0
    tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
206
0
    tif->tif_rawcp = op;
207
0
    return (1);
208
0
}
209
210
/*
211
 * Encode a rectangular chunk of pixels.  We break it up
212
 * into row-sized pieces to insure that encoded runs do
213
 * not span rows.  Otherwise, there can be problems with
214
 * the decoder if data is read, for example, by scanlines
215
 * when it was encoded by strips.
216
 */
217
static int PackBitsEncodeChunk(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
218
0
{
219
0
    tmsize_t rowsize = *(tmsize_t *)tif->tif_data;
220
221
0
    while (cc > 0)
222
0
    {
223
0
        tmsize_t chunk = rowsize;
224
225
0
        if (cc < chunk)
226
0
            chunk = cc;
227
228
0
        if (PackBitsEncode(tif, bp, chunk, s) < 0)
229
0
            return (-1);
230
0
        bp += chunk;
231
0
        cc -= chunk;
232
0
    }
233
0
    return (1);
234
0
}
235
236
#endif
237
238
static int PackBitsDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
239
0
{
240
0
    static const char module[] = "PackBitsDecode";
241
0
    int8_t *bp;
242
0
    tmsize_t cc;
243
0
    long n;
244
0
    int b;
245
246
0
    (void)s;
247
0
    bp = (int8_t *)tif->tif_rawcp;
248
0
    cc = tif->tif_rawcc;
249
0
    while (cc > 0 && occ > 0)
250
0
    {
251
0
        n = (long)*bp++;
252
0
        cc--;
253
0
        if (n < 0)
254
0
        {                  /* replicate next byte -n+1 times */
255
0
            if (n == -128) /* nop */
256
0
                continue;
257
0
            n = -n + 1;
258
0
            if (occ < (tmsize_t)n)
259
0
            {
260
0
                TIFFWarningExtR(tif, module,
261
0
                                "Discarding %" TIFF_SSIZE_FORMAT
262
0
                                " bytes to avoid buffer overrun",
263
0
                                (tmsize_t)n - occ);
264
0
                n = (long)occ;
265
0
            }
266
0
            if (cc == 0)
267
0
            {
268
0
                TIFFWarningExtR(
269
0
                    tif, module,
270
0
                    "Terminating PackBitsDecode due to lack of data.");
271
0
                break;
272
0
            }
273
0
            occ -= n;
274
0
            b = *bp++;
275
0
            cc--;
276
0
            while (n-- > 0)
277
0
                *op++ = (uint8_t)b;
278
0
        }
279
0
        else
280
0
        { /* copy next n+1 bytes literally */
281
0
            if (occ < (tmsize_t)(n + 1))
282
0
            {
283
0
                TIFFWarningExtR(tif, module,
284
0
                                "Discarding %" TIFF_SSIZE_FORMAT
285
0
                                " bytes to avoid buffer overrun",
286
0
                                (tmsize_t)n - occ + 1);
287
0
                n = (long)occ - 1;
288
0
            }
289
0
            if (cc < (tmsize_t)(n + 1))
290
0
            {
291
0
                TIFFWarningExtR(
292
0
                    tif, module,
293
0
                    "Terminating PackBitsDecode due to lack of data.");
294
0
                break;
295
0
            }
296
0
            _TIFFmemcpy(op, bp, ++n);
297
0
            op += n;
298
0
            occ -= n;
299
0
            bp += n;
300
0
            cc -= n;
301
0
        }
302
0
    }
303
0
    tif->tif_rawcp = (uint8_t *)bp;
304
0
    tif->tif_rawcc = cc;
305
0
    if (occ > 0)
306
0
    {
307
0
        memset(op, 0, (size_t)occ);
308
0
        TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
309
0
                      tif->tif_row);
310
0
        return (0);
311
0
    }
312
0
    return (1);
313
0
}
314
315
int TIFFInitPackBits(TIFF *tif, int scheme)
316
0
{
317
0
    (void)scheme;
318
0
    tif->tif_decoderow = PackBitsDecode;
319
0
    tif->tif_decodestrip = PackBitsDecode;
320
0
    tif->tif_decodetile = PackBitsDecode;
321
0
#ifndef PACKBITS_READ_ONLY
322
0
    tif->tif_preencode = PackBitsPreEncode;
323
0
    tif->tif_postencode = PackBitsPostEncode;
324
0
    tif->tif_encoderow = PackBitsEncode;
325
0
    tif->tif_encodestrip = PackBitsEncodeChunk;
326
0
    tif->tif_encodetile = PackBitsEncodeChunk;
327
0
#endif
328
0
    return (1);
329
0
}
330
#endif /* PACKBITS_SUPPORT */