Coverage Report

Created: 2025-11-09 06:41

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