Coverage Report

Created: 2025-07-11 06:20

/src/jbig2dec/jbig2_page.c
Line
Count
Source (jump to first uncovered line)
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
    jbig2dec
18
*/
19
20
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
#endif
23
#include "os_types.h"
24
25
#include <stdlib.h>
26
27
#ifdef OUTPUT_PBM
28
#include <stdio.h>
29
#endif
30
31
#include "jbig2.h"
32
#include "jbig2_priv.h"
33
#include "jbig2_image.h"
34
#include "jbig2_page.h"
35
#include "jbig2_segment.h"
36
37
/* dump the page struct info */
38
static void
39
dump_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, Jbig2Page *page)
40
4.26k
{
41
4.26k
    if (page->x_resolution == 0) {
42
1.09k
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (unknown res)", page->number, page->width, page->height);
43
3.17k
    } else if (page->x_resolution == page->y_resolution) {
44
154
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "page %d image is %dx%d (%d ppm)", page->number, page->width, page->height, page->x_resolution);
45
3.01k
    } else {
46
3.01k
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
47
3.01k
                    "page %d image is %dx%d (%dx%d ppm)", page->number, page->width, page->height, page->x_resolution, page->y_resolution);
48
3.01k
    }
49
4.26k
    if (page->striped) {
50
1.81k
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "\tmaximum stripe size: %d", page->stripe_size);
51
1.81k
    }
52
4.26k
}
53
54
/**
55
 * jbig2_page_info: parse page info segment
56
 *
57
 * Parse the page info segment data and fill out a corresponding
58
 * Jbig2Page struct and ready it for subsequent rendered data,
59
 * including allocating an image buffer for the page (or the first stripe)
60
 **/
61
int
62
jbig2_page_info(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
63
4.27k
{
64
4.27k
    Jbig2Page *page, *pages;
65
66
    /* a new page info segment implies the previous page is finished */
67
4.27k
    page = &(ctx->pages[ctx->current_page]);
68
4.27k
    if (page->number != 0 && (page->state == JBIG2_PAGE_NEW || page->state == JBIG2_PAGE_FREE)) {
69
2.97k
        page->state = JBIG2_PAGE_COMPLETE;
70
2.97k
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unexpected page info segment, marking previous page finished");
71
2.97k
    }
72
73
    /* find a free page */
74
4.27k
    {
75
4.27k
        size_t index, j;
76
77
4.27k
        index = ctx->current_page;
78
7.80k
        while (ctx->pages[index].state != JBIG2_PAGE_FREE) {
79
3.52k
            index++;
80
3.52k
            if (index >= ctx->max_page_index) {
81
                /* grow the list */
82
83
138
                if (ctx->max_page_index == UINT32_MAX) {
84
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many pages in jbig2 image");
85
0
                }
86
138
                else if (ctx->max_page_index > (UINT32_MAX >> 2)) {
87
0
                    ctx->max_page_index = UINT32_MAX;
88
0
                }
89
90
138
                pages = jbig2_renew(ctx, ctx->pages, Jbig2Page, (ctx->max_page_index <<= 2));
91
138
                if (pages == NULL) {
92
1
                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to reallocate pages");
93
1
                }
94
137
                ctx->pages = pages;
95
6.96k
                for (j = index; j < ctx->max_page_index; j++) {
96
6.82k
                    ctx->pages[j].state = JBIG2_PAGE_FREE;
97
6.82k
                    ctx->pages[j].number = 0;
98
6.82k
                    ctx->pages[j].image = NULL;
99
6.82k
                }
100
137
            }
101
3.52k
        }
102
4.27k
        page = &(ctx->pages[index]);
103
4.27k
        ctx->current_page = index;
104
4.27k
        page->state = JBIG2_PAGE_NEW;
105
4.27k
        page->number = segment->page_association;
106
4.27k
    }
107
108
    /* FIXME: would be nice if we tried to work around this */
109
4.27k
    if (segment->data_length < 19) {
110
3
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
111
3
    }
112
113
    /* 7.4.8.x */
114
4.27k
    page->width = jbig2_get_uint32(segment_data);
115
4.27k
    page->height = jbig2_get_uint32(segment_data + 4);
116
117
4.27k
    page->x_resolution = jbig2_get_uint32(segment_data + 8);
118
4.27k
    page->y_resolution = jbig2_get_uint32(segment_data + 12);
119
4.27k
    page->flags = segment_data[16];
120
    /* Check for T.88 amendment 3 */
121
4.27k
    if (page->flags & 0x80)
122
6
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "page segment indicates use of color segments (NYI)");
123
124
    /* 7.4.8.6 */
125
4.26k
    {
126
4.26k
        int16_t striping = jbig2_get_int16(segment_data + 17);
127
128
4.26k
        if (striping & 0x8000) {
129
1.26k
            page->striped = TRUE;
130
1.26k
            page->stripe_size = striping & 0x7FFF;
131
2.99k
        } else {
132
2.99k
            page->striped = FALSE;
133
2.99k
            page->stripe_size = 0;      /* would page->height be better? */
134
2.99k
        }
135
4.26k
    }
136
4.26k
    if (page->height == 0xFFFFFFFF && page->striped == FALSE) {
137
548
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "height is unspecified but page is not marked as striped, assuming striped with maximum strip size");
138
548
        page->striped = TRUE;
139
548
        page->stripe_size = 0x7FFF;
140
548
    }
