1// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "JBig2_ArithDecoder.h"
8
9#include "JBig2_BitStream.h"
10#include "core/include/fxcrt/fx_basic.h"
11
12namespace {
13
14struct JBig2ArithQe {
15  unsigned int Qe;
16  unsigned int NMPS;
17  unsigned int NLPS;
18  unsigned int nSwitch;
19};
20
21const JBig2ArithQe kQeTable[] = {
22    // Stupid hack to keep clang-format from reformatting this badly.
23    {0x5601, 1, 1, 1},
24    {0x3401, 2, 6, 0},
25    {0x1801, 3, 9, 0},
26    {0x0AC1, 4, 12, 0},
27    {0x0521, 5, 29, 0},
28    {0x0221, 38, 33, 0},
29    {0x5601, 7, 6, 1},
30    {0x5401, 8, 14, 0},
31    {0x4801, 9, 14, 0},
32    {0x3801, 10, 14, 0},
33    {0x3001, 11, 17, 0},
34    {0x2401, 12, 18, 0},
35    {0x1C01, 13, 20, 0},
36    {0x1601, 29, 21, 0},
37    {0x5601, 15, 14, 1},
38    {0x5401, 16, 14, 0},
39    {0x5101, 17, 15, 0},
40    {0x4801, 18, 16, 0},
41    {0x3801, 19, 17, 0},
42    {0x3401, 20, 18, 0},
43    {0x3001, 21, 19, 0},
44    {0x2801, 22, 19, 0},
45    {0x2401, 23, 20, 0},
46    {0x2201, 24, 21, 0},
47    {0x1C01, 25, 22, 0},
48    {0x1801, 26, 23, 0},
49    {0x1601, 27, 24, 0},
50    {0x1401, 28, 25, 0},
51    {0x1201, 29, 26, 0},
52    {0x1101, 30, 27, 0},
53    {0x0AC1, 31, 28, 0},
54    {0x09C1, 32, 29, 0},
55    {0x08A1, 33, 30, 0},
56    {0x0521, 34, 31, 0},
57    {0x0441, 35, 32, 0},
58    {0x02A1, 36, 33, 0},
59    {0x0221, 37, 34, 0},
60    {0x0141, 38, 35, 0},
61    {0x0111, 39, 36, 0},
62    {0x0085, 40, 37, 0},
63    {0x0049, 41, 38, 0},
64    {0x0025, 42, 39, 0},
65    {0x0015, 43, 40, 0},
66    {0x0009, 44, 41, 0},
67    {0x0005, 45, 42, 0},
68    {0x0001, 45, 43, 0},
69    {0x5601, 46, 46, 0}};
70
71const unsigned int kDefaultAValue = 0x8000;
72
73int DecodeNMPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) {
74  pCX->I = qe.NMPS;
75  return pCX->MPS;
76}
77
78int DecodeNLPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) {
79  // TODO(thestig): |D|, |MPS| and friends probably should be booleans.
80  int D = 1 - pCX->MPS;
81  if (qe.nSwitch == 1)
82    pCX->MPS = 1 - pCX->MPS;
83  pCX->I = qe.NLPS;
84  return D;
85}
86
87}  // namespace
88
89CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream)
90    : m_pStream(pStream) {
91  m_B = m_pStream->getCurByte_arith();
92  m_C = (m_B ^ 0xff) << 16;
93  BYTEIN();
94  m_C = m_C << 7;
95  m_CT = m_CT - 7;
96  m_A = kDefaultAValue;
97}
98
99CJBig2_ArithDecoder::~CJBig2_ArithDecoder() {
100}
101
102int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) {
103  if (!pCX || pCX->I >= FX_ArraySize(kQeTable))
104    return 0;
105
106  const JBig2ArithQe& qe = kQeTable[pCX->I];
107  m_A -= qe.Qe;
108  if ((m_C >> 16) < m_A) {
109    if (m_A & kDefaultAValue)
110      return pCX->MPS;
111
112    const int D = m_A < qe.Qe ? DecodeNLPS(pCX, qe) : DecodeNMPS(pCX, qe);
113    ReadValueA();
114    return D;
115  }
116
117  m_C -= m_A << 16;
118  const int D = m_A < qe.Qe ? DecodeNMPS(pCX, qe) : DecodeNLPS(pCX, qe);
119  m_A = qe.Qe;
120  ReadValueA();
121  return D;
122}
123
124void CJBig2_ArithDecoder::BYTEIN() {
125  unsigned char B1;
126  if (m_B == 0xff) {
127    B1 = m_pStream->getNextByte_arith();
128    if (B1 > 0x8f) {
129      m_CT = 8;
130    } else {
131      m_pStream->incByteIdx();
132      m_B = B1;
133      m_C = m_C + 0xfe00 - (m_B << 9);
134      m_CT = 7;
135    }
136  } else {
137    m_pStream->incByteIdx();
138    m_B = m_pStream->getCurByte_arith();
139    m_C = m_C + 0xff00 - (m_B << 8);
140    m_CT = 8;
141  }
142}
143
144void CJBig2_ArithDecoder::ReadValueA() {
145  do {
146    if (m_CT == 0)
147      BYTEIN();
148    m_A <<= 1;
149    m_C <<= 1;
150    --m_CT;
151  } while ((m_A & kDefaultAValue) == 0);
152}
153