Coverage Report

Created: 2026-11-12 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/jbig2dec/jbig2.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
    jbig2dec
18
*/
19
20
#ifdef HAVE_CONFIG_H
21
#include "config.h"
22
#endif
23
#include "os_types.h"
24
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <stdarg.h>
28
#include <string.h>
29
#include <limits.h>
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
static void *
38
jbig2_default_alloc(Jbig2Allocator *allocator, size_t size)
39
0
{
40
0
    (void) allocator;
41
0
    return malloc(size);
42
0
}
43
44
static void
45
jbig2_default_free(Jbig2Allocator *allocator, void *p)
46
0
{
47
0
    (void) allocator;
48
0
    free(p);
49
0
}
50
51
static void *
52
jbig2_default_realloc(Jbig2Allocator *allocator, void *p, size_t size)
53
0
{
54
0
    (void) allocator;
55
0
    return realloc(p, size);
56
0
}
57
58
static Jbig2Allocator jbig2_default_allocator = {
59
    jbig2_default_alloc,
60
    jbig2_default_free,
61
    jbig2_default_realloc
62
};
63
64
void *
65
jbig2_alloc(Jbig2Allocator *allocator, size_t size, size_t num)
66
4.95M
{
67
    /* Check for integer multiplication overflow when computing
68
    the full size of the allocation. */
69
4.95M
    if (num > 0 && size > SIZE_MAX / num)
70
0
        return NULL;
71
4.95M
    return allocator->alloc(allocator, size * num);
72
4.95M
}
73
74
/* jbig2_free and jbig2_realloc moved to the bottom of this file */
75
76
static void
77
jbig2_default_error(void *data, const char *msg, Jbig2Severity severity, uint32_t seg_idx)
78
0
{
79
0
    (void) data;
80
81
    /* report only fatal errors by default */
82
0
    if (severity == JBIG2_SEVERITY_FATAL) {
83
0
        fprintf(stderr, "jbig2 decoder FATAL ERROR: %s", msg);
84
0
        if (seg_idx != JBIG2_UNKNOWN_SEGMENT_NUMBER)
85
0
            fprintf(stderr, " (segment 0x%02x)", seg_idx);
86
0
        fprintf(stderr, "\n");
87
0
        fflush(stderr);
88
0
    }
89
0
}
90
91
int
92
jbig2_error(Jbig2Ctx *ctx, Jbig2Severity severity, uint32_t segment_number, const char *fmt, ...)
93
10.7k
{
94
10.7k
    char buf[1024];
95
10.7k
    va_list ap;
96
10.7k
    int n;
97
98
10.7k
    va_start(ap, fmt);
99
10.7k
    n = vsnprintf(buf, sizeof(buf), fmt, ap);
100
10.7k
    va_end(ap);
101
10.7k
    if (n < 0 || n == sizeof(buf))
102
0
        ctx->error_callback(ctx->error_callback_data, "failed to generate error string", severity, segment_number);
103
10.7k
    else
104
10.7k
        ctx->error_callback(ctx->error_callback_data, buf, severity, segment_number);
105
10.7k
    return -1;
106
10.7k
}
107
108
Jbig2Ctx *
109
jbig2_ctx_new_imp(Jbig2Allocator *allocator, Jbig2Options options, Jbig2GlobalCtx *global_ctx, Jbig2ErrorCallback error_callback, void *error_callback_data, int jbig2_version_major, int jbig2_version_minor)
110
622
{
111
622
    Jbig2Ctx *result;
112
113
622
    if (jbig2_version_major != JBIG2_VERSION_MAJOR || jbig2_version_minor != JBIG2_VERSION_MINOR) {
114
0
        Jbig2Ctx fakectx;
115
0
        fakectx.error_callback = error_callback;
116
0
        fakectx.error_callback_data = error_callback_data;
117
0
        jbig2_error(&fakectx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "incompatible jbig2dec header (%d.%d) and library (%d.%d) versions",
118
0
            jbig2_version_major, jbig2_version_minor, JBIG2_VERSION_MAJOR, JBIG2_VERSION_MINOR);
119
0
        return NULL;
120
0
    }
121
122
622
    if (allocator == NULL)
123
0
        allocator = &jbig2_default_allocator;
124
622
    if (error_callback == NULL)
125
0
        error_callback = &jbig2_default_error;
126
127
622
    result = (Jbig2Ctx *) jbig2_alloc(allocator, sizeof(Jbig2Ctx), 1);
128
622
    if (result == NULL) {
129
0
        error_callback(error_callback_data, "failed to allocate initial context", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
130
0
        return NULL;
131
0
    }
132
133
622
    result->allocator = allocator;
134
622
    result->options = options;
135
622
    result->global_ctx = (const Jbig2Ctx *)global_ctx;
136
622
    result->error_callback = error_callback;
137
622
    result->error_callback_data = error_callback_data;
138
139
622
    result->state = (options & JBIG2_OPTIONS_EMBEDDED) ? JBIG2_FILE_HEADER_MAYBE : JBIG2_FILE_HEADER;
140
141
622
    result->buf = NULL;
142
143
622
    result->n_segments = 0;
144
622
    result->n_segments_max = 16;
145
622
    result->segments = jbig2_new(result, Jbig2Segment *, result->n_segments_max);
146
622
    if (result->segments == NULL) {
147
0
        error_callback(error_callback_data, "failed to allocate initial segments", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
148
0
        jbig2_free(allocator, result);
149
0
        return NULL;
150
0
    }
151
622
    result->segment_index = 0;
152
153
622
    result->current_page = 0;
154
622
    result->max_page_index = 4;
155
622
    result->pages = jbig2_new(result, Jbig2Page, result->max_page_index);
156
622
    if (result->pages == NULL) {
157
0
        error_callback(error_callback_data, "failed to allocated initial pages", JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER);
158
0
        jbig2_free(allocator, result->segments);
159
0
        jbig2_free(allocator, result);
160
0
        return NULL;
161
0
    }
162
622
    {
163
622
        uint32_t index;
164
165
3.11k
        for (index = 0; index < result->max_page_index; index++) {
166
2.48k
            result->pages[index].state = JBIG2_PAGE_FREE;
167
2.48k
            result->pages[index].number = 0;
168
2.48k
            result->pages[index].width = 0;
169
2.48k
            result->pages[index].height = 0xffffffff;
170
2.48k
            result->pages[index].x_resolution = 0;
171
2.48k
            result->pages[index].y_resolution = 0;
172
2.48k
            result->pages[index].stripe_size = 0;
173
2.48k
            result->pages[index].striped = 0;
174
2.48k
            result->pages[index].end_row = 0;
175
2.48k
            result->pages[index].flags = 0;
176
2.48k
            result->pages[index].image = NULL;
177
2.48k
        }
178
622
    }
179
180
622
    return result;
181
622
}
182
183
#define get_uint16(bptr)\
184
12.6k
    (((bptr)[0] << 8) | (bptr)[1])
185
#define get_int16(bptr)\
186
298
    (((int)get_uint16(bptr) ^ 0x8000) - 0x8000)
187
188
/* coverity[ -tainted_data_return ] */
189
/* coverity[ -tainted_data_argument : arg-0 ] */
190
int16_t
191
jbig2_get_int16(const byte *bptr)
192
276
{
193
276
    return get_int16(bptr);
194
276
}
195
196
/* coverity[ -tainted_data_return ] */
197
/* coverity[ -tainted_data_argument : arg-0 ] */
198
uint16_t
199
jbig2_get_uint16(const byte *bptr)
200
144
{
201
144
    return get_uint16(bptr);
202
144
}
203
204
/* coverity[ -tainted_data_return ] */
205
/* coverity[ -tainted_data_argument : arg-0 ] */
206
int32_t
207
jbig2_get_int32(const byte *bptr)
208
22
{
209
22
    return ((int32_t) get_int16(bptr) << 16) | get_uint16(bptr + 2);
210
22
}
211
212
/* coverity[ -tainted_data_return ] */
213
/* coverity[ -tainted_data_argument : arg-0 ] */
214
uint32_t
215
jbig2_get_uint32(const byte *bptr)
216
6.08k
{
217
6.08k
    return ((uint32_t) get_uint16(bptr) << 16) | get_uint16(bptr + 2);
218
6.08k
}
219
220
static size_t
221
jbig2_find_buffer_size(size_t desired)
222
1.38k
{
223
1.38k
    const size_t initial_buf_size = 1024;
224
1.38k
    size_t size = initial_buf_size;
225
226
1.38k
    if (desired == SIZE_MAX)
227
0
        return SIZE_MAX;
228
229
4.22k
    while (size < desired)
230
2.84k
        size <<= 1;
231
232
1.38k
    return size;
233
1.38k
}
234
235
236
/**
237
 * jbig2_data_in: submit data for decoding
238
 * @ctx: The jbig2dec decoder context
239
 * @data: a pointer to the data buffer
240
 * @size: the size of the data buffer in bytes
241
 *
242
 * Copies the specified data into internal storage and attempts
243
 * to (continue to) parse it as part of a jbig2 data stream.
244
 *
245
 * Return code: 0 on success
246
 *             -1 if there is a parsing error
247
 **/
248
int
249
jbig2_data_in(Jbig2Ctx *ctx, const unsigned char *data, size_t size)
250
2.65k
{
251
2.65k
    if (ctx->buf == NULL) {
252
610
        size_t buf_size = jbig2_find_buffer_size(size);
253
610
        ctx->buf = jbig2_new(ctx, byte, buf_size);
254
610
        if (ctx->buf == NULL) {
255
0
            return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate buffer when reading data");
256
0
        }
257
610
        ctx->buf_size = buf_size;
258
610
        ctx->buf_rd_ix = 0;
259
610
        ctx->buf_wr_ix = 0;
260
2.04k
    } else if (size > ctx->buf_size - ctx->buf_wr_ix) {
261
774
        size_t already = ctx->buf_wr_ix - ctx->buf_rd_ix;
262
263
774
        if (ctx->buf_rd_ix <= (ctx->buf_size >> 1) && size <= ctx->buf_size - already) {
264
0
            memmove(ctx->buf, ctx->buf + ctx->buf_rd_ix, already);
265
774
        } else {
266
774
            byte *buf;
267
774
            size_t buf_size;
268
269
774
            if (already > SIZE_MAX - size) {
270
0
                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "read data causes buffer to grow too large");
271
0
            }
272
273
774
            buf_size = jbig2_find_buffer_size(size + already);
274
275
774
            buf = jbig2_new(ctx, byte, buf_size);
276
774
            if (buf == NULL) {
277
0
                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate bigger buffer when reading data");
278
0
            }
279
774
            memcpy(buf, ctx->buf + ctx->buf_rd_ix, already);
280
774
            jbig2_free(ctx->allocator, ctx->buf);
281
774
            ctx->buf = buf;
282
774
            ctx->buf_size = buf_size;
283
774
        }
284
774
        ctx->buf_wr_ix -= ctx->buf_rd_ix;
285
774
        ctx->buf_rd_ix = 0;
286
774
    }
287
288
2.65k
    memcpy(ctx->buf + ctx->buf_wr_ix, data, size);
289
2.65k
    ctx->buf_wr_ix += size;
290
291
    /* data has now been added to buffer */
292
293
4.89k
    for (;;) {
294
4.89k
        const byte jbig2_id_string[8] = { 0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a };
295
4.89k
        Jbig2Segment *segment;
296
4.89k
        size_t header_size;
297
4.89k
        int code;
298
299
4.89k
        switch (ctx->state) {
300
610
        case JBIG2_FILE_HEADER_MAYBE:
301
            /* Accept either a file header, or an embedded file. */
302
610
            if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
303
6
                return 0;
304
604
            if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8) == 0)
305
0
            {
306
0
                ctx->state = JBIG2_FILE_HEADER;
307
0
                (void)jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "JBIG2 file header ignored in supposedly embedded stream");
308
0
            }
309
604
            else
310
604
                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
311
604
            break;
312
0
        case JBIG2_FILE_HEADER:
313
            /* D.4.1 */
314
0
            if (ctx->buf_wr_ix - ctx->buf_rd_ix < 9)
315
0
                return 0;
316
0
            if (memcmp(ctx->buf + ctx->buf_rd_ix, jbig2_id_string, 8))
317
0
                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "not a JBIG2 file header");
