Coverage Report

Created: 2026-08-08 08:00

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