Coverage Report

Created: 2026-03-20 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jbig2dec/jbig2_text.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 <stddef.h>
26
#include <string.h>             /* memset() */
27
28
#include "jbig2.h"
29
#include "jbig2_priv.h"
30
#include "jbig2_arith.h"
31
#include "jbig2_arith_int.h"
32
#include "jbig2_arith_iaid.h"
33
#include "jbig2_generic.h"
34
#include "jbig2_huffman.h"
35
#include "jbig2_image.h"
36
#include "jbig2_page.h"
37
#include "jbig2_refinement.h"
38
#include "jbig2_segment.h"
39
#include "jbig2_symbol_dict.h"
40
#include "jbig2_text.h"
41
42
/**
43
 * jbig2_decode_text_region: decode a text region segment
44
 *
45
 * @ctx: jbig2 decoder context
46
 * @segment: jbig2 segment (header) structure
47
 * @params: parameters from the text region header
48
 * @dicts: an array of referenced symbol dictionaries
49
 * @n_dicts: the number of referenced symbol dictionaries
50
 * @image: image structure in which to store the decoded region bitmap
51
 * @data: pointer to text region data to be decoded
52
 * @size: length of text region data
53
 *
54
 * Implements the text region decoding procedure
55
 * described in section 6.4 of the JBIG2 spec.
56
 *
57
 * returns: 0 on success
58
 **/
59
int
60
jbig2_decode_text_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
61
                         const Jbig2TextRegionParams *params,
62
                         const Jbig2SymbolDict *const *dicts, const uint32_t n_dicts,
63
                         Jbig2Image *image, const byte *data, const size_t size, Jbig2ArithCx *GR_stats, Jbig2ArithState *as, Jbig2WordStream *ws)
64
6.49k
{
65
    /* relevant bits of 6.4.4 */
66
6.49k
    uint32_t NINSTANCES;
67
6.49k
    uint32_t ID;
68
6.49k
    int32_t STRIPT;
69
6.49k
    int32_t FIRSTS;
70
6.49k
    int32_t DT;
71
6.49k
    int32_t DFS;
72
6.49k
    int32_t IDS;
73
6.49k
    int32_t CURS;
74
6.49k
    int32_t CURT;
75
6.49k
    int S, T;
76
6.49k
    int x, y;
77
6.49k
    bool first_symbol;
78
6.49k
    uint32_t index, SBNUMSYMS;
79
6.49k
    Jbig2Image *IB = NULL;
80
6.49k
    Jbig2Image *IBO = NULL;
81
6.49k
    Jbig2Image *refimage = NULL;
82
6.49k
    Jbig2HuffmanState *hs = NULL;
83
6.49k
    Jbig2HuffmanTable *SBSYMCODES = NULL;
84
6.49k
    int code = 0;
85
6.49k
    int RI;
86
87
6.49k
    SBNUMSYMS = 0;
88
20.2k
    for (index = 0; index < n_dicts; index++) {
89
13.7k
        SBNUMSYMS += dicts[index]->n_symbols;
90
13.7k
    }
91
6.49k
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "symbol list contains %d glyphs in %d dictionaries", SBNUMSYMS, n_dicts);
92
93
6.49k
    if (params->SBHUFF) {
94
2.54k
        Jbig2HuffmanTable *runcodes = NULL;
95
2.54k
        Jbig2HuffmanParams runcodeparams;
96
2.54k
        Jbig2HuffmanLine runcodelengths[35];
97
2.54k
        Jbig2HuffmanLine *symcodelengths = NULL;
98
2.54k
        Jbig2HuffmanParams symcodeparams;
99
2.54k
        int err, len, range, r;
100
101
2.54k
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded text region");
102
2.54k
        hs = jbig2_huffman_new(ctx, ws);
103
2.54k
        if (hs == NULL)
104
1
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region");
105
106
        /* 7.4.3.1.7 - decode symbol ID Huffman table */
107
        /* this is actually part of the segment header, but it is more
108
           convenient to handle it here */
109
110
        /* parse and build the runlength code huffman table */
111
91.5k
        for (index = 0; index < 35; index++) {
112
89.0k
            runcodelengths[index].PREFLEN = jbig2_huffman_get_bits(hs, 4, &code);
113
89.0k
            if (code < 0) {
114
0
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to read huffman runcode lengths");
115
0
                goto cleanup1;
116
0
            }
117
89.0k
            if (code > 0) {
118
0
                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding huffman runcode lengths");
119
0
                goto cleanup1;
120
0
            }
121
89.0k
            runcodelengths[index].RANGELEN = 0;
122
89.0k
            runcodelengths[index].RANGELOW = index;
123
89.0k
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d length %d", index, runcodelengths[index].PREFLEN);
124
89.0k
        }
125
2.54k
        runcodeparams.HTOOB = 0;
126
2.54k
        runcodeparams.lines = runcodelengths;
127
2.54k
        runcodeparams.n_lines = 35;
128
2.54k
        runcodes = jbig2_build_huffman_table(ctx, &runcodeparams);
129
2.54k
        if (runcodes == NULL) {
130
8
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error constructing symbol ID runcode table");
131
8
            goto cleanup1;
132
8
        }
133
134
        /* decode the symbol ID code lengths using the runlength table */
135
2.53k
        symcodelengths = jbig2_new(ctx, Jbig2HuffmanLine, SBNUMSYMS);
136
2.53k
        if (symcodelengths == NULL) {
137
2
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "failed to allocate memory when reading symbol ID huffman table");
138
2
            goto cleanup1;
139
2
        }
140
2.53k
        index = 0;
