Coverage Report

Created: 2026-04-01 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/sjbig2.c
Line
Count
Source
1
/* Copyright (C) 2001-2023 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* jbig2decode filter implementation -- hooks in libjbig2dec */
18
19
#include "stdint_.h"
20
#include "memory_.h"
21
#include "stdio_.h" /* sprintf() for debug output */
22
23
#include "gserrors.h"
24
#include "gdebug.h"
25
#include "strimpl.h"
26
#include "sjbig2.h"
27
#include <limits.h>                     /* UINT_MAX */
28
29
/* stream implementation */
30
31
/* The /JBIG2Decode filter is a fairly memory intensive one to begin with,
32
   particularly in the initial jbig2dec library implementation. Furthermore,
33
   as a PDF 1.4 feature, we can assume a fairly large (host-level) machine.
34
   We therefore dispense with the normal Ghostscript memory discipline and
35
   let the library allocate all its resources on the heap. The pointers to
36
   these are not enumerated and so will not be garbage collected. We rely
37
   on our release() proc being called to deallocate state.
38
 */
39
40
private_st_jbig2decode_state(); /* creates a gc object for our state, defined in sjbig2.h */
41
42
/* error callback for jbig2 decoder */
43
static void
44
s_jbig2decode_error(void *callback_data, const char *msg, Jbig2Severity severity,
45
               uint32_t seg_idx)
