| 1 | /* Copyright (C) 2001-2019 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 | #if !defined (INT32_MIN) |
| 36 | #define INT32_MIN (-0x7fffffff - 1) |
| 37 | #endif |
| 38 | #if !defined (INT32_MAX) |
| 39 | #define INT32_MAX 0x7fffffff |
| 40 | #endif |
| 41 | |
| 42 | struct _Jbig2ArithIntCtx { |
| 43 | Jbig2ArithCx IAx[512]; |
| 44 | }; |
| 45 | |
| 46 | Jbig2ArithIntCtx * |
| 47 | jbig2_arith_int_ctx_new(Jbig2Ctx *ctx) |
| 48 | { |
| 49 | Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1); |
| 50 | |
| 51 | if (result == NULL) { |
| 52 | jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate arithmetic integer coding state" ); |
| 53 | return NULL; |
| 54 | } else { |
| 55 | memset(result->IAx, 0, sizeof(result->IAx)); |
| 56 | } |
| 57 | |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | /* A.2 */ |
| 62 | /* Return value: -1 on error, 0 on normal value, 1 on OOB return. */ |
| 63 | int |
| 64 | jbig2_arith_int_decode(Jbig2Ctx *ctx, Jbig2ArithIntCtx *actx, Jbig2ArithState *as, int32_t *p_result) |
| 65 | { |
| 66 | Jbig2ArithCx *IAx = actx->IAx; |
| 67 | int PREV = 1; |
| 68 | int S; |
| 69 | int32_t V; |
| 70 | int bit; |
| 71 | int n_tail, offset; |
| 72 | int i; |
| 73 | int code = 0; |
| 74 | |
| 75 | S = jbig2_arith_decode(as, &IAx[PREV], &code); |
| 76 | if (code) |
| 77 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAx S" ); |
| 78 | PREV = (PREV << 1) | S; |
| 79 | |
| 80 | bit = jbig2_arith_decode(as, &IAx[PREV], &code); |
| 81 | if (code) |
| 82 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAx decision bit 0" ); |
| 83 | PREV = (PREV << 1) | bit; |
| 84 | if (bit) { |
| 85 | bit = jbig2_arith_decode(as, &IAx[PREV], &code); |
| 86 | if (code) |
| 87 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAx decision bit 1" ); |
| 88 | PREV = (PREV << 1) | bit; |
| 89 | |
| 90 | if (bit) { |
| 91 | bit = jbig2_arith_decode(as, &IAx[PREV], &code); |
| 92 | if (code) |
| 93 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAx decision bit 2" ); |
| 94 | PREV = (PREV << 1) | bit; |
| 95 | |
| 96 | if (bit) { |
| 97 | bit = jbig2_arith_decode(as, &IAx[PREV], &code); |
| 98 | if (code) |
| 99 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAx decision bit 3" ); |
| 100 | PREV = (PREV << 1) | bit; |
| 101 | |
| 102 | if (bit) { |
| 103 | bit = jbig2_arith_decode(as, &IAx[PREV], &code); |
| 104 | if (code) |
| 105 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAx decision bit 4" ); |
| 106 | PREV = (PREV << 1) | bit; |
| 107 | |
| 108 | if (bit) { |
| 109 | n_tail = 32; |
| 110 | offset = 4436; |
| 111 | } else { |
| 112 | n_tail = 12; |
| 113 | offset = 340; |
| 114 | } |
| 115 | } else { |
| 116 | n_tail = 8; |
| 117 | offset = 84; |
| 118 | } |
| 119 | } else { |
| 120 | n_tail = 6; |
| 121 | offset = 20; |
| 122 | } |
| 123 | } else { |
| 124 | n_tail = 4; |
| 125 | offset = 4; |
| 126 | } |
| 127 | } else { |
| 128 | n_tail = 2; |
| 129 | offset = 0; |
| 130 | } |
| 131 | |
| 132 | V = 0; |
| 133 | for (i = 0; i < n_tail; i++) { |
| 134 | bit = jbig2_arith_decode(as, &IAx[PREV], &code); |
| 135 | if (code) |
| 136 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAx V bit %d" , i); |
| 137 | PREV = ((PREV << 1) & 511) | (PREV & 256) | bit; |
| 138 | V = (V << 1) | bit; |
| 139 | } |
| 140 | |
| 141 | /* make sure not to underflow/overflow 32 bit value */ |
| 142 | if (V < INT32_MAX - 4436 || V > INT32_MIN + 4436) |
| 143 | V += offset; |
| 144 | V = S ? -V : V; |
| 145 | *p_result = V; |
| 146 | return S && V == 0 ? 1 : 0; |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax) |
| 151 | { |
| 152 | jbig2_free(ctx->allocator, iax); |
| 153 | } |
| 154 | |