141
13.1M
        while (index < SBNUMSYMS) {
142
13.1M
            code = jbig2_huffman_get(hs, runcodes, &err);
143
13.1M
            if (err < 0) {
144
5
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error reading symbol ID huffman table");
145
5
                goto cleanup1;
146
5
            }
147
13.1M
            if (err > 0) {
148
0
                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding symbol ID huffman table");
149
0
                goto cleanup1;
150
0
            }
151
13.1M
            if (code < 0 || code >= 35) {
152
0
                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "symbol ID huffman table out of range");
153
0
                goto cleanup1;
154
0
            }
155
156
13.1M
            if (code < 32) {
157
11.7M
                len = code;
158
11.7M
                range = 1;
159
11.7M
            } else {
160
1.37M
                if (code == 32) {
161
508k
                    if (index < 1) {
162
2
                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "error decoding symbol ID table: run length with no antecedent");
163
2
                        goto cleanup1;
164
2
                    }
165
508k
                    len = symcodelengths[index - 1].PREFLEN;
166
869k
                } else {
167
869k
                    len = 0;    /* code == 33 or 34 */
168
869k
                }
169
1.37M
                err = 0;
170
1.37M
                if (code == 32)
171
508k
                    range = jbig2_huffman_get_bits(hs, 2, &err) + 3;
172
869k
                else if (code == 33)
173
762k
                    range = jbig2_huffman_get_bits(hs, 3, &err) + 3;
174
107k
                else if (code == 34)
175
107k
                    range = jbig2_huffman_get_bits(hs, 7, &err) + 11;
176
1.37M
                if (err < 0) {
177
0
                    code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to read huffman code");
178
0
                    goto cleanup1;
179
0
                }
180
1.37M
                if (err > 0) {
181
0
                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB decoding huffman code");
182
0
                    goto cleanup1;
183
0
                }
184
1.37M
            }
185
13.1M
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d at index %d (length %d range %d)", code, index, len, range);
186
13.1M
            if (index + range > SBNUMSYMS) {
187
1.71k
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
188
1.71k
                            "runlength extends %d entries beyond the end of symbol ID table", index + range - SBNUMSYMS);
189
1.71k
                range = SBNUMSYMS - index;
190
1.71k
            }
191
30.3M
            for (r = 0; r < range; r++) {
192
17.1M
                symcodelengths[index + r].PREFLEN = len;
193
17.1M
                symcodelengths[index + r].RANGELEN = 0;
194
17.1M
                symcodelengths[index + r].RANGELOW = index + r;
195
17.1M
            }
196
13.1M
            index += r;
197
13.1M
        }
198
199
2.52k
        if (index < SBNUMSYMS) {
200
0
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "runlength codes do not cover the available symbol set");
201
0
            goto cleanup1;
202
0
        }
203
204
2.52k
        symcodeparams.HTOOB = 0;
205
2.52k
        symcodeparams.lines = symcodelengths;
206
2.52k
        symcodeparams.n_lines = SBNUMSYMS;
207
208
        /* skip to byte boundary */
209
2.52k
        err = jbig2_huffman_skip(hs);
210
2.52k
        if (err < 0)
211
0
        {
212
0
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to skip to next byte when building huffman table");
213
0
            goto cleanup1;
214
0
        }
215
216
        /* finally, construct the symbol ID huffman table itself */
217
2.52k
        SBSYMCODES = jbig2_build_huffman_table(ctx, &symcodeparams);
218
219
2.54k
cleanup1:
220
2.54k
        jbig2_free(ctx->allocator, symcodelengths);
221
2.54k
        jbig2_release_huffman_table(ctx, runcodes);
222
223
2.54k
        if (SBSYMCODES == NULL) {
224
68
            jbig2_huffman_free(ctx, hs);
225
68
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to construct symbol ID huffman table");
226
68
        }
227
2.54k
    }
228
229
    /* 6.4.5 (1) */
230
6.43k
    jbig2_image_clear(ctx, image, params->SBDEFPIXEL);
231
232
    /* 6.4.6 */
233
6.43k
    if (params->SBHUFF) {
234
2.47k
        STRIPT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
235
3.95k
    } else {
236
3.95k
        code = jbig2_arith_int_decode(ctx, params->IADT, as, &STRIPT);
237
3.95k
    }
238
6.43k
    if (code < 0) {
239
1
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode strip T");
240
1
        goto cleanup2;
241
1
    }
242
6.42k
    if (code > 0) {
243
2
        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding strip T");
244
2
        goto cleanup2;
245
2
    }
246
247
    /* 6.4.5 (2) */
248
6.42k
    STRIPT *= -(params->SBSTRIPS);
249
6.42k
    FIRSTS = 0;
250
6.42k
    NINSTANCES = 0;
251
252
    /* 6.4.5 (3) */