46
8.57k
{
47
8.57k
    s_jbig2_callback_data_t *error_data = (s_jbig2_callback_data_t *)callback_data;
48
8.57k
    const char *type;
49
8.57k
    char segment[22];
50
51
8.57k
    switch (severity) {
52
4.57k
        case JBIG2_SEVERITY_DEBUG:
53
4.57k
            type = "DEBUG"; break;;
54
2.12k
        case JBIG2_SEVERITY_INFO:
55
2.12k
            type = "info"; break;;
56
1.43k
        case JBIG2_SEVERITY_WARNING:
57
1.43k
            type = "WARNING"; break;;
58
433
        case JBIG2_SEVERITY_FATAL:
59
433
            type = "FATAL ERROR decoding image:";
60
            /* pass the fatal error upstream if possible */
61
433
            if (error_data != NULL) error_data->error = gs_error_ioerror;
62
433
            break;;
63
0
        default: type = "unknown message:"; break;;
64
8.57k
    }
65
8.57k
    if (seg_idx == JBIG2_UNKNOWN_SEGMENT_NUMBER) segment[0] = '\0';
66
7.35k
    else gs_snprintf(segment, sizeof(segment), "(segment 0x%02x)", seg_idx);
67
68
8.57k
    if (error_data)
69
8.57k
    {
70
8.57k
        char *message;
71
8.57k
        int len;
72
73
8.57k
        len = snprintf(NULL, 0, "jbig2dec %s %s %s", type, msg, segment);
74
8.57k
        if (len < 0)
75
0
            return;
76
77
8.57k
        message = (char *)gs_alloc_bytes(error_data->memory, len + 1, "sjbig2decode_error(message)");
78
8.57k
        if (message == NULL)
79
0
            return;
80
81
8.57k
        len = snprintf(message, len + 1, "jbig2dec %s %s %s", type, msg, segment);
82
8.57k
        if (len < 0)
83
0
        {
84
0
            gs_free_object(error_data->memory, message, "s_jbig2decode_error(message)");
85
0
            return;
86
0
        }
87
88
8.57k
        if (error_data->last_message != NULL && strcmp(message, error_data->last_message)) {
89
7.70k
            if (error_data->repeats > 1)
90
39
            {
91
39
                if (error_data->severity == JBIG2_SEVERITY_FATAL || error_data->severity == JBIG2_SEVERITY_WARNING) {
92
34
                    dmlprintf1(error_data->memory, "jbig2dec last message repeated %ld times\n", error_data->repeats);
93
34
                } else {
94
5
                    if_debug1m('w', error_data->memory, "[w] jbig2dec last message repeated %ld times\n", error_data->repeats);
95
5
                }
96
39
            }
97
7.70k
            gs_free_object(error_data->memory, error_data->last_message, "s_jbig2decode_error(last_message)");
98
7.70k
            if (severity == JBIG2_SEVERITY_FATAL || severity == JBIG2_SEVERITY_WARNING) {
99
1.69k
                dmlprintf1(error_data->memory, "%s\n", message);
100
6.00k
            } else {
101
6.00k
                if_debug1m('w', error_data->memory, "[w] %s\n", message);
102
6.00k
            }
103
7.70k
            error_data->last_message = message;
104
7.70k
            error_data->severity = severity;
105
7.70k
            error_data->type = type;
106
7.70k
            error_data->repeats = 0;
107
7.70k
        }
108
872
        else if (error_data->last_message != NULL) {
109
177
            error_data->repeats++;
110
177
            if (error_data->repeats % 1000000 == 0)
111
0
            {
112
0
                if (error_data->severity == JBIG2_SEVERITY_FATAL || error_data->severity == JBIG2_SEVERITY_WARNING) {
113
0
                    dmlprintf1(error_data->memory, "jbig2dec last message repeated %ld times so far\n", error_data->repeats);
114
0
                } else {
115
0
                    if_debug1m('w', error_data->memory, "[w] jbig2dec last message repeated %ld times so far\n", error_data->repeats);
116
0
                }
117
0
            }
118
177
            gs_free_object(error_data->memory, message, "s_jbig2decode_error(message)");
119
177
        }
120
695
        else if (error_data->last_message == NULL) {
121
695
            if (severity == JBIG2_SEVERITY_FATAL || severity == JBIG2_SEVERITY_WARNING) {
122
19
                dmlprintf1(error_data->memory, "%s\n", message);
123
676
            } else {
124
676
                if_debug1m('w', error_data->memory, "[w] %s\n", message);
125
676
            }
126
695
            error_data->last_message = message;
127
695
            error_data->severity = severity;
128
695
            error_data->type = type;
129
695
            error_data->repeats = 0;
130
695
        }
131
8.57k
    }
132
0
    else
133
0
    {
134
/*
135
        FIXME s_jbig2_callback_data_t should be updated so that jbig2_ctx_new is not called
136
        with a NULL argument (see jbig2.h) and we never reach here with a NULL state
137
*/
138
0
        if (severity == JBIG2_SEVERITY_FATAL) {
139
0
            dlprintf3("jbig2dec %s %s %s\n", type, msg, segment);
140
0
        } else {
141
0
            if_debug3('w', "[w] jbig2dec %s %s %s\n", type, msg, segment);
142
0
        }
143
0
    }
144
8.57k
}
145
146
static void
147
s_jbig2decode_flush_errors(void *callback_data)
148
703
{
149
703
    s_jbig2_callback_data_t *error_data = (s_jbig2_callback_data_t *)callback_data;
150
151
703
    if (error_data == NULL)
152
0
        return;
153
154
703
    if (error_data->last_message != NULL) {
155
695
        if (error_data->repeats > 1)
156
0
        {
157
0
            if (error_data->severity == JBIG2_SEVERITY_FATAL || error_data->severity == JBIG2_SEVERITY_WARNING) {
158
0
                dmlprintf1(error_data->memory, "jbig2dec last message repeated %ld times\n", error_data->repeats);
159
0
            } else {
160
0
                if_debug1m('w', error_data->memory, "[w] jbig2dec last message repeated %ld times\n", error_data->repeats);
161
0
            }
162
0
        }
163
695
        gs_free_object(error_data->memory, error_data->last_message, "s_jbig2decode_error(last_message)");
164
695
        error_data->last_message = NULL;
165
695
        error_data->repeats = 0;
166
695
    }
167
703
}
168
169
/* invert the bits in a buffer */
170
/* jbig2 and postscript have different senses of what pixel
171
   value is black, so we must invert the image */
