Coverage Report

Created: 2026-09-14 07:34

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
151
{
63
    /* relevant bits of 6.4.4 */
64
151
    uint32_t NINSTANCES;
65
151
    uint32_t ID;
66
151
    int32_t STRIPT;
67
151
    int32_t FIRSTS;
68
151
    int32_t DT;
69
151
    int32_t DFS;
70
151
    int32_t IDS;
71
151
    int32_t CURS;
72
151
    int32_t CURT;
73
151
    int S, T;
74
151
    int x, y;
75
151
    bool first_symbol;
76
151
    uint32_t index, SBNUMSYMS;
77
151
    Jbig2Image *IB = NULL;
78
151
    Jbig2Image *IBO = NULL;
79
151
    Jbig2Image *refimage = NULL;
80
151
    Jbig2HuffmanState *hs = NULL;
81
151
    Jbig2HuffmanTable *SBSYMCODES = NULL;
82
151
    int code = 0;
83
151
    int RI;
84
85
151
    SBNUMSYMS = 0;
86
294
    for (index = 0; index < n_dicts; index++) {
87
143
        SBNUMSYMS += dicts[index]->n_symbols;
88
143
    }
89
151
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "symbol list contains %d glyphs in %d dictionaries", SBNUMSYMS, n_dicts);
90
91
151
    if (params->SBHUFF) {
92
28
        Jbig2HuffmanTable *runcodes = NULL;
93
28
        Jbig2HuffmanParams runcodeparams;
94
28
        Jbig2HuffmanLine runcodelengths[35];
95
28
        Jbig2HuffmanLine *symcodelengths = NULL;
96
28
        Jbig2HuffmanParams symcodeparams;
97
28
        int err, len, range, r;
98
99
28
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "huffman coded text region");
100
28
        hs = jbig2_huffman_new(ctx, ws);
101
28
        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.00k
        for (index = 0; index < 35; index++) {
110
980
            runcodelengths[index].PREFLEN = jbig2_huffman_get_bits(hs, 4, &code);
111
980
            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
980
            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
980
            runcodelengths[index].RANGELEN = 0;
120
980
            runcodelengths[index].RANGELOW = index;
121
980
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d length %d", index, runcodelengths[index].PREFLEN);
122
980
        }
123
28
        runcodeparams.HTOOB = 0;
124
28
        runcodeparams.lines = runcodelengths;
125
28
        runcodeparams.n_lines = 35;
126
28
        runcodes = jbig2_build_huffman_table(ctx, &runcodeparams);
127
28
        if (runcodes == NULL) {
128
3
            code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "error constructing symbol ID runcode table");
129
3
            goto cleanup1;
130
3
        }
131
132
        /* decode the symbol ID code lengths using the runlength table */
133
25
        symcodelengths = jbig2_new(ctx, Jbig2HuffmanLine, SBNUMSYMS);
134
25
        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
25
        index = 0;
139
143
        while (index < SBNUMSYMS) {
140
118
            code = jbig2_huffman_get(hs, runcodes, &err);
141
118
            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
118
            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
118
            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
118
            if (code < 32) {
155
118
                len = code;
156
118
                range = 1;
157
118
            } 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
118
            jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "  read runcode%d at index %d (length %d range %d)", code, index, len, range);
184
118
            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
236
            for (r = 0; r < range; r++) {
190
118
                symcodelengths[index + r].PREFLEN = len;
191
118
                symcodelengths[index + r].RANGELEN = 0;
192
118
                symcodelengths[index + r].RANGELOW = index + r;
193
118
            }
194
118
            index += r;
195
118
        }
196
197
25
        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
25
        symcodeparams.HTOOB = 0;
203
25
        symcodeparams.lines = symcodelengths;
204
25
        symcodeparams.n_lines = SBNUMSYMS;
205
206
        /* skip to byte boundary */
207
25
        err = jbig2_huffman_skip(hs);
208
25
        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
25
        SBSYMCODES = jbig2_build_huffman_table(ctx, &symcodeparams);
216
217
28
cleanup1:
218
28
        jbig2_free(ctx->allocator, symcodelengths);
219
28
        jbig2_release_huffman_table(ctx, runcodes);
220
221
28
        if (SBSYMCODES == NULL) {
222
5
            jbig2_huffman_free(ctx, hs);
223
5
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to construct symbol ID huffman table");
224
5
        }
