Coverage Report

Created: 2022-10-31 07:00

/src/ghostpdl/jbig2dec/jbig2_arith_iaid.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2021 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
    uint8_t SBSYMCODELEN;
41
    Jbig2ArithCx *IAIDx;
42
};
43
44
Jbig2ArithIaidCtx *
45
jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, uint8_t SBSYMCODELEN)
46
20
{
47
20
    Jbig2ArithIaidCtx *result;
48
20
    size_t ctx_size;
49
50
20
    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
20
    ctx_size = (size_t) 1U << SBSYMCODELEN;
57
58
20
    result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
59
20
    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
20
    result->SBSYMCODELEN = SBSYMCODELEN;
65
20
    result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
66
20
    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
20
    memset(result->IAIDx, 0, ctx_size);
74
20
    return result;
75
20
}
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
84
{
82
84
    Jbig2ArithCx *IAIDx = actx->IAIDx;
83
84
    uint8_t SBSYMCODELEN = actx->SBSYMCODELEN;
84
    /* A.3 (1) */
85
84
    int PREV = 1;
86
84
    int D;
87
84
    int i;
88
89
    /* A.3 (2) */
90
306
    for (i = 0; i < SBSYMCODELEN; i++) {
91
222
        D = jbig2_arith_decode(ctx, as, &IAIDx[PREV]);
92
222
        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
222
        PREV = (PREV << 1) | D;
98
222
    }
99
    /* A.3 (3) */
100
84
    PREV -= 1 << SBSYMCODELEN;
101
#ifdef VERBOSE
102
    fprintf(stderr, "IAID result: %d\n", PREV);
103
#endif
104
84
    *p_result = PREV;
105
84
    return 0;
106
84
}
107
108
void
109
jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax)
110
20
{
111
20
    if (iax != NULL) {
112
20
        jbig2_free(ctx->allocator, iax->IAIDx);
113
20
        jbig2_free(ctx->allocator, iax);
114
20
    }
115
20
}