Coverage Report

Created: 2026-09-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/libtiff/tif_jbig.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
 * JBIG Compression Algorithm Support.
29
 * Contributed by Lee Howard <faxguy@deanox.com>
30
 *
31
 */
32
33
#include "tiffiop.h"
34
35
#ifdef JBIG_SUPPORT
36
#ifdef __cplusplus
37
extern "C"
38
{
39
#endif
40
#include "jbig.h"
41
#ifdef __cplusplus
42
}
43
#endif
44
45
static int JBIGSetupDecode(TIFF *tif)
46
0
{
47
0
    if (TIFFNumberOfStrips(tif) != 1)
48
0
    {
49
0
        TIFFErrorExtR(tif, "JBIG",
50
0
                      "Multistrip images not supported in decoder");
51
0
        return 0;
52
0
    }
53
54
0
    return 1;
55
0
}
56
57
static int JBIGDecode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
58
0
{
59
0
    struct jbg_dec_state decoder;
60
0
    int decodeStatus = 0;
61
0
    unsigned char *pImage = NULL;
62
0
    unsigned long decodedSize;
63
0
    (void)s;
64
65
0
    if (isFillOrder(tif, tif->tif_dir.td_fillorder))
66
0
    {
67
0
        TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
68
0
    }
69
70
0
    jbg_dec_init(&decoder);
71
72
0
#if defined(HAVE_JBG_NEWLEN)
73
0
    jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
74
    /*
75
     * I do not check the return status of jbg_newlen because even if this
76
     * function fails it does not necessarily mean that decoding the image
77
     * will fail.  It is generally only needed for received fax images
78
     * that do not contain the actual length of the image in the BIE
79
     * header.  I do not log when an error occurs because that will cause
80
     * problems when converting JBIG encoded TIFF's to
81
     * PostScript.  As long as the actual image length is contained in the
82
     * BIE header jbg_dec_in should succeed.
83
     */
84
0
#endif /* HAVE_JBG_NEWLEN */
85
86
0
    decodeStatus = jbg_dec_in(&decoder, (unsigned char *)tif->tif_rawcp,
87
0
                              (size_t)tif->tif_rawcc, NULL);
88
0
    if (JBG_EOK != decodeStatus)
89
0
    {
90
        /*
91
         * XXX: JBG_EN constant was defined in pre-2.0 releases of the
92
         * JBIG-KIT. Since the 2.0 the error reporting functions were
93
         * changed. We will handle both cases here.
94
         */
95
0
        TIFFErrorExtR(tif, "JBIG", "Error (%d) decoding: %s", decodeStatus,
96
#if defined(JBG_EN)
97
                      jbg_strerror(decodeStatus, JBG_EN)
98
#else
99
0
                      jbg_strerror(decodeStatus)
100
0
#endif
101
0
        );
102
0
        memset(buffer, 0, (size_t)size);
103
0
        jbg_dec_free(&decoder);
104
0
        return 0;
105
0
    }
106
107
0
    decodedSize = jbg_dec_getsize(&decoder);
108
0
    if ((tmsize_t)decodedSize < size)
109
0
    {
110
0
        memset(buffer + decodedSize, 0, (size_t)(size - (tmsize_t)decodedSize));
111
0
        TIFFWarningExtR(tif, "JBIG",
112
0
                        "Only decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
113
0
                        " requested",
114
0
                        decodedSize, size);
115
0
    }
116
0
    else if ((tmsize_t)decodedSize > size)
117
0
    {
118
0
        TIFFErrorExtR(tif, "JBIG",
119
0
                      "Decoded %lu bytes, whereas %" TIFF_SSIZE_FORMAT
120
0
                      " were requested",
121
0
                      decodedSize, size);
122
0
        jbg_dec_free(&decoder);
123
0
        return 0;
124
0
    }
125
0
    pImage = jbg_dec_getimage(&decoder, 0);
126
0
    _TIFFmemcpy(buffer, pImage, (tmsize_t)decodedSize);
127
0
    jbg_dec_free(&decoder);
128
129
0
    tif->tif_rawcp += tif->tif_rawcc;
130
0
    tif->tif_rawcc = 0;
131
132
0
    return 1;
133
0
}
134
135
static int JBIGSetupEncode(TIFF *tif)
136
0
{
137
0
    if (TIFFNumberOfStrips(tif) != 1)
138
0
    {
139
0
        TIFFErrorExtR(tif, "JBIG",
140
0
                      "Multistrip images not supported in encoder");
141
0
        return 0;
142
0
    }
143
144
0
    return 1;
145
0
}
146
147
static int JBIGCopyEncodedData(TIFF *tif, unsigned char *pp, size_t cc,
148
                               uint16_t s)
