Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
JBig2_ArithDecoder.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
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 <array>
10#include <iterator>
11
12#include "core/fxcodec/jbig2/JBig2_BitStream.h"
13#include "core/fxcrt/check_op.h"
14#include "core/fxcrt/stl_util.h"
15
16namespace {
17
18constexpr auto kQeTable = fxcrt::ToArray<const JBig2ArithCtx::JBig2ArithQe>(
19 {// Stupid hack to keep clang-format from reformatting this badly.
20 {0x5601, 1, 1, true}, {0x3401, 2, 6, false}, {0x1801, 3, 9, false},
21 {0x0AC1, 4, 12, false}, {0x0521, 5, 29, false}, {0x0221, 38, 33, false},
22 {0x5601, 7, 6, true}, {0x5401, 8, 14, false}, {0x4801, 9, 14, false},
23 {0x3801, 10, 14, false}, {0x3001, 11, 17, false}, {0x2401, 12, 18, false},
24 {0x1C01, 13, 20, false}, {0x1601, 29, 21, false}, {0x5601, 15, 14, true},
25 {0x5401, 16, 14, false}, {0x5101, 17, 15, false}, {0x4801, 18, 16, false},
26 {0x3801, 19, 17, false}, {0x3401, 20, 18, false}, {0x3001, 21, 19, false},
27 {0x2801, 22, 19, false}, {0x2401, 23, 20, false}, {0x2201, 24, 21, false},
28 {0x1C01, 25, 22, false}, {0x1801, 26, 23, false}, {0x1601, 27, 24, false},
29 {0x1401, 28, 25, false}, {0x1201, 29, 26, false}, {0x1101, 30, 27, false},
30 {0x0AC1, 31, 28, false}, {0x09C1, 32, 29, false}, {0x08A1, 33, 30, false},
31 {0x0521, 34, 31, false}, {0x0441, 35, 32, false}, {0x02A1, 36, 33, false},
32 {0x0221, 37, 34, false}, {0x0141, 38, 35, false}, {0x0111, 39, 36, false},
33 {0x0085, 40, 37, false}, {0x0049, 41, 38, false}, {0x0025, 42, 39, false},
34 {0x0015, 43, 40, false}, {0x0009, 44, 41, false}, {0x0005, 45, 42, false},
35 {0x0001, 45, 43, false}, {0x5601, 46, 46, false}});
36
37const unsigned int kDefaultAValue = 0x8000;
38
39} // namespace
40
41JBig2ArithCtx::JBig2ArithCtx() = default;
42
44 bool D = !m_MPS;
45 if (qe.bSwitch)
46 m_MPS = !m_MPS;
47 m_I = qe.NLPS;
48 DCHECK_LT(m_I, std::size(kQeTable));
49 return D;
50}
51
53 m_I = qe.NMPS;
54 DCHECK_LT(m_I, std::size(kQeTable));
55 return MPS();
56}
57
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
69
71 CHECK_LT(pCX->I(), std::size(kQeTable));
72
73 const JBig2ArithCtx::JBig2ArithQe& qe = kQeTable[pCX->I()];
74 m_A -= qe.Qe;
75 if ((m_C >> 16) < m_A) {
76 if (m_A & kDefaultAValue)
77 return pCX->MPS();
78
79 const int D = m_A < qe.Qe ? pCX->DecodeNLPS(qe) : pCX->DecodeNMPS(qe);
80 ReadValueA();
81 return D;
82 }
83
84 m_C -= m_A << 16;
85 const int D = m_A < qe.Qe ? pCX->DecodeNMPS(qe) : pCX->DecodeNLPS(qe);
86 m_A = qe.Qe;
87 ReadValueA();
88 return D;
89}
90
91void CJBig2_ArithDecoder::BYTEIN() {
92 if (m_B == 0xff) {
93 unsigned char B1 = m_pStream->getNextByte_arith();
94 if (B1 > 0x8f) {
95 m_CT = 8;
96
97 switch (m_State) {
98 case StreamState::kDataAvailable:
99 // Finished decoding data (see JBIG2 spec, Section E.3.4).
100 m_State = StreamState::kDecodingFinished;
101 break;
102 case StreamState::kDecodingFinished:
103 // Allow one more call in the finished state. https://crbug.com/947622
104 m_State = StreamState::kLooping;
105 break;
106 case StreamState::kLooping:
107 // Looping state detected. Mark decoding as complete to bail out.
108 // https://crbug.com/767156
109 m_Complete = true;
110 break;
111 }
112 } else {
113 m_pStream->incByteIdx();
114 m_B = B1;
115 m_C = m_C + 0xfe00 - (m_B << 9);
116 m_CT = 7;
117 }
118 } else {
119 m_pStream->incByteIdx();
120 m_B = m_pStream->getCurByte_arith();
121 m_C = m_C + 0xff00 - (m_B << 8);
122 m_CT = 8;
123 }
124
125 if (!m_pStream->IsInBounds())
126 m_Complete = true;
127}
128
129void CJBig2_ArithDecoder::ReadValueA() {
130 do {
131 if (m_CT == 0)
132 BYTEIN();
133 m_A <<= 1;
134 m_C <<= 1;
135 --m_CT;
136 } while ((m_A & kDefaultAValue) == 0);
137}
#define CHECK_LT(x, y)
Definition check_op.h:12
#define DCHECK_LT(x, y)
Definition check_op.h:19
CJBig2_ArithDecoder(CJBig2_BitStream *pStream)
int Decode(JBig2ArithCtx *pCX)
int DecodeNLPS(const JBig2ArithQe &qe)
unsigned int I() const
int DecodeNMPS(const JBig2ArithQe &qe)
unsigned int MPS() const