• 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 "JBig2_HuffmanDecoder.h"
8 
9 #include "JBig2_Define.h"
10 
CJBig2_HuffmanDecoder(CJBig2_BitStream * pStream)11 CJBig2_HuffmanDecoder::CJBig2_HuffmanDecoder(CJBig2_BitStream* pStream)
12     : m_pStream(pStream) {
13 }
14 
~CJBig2_HuffmanDecoder()15 CJBig2_HuffmanDecoder::~CJBig2_HuffmanDecoder() {}
16 
decodeAValue(CJBig2_HuffmanTable * pTable,int * nResult)17 int CJBig2_HuffmanDecoder::decodeAValue(CJBig2_HuffmanTable* pTable,
18                                         int* nResult) {
19   int nVal = 0;
20   int nBits = 0;
21   while (1) {
22     FX_DWORD nTmp;
23     if (m_pStream->read1Bit(&nTmp) == -1)
24       break;
25 
26     nVal = (nVal << 1) | nTmp;
27     ++nBits;
28     for (FX_DWORD i = 0; i < pTable->Size(); ++i) {
29       if (pTable->GetPREFLEN()[i] == nBits && pTable->GetCODES()[i] == nVal) {
30         if (pTable->IsHTOOB() && i == pTable->Size() - 1)
31           return JBIG2_OOB;
32 
33         if (m_pStream->readNBits(pTable->GetRANGELEN()[i], &nTmp) == -1)
34           return -1;
35 
36         FX_DWORD offset = pTable->IsHTOOB() ? 3 : 2;
37         if (i == pTable->Size() - offset)
38           *nResult = pTable->GetRANGELOW()[i] - nTmp;
39         else
40           *nResult = pTable->GetRANGELOW()[i] + nTmp;
41         return 0;
42       }
43     }
44   }
45   return -1;
46 }
47