253
2.24M
    while (NINSTANCES < params->SBNUMINSTANCES) {
254
        /* (3b) */
255
2.23M
        if (params->SBHUFF) {
256
2.21M
            DT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
257
2.21M
        } else {
258
20.6k
            code = jbig2_arith_int_decode(ctx, params->IADT, as, &DT);
259
20.6k
        }
260
2.23M
        if (code < 0) {
261
1
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode delta T");
262
1
            goto cleanup2;
263
1
        }
264
2.23M
        if (code > 0) {
265
27
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding delta T");
266
27
            goto cleanup2;
267
27
        }
268
2.23M
        DT *= params->SBSTRIPS;
269
2.23M
        STRIPT += DT;
270
271
2.23M
        first_symbol = TRUE;
272
        /* 6.4.5 (3c) - decode symbols in strip */
273
43.2M
        for (;;) {
274
            /* (3c.i) */
275
43.2M
            if (first_symbol) {
276
                /* 6.4.7 */
277
2.23M
                if (params->SBHUFF) {
278
2.21M
                    DFS = jbig2_huffman_get(hs, params->SBHUFFFS, &code);
279
2.21M
                } else {
280
20.5k
                    code = jbig2_arith_int_decode(ctx, params->IAFS, as, &DFS);
281
20.5k
                }
282
2.23M
                if (code < 0) {
283
1
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode strip symbol S-difference");
284
1
                    goto cleanup2;
285
1
                }
286
2.23M
                if (code > 0) {
287
62
                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding strip symbol S-difference");
288
62
                    goto cleanup2;
289
62
                }
290
2.23M
                FIRSTS += DFS;
291
2.23M
                CURS = FIRSTS;
292
2.23M
                first_symbol = FALSE;
293
40.9M
            } else {
294
40.9M
                if (NINSTANCES > params->SBNUMINSTANCES) {
295
4.09k
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "too many NINSTANCES (%d) decoded", NINSTANCES);
296
4.09k
                    break;
297
4.09k
                }
298
                /* (3c.ii) / 6.4.8 */
299
40.9M
                if (params->SBHUFF) {
300
32.4M
                    IDS = jbig2_huffman_get(hs, params->SBHUFFDS, &code);
301
32.4M
                } else {
302
8.49M
                    code = jbig2_arith_int_decode(ctx, params->IADS, as, &IDS);
303
8.49M
                }
304
40.9M
                if (code < 0) {
305
1
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol instance S coordinate");
306
1
                    goto cleanup2;
307
1
                }
308
40.9M
                if (code > 0) {
309
2.23M
                    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "OOB obtained when decoding symbol instance S coordinate signals end of strip with T value %d", DT);
310
2.23M
                    break;
311
2.23M
                }
312
38.7M
                CURS += IDS + params->SBDSOFFSET;
313
38.7M
            }
314
315
            /* (3c.iii) / 6.4.9 */
316
40.9M
            if (params->SBSTRIPS == 1) {
317
27.0M
                CURT = 0;
318
27.0M
            } else if (params->SBHUFF) {
319
13.8M
                CURT = jbig2_huffman_get_bits(hs, params->LOGSBSTRIPS, &code);
320
13.8M
            } else {
321
91.1k
                code = jbig2_arith_int_decode(ctx, params->IAIT, as, &CURT);
322
91.1k
            }
323
40.9M
            if (code < 0) {
324
0
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol instance T coordinate");
325
0
                goto cleanup2;
326
0
            }
327
40.9M
            if (code > 0) {
328
47
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "OOB obtained when decoding symbol instance T coordinate");
329
47
                goto cleanup2;
330
47
            }
331
40.9M
            T = STRIPT + CURT;
332
333
            /* (3b.iv) / 6.4.10 - decode the symbol ID */
334
40.9M
            if (params->SBHUFF) {
335
32.4M
                ID = jbig2_huffman_get(hs, SBSYMCODES, &code);
336
32.4M
            } else {
337
8.49M
                code = jbig2_arith_iaid_decode(ctx, params->IAID, as, (int *)&ID);
338
8.49M
            }
339
40.9M
            if (code < 0) {
340
16
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to obtain symbol instance symbol ID");
341
16
                goto cleanup2;
342
16
            }
343
40.9M
            if (code > 0) {
344
0
                code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol instance symbol ID");
345
0
                goto cleanup2;
346
0
            }
347
40.9M
            if (ID >= SBNUMSYMS) {
348
4.05M
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring out of range symbol ID (%d/%d)", ID, SBNUMSYMS);
349
4.05M
                IB = NULL;
350
36.9M
            } else {
351
                /* (3c.v) / 6.4.11 - look up the symbol bitmap IB */
352
36.9M
                uint32_t id = ID;
353
354
36.9M
                index = 0;
355
45.5M
                while (id >= dicts[index]->n_symbols)
356
8.59M
                    id -= dicts[index++]->n_symbols;
357
36.9M
                if (dicts[index]->glyphs[id] == NULL) {
358
3.92M
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "missing glyph (%d/%d), ignoring", index, id);
359
33.0M
                } else {
360
33.0M
                    IB = jbig2_image_reference(ctx, dicts[index]->glyphs[id]);
361
33.0M
                }
362
36.9M
            }
363
40.9M
            if (params->SBREFINE) {
364
21.6M
                if (params->SBHUFF) {
365
21.5M
                    RI = jbig2_huffman_get_bits(hs, 1, &code);
366
21.5M
                } else {
367
108k
                    code = jbig2_arith_int_decode(ctx, params->IARI, as, &RI);
368
108k
                }
369
21.6M
                if (code < 0) {
370
0
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode symbol bitmap refinement indicator");
371
0
                    goto cleanup2;
372
0
                }
373
21.6M
                if (code > 0) {
374
19
                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol bitmap refinement indicator");
375
19
                    goto cleanup2;
376
19
                }
377
21.6M
            } else {
378
19.3M
                RI = 0;
379
19.3M
            }
