Coverage Report

Created: 2025-06-24 07:01

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