• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/fxcodec/jbig2/JBig2_ArithDecoder.h"
8 
9 #include "core/fxcodec/jbig2/JBig2_BitStream.h"
10 #include "core/fxcrt/fx_basic.h"
11 
12 namespace {
13 
14 struct JBig2ArithQe {
15   unsigned int Qe;
16   unsigned int NMPS;
17   unsigned int NLPS;
18   unsigned int nSwitch;
19 };
20 
21 const JBig2ArithQe kQeTable[] = {
22     // Stupid hack to keep clang-format from reformatting this badly.
23     {0x5601, 1, 1, 1},   {0x3401, 2, 6, 0},   {0x1801, 3, 9, 0},
24     {0x0AC1, 4, 12, 0},  {0x0521, 5, 29, 0},  {0x0221, 38, 33, 0},
25     {0x5601, 7, 6, 1},   {0x5401, 8, 14, 0},  {0x4801, 9, 14, 0},
26     {0x3801, 10, 14, 0}, {0x3001, 11, 17, 0}, {0x2401, 12, 18, 0},
27     {0x1C01, 13, 20, 0}, {0x1601, 29, 21, 0}, {0x5601, 15, 14, 1},
28     {0x5401, 16, 14, 0}, {0x5101, 17, 15, 0}, {0x4801, 18, 16, 0},
29     {0x3801, 19, 17, 0}, {0x3401, 20, 18, 0}, {0x3001, 21, 19, 0},
30     {0x2801, 22, 19, 0}, {0x2401, 23, 20, 0}, {0x2201, 24, 21, 0},
31     {0x1C01, 25, 22, 0}, {0x1801, 26, 23, 0}, {0x1601, 27, 24, 0},
32     {0x1401, 28, 25, 0}, {0x1201, 29, 26, 0}, {0x1101, 30, 27, 0},
33     {0x0AC1, 31, 28, 0}, {0x09C1, 32, 29, 0}, {0x08A1, 33, 30, 0},
34     {0x0521, 34, 31, 0}, {0x0441, 35, 32, 0}, {0x02A1, 36, 33, 0},
35     {0x0221, 37, 34, 0}, {0x0141, 38, 35, 0}, {0x0111, 39, 36, 0},
36     {0x0085, 40, 37, 0}, {0x0049, 41, 38, 0}, {0x0025, 42, 39, 0},
37     {0x0015, 43, 40, 0}, {0x0009, 44, 41, 0}, {0x0005, 45, 42, 0},
38     {0x0001, 45, 43, 0}, {0x5601, 46, 46, 0}};
39 
40 const unsigned int kDefaultAValue = 0x8000;
41 
DecodeNMPS(JBig2ArithCtx * pCX,const JBig2ArithQe & qe)42 int DecodeNMPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) {
43   pCX->I = qe.NMPS;
44   return pCX->MPS;
45 }
46 
DecodeNLPS(JBig2ArithCtx * pCX,const JBig2ArithQe & qe)47 int DecodeNLPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) {
48   // TODO(thestig): |D|, |MPS| and friends probably should be booleans.
49   int D = 1 - pCX->MPS;
50   if (qe.nSwitch == 1)
51     pCX->MPS = 1 - pCX->MPS;
52   pCX->I = qe.NLPS;
53   return D;
54 }
55 
56 }  // namespace
57 
CJBig2_ArithDecoder(CJBig2_BitStream * pStream)58 CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream)
59     : m_pStream(pStream) {
60   m_B = m_pStream->getCurByte_arith();
61   m_C = (m_B ^ 0xff) << 16;
62   BYTEIN();
63   m_C = m_C << 7;
64   m_CT = m_CT - 7;
65   m_A = kDefaultAValue;
66 }
67 
~CJBig2_ArithDecoder()68 CJBig2_ArithDecoder::~CJBig2_ArithDecoder() {}
69 
DECODE(JBig2ArithCtx * pCX)70 int CJBig2_ArithDecoder::DECODE(JBig2ArithCtx* pCX) {
71   if (!pCX || pCX->I >= FX_ArraySize(kQeTable))
72     return 0;
73 
74   const JBig2ArithQe& qe = kQeTable[pCX->I];
75   m_A -= qe.Qe;
76   if ((m_C >> 16) < m_A) {
77     if (m_A & kDefaultAValue)
78       return pCX->MPS;
79 
80     const int D = m_A < qe.Qe ? DecodeNLPS(pCX, qe) : DecodeNMPS(pCX, qe);
81     ReadValueA();
82     return D;
83   }
84 
85   m_C -= m_A << 16;
86   const int D = m_A < qe.Qe ? DecodeNMPS(pCX, qe) : DecodeNLPS(pCX, qe);
87   m_A = qe.Qe;
88   ReadValueA();
89   return D;
90 }
91 
BYTEIN()92 void CJBig2_ArithDecoder::BYTEIN() {
93   unsigned char B1;
94   if (m_B == 0xff) {
95     B1 = m_pStream->getNextByte_arith();
96     if (B1 > 0x8f) {
97       m_CT = 8;
98     } else {
99       m_pStream->incByteIdx();
100       m_B = B1;
101       m_C = m_C + 0xfe00 - (m_B << 9);
102       m_CT = 7;
103     }
104   } else {
105     m_pStream->incByteIdx();
106     m_B = m_pStream->getCurByte_arith();
107     m_C = m_C + 0xff00 - (m_B << 8);
108     m_CT = 8;
109   }
110 }
111 
ReadValueA()112 void CJBig2_ArithDecoder::ReadValueA() {
113   do {
114     if (m_CT == 0)
115       BYTEIN();
116     m_A <<= 1;
117     m_C <<= 1;
118     --m_CT;
119   } while ((m_A & kDefaultAValue) == 0);
120 }
121