|
Packit |
3f21c4 |
/* Copyright (C) 2001-2012 Artifex Software, Inc.
|
|
Packit |
3f21c4 |
All Rights Reserved.
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
This software is provided AS-IS with no warranty, either express or
|
|
Packit |
3f21c4 |
implied.
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
This software is distributed under license and may not be copied,
|
|
Packit |
3f21c4 |
modified or distributed except as expressly authorized under the terms
|
|
Packit |
3f21c4 |
of the license contained in the file LICENSE in this distribution.
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
Refer to licensing information at http://www.artifex.com or contact
|
|
Packit |
3f21c4 |
Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
|
|
Packit |
3f21c4 |
CA 94903, U.S.A., +1(415)492-9861, for further information.
|
|
Packit |
3f21c4 |
*/
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/*
|
|
Packit |
3f21c4 |
jbig2dec
|
|
Packit |
3f21c4 |
*/
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* Annex A */
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
3f21c4 |
#include "config.h"
|
|
Packit |
3f21c4 |
#endif
|
|
Packit |
3f21c4 |
#include "os_types.h"
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#include <stddef.h>
|
|
Packit |
3f21c4 |
#include <string.h> /* memset() */
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
#include "jbig2.h"
|
|
Packit |
3f21c4 |
#include "jbig2_priv.h"
|
|
Packit |
3f21c4 |
#include "jbig2_arith.h"
|
|
Packit |
3f21c4 |
#include "jbig2_arith_int.h"
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
struct _Jbig2ArithIntCtx {
|
|
Packit |
3f21c4 |
Jbig2ArithCx IAx[512];
|
|
Packit |
3f21c4 |
};
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
Jbig2ArithIntCtx *
|
|
Packit |
3f21c4 |
jbig2_arith_int_ctx_new(Jbig2Ctx *ctx)
|
|
Packit |
3f21c4 |
{
|
|
Packit |
3f21c4 |
Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
if (result == NULL) {
|
|
Packit |
3f21c4 |
jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate Jbig2ArithIntCtx in jbig2_arith_int_ctx_new");
|
|
Packit |
3f21c4 |
} else {
|
|
Packit |
3f21c4 |
memset(result->IAx, 0, sizeof(result->IAx));
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
return result;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
/* A.2 */
|
|
Packit |
3f21c4 |
/* Return value: -1 on error, 0 on normal value, 1 on OOB return. */
|
|
Packit |
3f21c4 |
int
|
|
Packit |
3f21c4 |
jbig2_arith_int_decode(Jbig2ArithIntCtx *ctx, Jbig2ArithState *as, int32_t *p_result)
|
|
Packit |
3f21c4 |
{
|
|
Packit |
3f21c4 |
Jbig2ArithCx *IAx = ctx->IAx;
|
|
Packit |
3f21c4 |
int PREV = 1;
|
|
Packit |
3f21c4 |
int S, V;
|
|
Packit |
3f21c4 |
int bit;
|
|
Packit |
3f21c4 |
int n_tail, offset;
|
|
Packit |
3f21c4 |
int i;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
S = jbig2_arith_decode(as, &IAx[PREV]);
|
|
Packit |
3f21c4 |
PREV = (PREV << 1) | S;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
bit = jbig2_arith_decode(as, &IAx[PREV]);
|
|
Packit |
3f21c4 |
PREV = (PREV << 1) | bit;
|
|
Packit |
3f21c4 |
if (bit) {
|
|
Packit |
3f21c4 |
bit = jbig2_arith_decode(as, &IAx[PREV]);
|
|
Packit |
3f21c4 |
PREV = (PREV << 1) | bit;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
if (bit) {
|
|
Packit |
3f21c4 |
bit = jbig2_arith_decode(as, &IAx[PREV]);
|
|
Packit |
3f21c4 |
PREV = (PREV << 1) | bit;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
if (bit) {
|
|
Packit |
3f21c4 |
bit = jbig2_arith_decode(as, &IAx[PREV]);
|
|
Packit |
3f21c4 |
PREV = (PREV << 1) | bit;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
if (bit) {
|
|
Packit |
3f21c4 |
bit = jbig2_arith_decode(as, &IAx[PREV]);
|
|
Packit |
3f21c4 |
PREV = (PREV << 1) | bit;
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
if (bit) {
|
|
Packit |
3f21c4 |
n_tail = 32;
|
|
Packit |
3f21c4 |
offset = 4436;
|
|
Packit |
3f21c4 |
} else {
|
|
Packit |
3f21c4 |
n_tail = 12;
|
|
Packit |
3f21c4 |
offset = 340;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
} else {
|
|
Packit |
3f21c4 |
n_tail = 8;
|
|
Packit |
3f21c4 |
offset = 84;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
} else {
|
|
Packit |
3f21c4 |
n_tail = 6;
|
|
Packit |
3f21c4 |
offset = 20;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
} else {
|
|
Packit |
3f21c4 |
n_tail = 4;
|
|
Packit |
3f21c4 |
offset = 4;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
} else {
|
|
Packit |
3f21c4 |
n_tail = 2;
|
|
Packit |
3f21c4 |
offset = 0;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
V = 0;
|
|
Packit |
3f21c4 |
for (i = 0; i < n_tail; i++) {
|
|
Packit |
3f21c4 |
bit = jbig2_arith_decode(as, &IAx[PREV]);
|
|
Packit |
3f21c4 |
PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
|
|
Packit |
3f21c4 |
V = (V << 1) | bit;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
V += offset;
|
|
Packit |
3f21c4 |
V = S ? -V : V;
|
|
Packit |
3f21c4 |
*p_result = V;
|
|
Packit |
3f21c4 |
return S && V == 0 ? 1 : 0;
|
|
Packit |
3f21c4 |
}
|
|
Packit |
3f21c4 |
|
|
Packit |
3f21c4 |
void
|
|
Packit |
3f21c4 |
jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax)
|
|
Packit |
3f21c4 |
{
|
|
Packit |
3f21c4 |
jbig2_free(ctx->allocator, iax);
|
|
Packit |
3f21c4 |
}
|