| 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.3 */ | 
|---|
| 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 | #ifdef VERBOSE | 
|---|
| 31 | #include <stdio.h>              /* for debug printing only */ | 
|---|
| 32 | #endif | 
|---|
| 33 |  | 
|---|
| 34 | #include "jbig2.h" | 
|---|
| 35 | #include "jbig2_priv.h" | 
|---|
| 36 | #include "jbig2_arith.h" | 
|---|
| 37 | #include "jbig2_arith_iaid.h" | 
|---|
| 38 |  | 
|---|
| 39 | struct _Jbig2ArithIaidCtx { | 
|---|
| 40 | int SBSYMCODELEN; | 
|---|
| 41 | Jbig2ArithCx *IAIDx; | 
|---|
| 42 | }; | 
|---|
| 43 |  | 
|---|
| 44 | Jbig2ArithIaidCtx * | 
|---|
| 45 | jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN) | 
|---|
| 46 | { | 
|---|
| 47 | Jbig2ArithIaidCtx *result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1); | 
|---|
| 48 | int ctx_size = 1 << SBSYMCODELEN; | 
|---|
| 49 |  | 
|---|
| 50 | if (result == NULL) { | 
|---|
| 51 | jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate IAID arithmetic coding state"); | 
|---|
| 52 | return NULL; | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | result->SBSYMCODELEN = SBSYMCODELEN; | 
|---|
| 56 | result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size); | 
|---|
| 57 | if (result->IAIDx == NULL) | 
|---|
| 58 | { | 
|---|
| 59 | jbig2_free(ctx->allocator, result); | 
|---|
| 60 | jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate symbol ID in IAID arithmetic coding state"); | 
|---|
| 61 | return NULL; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | memset(result->IAIDx, 0, ctx_size); | 
|---|
| 65 | return result; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | /* A.3 */ | 
|---|
| 69 | /* Return value: -1 on error, 0 on normal value */ | 
|---|
| 70 | int | 
|---|
| 71 | jbig2_arith_iaid_decode(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *actx, Jbig2ArithState *as, int32_t *p_result) | 
|---|
| 72 | { | 
|---|
| 73 | Jbig2ArithCx *IAIDx = actx->IAIDx; | 
|---|
| 74 | int SBSYMCODELEN = actx->SBSYMCODELEN; | 
|---|
| 75 | int PREV = 1; | 
|---|
| 76 | int D; | 
|---|
| 77 | int i; | 
|---|
| 78 | int code = 0; | 
|---|
| 79 |  | 
|---|
| 80 | /* A.3 (2) */ | 
|---|
| 81 | for (i = 0; i < SBSYMCODELEN; i++) { | 
|---|
| 82 | D = jbig2_arith_decode(as, &IAIDx[PREV], &code); | 
|---|
| 83 | if (code) | 
|---|
| 84 | return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to decode IAIDx code"); | 
|---|
| 85 | #ifdef VERBOSE | 
|---|
| 86 | fprintf(stderr, "IAID%x: D = %d\n", PREV, D); | 
|---|
| 87 | #endif | 
|---|
| 88 | PREV = (PREV << 1) | D; | 
|---|
| 89 | } | 
|---|
| 90 | /* A.3 (3) */ | 
|---|
| 91 | PREV -= 1 << SBSYMCODELEN; | 
|---|
| 92 | #ifdef VERBOSE | 
|---|
| 93 | fprintf(stderr, "IAID result: %d\n", PREV); | 
|---|
| 94 | #endif | 
|---|
| 95 | *p_result = PREV; | 
|---|
| 96 | return 0; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void | 
|---|
| 100 | jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax) | 
|---|
| 101 | { | 
|---|
| 102 | if (iax != NULL) { | 
|---|
| 103 | jbig2_free(ctx->allocator, iax->IAIDx); | 
|---|
| 104 | jbig2_free(ctx->allocator, iax); | 
|---|
| 105 | } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|