318
            /* D.4.2 */
319
0
            ctx->file_header_flags = ctx->buf[ctx->buf_rd_ix + 8];
320
            /* Check for T.88 amendment 2 */
321
0
            if (ctx->file_header_flags & 0x04)
322
0
                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates use of 12 adaptive template pixels (NYI)");
323
            /* Check for T.88 amendment 3 */
324
0
            if (ctx->file_header_flags & 0x08)
325
0
                return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates use of colored region segments (NYI)");
326
0
            if (ctx->file_header_flags & 0xFC) {
327
0
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "reserved bits (2-7) of file header flags are not zero (0x%02x)", ctx->file_header_flags);
328
0
            }
329
            /* D.4.3 */
330
0
            if (!(ctx->file_header_flags & 2)) {        /* number of pages is known */
331
0
                if (ctx->buf_wr_ix - ctx->buf_rd_ix < 13)
332
0
                    return 0;
333
0
                ctx->n_pages = jbig2_get_uint32(ctx->buf + ctx->buf_rd_ix + 9);
334
0
                ctx->buf_rd_ix += 13;
335
0
                if (ctx->n_pages == 1)
336
0
                    jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a single page document");
337
0
                else
338
0
                    jbig2_error(ctx, JBIG2_SEVERITY_INFO, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates a %d page document", ctx->n_pages);
339
0
            } else {            /* number of pages not known */
340
0
                ctx->n_pages = 0;
341
0
                ctx->buf_rd_ix += 9;
342
0
            }
343
            /* determine the file organization based on the flags - D.4.2 again */
344
0
            if (ctx->file_header_flags & 1) {
345
0
                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
346
0
                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates sequential organization");
347
0
            } else {
348
0
                ctx->state = JBIG2_FILE_RANDOM_HEADERS;
349
0
                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, JBIG2_UNKNOWN_SEGMENT_NUMBER, "file header indicates random-access organization");
350
0
            }