380
40.9M
            if (RI) {
381
107k
                Jbig2RefinementRegionParams rparams;
382
107k
                int32_t RDW, RDH, RDX, RDY;
383
107k
                size_t BMSIZE = 0;
384
107k
                int code1 = 0;
385
107k
                int code2 = 0;
386
107k
                int code3 = 0;
387
107k
                int code4 = 0;
388
107k
                int code5 = 0;
389
107k
                int code6 = 0;
390
391
                /* 6.4.11 (1, 2, 3, 4) */
392
107k
                if (!params->SBHUFF) {
393
99.8k
                    code1 = jbig2_arith_int_decode(ctx, params->IARDW, as, &RDW);
394
99.8k
                    code2 = jbig2_arith_int_decode(ctx, params->IARDH, as, &RDH);
395
99.8k
                    code3 = jbig2_arith_int_decode(ctx, params->IARDX, as, &RDX);
396
99.8k
                    code4 = jbig2_arith_int_decode(ctx, params->IARDY, as, &RDY);
397
99.8k
                } else {
398
7.23k
                    RDW = jbig2_huffman_get(hs, params->SBHUFFRDW, &code1);
399
7.23k
                    RDH = jbig2_huffman_get(hs, params->SBHUFFRDH, &code2);
400
7.23k
                    RDX = jbig2_huffman_get(hs, params->SBHUFFRDX, &code3);
401
7.23k
                    RDY = jbig2_huffman_get(hs, params->SBHUFFRDY, &code4);
402
7.23k
                    BMSIZE = jbig2_huffman_get(hs, params->SBHUFFRSIZE, &code5);
403
7.23k
                    code6 = jbig2_huffman_skip(hs);
404
7.23k
                }
405
406
107k
                if (code1 < 0 || code2 < 0 || code3 < 0 || code4 < 0 || code5 < 0 || code6 < 0) {
407
7
                    code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode data");
408
7
                    goto cleanup2;
409
7
                }
410
107k
                if (code1 > 0 || code2 > 0 || code3 > 0 || code4 > 0 || code5 > 0 || code6 > 0) {
411
42
                    code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "OOB obtained when decoding symbol instance refinement data");
412
42
                    goto cleanup2;
413
42
                }
414
415
                /* 6.4.11 (6) */
416
106k
                if (IB) {
417
90.8k
                    IBO = IB;
418
90.8k
                    IB = NULL;
419
90.8k
                    if (((int32_t) IBO->width) + RDW < 0 || ((int32_t) IBO->height) + RDH < 0) {
420
30
                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference image dimensions negative");
421
30
                        goto cleanup2;
422
30
                    }
423
90.8k
                    refimage = jbig2_image_new(ctx, IBO->width + RDW, IBO->height + RDH);
424
90.8k
                    if (refimage == NULL) {
425
12
                        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate reference image");
426
12
                        goto cleanup2;
427
12
                    }
428
90.8k
                    jbig2_image_clear(ctx, refimage, 0x00);
429
430
                    /* Table 12 */
431
90.8k
                    rparams.GRTEMPLATE = params->SBRTEMPLATE;
432
90.8k
                    rparams.GRREFERENCE = IBO;
433
90.8k
                    rparams.GRREFERENCEDX = (RDW >> 1) + RDX;
434
90.8k
                    rparams.GRREFERENCEDY = (RDH >> 1) + RDY;
435
90.8k
                    rparams.TPGRON = 0;
436
90.8k
                    memcpy(rparams.grat, params->sbrat, 4);
437
90.8k
                    code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, refimage, GR_stats);
438
90.8k
                    if (code < 0) {
439
12
                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode refinement region");
440
12
                        goto cleanup2;
441
12
                    }
442
443
90.8k
                    jbig2_image_release(ctx, IBO);
444
90.8k
                    IBO = NULL;
445
90.8k
                    IB = refimage;
446
90.8k
                    refimage = NULL;
447
90.8k
                }
448
449
                /* 6.4.11 (7) */
450
106k
                if (params->SBHUFF) {
451
7.20k
                    code = jbig2_huffman_advance(hs, BMSIZE);
452
7.20k
                    if (code < 0) {
453
0
                        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to advance after huffman decoding refinement region");
454
0
                        goto cleanup2;
455
0
                    }
456
7.20k
                }
457
106k
            }
458
459
            /* (3c.vi) */
460
40.9M
            if ((!params->TRANSPOSED) && (params->REFCORNER > 1) && IB) {
461
580k
                CURS += IB->width - 1;
462
40.4M
            } else if ((params->TRANSPOSED) && !(params->REFCORNER & 1) && IB) {
463
8.73M
                CURS += IB->height - 1;
464
8.73M
            }
465
466
            /* (3c.vii) */
467
40.9M
            S = CURS;
468
469
            /* (3c.viii) */
470
40.9M
            if (!params->TRANSPOSED) {
471
23.4M
                switch (params->REFCORNER) {
472
5.02M
                case JBIG2_CORNER_TOPLEFT:
473
5.02M
                    x = S;
474
5.02M
                    y = T;
475
5.02M
                    break;
476
2.44M
                case JBIG2_CORNER_TOPRIGHT:
477
2.44M
                    if (IB)
478
11.0k
                        x = S - IB->width + 1;
479
2.43M
                    else
480
2.43M
                        x = S + 1;
481
2.44M
                    y = T;
482
2.44M
                    break;
483
15.3M
                case JBIG2_CORNER_BOTTOMLEFT:
484
15.3M
                    x = S;
485
15.3M
                    if (IB)
486
13.9M
                        y = T - IB->height + 1;
487
1.39M
                    else
488
1.39M
                        y = T + 1;
489
15.3M
                    break;
490
0
                default:
491
609k
                case JBIG2_CORNER_BOTTOMRIGHT:
492
609k
                    if (IB ) {
493
569k
                        x = S - IB->width + 1;
494
569k
                        y = T - IB->height + 1;
495
569k
                    } else {
496
39.0k
                        x = S + 1;
497
39.0k
                        y = T + 1;
498
39.0k
                    }
499
609k
                    break;
500
23.4M
                }
501
23.4M
            } else {            /* TRANSPOSED */
502
17.5M
                switch (params->REFCORNER) {
503
728k
                case JBIG2_CORNER_TOPLEFT:
504
728k
                    x = T;
505
728k
                    y = S;
506
728k
                    break;
507
8.08M
                case JBIG2_CORNER_TOPRIGHT:
508
8.08M
                    if (IB)
509
8.08M
                        x = T - IB->width + 1;
510
3.47k
                    else
511
3.47k
                        x = T + 1;
512
8.08M
                    y = S;
513
8.08M
                    break;
514
8.73M
                case JBIG2_CORNER_BOTTOMLEFT:
515
8.73M
                    x = T;
516
8.73M
                    if (IB)
517
8.73M
                        y = S - IB->height + 1;
518
3.10k
                    else
519
3.10k
                        y = S + 1;
520
8.73M
                    break;
521
0
                default:
522
3.63k
                case JBIG2_CORNER_BOTTOMRIGHT:
523
3.63k
                    if (IB) {
524
887
                        x = T - IB->width + 1;
525
887
                        y = S - IB->height + 1;
526
2.74k
                    } else {
527
2.74k
                        x = T + 1;
528
2.74k
                        y = S + 1;
529
2.74k
                    }
530
3.63k
                    break;
531
17.5M
                }
532
17.5M
            }