225
28
    }
226
227
    /* 6.4.5 (1) */
228
146
    jbig2_image_clear(ctx, image, params->SBDEFPIXEL);
229
230
    /* 6.4.6 */
231
146
    if (params->SBHUFF) {
232
23
        STRIPT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
233
123
    } else {
234
123
        code = jbig2_arith_int_decode(ctx, params->IADT, as, &STRIPT);
235
123
    }
236
146
    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
146
    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
146
    STRIPT *= -(params->SBSTRIPS);
247
146
    FIRSTS = 0;
248
146
    NINSTANCES = 0;
249
250
    /* 6.4.5 (3) */
251
412
    while (NINSTANCES < params->SBNUMINSTANCES) {
252
        /* (3b) */
253
282
        if (params->SBHUFF) {
254
65
            DT = jbig2_huffman_get(hs, params->SBHUFFDT, &code);
255
217
        } else {
256
217
            code = jbig2_arith_int_decode(ctx, params->IADT, as, &DT);
257
217
        }
258
282
        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
282
        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
281
        DT *= params->SBSTRIPS;
267
281
        STRIPT += DT;
268
269
281
        first_symbol = TRUE;
270
        /* 6.4.5 (3c) - decode symbols in strip */
271
1.43k
        for (;;) {
272
            /* (3c.i) */
273
1.43k
            if (first_symbol) {
274
                /* 6.4.7 */
275
281
                if (params->SBHUFF) {
276
65
                    DFS = jbig2_huffman_get(hs, params->SBHUFFFS, &code);
277
216
                } else {
278
216
                    code = jbig2_arith_int_decode(ctx, params->IAFS, as, &DFS);
279
216
                }
280
281
                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
281
                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
279
                FIRSTS += DFS;
289
279
                CURS = FIRSTS;
290
279
                first_symbol = FALSE;
291
1.14k
            } else {
292
1.14k
                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
1.12k
                if (params->SBHUFF) {
298
69
                    IDS = jbig2_huffman_get(hs, params->SBHUFFDS, &code);
299
1.05k
                } else {
300
1.05k
                    code = jbig2_arith_int_decode(ctx, params->IADS, as, &IDS);
301
1.05k
                }
302
1.12k
                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
1.12k
                if (code > 0) {
307
245
                    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
245
                    break;
309
245
                }
310
883
                CURS += IDS + params->SBDSOFFSET;
311
883
            }
312
313
            /* (3c.iii) / 6.4.9 */
314
1.16k
            if (params->SBSTRIPS == 1) {
315
555
                CURT = 0;
316
607
            } else if (params->SBHUFF) {
317
78
                CURT = jbig2_huffman_get_bits(hs, params->LOGSBSTRIPS, &code);
318
529
            } else {
319
529
                code = jbig2_arith_int_decode(ctx, params->IAIT, as, &CURT);
320
529
            }
321
1.16k
            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
1.16k
            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
1.15k
            T = STRIPT + CURT;
330
331
            /* (3b.iv) / 6.4.10 - decode the symbol ID */
332
1.15k
            if (params->SBHUFF) {
333
78
                ID = jbig2_huffman_get(hs, SBSYMCODES, &code);
334
1.08k
            } else {
335
1.08k
                code = jbig2_arith_iaid_decode(ctx, params->IAID, as, (int *)&ID);
336
1.08k
            }
337
1.15k
            if (code < 0) {
338
9
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to obtain symbol instance symbol ID");
339
9
                goto cleanup2;
340
9
            }
341
1.15k
            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
1.15k
            if (ID >= SBNUMSYMS) {
346
628
                jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "ignoring out of range symbol ID (%d/%d)", ID, SBNUMSYMS);
347
628
                IB = NULL;
348
628
            } else {
349
                /* (3c.v) / 6.4.11 - look up the symbol bitmap IB */
350
522
                uint32_t id = ID;
351
352
522
                index = 0;
353
526
                while (id >= dicts[index]->n_symbols)
354
4
                    id -= dicts[index++]->n_symbols;
355
522
                if (dicts[index]->glyphs[id] == NULL) {
356
27
                    jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "missing glyph (%d/%d), ignoring", index, id);
357
495
                } else {
358
495
                    IB = jbig2_image_reference(ctx, dicts[index]->glyphs[id]);
359
495
                }
360
522
            }