351
0
            break;
352
1.20k
        case JBIG2_FILE_SEQUENTIAL_HEADER:
353
1.20k
        case JBIG2_FILE_RANDOM_HEADERS:
354
1.20k
            segment = jbig2_parse_segment_header(ctx, ctx->buf + ctx->buf_rd_ix, ctx->buf_wr_ix - ctx->buf_rd_ix, &header_size);
355
1.20k
            if (segment == NULL)
356
167
                return 0; /* need more data */
357
1.03k
            ctx->buf_rd_ix += header_size;
358
359
1.03k
            if (ctx->n_segments >= ctx->n_segments_max) {
360
0
                Jbig2Segment **segments;
361
362
0
                if (ctx->n_segments_max == UINT32_MAX) {
363
0
                    ctx->state = JBIG2_FILE_EOF;
364
0
                    jbig2_free_segment(ctx, segment);
365
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "too many segments in jbig2 image");
366
0
                }
367
0
                else if (ctx->n_segments_max > (UINT32_MAX >> 2)) {
368
0
                    ctx->n_segments_max = UINT32_MAX;
369
0
                } else {
370
0
                    ctx->n_segments_max <<= 2;
371
0
                }
372
373
0
                segments = jbig2_renew(ctx, ctx->segments, Jbig2Segment *, ctx->n_segments_max);
