Coverage Report

Created: 2026-05-16 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/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
0
{
245
0
    static const char module[] = "PackBitsDecode";
246
0
    int8_t *bp;
247
0
    tmsize_t cc;
248
0
    long n;
249
0
    int b;
250
251
0
    (void)s;
252
0
    bp = (int8_t *)tif->tif_rawcp;
253
0
    cc = tif->tif_rawcc;
254
0
    while (cc > 0 && occ > 0)
255
0
    {
256
0
        n = (long)*bp++;
257
0
        cc--;
258
0
        if (n < 0)
259
0
        {                  /* replicate next byte -n+1 times */
260
0
            if (n == -128) /* nop */
261
0
                continue;
262
0
            n = -n + 1;
263
0
            if (occ < (tmsize_t)n)
264
0
            {
265
0
                TIFFWarningExtR(tif, module,
266
0
                                "Discarding %" TIFF_SSIZE_FORMAT
267
0
                                " bytes to avoid buffer overrun",
268
0
                                (tmsize_t)n - occ);
269
0
                n = (long)occ;
270
0
            }
271
0
            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
0
            occ -= n;
279
0
            b = *bp++;
280
0
            cc--;
281
0
            while (n-- > 0)
282
0
                *op++ = (uint8_t)b;
283
0
        }
284
0
        else
285
0
        { /* copy next n+1 bytes literally */
286
0
            if (occ < (tmsize_t)(n + 1))
287
0
            {
288
0
                TIFFWarningExtR(tif, module,
289
0
                                "Discarding %" TIFF_SSIZE_FORMAT
290
0
                                " bytes to avoid buffer overrun",
291
0
                                (tmsize_t)n - occ + 1);
292
0
                n = (long)occ - 1;
293
0
            }
294
0
            if (cc < (tmsize_t)(n + 1))
295
0
            {
296
0
                TIFFWarningExtR(
297
0
                    tif, module,
298
0
                    "Terminating PackBitsDecode due to lack of data.");
299
0
                break;
300
0
            }
301
0
            _TIFFmemcpy(op, bp, ++n);
302
0
            op += n;
303
0
            occ -= n;
304
0
            bp += n;
305
0
            cc -= n;
306
0
        }
307
0
    }
308
0
    tif->tif_rawcp = (uint8_t *)bp;
309
0
    tif->tif_rawcc = cc;
310
0
    if (occ > 0)
311
0
    {
312
0
        memset(op, 0, (size_t)occ);
313
0
        TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
314
0
                      tif->tif_dir.td_row);
315
0
        return (0);
316
0
    }
317
0
    return (1);
318
0
}
319
320
static uint64_t PackBitsGetMaxCompressionRatio(TIFF *tif)
321
0
{
322
0
    (void)tif;
323
324
    /* See README_for_libtiff_developpers.md for raw data used to estimate
325
     * the maximum compression rate. */
326
327
0
    return 64;
328
0
}
329
330
int TIFFInitPackBits(TIFF *tif, int scheme)
331
0
{
332
0
    (void)scheme;
333
0
    tif->tif_decoderow = PackBitsDecode;
334
0
    tif->tif_decodestrip = PackBitsDecode;
335
0
    tif->tif_decodetile = PackBitsDecode;
336
0
#ifndef PACKBITS_READ_ONLY
337
0
    tif->tif_preencode = PackBitsPreEncode;
338
0
    tif->tif_postencode = PackBitsPostEncode;
339
0
    tif->tif_encoderow = PackBitsEncode;
340
0
    tif->tif_encodestrip = PackBitsEncodeChunk;
341
0
    tif->tif_encodetile = PackBitsEncodeChunk;
342
0
#endif
343
0
    tif->tif_getmaxcompressionratio = PackBitsGetMaxCompressionRatio;
344
345
0
    return (1);
346
0
}
347
#endif /* PACKBITS_SUPPORT */