533
534
            /* (3c.ix) */
535
#ifdef JBIG2_DEBUG
536
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
537
                        "composing glyph ID %d: %dx%d @ (%d,%d) symbol %d/%d", ID, IB->width, IB->height, x, y, NINSTANCES + 1, params->SBNUMINSTANCES);
538
#endif
539
40.9M
            code = jbig2_image_compose(ctx, image, IB, x, y, params->SBCOMBOP);
540
40.9M
            if (code < 0) {
541
0
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to compose symbol instance symbol bitmap into picture");
542
0
                goto cleanup2;
543
0
            }
544
545
            /* (3c.x) */
546
40.9M
            if (IB && (!params->TRANSPOSED) && (params->REFCORNER < 2)) {
547
14.8M
                CURS += IB->width - 1;
548
26.1M
            } else if (IB && (params->TRANSPOSED) && (params->REFCORNER & 1)) {
549
8.81M
                CURS += IB->height - 1;
550
8.81M
            }
551
552
            /* (3c.xi) */
553
40.9M
            NINSTANCES++;
554
555
40.9M
            jbig2_image_release(ctx, IB);
556
40.9M
            IB = NULL;
557
40.9M
        }
558
        /* end strip */
559
2.23M
    }
560
    /* 6.4.5 (4) */
561
562
6.43k
cleanup2:
563
6.43k
    jbig2_image_release(ctx, refimage);
564
6.43k
    jbig2_image_release(ctx, IBO);
565
6.43k
    jbig2_image_release(ctx, IB);
566
6.43k
    if (params->SBHUFF) {
567
2.47k
        jbig2_release_huffman_table(ctx, SBSYMCODES);
568
2.47k
    }
569
6.43k
    jbig2_huffman_free(ctx, hs);
570
571
6.43k
    return code;
572
6.42k
}
573
574
/**
575
 * jbig2_text_region: read a text region segment header
576
 **/
577
int
578
jbig2_text_region(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
579
3.82k
{
580
3.82k
    uint32_t offset = 0;
581
3.82k
    Jbig2RegionSegmentInfo region_info;
582
3.82k
    Jbig2TextRegionParams params;
583
3.82k
    Jbig2Image *image = NULL;
584
3.82k
    Jbig2SymbolDict **dicts = NULL;
585
3.82k
    uint32_t n_dicts = 0;
586
3.82k
    uint16_t flags = 0;
587
3.82k
    uint16_t huffman_flags = 0;
588
3.82k
    Jbig2ArithCx *GR_stats = NULL;
589
3.82k
    int code = 0;
590
3.82k
    Jbig2WordStream *ws = NULL;
591
3.82k
    Jbig2ArithState *as = NULL;
592
3.82k
    uint32_t table_index = 0;
593
3.82k
    const Jbig2HuffmanParams *huffman_params = NULL;
594
595
    /* zero params to ease cleanup later */
596
3.82k
    memset(&params, 0, sizeof(Jbig2TextRegionParams));
597
598
    /* 7.4.1 */
599
3.82k
    if (segment->data_length < 17) {
600
10
        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
601
10
        goto cleanup2;
602
10
    }
603
3.81k
    jbig2_get_region_segment_info(&region_info, segment_data);
604
3.81k
    offset += 17;
605
    /* Check for T.88 amendment 3 */
606
3.81k
    if (region_info.flags & 8)
607
4
        return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "region segment flags indicate use of colored bitmap (NYI)");
608
609
    /* 7.4.3.1.1 */
610
3.81k
    if (segment->data_length - offset < 2) {
611
3
        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
612
3
        goto cleanup2;
613
3
    }
614
3.81k
    flags = jbig2_get_uint16(segment_data + offset);
615
3.81k
    offset += 2;
616
617
3.81k
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region header flags 0x%04x", flags);
618
619
3.81k
    params.SBHUFF = flags & 0x0001;
620
3.81k
    params.SBREFINE = flags & 0x0002;
621
3.81k
    params.LOGSBSTRIPS = (flags & 0x000c) >> 2;
622
3.81k
    params.SBSTRIPS = 1 << params.LOGSBSTRIPS;
623
3.81k
    params.REFCORNER = (Jbig2RefCorner)((flags & 0x0030) >> 4);
624
3.81k
    params.TRANSPOSED = flags & 0x0040;
625
3.81k
    params.SBCOMBOP = (Jbig2ComposeOp)((flags & 0x0180) >> 7);
626
3.81k
    params.SBDEFPIXEL = flags & 0x0200;
627
    /* SBDSOFFSET is a signed 5 bit integer */
628
3.81k
    params.SBDSOFFSET = (flags & 0x7C00) >> 10;
629
3.81k
    if (params.SBDSOFFSET > 0x0f)
630
222
        params.SBDSOFFSET -= 0x20;
631
3.81k
    params.SBRTEMPLATE = flags & 0x8000;
632
633
3.81k
    if (params.SBDSOFFSET) {
634
2.88k
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region has SBDSOFFSET %d", params.SBDSOFFSET);
635
2.88k
    }
636
637
3.81k
    if (params.SBHUFF) {        /* Huffman coding */
638
        /* 7.4.3.1.2 */
639
528
        if (segment->data_length - offset < 2) {
640
5
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
641
5
            goto cleanup2;
642
5
        }
643
523
        huffman_flags = jbig2_get_uint16(segment_data + offset);
644
523
        offset += 2;
645
646
523
        if (huffman_flags & 0x8000)
647
59
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved bit 15 of text region huffman flags is not zero");
648
3.28k
    } else {                    /* arithmetic coding */
649
650
        /* 7.4.3.1.3 */
651
3.28k
        if (segment->data_length - offset < 4) {
652
2
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
653
2
            goto cleanup2;
654
2
        }
655
3.28k
        if ((params.SBREFINE) && !(params.SBRTEMPLATE)) {
656
74
            params.sbrat[0] = segment_data[offset];
657
74
            params.sbrat[1] = segment_data[offset + 1];
658
74
            params.sbrat[2] = segment_data[offset + 2];
659
74
            params.sbrat[3] = segment_data[offset + 3];
660
74
            offset += 4;
661
74
        }
662
3.28k
    }
663
664
    /* 7.4.3.1.4 */
665
3.80k
    if (segment->data_length - offset < 4) {
666
8
        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
667
8
        goto cleanup2;
668
8
    }
669
3.79k
    params.SBNUMINSTANCES = jbig2_get_uint32(segment_data + offset);
670
3.79k
    offset += 4;
671
672
3.79k
    if (params.SBHUFF) {
673
        /* 7.4.3.1.5 - Symbol ID Huffman table */
674
        /* ...this is handled in the segment body decoder */
675
676
        /* 7.4.3.1.6 - Other Huffman table selection */
677
516
        switch (huffman_flags & 0x0003) {
678
399
        case 0:                /* Table B.6 */
679
399
            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);
680
399
            break;
681
88
        case 1:                /* Table B.7 */
682
88
            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_G);
683
88
            break;
684
25
        case 3:                /* Custom table from referred segment */
685
25
            huffman_params = jbig2_find_table(ctx, segment, table_index);
686
25
            if (huffman_params == NULL) {
687
12
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom FS huffman table not found (%d)", table_index);
688
12
                goto cleanup1;
689
12
            }
690
13
            params.SBHUFFFS = jbig2_build_huffman_table(ctx, huffman_params);
691
13
            ++table_index;
692
13
            break;
693
4
        case 2:                /* invalid */
694
4
        default:
695
4
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid FS huffman table");
696
4
            goto cleanup1;
697
0
            break;
698
516
        }