374
0
                if (segments == NULL) {
375
0
                    ctx->state = JBIG2_FILE_EOF;
376
0
                    jbig2_free_segment(ctx, segment);
377
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate space for more segments");
378
0
                }
379
0
                ctx->segments = segments;
380
0
            }
381
382
1.03k
            ctx->segments[ctx->n_segments++] = segment;
383
1.03k
            if (ctx->state == JBIG2_FILE_RANDOM_HEADERS) {
384
0
                if ((segment->flags & 63) == 51)        /* end of file */
385
0
                    ctx->state = JBIG2_FILE_RANDOM_BODIES;
386
0
            } else              /* JBIG2_FILE_SEQUENTIAL_HEADER */
387
1.03k
                ctx->state = JBIG2_FILE_SEQUENTIAL_BODY;
388
1.03k
            break;
389
3.07k
        case JBIG2_FILE_SEQUENTIAL_BODY:
390
3.07k
        case JBIG2_FILE_RANDOM_BODIES:
391
3.07k
            segment = ctx->segments[ctx->segment_index];
392
393
            /* immediate generic regions may have unknown size */
394
3.07k
            if (segment->data_length == 0xffffffff && (segment->flags & 63) == 38) {
395
1
                byte *s, *e, *p;
396
1
                int mmr;
397
1
                byte mmr_marker[2] = { 0x00, 0x00 };
398
1
                byte arith_marker[2] = { 0xff, 0xac };
399
1
                byte *desired_marker;
400
401
1
                s = p = ctx->buf + ctx->buf_rd_ix;
402
1
                e = ctx->buf + ctx->buf_wr_ix;
403
404
1
                if (e - p < 18)
405
0
                        return 0; /* need more data */
406
407
1
                mmr = p[17] & 1;
408
1
                p += 18;
409
1
                desired_marker = mmr ? mmr_marker : arith_marker;
410
411
                /* look for two byte marker */
412
1
                if (e - p < 2)
413
0
                    return 0; /* need more data */
414
415
229
                while (p[0] != desired_marker[0] || p[1] != desired_marker[1]) {
416
228
                    p++;
417
228
                    if (e - p < 2)
418
0
                        return 0; /* need more data */
419
228
                }
420
1
                p += 2;
421
422
                /* the marker is followed by a four byte row count */
423
1
                if (e - p < 4)
424
0
                        return 0; /* need more data */
425
1
                segment->rows = jbig2_get_uint32(p);
426
1
                p += 4;
427
428
1
                segment->data_length = (size_t) (p - s);
429
1
                jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number, "unknown length determined to be %lu", (long) segment->data_length);
