Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     jbig2dec
  3.  
  4.     Copyright (C) 2001 Artifex Software, Inc.
  5.  
  6.     This software is provided AS-IS with no warranty,
  7.     either express or implied.
  8.  
  9.     This software is distributed under license and may not
  10.     be copied, modified or distributed except as expressly
  11.     authorized under the terms of the license contained in
  12.     the file LICENSE in this distribution.
  13.  
  14.     For further licensing information refer to http://artifex.com/ or
  15.     contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
  16.     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  17. */
  18.  
  19. /* Annex A */
  20.  
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "os_types.h"
  25.  
  26. #include <stddef.h>
  27. #include <string.h> /* memset() */
  28.  
  29. #include "jbig2.h"
  30. #include "jbig2_priv.h"
  31. #include "jbig2_arith.h"
  32. #include "jbig2_arith_int.h"
  33.  
  34. struct _Jbig2ArithIntCtx {
  35.   Jbig2ArithCx IAx[512];
  36. };
  37.  
  38. Jbig2ArithIntCtx *
  39. jbig2_arith_int_ctx_new(Jbig2Ctx *ctx)
  40. {
  41.   Jbig2ArithIntCtx *result = jbig2_new(ctx, Jbig2ArithIntCtx, 1);
  42.  
  43.   memset(result->IAx, 0, sizeof(result->IAx));
  44.  
  45.   return result;
  46. }
  47.  
  48. /* A.2 */
  49. /* Return value: -1 on error, 0 on normal value, 1 on OOB return. */
  50. int
  51. jbig2_arith_int_decode(Jbig2ArithIntCtx *ctx, Jbig2ArithState *as,
  52.                        int32_t *p_result)
  53. {
  54.   Jbig2ArithCx *IAx = ctx->IAx;
  55.   int PREV = 1;
  56.   int S, V;
  57.   int bit;
  58.   int n_tail, offset;
  59.   int i;
  60.  
  61.   S = jbig2_arith_decode(as, &IAx[PREV]);
  62.   PREV = (PREV << 1) | S;
  63.  
  64.   bit = jbig2_arith_decode(as, &IAx[PREV]);
  65.   PREV = (PREV << 1) | bit;
  66.   if (bit)
  67.     {
  68.       bit = jbig2_arith_decode(as, &IAx[PREV]);
  69.       PREV = (PREV << 1) | bit;
  70.  
  71.       if (bit)
  72.         {
  73.           bit = jbig2_arith_decode(as, &IAx[PREV]);
  74.           PREV = (PREV << 1) | bit;
  75.  
  76.           if (bit)
  77.             {
  78.               bit = jbig2_arith_decode(as, &IAx[PREV]);
  79.               PREV = (PREV << 1) | bit;
  80.  
  81.               if (bit)
  82.                 {
  83.                   bit = jbig2_arith_decode(as, &IAx[PREV]);
  84.                   PREV = (PREV << 1) | bit;
  85.  
  86.                   if (bit)
  87.                     {
  88.                       n_tail = 32;
  89.                       offset = 4436;
  90.                     }
  91.                   else
  92.                     {
  93.                       n_tail = 12;
  94.                       offset = 340;
  95.                     }
  96.                 }
  97.               else
  98.                 {
  99.                   n_tail = 8;
  100.                   offset = 84;
  101.                 }
  102.             }
  103.           else
  104.             {
  105.               n_tail = 6;
  106.               offset = 20;
  107.             }
  108.         }
  109.       else
  110.         {
  111.           n_tail = 4;
  112.           offset = 4;
  113.         }
  114.     }
  115.   else
  116.     {
  117.       n_tail = 2;
  118.       offset = 0;
  119.     }
  120.  
  121.   V = 0;
  122.   for (i = 0; i < n_tail; i++)
  123.     {
  124.       bit = jbig2_arith_decode(as, &IAx[PREV]);
  125.       PREV = ((PREV << 1) & 511) | (PREV & 256) | bit;
  126.       V = (V << 1) | bit;
  127.     }
  128.  
  129.   V += offset;
  130.   V = S ? -V : V;
  131.   *p_result = V;
  132.   return S && V == 0 ? 1 : 0;
  133. }
  134.  
  135. void
  136. jbig2_arith_int_ctx_free(Jbig2Ctx *ctx, Jbig2ArithIntCtx *iax)
  137. {
  138.   jbig2_free(ctx->allocator, iax);
  139. }
  140.