361
1.15k
            if (params->SBREFINE) {
362
60
                if (params->SBHUFF) {
363
0
                    RI = jbig2_huffman_get_bits(hs, 1, &code);
364
60
                } else {
365
60
                    code = jbig2_arith_int_decode(ctx, params->IARI, as, &RI);
366
60
                }
367
60
                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
60
                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
1.09k
            } else {
376
1.09k
                RI = 0;
377
1.09k
            }
378
1.15k
            if (RI) {
379
10
                Jbig2RefinementRegionParams rparams;
380
10
                int32_t RDW, RDH, RDX, RDY;
381
10
                size_t BMSIZE = 0;
382
10
                int code1 = 0;
383
10
                int code2 = 0;
384
10
                int code3 = 0;
385
10
                int code4 = 0;
386
10
                int code5 = 0;
387
10
                int code6 = 0;
388
389
                /* 6.4.11 (1, 2, 3, 4) */
390
10
                if (!params->SBHUFF) {
391
10
                    code1 = jbig2_arith_int_decode(ctx, params->IARDW, as, &RDW);
392
10
                    code2 = jbig2_arith_int_decode(ctx, params->IARDH, as, &RDH);
393
10
                    code3 = jbig2_arith_int_decode(ctx, params->IARDX, as, &RDX);
394
10
                    code4 = jbig2_arith_int_decode(ctx, params->IARDY, as, &RDY);
395
10
                } 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
10
                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
10
                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
10
                if (IB) {
415
9
                    IBO = IB;
416
9
                    IB = NULL;
417
9
                    if (((int32_t) IBO->width) + RDW < 0 || ((int32_t) IBO->height) + RDH < 0) {
418
1
                        code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "reference image dimensions negative");
419
1
                        goto cleanup2;
420
1
                    }
421
8
                    refimage = jbig2_image_new(ctx, IBO->width + RDW, IBO->height + RDH);
422
8
                    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
8
                    jbig2_image_clear(ctx, refimage, 0x00);
427
428
                    /* Table 12 */
429
8
                    rparams.GRTEMPLATE = params->SBRTEMPLATE;
430
8
                    rparams.GRREFERENCE = IBO;
431
8
                    rparams.GRREFERENCEDX = (RDW >> 1) + RDX;
432
8
                    rparams.GRREFERENCEDY = (RDH >> 1) + RDY;
433
8
                    rparams.TPGRON = 0;
434
8
                    memcpy(rparams.grat, params->sbrat, 4);
435
8
                    code = jbig2_decode_refinement_region(ctx, segment, &rparams, as, refimage, GR_stats);
436
8
                    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
8
                    jbig2_image_release(ctx, IBO);
442
8
                    IBO = NULL;
443
8
                    IB = refimage;
444
8
                    refimage = NULL;
445
8
                }
446
447
                /* 6.4.11 (7) */
448
9
                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
9
            }
456
457
            /* (3c.vi) */
458
1.14k
            if ((!params->TRANSPOSED) && (params->REFCORNER > 1) && IB) {
459
45
                CURS += IB->width - 1;
460
1.10k
            } else if ((params->TRANSPOSED) && !(params->REFCORNER & 1) && IB) {
461
28
                CURS += IB->height - 1;
462
28
            }
463
464
            /* (3c.vii) */
465
1.14k
            S = CURS;
466
467
            /* (3c.viii) */