430
1
            }
431
3.07k
            else if (segment->data_length > ctx->buf_wr_ix - ctx->buf_rd_ix)
432
2.44k
                    return 0; /* need more data */
433
434
636
            code = jbig2_parse_segment(ctx, segment, ctx->buf + ctx->buf_rd_ix);
435
636
            ctx->buf_rd_ix += segment->data_length;
436
636
            ctx->segment_index++;
437
636
            if (ctx->state == JBIG2_FILE_RANDOM_BODIES) {
438
0
                if (ctx->segment_index == ctx->n_segments)
439
0
                    ctx->state = JBIG2_FILE_EOF;
440
636
            } else {            /* JBIG2_FILE_SEQUENTIAL_BODY */
441
636
                ctx->state = JBIG2_FILE_SEQUENTIAL_HEADER;
442
636
            }
443
636
            if (code < 0) {
444
36
                ctx->state = JBIG2_FILE_EOF;
445
36
                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode; treating as end of file");
446
36
            }
447
600
            break;
448
600
        case JBIG2_FILE_EOF:
449
1
            if (ctx->buf_rd_ix == ctx->buf_wr_ix)
450
0
                return 0;
451
1
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "garbage beyond end of file");
452
4.89k
        }
453
4.89k
    }
454
2.65k
}
455
456
Jbig2Allocator *
457
jbig2_ctx_free(Jbig2Ctx *ctx)
458
622
{
459
622
    Jbig2Allocator *ca;
460
622
    uint32_t i;
461
462
622
    if (ctx == NULL)
463
0
        return NULL;
464
465
622
    ca = ctx->allocator;
466
622
    jbig2_free(ca, ctx->buf);
467
622
    if (ctx->segments != NULL) {
468
1.66k
        for (i = 0; i < ctx->n_segments; i++)
469
1.03k
            jbig2_free_segment(ctx, ctx->segments[i]);
470
622
        jbig2_free(ca, ctx->segments);
471
622
    }
472
473
622
    if (ctx->pages != NULL) {
474
1.24k
        for (i = 0; i <= ctx->current_page; i++)
475
623
            if (ctx->pages[i].image != NULL)
476
276
                jbig2_image_release(ctx, ctx->pages[i].image);
477
622
        jbig2_free(ca, ctx->pages);
478
622
    }
479
480
622
    jbig2_free(ca, ctx);
481
482
622
    return ca;
483
622
}
484
485
Jbig2GlobalCtx *
486
jbig2_make_global_ctx(Jbig2Ctx *ctx)
487
1
{
488
1
    return (Jbig2GlobalCtx *) ctx;
489
1
}
490
491
Jbig2Allocator *
492
jbig2_global_ctx_free(Jbig2GlobalCtx *global_ctx)
493
1
{
494
1
    return jbig2_ctx_free((Jbig2Ctx *) global_ctx);
495
1
}
496
497
/* I'm not committed to keeping the word stream interface. It's handy
498
   when you think you may be streaming your input, but if you're not
499
   (as is currently the case), it just adds complexity.
500
*/
501
502
typedef struct {
503
    Jbig2WordStream super;
504
    const byte *data;
505
    size_t size;
506
} Jbig2WordStreamBuf;
507
508
static int
509
jbig2_word_stream_buf_get_next_word(Jbig2Ctx *ctx, Jbig2WordStream *self, size_t offset, uint32_t *word)
510
924k
{
511
924k
    Jbig2WordStreamBuf *z = (Jbig2WordStreamBuf *) self;
512
924k
    uint32_t val = 0;
513
924k
    int ret = 0;
514
515
924k
    if (self == NULL || word == NULL) {
516
0
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to read next word of stream because stream or output missing");
517
0
    }
518
924k
    if (offset >= z->size) {
519
100
        *word = 0;
520
100
        return 0;
521
100
    }
522
523
924k
    if (offset < z->size) {
524
924k
        val = (uint32_t) z->data[offset] << 24;
525
924k
        ret++;
526
924k
    }
527
924k
    if (offset + 1 < z->size) {
528
924k
        val |= (uint32_t) z->data[offset + 1] << 16;
529
924k
        ret++;
530
924k
    }
531
924k
    if (offset + 2 < z->size) {
532
924k
        val |= (uint32_t) z->data[offset + 2] << 8;
533
924k
        ret++;
534
924k
    }
535
924k
    if (offset + 3 < z->size) {
536
924k
        val |= z->data[offset + 3];
537
924k
        ret++;
538
924k
    }
539
924k
    *word = val;
540
924k
    return ret;
541
924k
}
542
543
Jbig2WordStream *
544
jbig2_word_stream_buf_new(Jbig2Ctx *ctx, const byte *data, size_t size)
545
318
{
546
318
    Jbig2WordStreamBuf *result = jbig2_new(ctx, Jbig2WordStreamBuf, 1);
547
548
318
    if (result == NULL) {
549
0
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate word stream");
550
0
        return NULL;
551
0
    }
552
553
318
    result->super.get_next_word = jbig2_word_stream_buf_get_next_word;
554
318
    result->data = data;
555
318
    result->size = size;
556
557
318
    return &result->super;
558
318
}
559
560
void
561
jbig2_word_stream_buf_free(Jbig2Ctx *ctx, Jbig2WordStream *ws)
562
320
{
563
320
    jbig2_free(ctx->allocator, ws);
564
320
}
565
566
/* When Memento is in use, the ->free and ->realloc calls get
567
 * turned into ->Memento_free and ->Memento_realloc, which is
568
 * obviously problematic. Undefine free and realloc here to
569
 * avoid this. */
570
#ifdef MEMENTO
571
#undef free
572
#undef realloc
573
#endif
574
575
void
576
jbig2_free(Jbig2Allocator *allocator, void *p)
577
4.95M
{
578
4.95M
    allocator->free(allocator, p);
579
4.95M
}
580
581
void *
582
jbig2_realloc(Jbig2Allocator *allocator, void *p, size_t size, size_t num)
583
5
{
584
    /* check for integer multiplication overflow */
585
5
    if (num > 0 && size >= SIZE_MAX / num)
586
0
        return NULL;
587
5
    return allocator->realloc(allocator, p, size * num);
588
5
}