Coverage Report

Created: 2025-11-16 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opencv/3rdparty/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 = (((*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
6.02k
{
240
6.02k
    static const char module[] = "PackBitsDecode";
241
6.02k
    int8_t *bp;
242
6.02k
    tmsize_t cc;
243
6.02k
    long n;
244
6.02k
    int b;
245
246
6.02k
    (void)s;
247
6.02k
    bp = (int8_t *)tif->tif_rawcp;
248
6.02k
    cc = tif->tif_rawcc;
249
304k
    while (cc > 0 && occ > 0)
250
299k
    {
251
299k
        n = (long)*bp++;
252
299k
        cc--;
253
299k
        if (n < 0)
254
222k
        {                  /* replicate next byte -n+1 times */
255
222k
            if (n == -128) /* nop */
256
15.8k
                continue;
257
206k
            n = -n + 1;
258
206k
            if (occ < (tmsize_t)n)
259
4.36k
            {
260
4.36k
                TIFFWarningExtR(tif, module,
261
4.36k
                                "Discarding %" TIFF_SSIZE_FORMAT
262
4.36k
                                " bytes to avoid buffer overrun",
263
4.36k
                                (tmsize_t)n - occ);
264
4.36k
                n = (long)occ;
265
4.36k
            }
266
206k
            if (cc == 0)
267
80
            {
268
80
                TIFFWarningExtR(
269
80
                    tif, module,
270
80
                    "Terminating PackBitsDecode due to lack of data.");
271
80
                break;
272
80
            }
273
206k
            occ -= n;
274
206k
            b = *bp++;
275
206k
            cc--;
276
5.70M
            while (n-- > 0)
277
5.50M
                *op++ = (uint8_t)b;
278
206k
        }
279
76.9k
        else
280
76.9k
        { /* copy next n+1 bytes literally */
281
76.9k
            if (occ < (tmsize_t)(n + 1))
282
1.03k
            {
283
1.03k
                TIFFWarningExtR(tif, module,
284
1.03k
                                "Discarding %" TIFF_SSIZE_FORMAT
285
1.03k
                                " bytes to avoid buffer overrun",
286
1.03k
                                (tmsize_t)n - occ + 1);
287
1.03k
                n = (long)occ - 1;
288
1.03k
            }
289
76.9k
            if (cc < (tmsize_t)(n + 1))
290
306
            {
291
306
                TIFFWarningExtR(
292
306
                    tif, module,
293
306
                    "Terminating PackBitsDecode due to lack of data.");
294
306
                break;
295
306
            }
296
76.6k
            _TIFFmemcpy(op, bp, ++n);
297
76.6k
            op += n;
298
76.6k
            occ -= n;
299
76.6k
            bp += n;
300
76.6k
            cc -= n;
301
76.6k
        }
302
299k
    }
303
6.02k
    tif->tif_rawcp = (uint8_t *)bp;
304
6.02k
    tif->tif_rawcc = cc;
305
6.02k
    if (occ > 0)
306
678
    {
307
678
        memset(op, 0, (size_t)occ);
308
678
        TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
309
678
                      tif->tif_row);
310
678
        return (0);
311
678
    }
312
5.34k
    return (1);
313
6.02k
}
314
315
int TIFFInitPackBits(TIFF *tif, int scheme)
316
72
{
317
72
    (void)scheme;
318
72
    tif->tif_decoderow = PackBitsDecode;
319
72
    tif->tif_decodestrip = PackBitsDecode;
320
72
    tif->tif_decodetile = PackBitsDecode;
321
72
#ifndef PACKBITS_READ_ONLY
322
72
    tif->tif_preencode = PackBitsPreEncode;
323
72
    tif->tif_postencode = PackBitsPostEncode;
324
72
    tif->tif_encoderow = PackBitsEncode;
325
72
    tif->tif_encodestrip = PackBitsEncodeChunk;
326
72
    tif->tif_encodetile = PackBitsEncodeChunk;
327
72
#endif
328
72
    return (1);
329
72
}
330
#endif /* PACKBITS_SUPPORT */