Make JBig2ArithCtx a class.
For better encapsulation.
Change-Id: Ia6fd8056112d97d672b91a9a521a2978c807060f
Reviewed-on: https://pdfium-review.googlesource.com/42605
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcodec/jbig2/JBig2_ArithDecoder.cpp b/core/fxcodec/jbig2/JBig2_ArithDecoder.cpp
index 7d9f8fe..b3a8515 100644
--- a/core/fxcodec/jbig2/JBig2_ArithDecoder.cpp
+++ b/core/fxcodec/jbig2/JBig2_ArithDecoder.cpp
@@ -11,14 +11,7 @@
namespace {
-struct JBig2ArithQe {
- uint16_t Qe;
- uint8_t NMPS;
- uint8_t NLPS;
- uint8_t nSwitch;
-};
-
-const JBig2ArithQe kQeTable[] = {
+const JBig2ArithCtx::JBig2ArithQe kQeTable[] = {
// Stupid hack to keep clang-format from reformatting this badly.
{0x5601, 1, 1, 1}, {0x3401, 2, 6, 0}, {0x1801, 3, 9, 0},
{0x0AC1, 4, 12, 0}, {0x0521, 5, 29, 0}, {0x0221, 38, 33, 0},
@@ -39,21 +32,23 @@
const unsigned int kDefaultAValue = 0x8000;
-int DecodeNMPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) {
- pCX->I = qe.NMPS;
- return pCX->MPS;
-}
+} // namespace
-int DecodeNLPS(JBig2ArithCtx* pCX, const JBig2ArithQe& qe) {
+JBig2ArithCtx::JBig2ArithCtx() = default;
+
+int JBig2ArithCtx::DecodeNLPS(const JBig2ArithQe& qe) {
// TODO(thestig): |D|, |MPS| and friends probably should be booleans.
- int D = 1 - pCX->MPS;
+ int D = 1 - m_MPS;
if (qe.nSwitch == 1)
- pCX->MPS = 1 - pCX->MPS;
- pCX->I = qe.NLPS;
+ m_MPS = 1 - m_MPS;
+ m_I = qe.NLPS;
return D;
}
-} // namespace
+int JBig2ArithCtx::DecodeNMPS(const JBig2ArithQe& qe) {
+ m_I = qe.NMPS;
+ return m_MPS;
+}
CJBig2_ArithDecoder::CJBig2_ArithDecoder(CJBig2_BitStream* pStream)
: m_Complete(false), m_FinishedStream(false), m_pStream(pStream) {
@@ -68,22 +63,22 @@
CJBig2_ArithDecoder::~CJBig2_ArithDecoder() {}
int CJBig2_ArithDecoder::Decode(JBig2ArithCtx* pCX) {
- if (!pCX || pCX->I >= FX_ArraySize(kQeTable))
+ if (!pCX || pCX->I() >= FX_ArraySize(kQeTable))
return 0;
- const JBig2ArithQe& qe = kQeTable[pCX->I];
+ const JBig2ArithCtx::JBig2ArithQe& qe = kQeTable[pCX->I()];
m_A -= qe.Qe;
if ((m_C >> 16) < m_A) {
if (m_A & kDefaultAValue)
- return pCX->MPS;
+ return pCX->MPS();
- const int D = m_A < qe.Qe ? DecodeNLPS(pCX, qe) : DecodeNMPS(pCX, qe);
+ const int D = m_A < qe.Qe ? pCX->DecodeNLPS(qe) : pCX->DecodeNMPS(qe);
ReadValueA();
return D;
}
m_C -= m_A << 16;
- const int D = m_A < qe.Qe ? DecodeNMPS(pCX, qe) : DecodeNLPS(pCX, qe);
+ const int D = m_A < qe.Qe ? pCX->DecodeNMPS(qe) : pCX->DecodeNLPS(qe);
m_A = qe.Qe;
ReadValueA();
return D;
diff --git a/core/fxcodec/jbig2/JBig2_ArithDecoder.h b/core/fxcodec/jbig2/JBig2_ArithDecoder.h
index 250e700..0a23323 100644
--- a/core/fxcodec/jbig2/JBig2_ArithDecoder.h
+++ b/core/fxcodec/jbig2/JBig2_ArithDecoder.h
@@ -13,11 +13,26 @@
class CJBig2_BitStream;
-struct JBig2ArithCtx {
- JBig2ArithCtx() : MPS(0), I(0) {}
+class JBig2ArithCtx {
+ public:
+ struct JBig2ArithQe {
+ uint16_t Qe;
+ uint8_t NMPS;
+ uint8_t NLPS;
+ uint8_t nSwitch;
+ };
- unsigned int MPS;
- unsigned int I;
+ JBig2ArithCtx();
+
+ int DecodeNLPS(const JBig2ArithQe& qe);
+ int DecodeNMPS(const JBig2ArithQe& qe);
+
+ unsigned int MPS() const { return m_MPS; }
+ unsigned int I() const { return m_I; }
+
+ private:
+ unsigned int m_MPS = 0;
+ unsigned int m_I = 0;
};
class CJBig2_ArithDecoder {
diff --git a/core/fxcodec/jbig2/JBig2_GrdProc.h b/core/fxcodec/jbig2/JBig2_GrdProc.h
index a78e8cd..af6f223 100644
--- a/core/fxcodec/jbig2/JBig2_GrdProc.h
+++ b/core/fxcodec/jbig2/JBig2_GrdProc.h
@@ -17,8 +17,8 @@
class CJBig2_ArithDecoder;
class CJBig2_BitStream;
class CJBig2_Image;
+class JBig2ArithCtx;
class PauseIndicatorIface;
-struct JBig2ArithCtx;
class CJBig2_GRDProc {
public:
diff --git a/core/fxcodec/jbig2/JBig2_GrrdProc.h b/core/fxcodec/jbig2/JBig2_GrrdProc.h
index ec178a2..c7e3bf9 100644
--- a/core/fxcodec/jbig2/JBig2_GrrdProc.h
+++ b/core/fxcodec/jbig2/JBig2_GrrdProc.h
@@ -14,7 +14,7 @@
class CJBig2_ArithDecoder;
class CJBig2_Image;
-struct JBig2ArithCtx;
+class JBig2ArithCtx;
class CJBig2_GRRDProc {
public:
diff --git a/core/fxcodec/jbig2/JBig2_HtrdProc.h b/core/fxcodec/jbig2/JBig2_HtrdProc.h
index eab3f57..cd90aae 100644
--- a/core/fxcodec/jbig2/JBig2_HtrdProc.h
+++ b/core/fxcodec/jbig2/JBig2_HtrdProc.h
@@ -15,8 +15,8 @@
class CJBig2_ArithDecoder;
class CJBig2_BitStream;
+class JBig2ArithCtx;
class PauseIndicatorIface;
-struct JBig2ArithCtx;
class CJBig2_HTRDProc {
public:
diff --git a/core/fxcodec/jbig2/JBig2_PddProc.h b/core/fxcodec/jbig2/JBig2_PddProc.h
index 13590ed..be5fadf 100644
--- a/core/fxcodec/jbig2/JBig2_PddProc.h
+++ b/core/fxcodec/jbig2/JBig2_PddProc.h
@@ -15,8 +15,8 @@
class CJBig2_BitStream;
class CJBig2_GRDProc;
class CJBig2_PatternDict;
+class JBig2ArithCtx;
class PauseIndicatorIface;
-struct JBig2ArithCtx;
class CJBig2_PDDProc {
public:
diff --git a/core/fxcodec/jbig2/JBig2_TrdProc.h b/core/fxcodec/jbig2/JBig2_TrdProc.h
index 855a13a..8f4e42d 100644
--- a/core/fxcodec/jbig2/JBig2_TrdProc.h
+++ b/core/fxcodec/jbig2/JBig2_TrdProc.h
@@ -19,7 +19,7 @@
class CJBig2_ArithIntDecoder;
class CJBig2_BitStream;
class CJBig2_HuffmanTable;
-struct JBig2ArithCtx;
+class JBig2ArithCtx;
struct JBig2HuffmanCode;
struct JBig2IntDecoderState {