468
1.14k
            if (!params->TRANSPOSED) {
469
1.09k
                switch (params->REFCORNER) {
470
211
                case JBIG2_CORNER_TOPLEFT:
471
211
                    x = S;
472
211
                    y = T;
473
211
                    break;
474
22
                case JBIG2_CORNER_TOPRIGHT:
475
22
                    if (IB)
476
22
                        x = S - IB->width + 1;
477
0
                    else
478
0
                        x = S + 1;
479
22
                    y = T;
480
22
                    break;
481
837
                case JBIG2_CORNER_BOTTOMLEFT:
482
837
                    x = S;
483
837
                    if (IB)
484
187
                        y = T - IB->height + 1;
485
650
                    else
486
650
                        y = T + 1;
487
837
                    break;
488
0
                default:
489
23
                case JBIG2_CORNER_BOTTOMRIGHT:
490
23
                    if (IB ) {
491
23
                        x = S - IB->width + 1;
492
23
                        y = T - IB->height + 1;
493
23
                    } else {
494
0
                        x = S + 1;
495
0
                        y = T + 1;
496
0
                    }
497
23
                    break;
498
1.09k
                }
499
1.09k
            } else {            /* TRANSPOSED */
500
56
                switch (params->REFCORNER) {
501
14
                case JBIG2_CORNER_TOPLEFT:
502
14
                    x = T;
503
14
                    y = S;
504
14
                    break;
505
14
                case JBIG2_CORNER_TOPRIGHT:
506
14
                    if (IB)
507
14
                        x = T - IB->width + 1;
508
0
                    else
509
0
                        x = T + 1;
510
14
                    y = S;
511
14
                    break;
512
14
                case JBIG2_CORNER_BOTTOMLEFT:
513
14
                    x = T;
514
14
                    if (IB)
515
14
                        y = S - IB->height + 1;
516
0
                    else
517
0
                        y = S + 1;
518
14
                    break;
519
0
                default:
520
14
                case JBIG2_CORNER_BOTTOMRIGHT:
521
14
                    if (IB) {
522
14
                        x = T - IB->width + 1;
523
14
                        y = S - IB->height + 1;
524
14
                    } else {
525
0
                        x = T + 1;
526
0
                        y = S + 1;
527
0
                    }
528
14
                    break;
529
56
                }
530
56
            }
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
1.14k
            code = jbig2_image_compose(ctx, image, IB, x, y, params->SBCOMBOP);
538
1.14k
            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
1.14k
            if (IB && (!params->TRANSPOSED) && (params->REFCORNER < 2)) {
545
393
                CURS += IB->width - 1;
546
756
            } else if (IB && (params->TRANSPOSED) && (params->REFCORNER & 1)) {
547
28
                CURS += IB->height - 1;
548
28
            }
549
550
            /* (3c.xi) */
551
1.14k
            NINSTANCES++;
552
553
1.14k
            jbig2_image_release(ctx, IB);
554
1.14k
            IB = NULL;
555
1.14k
        }
556
        /* end strip */
557
281
    }
558
    /* 6.4.5 (4) */
559
560
146
cleanup2:
561
146
    jbig2_image_release(ctx, refimage);
562
146
    jbig2_image_release(ctx, IBO);
563
146
    jbig2_image_release(ctx, IB);
564
146
    if (params->SBHUFF) {
565
23
        jbig2_release_huffman_table(ctx, SBSYMCODES);
566
23
    }
567
146
    jbig2_huffman_free(ctx, hs);
568
569
146
    return code;
570
146
}
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
141
{
578
141
    uint32_t offset = 0;
579
141
    Jbig2RegionSegmentInfo region_info;
580
141
    Jbig2TextRegionParams params;
581
141
    Jbig2Image *image = NULL;
582
141
    Jbig2SymbolDict **dicts = NULL;
583
141
    uint32_t n_dicts = 0;
584
141
    uint16_t flags = 0;
585
141
    uint16_t huffman_flags = 0;
586
141
    Jbig2ArithCx *GR_stats = NULL;
587
141
    int code = 0;
588
141
    Jbig2WordStream *ws = NULL;
589
141
    Jbig2ArithState *as = NULL;
590
141
    uint32_t table_index = 0;
591
141
    const Jbig2HuffmanParams *huffman_params = NULL;
592
593
    /* zero params to ease cleanup later */
594
141
    memset(&params, 0, sizeof(Jbig2TextRegionParams));
595
596
    /* 7.4.1 */
597
141
    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
141
    jbig2_get_region_segment_info(&region_info, segment_data);
602
141
    offset += 17;
603
    /* Check for T.88 amendment 3 */
604
141
    if (region_info.flags & 8)
605
1
        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
140
    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
140
    flags = jbig2_get_uint16(segment_data + offset);
613
140
    offset += 2;
614
615
140
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region header flags 0x%04x", flags);
616
617
140
    params.SBHUFF = flags & 0x0001;
618
140
    params.SBREFINE = flags & 0x0002;
619
140
    params.LOGSBSTRIPS = (flags & 0x000c) >> 2;
620
140
    params.SBSTRIPS = 1 << params.LOGSBSTRIPS;
621
140
    params.REFCORNER = (Jbig2RefCorner)((flags & 0x0030) >> 4);
622
140
    params.TRANSPOSED = flags & 0x0040;
623
140
    params.SBCOMBOP = (Jbig2ComposeOp)((flags & 0x0180) >> 7);
624
140
    params.SBDEFPIXEL = flags & 0x0200;
625
    /* SBDSOFFSET is a signed 5 bit integer */
626
140
    params.SBDSOFFSET = (flags & 0x7C00) >> 10;
627
140
    if (params.SBDSOFFSET > 0x0f)
628
4
        params.SBDSOFFSET -= 0x20;
629
140
    params.SBRTEMPLATE = flags & 0x8000;
630
631
140
    if (params.SBDSOFFSET) {
632
48
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number, "text region has SBDSOFFSET %d", params.SBDSOFFSET);
633
48
    }
