From 724f61671bec82f521be75b0ace069fb3130902a Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 27 Apr 2018 20:53:58 +0000 Subject: Change CJBig2_HuffmanTable to use struct JBig2HuffmanCode. Change-Id: I6461f81a3d8005efa75b8141c18c502a63252883 Reviewed-on: https://pdfium-review.googlesource.com/31537 Commit-Queue: Lei Zhang Reviewed-by: Henrique Nakashima --- core/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp | 30 +++++++++++++++-------------- core/fxcodec/jbig2/JBig2_HuffmanTable.cpp | 28 +++++++++++++-------------- core/fxcodec/jbig2/JBig2_HuffmanTable.h | 7 +++---- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/core/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp b/core/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp index c6dd3924b8..cdb6fbe752 100644 --- a/core/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp +++ b/core/fxcodec/jbig2/JBig2_HuffmanDecoder.cpp @@ -25,20 +25,22 @@ int CJBig2_HuffmanDecoder::DecodeAValue(CJBig2_HuffmanTable* pTable, nVal = (nVal << 1) | nTmp; ++nBits; for (uint32_t i = 0; i < pTable->Size(); ++i) { - if (pTable->GetPREFLEN()[i] == nBits && pTable->GetCODES()[i] == nVal) { - if (pTable->IsHTOOB() && i == pTable->Size() - 1) - return JBIG2_OOB; - - if (m_pStream->readNBits(pTable->GetRANGELEN()[i], &nTmp) == -1) - return -1; - - uint32_t offset = pTable->IsHTOOB() ? 3 : 2; - if (i == pTable->Size() - offset) - *nResult = pTable->GetRANGELOW()[i] - nTmp; - else - *nResult = pTable->GetRANGELOW()[i] + nTmp; - return 0; - } + const JBig2HuffmanCode& code = pTable->GetCODES()[i]; + if (code.codelen != nBits || code.code != nVal) + continue; + + if (pTable->IsHTOOB() && i == pTable->Size() - 1) + return JBIG2_OOB; + + if (m_pStream->readNBits(pTable->GetRANGELEN()[i], &nTmp) == -1) + return -1; + + uint32_t offset = pTable->IsHTOOB() ? 3 : 2; + if (i == pTable->Size() - offset) + *nResult = pTable->GetRANGELOW()[i] - nTmp; + else + *nResult = pTable->GetRANGELOW()[i] + nTmp; + return 0; } } return -1; diff --git a/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp b/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp index 5a49e1e4be..aa3cd0be10 100644 --- a/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp +++ b/core/fxcodec/jbig2/JBig2_HuffmanTable.cpp @@ -11,7 +11,6 @@ #include #include "core/fxcodec/jbig2/JBig2_BitStream.h" -#include "core/fxcodec/jbig2/JBig2_Define.h" #include "core/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h" #include "core/fxcrt/fx_memory.h" #include "third_party/base/numerics/safe_math.h" @@ -31,11 +30,11 @@ CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream) CJBig2_HuffmanTable::~CJBig2_HuffmanTable() {} void CJBig2_HuffmanTable::ParseFromStandardTable(const JBig2TableLine* pTable) { - PREFLEN.resize(NTEMP); + CODES.resize(NTEMP); RANGELEN.resize(NTEMP); RANGELOW.resize(NTEMP); for (uint32_t i = 0; i < NTEMP; ++i) { - PREFLEN[i] = pTable[i].PREFLEN; + CODES[i].codelen = pTable[i].PREFLEN; RANGELEN[i] = pTable[i].RANDELEN; RANGELOW[i] = pTable[i].RANGELOW; } @@ -65,7 +64,7 @@ bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) { ExtendBuffers(false); pdfium::base::CheckedNumeric cur_low = low; do { - if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) || + if ((pStream->readNBits(HTPS, &CODES[NTEMP].codelen) == -1) || (pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1) || (static_cast(RANGELEN[NTEMP]) >= 8 * sizeof(cur_low))) { return false; @@ -81,7 +80,7 @@ bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) { ExtendBuffers(true); } while (cur_low.ValueOrDie() < high); - if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) + if (pStream->readNBits(HTPS, &CODES[NTEMP].codelen) == -1) return false; RANGELEN[NTEMP] = 32; @@ -91,7 +90,7 @@ bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) { RANGELOW[NTEMP] = low - 1; ExtendBuffers(true); - if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) + if (pStream->readNBits(HTPS, &CODES[NTEMP].codelen) == -1) return false; RANGELEN[NTEMP] = 32; @@ -99,7 +98,7 @@ bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) { ExtendBuffers(true); if (HTOOB) { - if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) + if (pStream->readNBits(HTPS, &CODES[NTEMP].codelen) == -1) return false; ++NTEMP; @@ -111,13 +110,12 @@ bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) { bool CJBig2_HuffmanTable::InitCodes() { int lenmax = 0; for (uint32_t i = 0; i < NTEMP; ++i) - lenmax = std::max(PREFLEN[i], lenmax); + lenmax = std::max(CODES[i].codelen, lenmax); - CODES.resize(NTEMP); std::vector LENCOUNT(lenmax + 1); std::vector FIRSTCODE(lenmax + 1); - for (int len : PREFLEN) - ++LENCOUNT[len]; + for (uint32_t i = 0; i < NTEMP; ++i) + ++LENCOUNT[CODES[i].codelen]; FIRSTCODE[0] = 0; LENCOUNT[0] = 0; @@ -131,8 +129,8 @@ bool CJBig2_HuffmanTable::InitCodes() { FIRSTCODE[i] = shifted.ValueOrDie(); int CURCODE = FIRSTCODE[i]; for (uint32_t j = 0; j < NTEMP; ++j) { - if (PREFLEN[j] == i) - CODES[j] = CURCODE++; + if (CODES[j].codelen == i) + CODES[j].code = CURCODE++; } } @@ -143,13 +141,13 @@ void CJBig2_HuffmanTable::ExtendBuffers(bool increment) { if (increment) ++NTEMP; - size_t size = PREFLEN.size(); + size_t size = CODES.size(); if (NTEMP < size) return; size += 16; ASSERT(NTEMP < size); - PREFLEN.resize(size); + CODES.resize(size); RANGELEN.resize(size); RANGELOW.resize(size); } diff --git a/core/fxcodec/jbig2/JBig2_HuffmanTable.h b/core/fxcodec/jbig2/JBig2_HuffmanTable.h index 4f75fba873..cdfb53f4fc 100644 --- a/core/fxcodec/jbig2/JBig2_HuffmanTable.h +++ b/core/fxcodec/jbig2/JBig2_HuffmanTable.h @@ -9,6 +9,7 @@ #include +#include "core/fxcodec/jbig2/JBig2_Define.h" #include "core/fxcrt/fx_system.h" class CJBig2_BitStream; @@ -24,8 +25,7 @@ class CJBig2_HuffmanTable { bool IsHTOOB() const { return HTOOB; } uint32_t Size() const { return NTEMP; } - const std::vector& GetCODES() const { return CODES; } - const std::vector& GetPREFLEN() const { return PREFLEN; } + const std::vector& GetCODES() const { return CODES; } const std::vector& GetRANGELEN() const { return RANGELEN; } const std::vector& GetRANGELOW() const { return RANGELOW; } bool IsOK() const { return m_bOK; } @@ -39,8 +39,7 @@ class CJBig2_HuffmanTable { bool m_bOK; bool HTOOB; uint32_t NTEMP; - std::vector CODES; - std::vector PREFLEN; + std::vector CODES; std::vector RANGELEN; std::vector RANGELOW; }; -- cgit v1.2.3