blob: 1127f52a52c7f4be72a1830f4f0456ffa0d72949 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fxcodec/jbig2/JBig2_HuffmanTable.h"
#include <algorithm>
#include <limits>
#include <vector>
#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"
CJBig2_HuffmanTable::CJBig2_HuffmanTable(const JBig2TableLine* pTable,
uint32_t nLines,
bool bHTOOB)
: m_bOK(true), HTOOB(bHTOOB), NTEMP(nLines) {
ParseFromStandardTable(pTable);
}
CJBig2_HuffmanTable::CJBig2_HuffmanTable(CJBig2_BitStream* pStream)
: HTOOB(false), NTEMP(0) {
m_bOK = ParseFromCodedBuffer(pStream);
}
CJBig2_HuffmanTable::~CJBig2_HuffmanTable() {}
void CJBig2_HuffmanTable::ParseFromStandardTable(const JBig2TableLine* pTable) {
PREFLEN.resize(NTEMP);
RANGELEN.resize(NTEMP);
RANGELOW.resize(NTEMP);
for (uint32_t i = 0; i < NTEMP; ++i) {
PREFLEN[i] = pTable[i].PREFLEN;
RANGELEN[i] = pTable[i].RANDELEN;
RANGELOW[i] = pTable[i].RANGELOW;
}
InitCodes();
}
bool CJBig2_HuffmanTable::ParseFromCodedBuffer(CJBig2_BitStream* pStream) {
unsigned char cTemp;
if (pStream->read1Byte(&cTemp) == -1)
return false;
HTOOB = !!(cTemp & 0x01);
unsigned char HTPS = ((cTemp >> 1) & 0x07) + 1;
unsigned char HTRS = ((cTemp >> 4) & 0x07) + 1;
uint32_t HTLOW;
uint32_t HTHIGH;
if (pStream->readInteger(&HTLOW) == -1 ||
pStream->readInteger(&HTHIGH) == -1 ||
HTLOW > static_cast<uint32_t>(std::numeric_limits<int>::max()) ||
HTHIGH > static_cast<uint32_t>(std::numeric_limits<int>::max())) {
return false;
}
const int low = static_cast<int>(HTLOW);
const int high = static_cast<int>(HTHIGH);
if (low > high)
return false;
ExtendBuffers(false);
pdfium::base::CheckedNumeric<int> cur_low = low;
do {
if ((pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1) ||
(pStream->readNBits(HTRS, &RANGELEN[NTEMP]) == -1) ||
(static_cast<size_t>(RANGELEN[NTEMP]) >= 8 * sizeof(cur_low))) {
return false;
}
RANGELOW[NTEMP] = cur_low.ValueOrDie();
if (RANGELEN[NTEMP] >= 32)
return false;
cur_low += (1 << RANGELEN[NTEMP]);
if (!cur_low.IsValid())
return false;
ExtendBuffers(true);
} while (cur_low.ValueOrDie() < high);
if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
return false;
RANGELEN[NTEMP] = 32;
RANGELOW[NTEMP] = low - 1;
ExtendBuffers(true);
if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
return false;
RANGELEN[NTEMP] = 32;
RANGELOW[NTEMP] = high;
ExtendBuffers(true);
if (HTOOB) {
if (pStream->readNBits(HTPS, &PREFLEN[NTEMP]) == -1)
return false;
++NTEMP;
}
return InitCodes();
}
bool CJBig2_HuffmanTable::InitCodes() {
int lenmax = 0;
for (uint32_t i = 0; i < NTEMP; ++i)
lenmax = std::max(PREFLEN[i], lenmax);
CODES.resize(NTEMP);
std::vector<int> LENCOUNT(lenmax + 1);
std::vector<int> FIRSTCODE(lenmax + 1);
for (int len : PREFLEN)
++LENCOUNT[len];
FIRSTCODE[0] = 0;
LENCOUNT[0] = 0;
for (int i = 1; i <= lenmax; ++i) {
pdfium::base::CheckedNumeric<int> shifted;
shifted = FIRSTCODE[i - 1] + LENCOUNT[i - 1];
shifted <<= 1;
if (!shifted.IsValid())
return false;
FIRSTCODE[i] = shifted.ValueOrDie();
int CURCODE = FIRSTCODE[i];
for (uint32_t j = 0; j < NTEMP; ++j) {
if (PREFLEN[j] == i)
CODES[j] = CURCODE++;
}
}
return true;
}
void CJBig2_HuffmanTable::ExtendBuffers(bool increment) {
if (increment)
++NTEMP;
size_t size = PREFLEN.size();
if (NTEMP < size)
return;
size += 16;
ASSERT(NTEMP < size);
PREFLEN.resize(size);
RANGELEN.resize(size);
RANGELOW.resize(size);
}
|