172
static void
173
s_jbig2decode_invert_buffer(unsigned char *buf, size_t length)
174
98.5k
{
175
98.5k
    size_t i;
176
177
201M
    for (i = 0; i < length; i++)
178
201M
        *buf++ ^= 0xFF;
179
98.5k
}
180
181
typedef struct {
182
        Jbig2Allocator allocator;
183
        gs_memory_t *mem;
184
} s_jbig2decode_allocator_t;
185
186
static void *s_jbig2decode_alloc(Jbig2Allocator *_allocator, size_t size)
187
5.06M
{
188
5.06M
        s_jbig2decode_allocator_t *allocator = (s_jbig2decode_allocator_t *) _allocator;
189
5.06M
        if (size > UINT_MAX)
190
2
            return NULL;
191
5.06M
        return gs_alloc_bytes(allocator->mem, size, "s_jbig2decode_alloc");
192
5.06M
}
193
194
static void s_jbig2decode_free(Jbig2Allocator *_allocator, void *p)
195
5.06M
{
196
5.06M
        s_jbig2decode_allocator_t *allocator = (s_jbig2decode_allocator_t *) _allocator;
197
5.06M
        gs_free_object(allocator->mem, p, "s_jbig2decode_free");
198
5.06M
}
199
200
static void *s_jbig2decode_realloc(Jbig2Allocator *_allocator, void *p, size_t size)
201
0
{
202
0
        s_jbig2decode_allocator_t *allocator = (s_jbig2decode_allocator_t *) _allocator;
203
0
        if (size > UINT_MAX)
204
0
            return NULL;
205
0
        return gs_resize_object(allocator->mem, p, size, "s_jbig2decode_realloc");
206
0
}
207
208
/* parse a globals stream packed into a gs_bytestring for us by the postscript
209
   layer and stuff the resulting context into a pointer for use in later decoding */
210
int
211
s_jbig2decode_make_global_data(gs_memory_t *mem, byte *data, uint length, void **result)
212
0
{
213
0
    Jbig2Ctx *ctx = NULL;
214
0
    int code;
215
0
    s_jbig2decode_allocator_t *allocator;
216
217
    /* the cvision encoder likes to include empty global streams */
218
0
    if (length == 0) {
219
0
        if_debug0('w', "[w] ignoring zero-length jbig2 global stream.\n");
220
0
        *result = NULL;
221
0
        return 0;
222
0
    }
223
224
0
    allocator = (s_jbig2decode_allocator_t *) gs_alloc_bytes(mem,
225
0
            sizeof (s_jbig2decode_allocator_t), "s_jbig2_make_global_data");
226
0
    if (allocator == NULL) {
227
0
        *result = NULL;
228
0
        return_error(gs_error_VMerror);
229
0
    }
230
231
0
    allocator->allocator.alloc = s_jbig2decode_alloc;
232
0
    allocator->allocator.free = s_jbig2decode_free;
233
0
    allocator->allocator.realloc = s_jbig2decode_realloc;
234
0
    allocator->mem = mem;
235
236
    /* allocate a context with which to parse our global segments */
237
0
    ctx = jbig2_ctx_new((Jbig2Allocator *) allocator, JBIG2_OPTIONS_EMBEDDED,
238
0
                            NULL, s_jbig2decode_error, NULL);
239
0
    if (ctx == NULL) {
240
0
        gs_free_object(mem, allocator, "s_jbig2_make_global_data");
241
0
        return_error(gs_error_VMerror);
242
0
    }
243
244
    /* parse the global bitstream */
245
0
    code = jbig2_data_in(ctx, data, length);
246
0
    if (code) {
247
        /* error parsing the global stream */
248
0
        allocator = (s_jbig2decode_allocator_t *) jbig2_ctx_free(ctx);
249
0
        gs_free_object(allocator->mem, allocator, "s_jbig2_make_global_data");
250
0
        *result = NULL;
251
0
        return_error(gs_error_ioerror);
252
0
    }
253
254
    /* canonize and store our global state */
255
0
    *result = jbig2_make_global_ctx(ctx);
256
257
0
    return 0; /* todo: check for allocation failure */
258
0
}
259
260
/* release a global ctx pointer */
261
void
262
s_jbig2decode_free_global_data(void *data)
263
0
{
264
0
    Jbig2GlobalCtx *global_ctx = (Jbig2GlobalCtx*)data;
265
0
    s_jbig2decode_allocator_t *allocator;
266
267
0
    allocator = (s_jbig2decode_allocator_t *) jbig2_global_ctx_free(global_ctx);
268
269
0
    gs_free_object(allocator->mem, allocator, "s_jbig2decode_free_global_data");
270
0
}
271
272
/* store a global ctx pointer in our state structure.
273
 * If "gd" is NULL, then this library must free the global context.
274
 * If not-NULL, then it will be memory managed by caller, for example,
275
 * garbage collected in the case of the PS interpreter.
276
 * Currently gpdf will use NULL, and the PDF implemented in the gs interpreter would use
277
 * the garbage collection.
278
 */
