/src/ghostpdl/jbig2dec/jbig2_arith_int.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2021 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., 1305 Grant Avenue - Suite 200, Novato, |
13 | | CA 94945, U.S.A., +1(415)492-9861, for further information. |
14 | | */ |
15 | | |
16 | | /* |
17 | | jbig2dec |
18 | | */ |
19 | | |
20 | | /* Annex A */ |
21 | | |
22 | | #ifdef HAVE_CONFIG_H |
23 | | #include "config.h" |
24 | | #endif |
25 | | #include "os_types.h" |
26 | | |
27 | | #include <stddef.h> |
28 | | #include <string.h> /* memset() */ |
29 | | |
30 | | #include "jbig2.h" |
31 | | #include "jbig2_priv.h" |
32 | | #include "jbig2_arith.h" |
33 | | #include "jbig2_arith_int.h" |
34 | | |
35 | | struct _Jbig2ArithIntCtx { |
36 | | Jbig2ArithCx IAx[512]; |
37 | | }; |
38 | | |
39 | | Jbig2ArithIntCtx * |
40 | | jbig2_arith_int_ctx_new(Jbig2Ctx *ctx) |
41 | 147 | { |
42 | 147 | Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1); |
43 | | |
44 | 147 | if (result == NULL) { |
45 | 0 | jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate arithmetic integer coding state"); |
46 | 0 | return NULL; |
47 | 147 | } else { |
48 | 147 | memset(result->IAx, 0, sizeof(result->IAx)); |
49 | 147 | } |
50 | | |
51 | 147 | return result; |
52 | 147 | } |
53 | | |
54 | | /* A.2 */ |
55 | | /* Return value: -1 on error, 0 on normal value, 1 on OOB return. */ |
56 | | int |
57 | | jbig2_arith_int_decode(Jbig2Ctx *ctx, Jbig2ArithIntCtx *actx, Jbig2ArithState *as, int32_t *p_result) |
58 | 318 | { |
59 | 318 | Jbig2ArithCx *IAx = actx->IAx; |
60 | 318 | int PREV = 1; |
61 | 318 | int S; |
62 | 318 | int32_t V; |
63 | 318 | int bit; |
64 | 318 | int n_tail, offset; |
65 | 318 | int i; |
66 | | |
67 | 318 | S = jbig2_arith_decode(ctx, as, &IAx[PREV]); |
68 | 318 | if (S < 0) |
69 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx S"); |
70 | 318 | PREV = (PREV << 1) | S; |
71 | | |
72 | 318 | bit = jbig2_arith_decode(ctx, as, &IAx[PREV]); |
73 | 318 | if (bit < 0) |
74 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 0"); |
75 | 318 | PREV = (PREV << 1) | bit; |
76 | 318 | if (bit) { |
77 | 100 | bit = jbig2_arith_decode(ctx, as, &IAx[PREV]); |
78 | 100 | if (bit < 0) |
79 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 1"); |
80 | 100 | PREV = (PREV << 1) | bit; |
81 | | |
82 | 100 | if (bit) { |
83 | 8 | bit = jbig2_arith_decode(ctx, as, &IAx[PREV]); |
84 | 8 | if (bit < 0) |
85 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 2"); |
86 | 8 | PREV = (PREV << 1) | bit; |
87 | | |
88 | 8 | if (bit) { |
89 | 3 | bit = jbig2_arith_decode(ctx, as, &IAx[PREV]); |
90 | 3 | if (bit < 0) |
91 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 3"); |
92 | 3 | PREV = (PREV << 1) | bit; |
93 | | |
94 | 3 | if (bit) { |
95 | 2 | bit = jbig2_arith_decode(ctx, as, &IAx[PREV]); |
96 | 2 | if (bit < 0) |
97 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 4"); |
98 | 2 | PREV = (PREV << 1) | bit; |
99 | | |
100 | 2 | if (bit) { |
101 | 2 | n_tail = 32; |
102 | 2 | offset = 4436; |
103 | 2 | } else { |
104 | 0 | n_tail = 12; |
105 | 0 | offset = 340; |
106 | 0 | } |
107 | 2 | } else { |
108 | 1 | n_tail = 8; |
109 | 1 | offset = 84; |
110 | 1 | } |
111 | 5 | } else { |
112 | 5 | n_tail = 6; |
113 | 5 | offset = 20; |
114 | 5 | } |
115 | 92 | } else { |
116 | 92 | n_tail = 4; |
117 | 92 | offset = 4; |
118 | 92 | } |
119 | 218 | } else { |
120 | 218 | n_tail = 2; |
121 | 218 | offset = 0; |
122 | 218 | } |
123 | | |
124 | 318 | V = 0; |
125 | 1.22k | for (i = 0; i < n_tail; i++) { |
126 | 906 | bit = jbig2_arith_decode(ctx, as, &IAx[PREV]); |
127 | 906 | if (bit < 0) |
128 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx V bit %d", i); |
129 | 906 | PREV = ((PREV << 1) & 511) | (PREV & 256) | bit; |
130 | 906 | V = (V << 1) | bit; |
131 | 906 | } |
132 | | |
133 | | /* offset is always >=0, so underflow can't happen. */ |
134 | | /* avoid overflow by clamping 32 bit value. */ |
135 | 318 | if (V > INT32_MAX - offset) |
136 | 0 | V = INT32_MAX; |
137 | 318 | else |
138 | 318 | V += offset; |
139 | 318 | V = S ? -V : V; |
140 | 318 | *p_result = V; |
141 | 318 | return S && V == 0 ? 1 : 0; |
142 | 318 | } |
143 | | |
144 | | void |
145 | | jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax) |
146 | 224 | { |
147 | 224 | jbig2_free(ctx->allocator, iax); |
148 | 224 | } |