634
635
140
    if (params.SBHUFF) {        /* Huffman coding */
636
        /* 7.4.3.1.2 */
637
23
        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
23
        huffman_flags = jbig2_get_uint16(segment_data + offset);
642
23
        offset += 2;
643
644
23
        if (huffman_flags & 0x8000)
645
0
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "reserved bit 15 of text region huffman flags is not zero");
646
117
    } else {                    /* arithmetic coding */
647
648
        /* 7.4.3.1.3 */
649
117
        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
117
        if ((params.SBREFINE) && !(params.SBRTEMPLATE)) {
654
12
            params.sbrat[0] = segment_data[offset];
655
12
            params.sbrat[1] = segment_data[offset + 1];
656
12
            params.sbrat[2] = segment_data[offset + 2];
657
12
            params.sbrat[3] = segment_data[offset + 3];
658
12
            offset += 4;
659
12
        }
660
117
    }
661
662
    /* 7.4.3.1.4 */
663
140
    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
140
    params.SBNUMINSTANCES = jbig2_get_uint32(segment_data + offset);
668
140
    offset += 4;
669
670
140
    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
23
        switch (huffman_flags & 0x0003) {
676
16
        case 0:                /* Table B.6 */
677
16
            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_F);
678
16
            break;
679
5
        case 1:                /* Table B.7 */
680
5
            params.SBHUFFFS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_G);
681
5
            break;
682
2
        case 3:                /* Custom table from referred segment */
683
2
            huffman_params = jbig2_find_table(ctx, segment, table_index);
684
2
            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
2
            params.SBHUFFFS = jbig2_build_huffman_table(ctx, huffman_params);
689
2
            ++table_index;
690
2
            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
23
        }
697
23
        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
23
        switch ((huffman_flags & 0x000c) >> 2) {
703
14
        case 0:                /* Table B.8 */
704
14
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_H);
705
14
            break;
706
5
        case 1:                /* Table B.9 */
707
5
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_I);
708
5
            break;
709
2
        case 2:                /* Table B.10 */
710
2
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_J);
711
2
            break;
712
2
        case 3:                /* Custom table from referred segment */
713
2
            huffman_params = jbig2_find_table(ctx, segment, table_index);
714
2
            if (huffman_params == NULL) {
715
0
                code = jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "custom DS huffman table not found (%d)", table_index);
716
0
                goto cleanup1;
717
0
            }
718
2
            params.SBHUFFDS = jbig2_build_huffman_table(ctx, huffman_params);
719
2
            ++table_index;
720
2
            break;
721
23
        }
722
23
        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
23
        switch ((huffman_flags & 0x0030) >> 4) {
728
14
        case 0:                /* Table B.11 */
729
14
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_K);
730
14
            break;
731
5
        case 1:                /* Table B.12 */
732
5
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_L);
733
5
            break;
734
2
        case 2:                /* Table B.13 */
735
2
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_M);
736
2
            break;
737
2
        case 3:                /* Custom table from referred segment */
738
2
            huffman_params = jbig2_find_table(ctx, segment, table_index);
739
2
            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
2
            params.SBHUFFDT = jbig2_build_huffman_table(ctx, huffman_params);
744
2
            ++table_index;
745
2
            break;
746
23
        }
747
23
        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
