Blame jbig2_arith_iaid.c

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.3 */
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
#ifdef VERBOSE
Packit 3f21c4
#include <stdio.h>              /* for debug printing only */
Packit 3f21c4
#endif
Packit 3f21c4
Packit 3f21c4
#include "jbig2.h"
Packit 3f21c4
#include "jbig2_priv.h"
Packit 3f21c4
#include "jbig2_arith.h"
Packit 3f21c4
#include "jbig2_arith_iaid.h"
Packit 3f21c4
Packit 3f21c4
struct _Jbig2ArithIaidCtx {
Packit 3f21c4
    int SBSYMCODELEN;
Packit 3f21c4
    Jbig2ArithCx *IAIDx;
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
Jbig2ArithIaidCtx *
Packit 3f21c4
jbig2_arith_iaid_ctx_new(Jbig2Ctx *ctx, int SBSYMCODELEN)
Packit 3f21c4
{
Packit 3f21c4
    Jbig2ArithIaidCtx *result = jbig2_new(ctx, Jbig2ArithIaidCtx, 1);
Packit 3f21c4
    int ctx_size = 1 << SBSYMCODELEN;
Packit 3f21c4
Packit 3f21c4
    if (result == NULL) {
Packit 3f21c4
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate storage in jbig2_arith_iaid_ctx_new");
Packit 3f21c4
        return result;
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    result->SBSYMCODELEN = SBSYMCODELEN;
Packit 3f21c4
    result->IAIDx = jbig2_new(ctx, Jbig2ArithCx, ctx_size);
Packit 3f21c4
    if (result->IAIDx != NULL) {
Packit 3f21c4
        memset(result->IAIDx, 0, ctx_size);
Packit 3f21c4
    } else {
Packit 3f21c4
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate symbol ID storage in jbig2_arith_iaid_ctx_new");
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    return result;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* A.3 */
Packit 3f21c4
/* Return value: -1 on error, 0 on normal value */
Packit 3f21c4
int
Packit 3f21c4
jbig2_arith_iaid_decode(Jbig2ArithIaidCtx *ctx, Jbig2ArithState *as, int32_t *p_result)
Packit 3f21c4
{
Packit 3f21c4
    Jbig2ArithCx *IAIDx = ctx->IAIDx;
Packit 3f21c4
    int SBSYMCODELEN = ctx->SBSYMCODELEN;
Packit 3f21c4
    int PREV = 1;
Packit 3f21c4
    int D;
Packit 3f21c4
    int i;
Packit 3f21c4
Packit 3f21c4
    /* A.3 (2) */
Packit 3f21c4
    for (i = 0; i < SBSYMCODELEN; i++) {
Packit 3f21c4
        D = jbig2_arith_decode(as, &IAIDx[PREV]);
Packit 3f21c4
#ifdef VERBOSE
Packit 3f21c4
        fprintf(stderr, "IAID%x: D = %d\n", PREV, D);
Packit 3f21c4
#endif
Packit 3f21c4
        PREV = (PREV << 1) | D;
Packit 3f21c4
    }
Packit 3f21c4
    /* A.3 (3) */
Packit 3f21c4
    PREV -= 1 << SBSYMCODELEN;
Packit 3f21c4
#ifdef VERBOSE
Packit 3f21c4
    fprintf(stderr, "IAID result: %d\n", PREV);
Packit 3f21c4
#endif
Packit 3f21c4
    *p_result = PREV;
Packit 3f21c4
    return 0;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
void
Packit 3f21c4
jbig2_arith_iaid_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIaidCtx *iax)
Packit 3f21c4
{
Packit 3f21c4
    if (iax != NULL) {
Packit 3f21c4
        jbig2_free(ctx->allocator, iax->IAIDx);
Packit 3f21c4
        jbig2_free(ctx->allocator, iax);
Packit 3f21c4
    }
Packit 3f21c4
}