279
int
280
s_jbig2decode_set_global_data(stream_state *ss, s_jbig2_global_data_t *gd, void *global_ctx)
281
703
{
282
703
    stream_jbig2decode_state *state = (stream_jbig2decode_state*)ss;
283
703
    state->global_struct = gd;
284
703
    state->global_ctx = global_ctx;
285
703
    return 0;
286
703
}
287
288
/* initialize the steam.
289
   this involves allocating the context structures, and
290
   initializing the global context from the /JBIG2Globals object reference
291
 */
292
static int
293
s_jbig2decode_init(stream_state * ss)
294
703
{
295
703
    stream_jbig2decode_state *const state = (stream_jbig2decode_state *) ss;
296
703
    Jbig2GlobalCtx *global_ctx = state->global_ctx; /* may be NULL */
297
703
    int code = 0;
298
703
    s_jbig2decode_allocator_t *allocator = NULL;
299
300
703
    state->callback_data = (s_jbig2_callback_data_t *)gs_alloc_bytes(
301
703
                                                ss->memory->non_gc_memory,
302
703
                                                sizeof(s_jbig2_callback_data_t),
303
703
            "s_jbig2decode_init(callback_data)");
304
703
    if (state->callback_data) {
305
703
        state->callback_data->memory = ss->memory->non_gc_memory;
306
703
        state->callback_data->error = 0;
307
703
        state->callback_data->last_message = NULL;
308
703
        state->callback_data->repeats = 0;
309
310
703
        allocator = (s_jbig2decode_allocator_t *) gs_alloc_bytes(ss->memory->non_gc_memory, sizeof (s_jbig2decode_allocator_t), "s_jbig2decode_init(allocator)");
311
703
        if (allocator == NULL) {
312
0
                s_jbig2decode_error(state->callback_data, "failed to allocate custom jbig2dec allocator", JBIG2_SEVERITY_FATAL, -1);
313
0
        }
314
703
        else {
315
703
                allocator->allocator.alloc = s_jbig2decode_alloc;
316
703
                allocator->allocator.free = s_jbig2decode_free;
317
703
                allocator->allocator.realloc = s_jbig2decode_realloc;
318
703
                allocator->mem = ss->memory->non_gc_memory;
319
320
                /* initialize the decoder with the parsed global context if any */
321
703
                state->decode_ctx = jbig2_ctx_new((Jbig2Allocator *) allocator, JBIG2_OPTIONS_EMBEDDED,
322
703
                             global_ctx, s_jbig2decode_error, state->callback_data);
323
324
703
                if (state->decode_ctx == NULL) {
325
0
                        gs_free_object(allocator->mem, allocator, "s_jbig2decode_release");
326
0
                }
327
328
703
        }
329
330
703
        code = state->callback_data->error;
331
703
    }
332
0
    else {
333
0
        code = gs_error_VMerror;
334
0
    }
335
703
    state->image = 0;
336
337
338
703
    return_error (code);
339
703
}
340
341
/* process a section of the input and return any decoded data.
342
   see strimpl.h for return codes.
343
 */
344
static int
345
s_jbig2decode_process(stream_state * ss, stream_cursor_read * pr,
346
                  stream_cursor_write * pw, bool last)