23
        switch ((huffman_flags & 0x00c0) >> 6) {
753
19
        case 0:                /* Table B.14 */
754
19
            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
755
19
            break;
756
2
        case 1:                /* Table B.15 */
757
2
            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
758
2
            break;
759
2
        case 3:                /* Custom table from referred segment */
760
2
            huffman_params = jbig2_find_table(ctx, segment, table_index);
761
2
            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
2
            params.SBHUFFRDW = jbig2_build_huffman_table(ctx, huffman_params);
766
2
            ++table_index;
767
2
            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
23
        }
774
23
        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
23
        switch ((huffman_flags & 0x0300) >> 8) {
780
19
        case 0:                /* Table B.14 */
781
19
            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
782
19
            break;
783
2
        case 1:                /* Table B.15 */
784
2
            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
785
2
            break;
786
2
        case 3:                /* Custom table from referred segment */
787
2
            huffman_params = jbig2_find_table(ctx, segment, table_index);
788
2
            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
2
            params.SBHUFFRDH = jbig2_build_huffman_table(ctx, huffman_params);
793
2
            ++table_index;
794
2
            break;
795
0
        case 2:                /* invalid */
796
0
        default:
797
0
            code = jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "text region specified invalid RDH huffman table");
798
0
            goto cleanup1;
799
0
            break;
800
23
        }
801
23
        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
23
        switch ((huffman_flags & 0x0c00) >> 10) {
807
18
        case 0:                /* Table B.14 */
808
18
            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
809
18
            break;
810
2
        case 1:                /* Table B.15 */
811
2
            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
812
2
            break;
813
3
        case 3:                /* Custom table from referred segment */
814
3
            huffman_params = jbig2_find_table(ctx, segment, table_index);
815
3
            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
3
            params.SBHUFFRDX = jbig2_build_huffman_table(ctx, huffman_params);
820
3
            ++table_index;
821
3
            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
23
        }
828
23
        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
23
        switch ((huffman_flags & 0x3000) >> 12) {
834
18
        case 0:                /* Table B.14 */
835
18
            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_N);
836
18
            break;
837
2
        case 1:                /* Table B.15 */
838
2
            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_O);
839
2
            break;
840
3
        case 3:                /* Custom table from referred segment */
841
3
            huffman_params = jbig2_find_table(ctx, segment, table_index);
842
3
            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
3
            params.SBHUFFRDY = jbig2_build_huffman_table(ctx, huffman_params);
847
3
            ++table_index;
848
3
            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
23
        }
855
23
        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
23
        switch ((huffman_flags & 0x4000) >> 14) {
861
21
        case 0:                /* Table B.1 */
862
21
            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
863
21
            break;
864
2
        case 1:                /* Custom table from referred segment */
865
2
            huffman_params = jbig2_find_table(ctx, segment, table_index);
866
2
            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
2
            params.SBHUFFRSIZE = jbig2_build_huffman_table(ctx, huffman_params);
871
2
            ++table_index;
872
2
            break;
873
23
        }
874
23
        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
23
        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
23
    }
886
887
140
    jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
888
140
                "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
140
    n_dicts = jbig2_sd_count_referred(ctx, segment);
892
140
    if (n_dicts == 0) {
893
19
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "text region refers to no symbol dictionaries");
894
121
    } else {
895
121
        dicts = jbig2_sd_list_referred(ctx, segment);
896
121
        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
121
        } else {
900
121
            uint32_t index;
901
902
121
            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
121
            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
121
        }
912
121
    }
913
914
    /* 7.4.3.2 (3) */
915
140
    {
916
140
        int stats_size = params.SBRTEMPLATE ? 1 << 10 : 1 << 13;
917
918
140
        GR_stats = jbig2_new(ctx, Jbig2ArithCx, stats_size);
919
140
        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
140
        memset(GR_stats, 0, stats_size);
924
140
    }
925
926
0
    image = jbig2_image_new(ctx, region_info.width, region_info.height);
927
140
    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
140
    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
140
    ws = jbig2_word_stream_buf_new(ctx, segment_data + offset, segment->data_length - offset);
937
140
    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
140
    as = jbig2_arith_new(ctx, ws);
943
140
    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