699
500
        if (params.SBHUFFFS == NULL) {
700
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified FS huffman table");
701
1
            goto cleanup1;
702
1
        }
703
704
499
        switch ((huffman_flags & 0x000c) >> 2) {
705
350
        case 0:                /* Table B.8 */
706
350
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);
707
350
            break;
708
71
        case 1:                /* Table B.9 */
709
71
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_I);
710
71
            break;
711
48
        case 2:                /* Table B.10 */
712
48
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_J);
713
48
            break;
714
30
        case 3:                /* Custom table from referred segment */
715
30
            huffman_params = jbig2_find_table(ctx, segment, table_index);
716
30
            if (huffman_params == NULL) {
717
3
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DS huffman table not found (%d)", table_index);
718
3
                goto cleanup1;
719
3
            }
720
27
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, huffman_params);
721
27
            ++table_index;
722
27
            break;
723
499
        }
724
496
        if (params.SBHUFFDS == NULL) {
725
2
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified DS huffman table");
726
2
            goto cleanup1;
727
2
        }
728
729
494
        switch ((huffman_flags & 0x0030) >> 4) {
730
93
        case 0:                /* Table B.11 */
731
93
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);
732
93
            break;
733
259
        case 1:                /* Table B.12 */
734
259
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_L);
735
259
            break;
736
123
        case 2:                /* Table B.13 */
737
123
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_M);
738
123
            break;
739
19
        case 3:                /* Custom table from referred segment */
740
19
            huffman_params = jbig2_find_table(ctx, segment, table_index);
741
19
            if (huffman_params == NULL) {
742
3
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DT huffman table not found (%d)", table_index);
743
3
                goto cleanup1;
744
3
            }
745
16
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, huffman_params);
746
16
            ++table_index;
747
16
            break;
748
494
        }
749
491
        if (params.SBHUFFDT == NULL) {
750
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified DT huffman table");
751
1
            goto cleanup1;
752
1
        }
753
754
490
        switch ((huffman_flags & 0x00c0) >> 6) {
755
412
        case 0:                /* Table B.14 */
756
412
            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
757
412
            break;
758
49
        case 1:                /* Table B.15 */
759
49
            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
760
49
            break;
761
28
        case 3:                /* Custom table from referred segment */
762
28
            huffman_params = jbig2_find_table(ctx, segment, table_index);
763
28
            if (huffman_params == NULL) {
764
5
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDW huffman table not found (%d)", table_index);
765
5
                goto cleanup1;
766
5
            }
767
23
            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, huffman_params);
768
23
            ++table_index;
769
23
            break;
770
1
        case 2:                /* invalid */
771
1
        default:
772
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDW huffman table");
773
1
            goto cleanup1;
774
0
            break;
775
490
        }
776
484
        if (params.SBHUFFRDW == NULL) {
777
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDW huffman table");
778
1
            goto cleanup1;
779
1
        }