141
4.26k
    page->end_row = 0;
142
143
4.26k
    if (segment->data_length > 19) {
144
2.21k
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "extra data in segment");
145
2.21k
    }
146
147
4.26k
    dump_page_info(ctx, segment, page);
148
149
    /* allocate an appropriate page image buffer */
150
    /* 7.4.8.2 */
151
4.26k
    if (page->height == 0xFFFFFFFF) {
152
1.45k
        page->image = jbig2_image_new(ctx, page->width, page->stripe_size);
153
2.81k
    } else {
154
2.81k
        page->image = jbig2_image_new(ctx, page->width, page->height);
155
2.81k
    }
156
4.26k
    if (page->image == NULL) {
157
16
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate buffer for page image");
158
4.25k
    } else {
159
        /* 8.2 (3) fill the page with the default pixel value */
160
4.25k
        jbig2_image_clear(ctx, page->image, (page->flags & 4));
161
4.25k
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
162
4.25k
                    "allocated %dx%d page image (%d bytes)", page->image->width, page->image->height, page->image->stride * page->image->height);
163
4.25k
    }
164
165
4.25k
    return 0;
166
4.26k
}
167
168
/**
169
 * jbig2_end_of_stripe: parse and implement an end of stripe segment
170
 **/
171
int
172
jbig2_end_of_stripe(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
173
420
{
174
420
    Jbig2Page *page = &ctx->pages[ctx->current_page];
175
420
    uint32_t end_row;
176
177
420
    if (segment->data_length < 4)
178
2
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
179
418
    end_row = jbig2_get_uint32(segment_data);
180
418
    if (end_row < page->end_row) {
181
76
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
182
76
                    "end of stripe segment with non-positive end row advance (new end row %d vs current end row %d)", end_row, page->end_row);
183
342
    } else {
184
342
        jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of stripe: advancing end row from %u to %u", page->end_row, end_row);
185
342
    }
186
187
418
    page->end_row = end_row;
188
189
418
    return 0;
190
420
}
191
192
/**
193
 * jbig2_complete_page: complete a page image
194
 *
195
 * called upon seeing an 'end of page' segment, this routine
196
 * marks a page as completed so it can be returned.
197
 * compositing will have already happened in the previous
198
 * segment handlers.
199
 **/
200
int
201
jbig2_complete_page(Jbig2Ctx *ctx)
202
2.68k
{
203
2.68k
    int code;
204
205
    /* check for unfinished segments */
206
2.68k
    if (ctx->segment_index != ctx->n_segments) {
207
2.53k
        Jbig2Segment *segment = ctx->segments[ctx->segment_index];
208
209
        /* Some versions of Xerox Workcentre generate PDF files
210
           with the segment data length field of the last segment
211
           set to -1. Try to cope with this here. */
212
2.53k
        if ((segment->data_length & 0xffffffff) == 0xffffffff) {
213
930
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "file has an invalid segment data length; trying to decode using the available data");
214
930
            segment->data_length = ctx->buf_wr_ix - ctx->buf_rd_ix;
215
930
            code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
216
930
            ctx->buf_rd_ix += segment->data_length;
217
930
            ctx->segment_index++;
218
930
            if (code < 0) {
219
760
                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to parse segment");
220
760
            }
221
930
        }
222
2.53k
    }
223
224
    /* ensure image exists before marking page as complete */
225
1.92k
    if (ctx->pages[ctx->current_page].image == NULL) {
226
236
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page has no image, cannot be completed");
227
236
    }
228
229
1.68k
    ctx->pages[ctx->current_page].state = JBIG2_PAGE_COMPLETE;
230
1.68k
    return 0;
231
1.92k
}
232
233
/**
234
 * jbig2_end_of_page: parse and implement an end of page segment
235
 **/
236
int
237
jbig2_end_of_page(Jbig2Ctx *ctx, Jbig2Segment *segment, const uint8_t *segment_data)
238
1.27k
{
239
1.27k
    uint32_t page_number = ctx->pages[ctx->current_page].number;
240
1.27k
    int code;
241
242
1.27k
    if (segment->page_association != page_number) {
243
845
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
244
845
                    "end of page marker for page %d doesn't match current page number %d", segment->page_association, page_number);
245
845
    }