140
    if (!params.SBHUFF) {
949
117
        uint8_t SBSYMCODELEN;
950
117
        uint32_t index;
951
117
        uint32_t SBNUMSYMS = 0;
952
953
215
        for (index = 0; index < n_dicts; index++) {
954
98
            SBNUMSYMS += dicts[index]->n_symbols;
955
98
        }
956
957
117
        params.IADT = jbig2_arith_int_ctx_new(ctx);
958
117
        params.IAFS = jbig2_arith_int_ctx_new(ctx);
959
117
        params.IADS = jbig2_arith_int_ctx_new(ctx);
960
117
        params.IAIT = jbig2_arith_int_ctx_new(ctx);
961
117
        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
357
        for (SBSYMCODELEN = 0; ((uint64_t) 1 << SBSYMCODELEN) < (uint64_t) SBNUMSYMS; SBSYMCODELEN++);
968
969
117
        params.IAID = jbig2_arith_iaid_ctx_new(ctx, SBSYMCODELEN);
970
117
        params.IARI = jbig2_arith_int_ctx_new(ctx);
971
117
        params.IARDW = jbig2_arith_int_ctx_new(ctx);
972
117
        params.IARDH = jbig2_arith_int_ctx_new(ctx);
973
117
        params.IARDX = jbig2_arith_int_ctx_new(ctx);
974
117
        params.IARDY = jbig2_arith_int_ctx_new(ctx);
975
117
        if (params.IAID == NULL || params.IARI == NULL ||
976
117
            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
117
    }
981
982
140
    code = jbig2_decode_text_region(ctx, segment, &params,
983
140
                                    (const Jbig2SymbolDict * const *)dicts, n_dicts, image, GR_stats, as, ws);
984
140
    if (code < 0) {
985
16
        jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "failed to decode text region image data");
986
16
        goto cleanup5;
987
16
    }
988
989
124
    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
121
    } else {
993
        /* otherwise composite onto the page */
994
121
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
995
121
                    "composing %dx%d decoded text region onto page at (%d, %d)", region_info.width, region_info.height, region_info.x, region_info.y);
996
121
        code = jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page], image, region_info.x, region_info.y, region_info.op);
997
121
        if (code < 0)
998
0
            jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number, "unable to add text region to page");
999
121
    }
1000
1001
140
cleanup5:
1002
140
    if (!params.SBHUFF) {
1003
117
        jbig2_arith_iaid_ctx_free(ctx, params.IAID);
1004
117
        jbig2_arith_int_ctx_free(ctx, params.IARI);
1005
117
        jbig2_arith_int_ctx_free(ctx, params.IARDW);
1006
117
        jbig2_arith_int_ctx_free(ctx, params.IARDH);
1007
117
        jbig2_arith_int_ctx_free(ctx, params.IARDX);
1008
117
        jbig2_arith_int_ctx_free(ctx, params.IARDY);
1009
117
    }
1010
1011
140
cleanup4:
1012
140
    if (!params.SBHUFF) {
1013
117
        jbig2_arith_int_ctx_free(ctx, params.IADT);
1014
117
        jbig2_arith_int_ctx_free(ctx, params.IAFS);
1015
117
        jbig2_arith_int_ctx_free(ctx, params.IADS);
1016
117
        jbig2_arith_int_ctx_free(ctx, params.IAIT);
1017
117
    }
1018
1019
140
cleanup3:
1020
140
    jbig2_free(ctx->allocator, as);
1021
140
    jbig2_word_stream_buf_free(ctx, ws);
1022
1023
140
cleanup2:
1024
140
    jbig2_free(ctx->allocator, GR_stats);
1025
140
    jbig2_image_release(ctx, image);
1026
1027
140
cleanup1:
1028
140
    if (params.SBHUFF) {
1029
23
        jbig2_release_huffman_table(ctx, params.SBHUFFFS);
1030
23
        jbig2_release_huffman_table(ctx, params.SBHUFFDS);
1031
23
        jbig2_release_huffman_table(ctx, params.SBHUFFDT);
1032
23
        jbig2_release_huffman_table(ctx, params.SBHUFFRDX);
1033
23
        jbig2_release_huffman_table(ctx, params.SBHUFFRDY);
1034
23
        jbig2_release_huffman_table(ctx, params.SBHUFFRDW);
1035
23
        jbig2_release_huffman_table(ctx, params.SBHUFFRDH);
1036
23
        jbig2_release_huffman_table(ctx, params.SBHUFFRSIZE);
1037
23
    }
1038
140
    jbig2_free(ctx->allocator, dicts);
1039
1040
140
    return code;
1041
140
}