780
781
483
        switch ((huffman_flags & 0x0300) >> 8) {
782
191
        case 0:                /* Table B.14 */
783
191
            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
784
191
            break;
785
286
        case 1:                /* Table B.15 */
786
286
            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
787
286
            break;
788
4
        case 3:                /* Custom table from referred segment */
789
4
            huffman_params = jbig2_find_table(ctx, segment, table_index);
790
4
            if (huffman_params == NULL) {
791
1
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDH huffman table not found (%d)", table_index);
792
1
                goto cleanup1;
793
1
            }
794
3
            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, huffman_params);
795
3
            ++table_index;
796
3
            break;
797
2
        case 2:                /* invalid */
798
2
        default:
799
2
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDH huffman table");
800
2
            goto cleanup1;
801
0
            break;
802
483
        }
803
480
        if (params.SBHUFFRDH == NULL) {
804
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDH huffman table");
805
1
            goto cleanup1;
806
1
        }
807
808
479
        switch ((huffman_flags & 0x0c00) >> 10) {
809
421
        case 0:                /* Table B.14 */
810
421
            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
811
421
            break;
812
47
        case 1:                /* Table B.15 */
813
47
            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
814
47
            break;
815
10
        case 3:                /* Custom table from referred segment */
816
10
            huffman_params = jbig2_find_table(ctx, segment, table_index);
817
10
            if (huffman_params == NULL) {
818
3
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDX huffman table not found (%d)", table_index);
819
3
                goto cleanup1;
820
3
            }
821
7
            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, huffman_params);
822
7
            ++table_index;
823
7
            break;
824
1
        case 2:                /* invalid */
825
1
        default:
826
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDX huffman table");
827
1
            goto cleanup1;
828
0
            break;
829
479
        }
830
475
        if (params.SBHUFFRDX == NULL) {
831
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDX huffman table");
832
1
            goto cleanup1;
833
1
        }
834
835
474
        switch ((huffman_flags & 0x3000) >> 12) {
836
417
        case 0:                /* Table B.14 */
837
417
            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
838
417
            break;
839
44
        case 1:                /* Table B.15 */
840
44
            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
841
44
            break;
842
12
        case 3:                /* Custom table from referred segment */
843
12
            huffman_params = jbig2_find_table(ctx, segment, table_index);
844
12
            if (huffman_params == NULL) {
845
3
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RDY huffman table not found (%d)", table_index);
846
3
                goto cleanup1;
847
3
            }
848
9
            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, huffman_params);
849
9
            ++table_index;
850
9
            break;
851
1
        case 2:                /* invalid */
852
1
        default:
853
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDY huffman table");
854
1
            goto cleanup1;
855
0
            break;
856
474
        }
857
470
        if (params.SBHUFFRDY == NULL) {
858
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RDY huffman table");
859
1
            goto cleanup1;
860
1
        }
861
862
469
        switch ((huffman_flags & 0x4000) >> 14) {
863
460
        case 0:                /* Table B.1 */
864
460
            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
865
460
            break;
866
9
        case 1:                /* Custom table from referred segment */
867
9
            huffman_params = jbig2_find_table(ctx, segment, table_index);
868
9
            if (huffman_params == NULL) {
869
3
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom RSIZE huffman table not found (%d)", table_index);
870
3
                goto cleanup1;
871
3
            }
872
6
            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, huffman_params);
873
6
            ++table_index;
874
6
            break;
875
469
        }
876
466
        if (params.SBHUFFRSIZE == NULL) {
877
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region specified RSIZE huffman table");
878
1
            goto cleanup1;
879
1
        }
880
881
465
        if (huffman_flags & 0x8000) {
882
21
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region huffman flags bit 15 is set, contrary to spec");
883
21
        }
884
885
        /* 7.4.3.1.7 */
886
        /* For convenience this is done in the body decoder routine */
887
465
    }
888
889
3.74k
    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
890
3.74k
                "text region: %d x %d @ (%d,%d) %d symbols", region_info.width, region_info.height, region_info.x, region_info.y, params.SBNUMINSTANCES);
891
892
    /* 7.4.3.2 (2) - compose the list of symbol dictionaries */
893
3.74k
    n_dicts = jbig2_sd_count_referred(ctx, segment);
894
3.74k
    if (n_dicts == 0) {
895
715
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region refers to no symbol dictionaries");
896
3.03k
    } else {
897
3.03k
        dicts = jbig2_sd_list_referred(ctx, segment);
898
3.03k
        if (dicts == NULL) {
899
1
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "unable to retrieve symbol dictionaries! previous parsing error?");
900
1
            goto cleanup1;
901
3.02k
        } else {
902
3.02k
            uint32_t index;
903
904
3.02k
            if (dicts[0] == NULL) {
905
0
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to find first referenced symbol dictionary");
906
0
                goto cleanup1;
907
0
            }
908
9.51k
            for (index = 1; index < n_dicts; index++)
909
6.48k
                if (dicts[index] == NULL) {
910
0
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to find all referenced symbol dictionaries");
911
0
                    n_dicts = index;
912
0
                }
913
3.02k
        }
914
3.03k
    }
915
916
    /* 7.4.3.2 (3) */
917
3.74k
    {
918
3.74k
        int stats_size = params.SBRTEMPLATE ? 1 << 10 : 1 << 13;
919
920
3.74k
        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
921
3.74k
        if (GR_stats == NULL) {
922
2
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "could not allocate arithmetic decoder state");
923
2
            goto cleanup1;
924
2
        }
925
3.74k
        memset(GR_stats, 0, stats_size);
926
3.74k
    }
927
928
0
    image = jbig2_image_new(ctx, region_info.width, region_info.height);
929
3.74k
    if (image == NULL) {
930
37
        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image");
931
37
        goto cleanup2;
932
37
    }
933
934
3.70k
    if (offset >= segment->data_length) {
935
1
        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "segment too short");
936
1
        goto cleanup2;
937
1
    }
938
3.70k
    ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
