summaryrefslogtreecommitdiff
path: root/core/src/fxcodec/jbig2/JBig2_ArithIntDecoder.cpp
blob: b194faf4ee7fb281c248d95f48f2b64e70d2e06f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "JBig2_ArithIntDecoder.h"

#include "../../../include/fxcrt/fx_memory.h"
#include "JBig2_Define.h"

CJBig2_ArithIntDecoder::CJBig2_ArithIntDecoder() {
  IAx = FX_Alloc(JBig2ArithCtx, 512);
  JBIG2_memset(IAx, 0, sizeof(JBig2ArithCtx) * 512);
}
CJBig2_ArithIntDecoder::~CJBig2_ArithIntDecoder() {
  FX_Free(IAx);
}
int CJBig2_ArithIntDecoder::decode(CJBig2_ArithDecoder* pArithDecoder,
                                   int* nResult) {
  int PREV, V;
  int S, D;
  int nNeedBits, nTemp, i;
  PREV = 1;
  S = pArithDecoder->DECODE(IAx + PREV);
  PREV = (PREV << 1) | S;
  D = pArithDecoder->DECODE(IAx + PREV);
  PREV = (PREV << 1) | D;
  if (D) {
    D = pArithDecoder->DECODE(IAx + PREV);
    PREV = (PREV << 1) | D;
    if (D) {
      D = pArithDecoder->DECODE(IAx + PREV);
      PREV = (PREV << 1) | D;
      if (D) {
        D = pArithDecoder->DECODE(IAx + PREV);
        PREV = (PREV << 1) | D;
        if (D) {
          D = pArithDecoder->DECODE(IAx + PREV);
          PREV = (PREV << 1) | D;
          if (D) {
            nNeedBits = 32;
            V = 4436;
          } else {
            nNeedBits = 12;
            V = 340;
          }
        } else {
          nNeedBits = 8;
          V = 84;
        }
      } else {
        nNeedBits = 6;
        V = 20;
      }
    } else {
      nNeedBits = 4;
      V = 4;
    }
  } else {
    nNeedBits = 2;
    V = 0;
  }
  nTemp = 0;
  for (i = 0; i < nNeedBits; i++) {
    D = pArithDecoder->DECODE(IAx + PREV);
    if (PREV < 256) {
      PREV = (PREV << 1) | D;
    } else {
      PREV = (((PREV << 1) | D) & 511) | 256;
    }
    nTemp = (nTemp << 1) | D;
  }
  V += nTemp;
  if (S == 1 && V > 0) {
    V = -V;
  }
  *nResult = V;
  if (S == 1 && V == 0) {
    return JBIG2_OOB;
  }
  return 0;
}
CJBig2_ArithIaidDecoder::CJBig2_ArithIaidDecoder(unsigned char SBSYMCODELENA) {
  SBSYMCODELEN = SBSYMCODELENA;
  IAID = FX_Alloc(JBig2ArithCtx, 1 << SBSYMCODELEN);
  JBIG2_memset(IAID, 0, sizeof(JBig2ArithCtx) * (int)(1 << SBSYMCODELEN));
}
CJBig2_ArithIaidDecoder::~CJBig2_ArithIaidDecoder() {
  FX_Free(IAID);
}
int CJBig2_ArithIaidDecoder::decode(CJBig2_ArithDecoder* pArithDecoder,
                                    int* nResult) {
  int PREV;
  int D;
  int i;
  PREV = 1;
  for (i = 0; i < SBSYMCODELEN; i++) {
    D = pArithDecoder->DECODE(IAID + PREV);
    PREV = (PREV << 1) | D;
  }
  PREV = PREV - (1 << SBSYMCODELEN);
  *nResult = PREV;
  return 0;
}