347
101k
{
348
101k
    stream_jbig2decode_state *const state = (stream_jbig2decode_state *) ss;
349
101k
    Jbig2Image *image = state->image;
350
101k
    size_t in_size = pr->limit - pr->ptr;
351
101k
    size_t out_size = pw->limit - pw->ptr;
352
101k
    int status = 0;
353
354
    /* there will only be a single page image,
355
       so pass all data in before looking for any output.
356
       note that the gs stream library expects offset-by-one
357
       indexing of the buffers, while jbig2dec uses normal 0 indexes */
358
101k
    if (in_size > 0) {
359
        /* pass all available input to the decoder */
360
2.94k
        jbig2_data_in(state->decode_ctx, pr->ptr + 1, in_size);
361
2.94k
        pr->ptr += in_size;
362
        /* simulate end-of-page segment */
363
2.94k
        if (last == 1) {
364
681
            jbig2_complete_page(state->decode_ctx);
365
681
        }
366
        /* handle fatal decoding errors reported through our callback */
367
2.94k
        if (state->callback_data->error) return state->callback_data->error;
368
98.5k
    } else {
369
        /* Ran out of input, try and terminate cleanly */
370
98.5k
        if (last == 1) {
371
98.2k
            jbig2_complete_page(state->decode_ctx);
372
98.2k
        }
373
98.5k
    }
374
101k
    if (out_size > 0) {
375
101k
        if (image == NULL) {
376
            /* see if a page image in available */
377
2.85k
            image = jbig2_page_out(state->decode_ctx);
378
2.85k
            if (image != NULL) {
379
276
                state->image = image;
380
276
                state->offset = 0;
381
276
            }
382
2.85k
        }
383
101k
        if (image != NULL) {
384
            /* copy data out of the decoded image, if any */
385
98.5k
            size_t image_size = (size_t)image->height*image->stride;
386
98.5k
            size_t usable = min(image_size - state->offset, out_size);
387
98.5k
            memcpy(pw->ptr + 1, image->data + state->offset, usable);
388
98.5k
            s_jbig2decode_invert_buffer(pw->ptr + 1, usable);
389
98.5k
            state->offset += usable;
390
98.5k
            pw->ptr += usable;
391
98.5k
            status = (state->offset < image_size) ? 1 : 0;
392
98.5k
        }
393
101k
    }
394
395
101k
    return status;
396
101k
}
397
398
/* stream release.
399
   free all our decoder state.
400
 */
401
static void
402
s_jbig2decode_release(stream_state *ss)
403
1.40k
{
404
1.40k
    stream_jbig2decode_state *const state = (stream_jbig2decode_state *) ss;
405
406
1.40k
    if (state->decode_ctx) {
407
703
        s_jbig2decode_allocator_t *allocator = NULL;
408
409
703
        if (state->image) jbig2_release_page(state->decode_ctx, state->image);
410
703
  state->image = NULL;
411
703
        s_jbig2decode_flush_errors(state->callback_data);
412
703
        allocator = (s_jbig2decode_allocator_t *) jbig2_ctx_free(state->decode_ctx);
413
703
  state->decode_ctx = NULL;
414
415
703
        gs_free_object(allocator->mem, allocator, "s_jbig2decode_release");
416
703
    }
417
1.40k
    if (state->callback_data) {
418
703
        gs_memory_t *mem = state->callback_data->memory;
419
703
        gs_free_object(state->callback_data->memory, state->callback_data->last_message, "s_jbig2decode_release(message)");
420
703
        gs_free_object(mem, state->callback_data, "s_jbig2decode_release(callback_data)");
421
703
  state->callback_data = NULL;
422
703
    }
423
1.40k
    if (state->global_struct != NULL) {
424
        /* the interpreter calls jbig2decode_free_global_data() separately */
425
1.40k
    } else {
426
        /* We are responsible for freeing global context */
427
1.40k
        if (state->global_ctx) {
428
0
            s_jbig2decode_free_global_data(state->global_ctx);
429
0
            state->global_ctx = NULL;
430
0
        }
431
1.40k
    }
432
1.40k
}
433
434
void
435
s_jbig2decode_finalize(const gs_memory_t *cmem, void *vptr)
436
703
{
437
703
    (void)cmem;
438
439
703
    s_jbig2decode_release((stream_state *)vptr);
440
703
}
441
442
/* set stream defaults.
443
   this hook exists to avoid confusing the gc with bogus
444
   pointers. we use it similarly just to NULL all the pointers.
445
   (could just be done in _init?)
446
 */
447
static void
448
s_jbig2decode_set_defaults(stream_state *ss)
449
703
{
450
703
    stream_jbig2decode_state *const state = (stream_jbig2decode_state *) ss;
451
452
    /* state->global_ctx is not owned by us */
453
703
    state->global_struct = NULL;
454
703
    state->global_ctx = NULL;
455
703
    state->decode_ctx = NULL;
456
703
    state->image = NULL;
457
703
    state->offset = 0;
458
    state->callback_data = NULL;
459
703
}
460
461
/* stream template */
462
const stream_template s_jbig2decode_template = {
463
    &st_jbig2decode_state,
464
    s_jbig2decode_init,
465
    s_jbig2decode_process,
466
    1, 1, /* min in and out buffer sizes we can handle --should be ~32k,64k for efficiency? */
467
    s_jbig2decode_release,
468
    s_jbig2decode_set_defaults
469
};