939
3.70k
    if (ws == NULL) {
940
1
        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate word stream when handling text region image");
941
1
        goto cleanup2;
942
1
    }
943
944
3.70k
    as = jbig2_arith_new(ctx, ws);
945
3.70k
    if (as == NULL) {
946
1
        code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate arithmetic coding context when handling text region image");
947
1
        goto cleanup3;
948
1
    }
949
950
3.70k
    if (!params.SBHUFF) {
951
3.24k
        uint8_t SBSYMCODELEN;
952
3.24k
        uint32_t index;
953
3.24k
        uint32_t SBNUMSYMS = 0;
954
955
10.5k
        for (index = 0; index < n_dicts; index++) {
956
7.32k
            SBNUMSYMS += dicts[index]->n_symbols;
957
7.32k
        }
958
959
3.24k
        params.IADT = jbig2_arith_int_ctx_new(ctx);
960
3.24k
        params.IAFS = jbig2_arith_int_ctx_new(ctx);
961
3.24k
        params.IADS = jbig2_arith_int_ctx_new(ctx);
962
3.24k
        params.IAIT = jbig2_arith_int_ctx_new(ctx);
963
3.24k
        if (params.IADT == NULL || params.IAFS == NULL || params.IADS == NULL || params.IAIT == NULL) {
964
6
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image data");
965
6
            goto cleanup4;
966
6
        }
967
968
        /* Table 31 */
969
6.73k
        for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < (uint64_t) SBNUMSYMS; SBSYMCODELEN++);
970
971
3.23k
        params.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
972
3.23k
        params.IARI = jbig2_arith_int_ctx_new(ctx);
973
3.23k
        params.IARDW = jbig2_arith_int_ctx_new(ctx);
974
3.23k
        params.IARDH = jbig2_arith_int_ctx_new(ctx);
975
3.23k
        params.IARDX = jbig2_arith_int_ctx_new(ctx);
976
3.23k
        params.IARDY = jbig2_arith_int_ctx_new(ctx);
977
3.23k
        if (params.IAID == NULL || params.IARI == NULL ||
978
3.23k
            params.IARDW == NULL || params.IARDH == NULL || params.IARDX == NULL || params.IARDY == NULL) {
979
9
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to allocate text region image data");
980
9
            goto cleanup5;
981
9
        }
982
3.23k
    }
983
984
3.68k
    code = jbig2_decode_text_region(ctx, segment, &params,
985
3.68k
                                    (const Jbig2SymbolDict * const *)dicts, n_dicts, image,
986
3.68k
                                    segment_data + offset, segment->data_length - offset, GR_stats, as, ws);
987
3.68k
    if (code < 0) {
988
204
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region image data");
989
204
        goto cleanup5;
990
204
    }
991
992
3.48k
    if ((segment->flags & 63) == 4) {
993
        /* we have an intermediate region here. save it for later */
994
144
        segment->result = jbig2_image_reference(ctx, image);
995
3.33k
    } else {
996
        /* otherwise composite onto the page */
997
3.33k
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
998
3.33k
                    "composing %dx%d decoded text region onto page at (%d, %d)", region_info.width, region_info.height, region_info.x, region_info.y);
999
3.33k
        code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
1000
3.33k
        if (code < 0)
1001
9
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add text region to page");
1002
3.33k
    }
1003
1004
3.69k
cleanup5:
1005
3.69k
    if (!params.SBHUFF) {
1006
3.23k
        jbig2_arith_iaid_ctx_free(ctx, params.IAID);
1007
3.23k
        jbig2_arith_int_ctx_free(ctx, params.IARI);
1008
3.23k
        jbig2_arith_int_ctx_free(ctx, params.IARDW);
1009
3.23k
        jbig2_arith_int_ctx_free(ctx, params.IARDH);
1010
3.23k
        jbig2_arith_int_ctx_free(ctx, params.IARDX);
1011
3.23k
        jbig2_arith_int_ctx_free(ctx, params.IARDY);
1012
3.23k
    }
1013
1014
3.70k
cleanup4:
1015
3.70k
    if (!params.SBHUFF) {
1016
3.24k
        jbig2_arith_int_ctx_free(ctx, params.IADT);
1017
3.24k
        jbig2_arith_int_ctx_free(ctx, params.IAFS);
1018
3.24k
        jbig2_arith_int_ctx_free(ctx, params.IADS);
1019
3.24k
        jbig2_arith_int_ctx_free(ctx, params.IAIT);
1020
3.24k
    }
1021
1022
3.70k
cleanup3:
1023
3.70k
    jbig2_free(ctx->allocator, as);
1024
3.70k
    jbig2_word_stream_buf_free(ctx, ws);
1025
1026
3.77k
cleanup2:
1027
3.77k
    jbig2_free(ctx->allocator, GR_stats);
1028
3.77k
    jbig2_image_release(ctx, image);
1029
1030
3.82k
cleanup1:
1031
3.82k
    if (params.SBHUFF) {
1032
528
        jbig2_release_huffman_table(ctx, params.SBHUFFFS);
1033
528
        jbig2_release_huffman_table(ctx, params.SBHUFFDS);
1034
528
        jbig2_release_huffman_table(ctx, params.SBHUFFDT);
1035
528
        jbig2_release_huffman_table(ctx, params.SBHUFFRDX);
1036
528
        jbig2_release_huffman_table(ctx, params.SBHUFFRDY);
1037
528
        jbig2_release_huffman_table(ctx, params.SBHUFFRDW);
1038
528
        jbig2_release_huffman_table(ctx, params.SBHUFFRDH);
1039
528
        jbig2_release_huffman_table(ctx, params.SBHUFFRSIZE);
1040
528
    }
1041
3.82k
    jbig2_free(ctx->allocator, dicts);
1042
1043
3.82k
    return code;
1044
3.77k
}