149
0
{
150
0
    (void)s;
151
0
    while (cc > 0)
152
0
    {
153
0
        tmsize_t n = (tmsize_t)cc;
154
155
0
        if (tif->tif_rawcc + n > tif->tif_rawdatasize)
156
0
        {
157
0
            n = tif->tif_rawdatasize - tif->tif_rawcc;
158
0
        }
159
160
0
        assert(n > 0);
161
0
        _TIFFmemcpy(tif->tif_rawcp, pp, n);
162
0
        tif->tif_rawcp += n;
163
0
        tif->tif_rawcc += n;
164
0
        pp += n;
165
0
        cc -= (size_t)n;
166
0
        if (tif->tif_rawcc >= tif->tif_rawdatasize && !TIFFFlushData1(tif))
167
0
        {
168
0
            return (-1);
169
0
        }
170
0
    }
171
172
0
    return (1);
173
0
}
174
175
static void JBIGOutputBie(unsigned char *buffer, size_t len, void *userData)
176
0
{
177
0
    TIFF *tif = (TIFF *)userData;
178
179
0
    if (isFillOrder(tif, tif->tif_dir.td_fillorder))
180
0
    {
181
0
        TIFFReverseBits(buffer, (tmsize_t)len);
182
0
    }
183
184
0
    JBIGCopyEncodedData(tif, buffer, len, 0);
185
0
}
186
187
static int JBIGEncode(TIFF *tif, uint8_t *buffer, tmsize_t size, uint16_t s)
188
0
{
189
0
    static const char module[] = "JBIG";
190
0
    TIFFDirectory *dir = &tif->tif_dir;
191
0
    struct jbg_enc_state encoder;
192
0
    uint64_t rowbytes64;
193
0
    uint64_t required64;
194
0
    tmsize_t required;
195
196
0
    (void)s;
197
198
0
    if (dir->td_bitspersample != 1 || dir->td_samplesperpixel != 1)
199
0
    {
200
0
        TIFFErrorExtR(tif, module,
201
0
                      "JBIG encoder supports only single-sample 1-bit image "
202
0
                      "planes");
203
0
        return 0;
204
0
    }
205
206
0
    if (TIFFNumberOfStrips(tif) != 1)
207
0
    {
208
0
        TIFFErrorExtR(tif, module,
209
0
                      "Multistrip images not supported in encoder");
210
0
        return 0;
211
0
    }
212
213
0
    rowbytes64 = TIFFhowmany8_64(dir->td_imagewidth);
214
0
    required64 = _TIFFMultiply64(tif, rowbytes64, dir->td_imagelength, module);
215
0
    required = _TIFFCastUInt64ToSSize(tif, required64, module);
216
0
    if (required == 0)
217
0
        return 0;
218
0
    if (size < required)
219
0
    {
220
0
        TIFFErrorExtR(tif, module,
221
0
                      "Application buffer too small for JBIG source image "
222
0
                      "plane: got %" TIFF_SSIZE_FORMAT
223
0
                      " bytes, need %" TIFF_SSIZE_FORMAT,
224
0
                      size, required);
225
0
        return 0;
226
0
    }
227
228
0
    jbg_enc_init(&encoder, dir->td_imagewidth, dir->td_imagelength, 1, &buffer,
229
0
                 JBIGOutputBie, tif);
230
    /*
231
     * jbg_enc_out does the "real" encoding.  As data is encoded,
232
     * JBIGOutputBie is called, which writes the data to the directory.
233
     */
234
0
    jbg_enc_out(&encoder);
235
0
    jbg_enc_free(&encoder);
236
237
0
    return 1;
238
0
}
239
240
int TIFFInitJBIG(TIFF *tif, int scheme)
241
0
{
242
0
    (void)scheme;
243
0
    assert(scheme == COMPRESSION_JBIG);
244
245
    /*
246
     * These flags are set so the JBIG Codec can control when to reverse
247
     * bits and when not to and to allow the jbig decoder and bit reverser
248
     * to write to memory when necessary.
249
     */
250
0
    tif->tif_flags |= TIFF_NOBITREV;
251
0
    tif->tif_flags &= ~TIFF_MAPPED;
252
    /* We may have read from a previous IFD and thus set TIFF_BUFFERMMAP and
253
     * cleared TIFF_MYBUFFER. It is necessary to restore them to their initial
254
     * value to be consistent with the state of a non-memory mapped file.
255
     */
256
0
    if (tif->tif_flags & TIFF_BUFFERMMAP)
257
0
    {
258
0
        tif->tif_rawdata = NULL;
259
0
        tif->tif_rawdatasize = 0;
260
0
        tif->tif_flags &= ~TIFF_BUFFERMMAP;
261
0
        tif->tif_flags |= TIFF_MYBUFFER;
262
0
    }
263
264
    /* Setup the function pointers for encode, decode, and cleanup. */
265
0
    tif->tif_setupdecode = JBIGSetupDecode;
266
0
    tif->tif_decodestrip = JBIGDecode;
267
268
0
    tif->tif_setupencode = JBIGSetupEncode;
269
0
    tif->tif_encodestrip = JBIGEncode;
270
271
0
    return 1;
272
0
}
273
274
#endif /* JBIG_SUPPORT */