comparison mupdf-source/thirdparty/jbig2dec/jbig2_arith_iaid.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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 {
47 Jbig2ArithIaidCtx *result;
48 size_t ctx_size;
49
50 if (sizeof(ctx_size) * 8 <= SBSYMCODELEN)
51 {
52 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "requested IAID arithmetic coding state size too large");
53 return NULL;
54 }
55
56 ctx_size = (size_t) 1U << SBSYMCODELEN;
57
58 result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
59 if (result == NULL) {
60 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate IAID arithmetic coding state");
61 return NULL;
62 }
63
64 result->SBSYMCODELEN = SBSYMCODELEN;
65 result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
66 if (result->IAIDx == NULL)
67 {
68 jbig2_free(ctx->allocator, result);
69 jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate symbol ID in IAID arithmetic coding state");
70 return NULL;
71 }
72
73 memset(result->IAIDx, 0, ctx_size);
74 return result;
75 }
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 {
82 Jbig2ArithCx *IAIDx = actx->IAIDx;
83 uint8_t SBSYMCODELEN = actx->SBSYMCODELEN;
84 /* A.3 (1) */
85 int PREV = 1;
86 int D;
87 int i;
88
89 /* A.3 (2) */
90 for (i = 0; i < SBSYMCODELEN; i++) {
91 D = jbig2_arith_decode(ctx, as, &IAIDx[PREV]);
92 if (D < 0)
93 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 PREV = (PREV << 1) | D;
98 }
99 /* A.3 (3) */
100 PREV -= 1 << SBSYMCODELEN;
101 #ifdef VERBOSE
102 fprintf(stderr, "IAID result: %d\n", PREV);
103 #endif
104 *p_result = PREV;
105 return 0;
106 }
107
108 void
109 jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax)
110 {
111 if (iax != NULL) {
112 jbig2_free(ctx->allocator, iax->IAIDx);
113 jbig2_free(ctx->allocator, iax);
114 }
115 }