| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "core/fxcodec/jbig2/JBig2_HuffmanTable.h" | 7 #include "core/fxcodec/jbig2/JBig2_HuffmanTable.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "core/fxcodec/jbig2/JBig2_BitStream.h" | 12 #include "core/fxcodec/jbig2/JBig2_BitStream.h" |
| 13 #include "core/fxcodec/jbig2/JBig2_Define.h" | 13 #include "core/fxcodec/jbig2/JBig2_Define.h" |
| 14 #include "core/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h" | 14 #include "core/fxcodec/jbig2/JBig2_HuffmanTable_Standard.h" |
| 15 #include "core/fxcrt/include/fx_memory.h" | 15 #include "core/fxcrt/include/fx_memory.h" |
| 16 | 16 |
| 17 CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable, | 17 CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable, |
| 18 FX_DWORD nLines, | 18 uint32_t nLines, |
| 19 bool bHTOOB) | 19 bool bHTOOB) |
| 20 : m_bOK(true), HTOOB(bHTOOB), NTEMP(nLines) { | 20 : m_bOK(true), HTOOB(bHTOOB), NTEMP(nLines) { |
| 21 ParseFromStandardTable(pTable); | 21 ParseFromStandardTable(pTable); |
| 22 } | 22 } |
| 23 | 23 |
| 24 CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream) | 24 CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream) |
| 25 : HTOOB(false), NTEMP(0) { | 25 : HTOOB(false), NTEMP(0) { |
| 26 m_bOK = ParseFromCodedBuffer(pStream); | 26 m_bOK = ParseFromCodedBuffer(pStream); |
| 27 } | 27 } |
| 28 | 28 |
| 29 CJBig2_HuffmanTable::~CJBig2_HuffmanTable() {} | 29 CJBig2_HuffmanTable::~CJBig2_HuffmanTable() {} |
| 30 | 30 |
| 31 void CJBig2_HuffmanTable::ParseFromStandardTable(const JBig2TableLine* pTable) { | 31 void CJBig2_HuffmanTable::ParseFromStandardTable(const JBig2TableLine* pTable) { |
| 32 PREFLEN.resize(NTEMP); | 32 PREFLEN.resize(NTEMP); |
| 33 RANGELEN.resize(NTEMP); | 33 RANGELEN.resize(NTEMP); |
| 34 RANGELOW.resize(NTEMP); | 34 RANGELOW.resize(NTEMP); |
| 35 for (FX_DWORD i = 0; i < NTEMP; ++i) { | 35 for (uint32_t i = 0; i < NTEMP; ++i) { |
| 36 PREFLEN[i] = pTable[i].PREFLEN; | 36 PREFLEN[i] = pTable[i].PREFLEN; |
| 37 RANGELEN[i] = pTable[i].RANDELEN; | 37 RANGELEN[i] = pTable[i].RANDELEN; |
| 38 RANGELOW[i] = pTable[i].RANGELOW; | 38 RANGELOW[i] = pTable[i].RANGELOW; |
| 39 } | 39 } |
| 40 InitCodes(); | 40 InitCodes(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) { | 43 bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) { |
| 44 unsigned char cTemp; | 44 unsigned char cTemp; |
| 45 if (pStream->read1Byte(&cTemp) == -1) | 45 if (pStream->read1Byte(&cTemp) == -1) |
| 46 return false; | 46 return false; |
| 47 | 47 |
| 48 HTOOB = !!(cTemp & 0x01); | 48 HTOOB = !!(cTemp & 0x01); |
| 49 unsigned char HTPS = ((cTemp >> 1) & 0x07) + 1; | 49 unsigned char HTPS = ((cTemp >> 1) & 0x07) + 1; |
| 50 unsigned char HTRS = ((cTemp >> 4) & 0x07) + 1; | 50 unsigned char HTRS = ((cTemp >> 4) & 0x07) + 1; |
| 51 FX_DWORD HTLOW; | 51 uint32_t HTLOW; |
| 52 FX_DWORD HTHIGH; | 52 uint32_t HTHIGH; |
| 53 if (pStream->readInteger(&HTLOW) == -1 || | 53 if (pStream->readInteger(&HTLOW) == -1 || |
| 54 pStream->readInteger(&HTHIGH) == -1) { | 54 pStream->readInteger(&HTHIGH) == -1) { |
| 55 return false; | 55 return false; |
| 56 } | 56 } |
| 57 | 57 |
| 58 const int low = static_cast<int>(HTLOW); | 58 const int low = static_cast<int>(HTLOW); |
| 59 const int high = static_cast<int>(HTHIGH); | 59 const int high = static_cast<int>(HTHIGH); |
| 60 if (low > high) | 60 if (low > high) |
| 61 return false; | 61 return false; |
| 62 | 62 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 92 | 92 |
| 93 ++NTEMP; | 93 ++NTEMP; |
| 94 } | 94 } |
| 95 | 95 |
| 96 InitCodes(); | 96 InitCodes(); |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 | 99 |
| 100 void CJBig2_HuffmanTable::InitCodes() { | 100 void CJBig2_HuffmanTable::InitCodes() { |
| 101 int lenmax = 0; | 101 int lenmax = 0; |
| 102 for (FX_DWORD i = 0; i < NTEMP; ++i) | 102 for (uint32_t i = 0; i < NTEMP; ++i) |
| 103 lenmax = std::max(PREFLEN[i], lenmax); | 103 lenmax = std::max(PREFLEN[i], lenmax); |
| 104 | 104 |
| 105 CODES.resize(NTEMP); | 105 CODES.resize(NTEMP); |
| 106 std::vector<int> LENCOUNT(lenmax + 1); | 106 std::vector<int> LENCOUNT(lenmax + 1); |
| 107 std::vector<int> FIRSTCODE(lenmax + 1); | 107 std::vector<int> FIRSTCODE(lenmax + 1); |
| 108 for (int len : PREFLEN) | 108 for (int len : PREFLEN) |
| 109 ++LENCOUNT[len]; | 109 ++LENCOUNT[len]; |
| 110 | 110 |
| 111 FIRSTCODE[0] = 0; | 111 FIRSTCODE[0] = 0; |
| 112 LENCOUNT[0] = 0; | 112 LENCOUNT[0] = 0; |
| 113 for (int i = 1; i <= lenmax; ++i) { | 113 for (int i = 1; i <= lenmax; ++i) { |
| 114 FIRSTCODE[i] = (FIRSTCODE[i - 1] + LENCOUNT[i - 1]) << 1; | 114 FIRSTCODE[i] = (FIRSTCODE[i - 1] + LENCOUNT[i - 1]) << 1; |
| 115 int CURCODE = FIRSTCODE[i]; | 115 int CURCODE = FIRSTCODE[i]; |
| 116 for (FX_DWORD j = 0; j < NTEMP; ++j) { | 116 for (uint32_t j = 0; j < NTEMP; ++j) { |
| 117 if (PREFLEN[j] == i) | 117 if (PREFLEN[j] == i) |
| 118 CODES[j] = CURCODE++; | 118 CODES[j] = CURCODE++; |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 void CJBig2_HuffmanTable::ExtendBuffers(bool increment) { | 123 void CJBig2_HuffmanTable::ExtendBuffers(bool increment) { |
| 124 if (increment) | 124 if (increment) |
| 125 ++NTEMP; | 125 ++NTEMP; |
| 126 | 126 |
| 127 size_t size = PREFLEN.size(); | 127 size_t size = PREFLEN.size(); |
| 128 if (NTEMP < size) | 128 if (NTEMP < size) |
| 129 return; | 129 return; |
| 130 | 130 |
| 131 size += 16; | 131 size += 16; |
| 132 ASSERT(NTEMP < size); | 132 ASSERT(NTEMP < size); |
| 133 PREFLEN.resize(size); | 133 PREFLEN.resize(size); |
| 134 RANGELEN.resize(size); | 134 RANGELEN.resize(size); |
| 135 RANGELOW.resize(size); | 135 RANGELOW.resize(size); |
| 136 } | 136 } |
| OLD | NEW |