Coverage Report

Created: 2025-12-11 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/jbig2dec/jbig2_arith_int.c
Line
Count
Source
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 */
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
20.8k
{
42
20.8k
    Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
43
44
20.8k
    if (result == NULL) {
45
71
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to allocate arithmetic integer coding state");
46
71
        return NULL;
47
20.7k
    } else {
48
20.7k
        memset(result->IAx, 0, sizeof(result->IAx));
49
20.7k
    }
50
51
20.7k
    return result;
52
20.8k
}
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
9.35M
{
59
9.35M
    Jbig2ArithCx *IAx = actx->IAx;
60
9.35M
    int PREV = 1;
61
9.35M
    int S;
62
9.35M
    int32_t V;
63
9.35M
    int bit;
64
9.35M
    int n_tail, offset;
65
9.35M
    int i;
66
67
9.35M
    S = jbig2_arith_decode(ctx, as, &IAx[PREV]);
68
9.35M
    if (S < 0)
69
0
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx S");
70
9.35M
    PREV = (PREV << 1) | S;
71
72
9.35M
    bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
73
9.35M
    if (bit < 0)
74
0
        return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 0");
75
9.35M
    PREV = (PREV << 1) | bit;
76
9.35M
    if (bit) {
77
2.87M
        bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
78
2.87M
        if (bit < 0)
79
0
            return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 1");
80
2.87M
        PREV = (PREV << 1) | bit;
81
82
2.87M
        if (bit) {
83
2.49M
            bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
84
2.49M
            if (bit < 0)
85
0
                return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 2");
86
2.49M
            PREV = (PREV << 1) | bit;
87
88
2.49M
            if (bit) {
89
2.44M
                bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
90
2.44M
                if (bit < 0)
91
0
                    return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, JBIG2_UNKNOWN_SEGMENT_NUMBER, "failed to decode IAx decision bit 3");
92
2.44M
                PREV = (PREV << 1) | bit;
93
94
2.44M
                if (bit) {
95
2.36M
                    bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
96
2.36M
                    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.36M
                    PREV = (PREV << 1) | bit;
99
100
2.36M
                    if (bit) {
101
764k
                        n_tail = 32;
102
764k
                        offset = 4436;
103
1.60M
                    } else {
104
1.60M
                        n_tail = 12;
105
1.60M
                        offset = 340;
106
1.60M
                    }
107
2.36M
                } else {
108
78.6k
                    n_tail = 8;
109
78.6k
                    offset = 84;
110
78.6k
                }
111
2.44M
            } else {
112
53.2k
                n_tail = 6;
113
53.2k
                offset = 20;
114
53.2k
            }
115
2.49M
        } else {
116
376k
            n_tail = 4;
117
376k
            offset = 4;
118
376k
        }
119
6.47M
    } else {
120
6.47M
        n_tail = 2;
121
6.47M
        offset = 0;
122
6.47M
    }
123
124
9.35M
    V = 0;
125
68.4M
    for (i = 0; i < n_tail; i++) {
126
59.1M
        bit = jbig2_arith_decode(ctx, as, &IAx[PREV]);
127
59.1M
        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
59.1M
        PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
130
59.1M
        V = (V << 1) | bit;
131
59.1M
    }
132
133
    /* offset is always >=0, so underflow can't happen. */
134
    /* avoid overflow by clamping 32 bit value. */
135
9.35M
    if (V > INT32_MAX - offset)
136
757k
        V = INT32_MAX;
137
8.59M
    else
138
8.59M
        V += offset;
139
9.35M
    V = S ? -V : V;
140
9.35M
    *p_result = V;
141
9.35M
    return S && V == 0 ? 1 : 0;
142
9.35M
}
143
144
void
145
jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax)
146
28.4k
{
147
28.4k
    jbig2_free(ctx->allocator, iax);
148
28.4k
}