246
247
1.27k
    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "end of page %d", page_number);
248
249
1.27k
    code = jbig2_complete_page(ctx);
250
1.27k
    if (code < 0)
251
8
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to complete page");
252
253
#ifdef OUTPUT_PBM
254
    code = jbig2_image_write_pbm(ctx->pages[ctx->current_page].image, stdout);
255
    if (code < 0)
256
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to write page image");
257
#endif
258
259
1.26k
    return 0;
260
1.27k
}
261
262
/**
263
 * jbig2_add_page_result: composite a decoding result onto a page
264
 *
265
 * this is called to add the results of segment decode (when it
266
 * is an image) to a page image buffer
267
 **/
268
int
269
jbig2_page_add_result(Jbig2Ctx *ctx, Jbig2Page *page, Jbig2Image *image, uint32_t x, uint32_t y, Jbig2ComposeOp op)
270
10.7k
{
271
10.7k
    int code;
272
273
10.7k
    if (x > INT32_MAX || y > INT32_MAX)
274
133
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unsupported image coordinates");
275
276
    /* ensure image exists first */
277
10.5k
    if (page->image == NULL)
278
30
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page info possibly missing, no image defined");
279
280
    /* grow the page to accommodate a new stripe if necessary */
281
10.5k
    if (page->striped && page->height == 0xFFFFFFFF) {
282
2.98k
        uint32_t new_height;
283
284
2.98k
        if (y > UINT32_MAX - image->height)
285
0
                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "adding image at coordinate would grow page out of bounds");
286
2.98k
        new_height = y + image->height;
287
288
2.98k
        if (page->image->height < new_height) {
289
1.06k
            Jbig2Image *resized_image = NULL;
290
291
1.06k
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "growing page buffer to %u rows to accommodate new stripe", new_height);
292
1.06k
            resized_image = jbig2_image_resize(ctx, page->image, page->image->width, new_height, page->flags & 4);
293
1.06k
            if (resized_image == NULL) {
294
16
                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "unable to resize image to accommodate new stripe");
295
16
            }
296
1.04k
            page->image = resized_image;
297
1.04k
        }
298
2.98k
    }
299
300
10.5k
    code = jbig2_image_compose(ctx, page->image, image, x, y, op);
301
10.5k
    if (code < 0)
302
0
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to compose image with page");
303
304
10.5k
    return 0;
305
10.5k
}
306
307
/**
308
 * jbig2_get_page: return the next available page image buffer
309
 *
310
 * the client can call this at any time to check if any pages
311
 * have been decoded. If so, it returns the first available
312
 * one. The client should then call jbig2_release_page() when
313
 * it no longer needs to refer to the image buffer.
314
 *
315
 * since this is a public routine for the library clients, we
316
 * return an image structure pointer, even though the function
317
 * name refers to a page; the page structure is private.
318
 **/
319
Jbig2Image *
320
jbig2_page_out(Jbig2Ctx *ctx)
321
419
{
322
419
    uint32_t index;
323
324
    /* search for a completed page */
325
684
    for (index = 0; index < ctx->max_page_index; index++) {
326
684
        if (ctx->pages[index].state == JBIG2_PAGE_COMPLETE) {
327
419
            Jbig2Image *img = ctx->pages[index].image;
328
419
            uint32_t page_number = ctx->pages[index].number;
329
330
419
            if (img == NULL) {
331
0
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d returned with no associated image", page_number);
332
0
                continue;
333
0
            }
334
335
419
            ctx->pages[index].state = JBIG2_PAGE_RETURNED;
336
419
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d returned to the client", page_number);
337
419
            return jbig2_image_reference(ctx, img);
338
419
        }
339
684
    }
340
341
    /* no pages available */
342
0
    return NULL;
343
419
}
344
345
/**
346
 * jbig2_release_page: tell the library a page can be freed
347
 **/
348
void
349
jbig2_release_page(Jbig2Ctx *ctx, Jbig2Image *image)
350
419
{
351
419
    uint32_t index;
352
353
419
    if (image == NULL)
354
0
        return;
355
356
    /* find the matching page struct and mark it released */
357
684
    for (index = 0; index < ctx->max_page_index; index++) {
358
684
        if (ctx->pages[index].image == image) {
359
419
            jbig2_image_release(ctx, image);
360
419
            ctx->pages[index].state = JBIG2_PAGE_RELEASED;
361
419
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "page %d released by the client", ctx->pages[index].number);
362
419
            return;
363
419
        }
364
684
    }
365
366
    /* no matching pages */
367
0
    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to release unknown page");
368
0
}