/src/ghostpdl/jbig2dec/jbig2_arith_iaid.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* 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 | | uint8_t SBSYMCODELEN; |
41 | | Jbig2ArithCx *IAIDx; |
42 | | }; |
43 | | |
44 | | Jbig2ArithIaidCtx * |
45 | | jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, uint8_t SBSYMCODELEN) |
46 | 102 | { |
47 | 102 | Jbig2ArithIaidCtx *result; |
48 | 102 | size_t ctx_size; |
49 | | |
50 | 102 | if (sizeof(ctx_size) * 8 <= SBSYMCODELEN) |
51 | 0 | { |
52 | 0 | jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "requested IAID arithmetic coding state size too large"); |
53 | 0 | return NULL; |
54 | 0 | } |
55 | | |
56 | 102 | ctx_size = (size_t) 1U << SBSYMCODELEN; |
57 | | |
58 | 102 | result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1); |
59 | 102 | if (result == NULL) { |
60 | 0 | jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate IAID arithmetic coding state"); |
61 | 0 | return NULL; |
62 | 0 | } |
63 | | |
64 | 102 | result->SBSYMCODELEN = SBSYMCODELEN; |
65 | 102 | result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size); |
66 | 102 | if (result->IAIDx == NULL) |
67 | 0 | { |
68 | 0 | jbig2_free(ctx->allocator, result); |
69 | 0 | jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate symbol ID in IAID arithmetic coding state"); |
70 | 0 | return NULL; |
71 | 0 | } |
72 | | |
73 | 102 | memset(result->IAIDx, 0, ctx_size); |
74 | 102 | return result; |
75 | 102 | } |
76 | | |
77 | | /* A.3 */ |
78 | | /* Return value: -1 on error, 0 on normal value */ |
79 | | int |
80 | | jbig2_arith_iaid_decode(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *actx, Jbig2ArithState *as, int32_t *p_result) |
81 | 357 | { |
82 | 357 | Jbig2ArithCx *IAIDx = actx->IAIDx; |
83 | 357 | uint8_t SBSYMCODELEN = actx->SBSYMCODELEN; |
84 | | /* A.3 (1) */ |
85 | 357 | int PREV = 1; |
86 | 357 | int D; |
87 | 357 | int i; |
88 | | |
89 | | /* A.3 (2) */ |
90 | 1.10k | for (i = 0; i < SBSYMCODELEN; i++) { |
91 | 747 | D = jbig2_arith_decode(ctx, as, &IAIDx[PREV]); |
92 | 747 | if (D < 0) |
93 | 0 | return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAIDx code"); |
94 | | #ifdef VERBOSE |
95 | | fprintf(stderr, "IAID%x: D = %d\n", PREV, D); |
96 | | #endif |
97 | 747 | PREV = (PREV << 1) | D; |
98 | 747 | } |
99 | | /* A.3 (3) */ |
100 | 357 | PREV -= 1 << SBSYMCODELEN; |
101 | | #ifdef VERBOSE |
102 | | fprintf(stderr, "IAID result: %d\n", PREV); |
103 | | #endif |
104 | 357 | *p_result = PREV; |
105 | 357 | return 0; |
106 | 357 | } |
107 | | |
108 | | void |
109 | | jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax) |
110 | 102 | { |
111 | 102 | if (iax != NULL) { |
112 | 102 | jbig2_free(ctx->allocator, iax->IAIDx); |
113 | 102 | jbig2_free(ctx->allocator, iax); |
114 | 102 | } |
115 | 102 | } |