-
Notifications
You must be signed in to change notification settings - Fork 2
/
preflate_block_decoder.cpp
192 lines (179 loc) · 5.93 KB
/
preflate_block_decoder.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Copyright 2018 Dirk Steinke
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include <algorithm>
#include <string.h>
#include "preflate_block_decoder.h"
#include "preflate_block_trees.h"
#include "support/bit_helper.h"
PreflateBlockDecoder::PreflateBlockDecoder(
BitInputStream& input,
OutputCacheStream& output)
: _input(input)
, _output(output)
, _errorCode(OK)
, _dynamicLitLenDecoder(nullptr, 0, false, 0)
, _dynamicDistDecoder(nullptr, 0, false, 0) {
}
bool PreflateBlockDecoder::_error(const ErrorCode code) {
_errorCode = code;
return false;
}
bool PreflateBlockDecoder::readBlock(PreflateTokenBlock &block, bool &last) {
block.uncompressedStartPos = _output.cacheEndPos();
int32_t earliest_reference = INT32_MAX, curPos = 0;
if (_input.eof()) {
return false;
}
last = _readBit() != 0;
unsigned char mode = _readBits(2);
switch (mode) {
default:
return false;
case 0: {
block.type = PreflateTokenBlock::STORED;
block.paddingBitCount = (-_input.bitPos()) & 7;
block.paddingBits = _input.get(block.paddingBitCount);
size_t len = _readBits(16);
size_t ilen = _readBits(16);
if ((len ^ ilen) != 0xffff) {
return _error(STORED_BLOCK_LEN_MISMATCH);
}
block.uncompressedLen = len;
block.contextLen = 0;
return _input.copyBytesTo(_output, len) == len;
}
case 1:
case 2:
if (mode == 1) {
block.type = PreflateTokenBlock::STATIC_HUFF;
_setupStaticTables();
} else {
block.type = PreflateTokenBlock::DYNAMIC_HUFF;
if (!_readDynamicTables(block)) {
return false;
}
}
while (true) {
if (_input.eof()) {
return false;
}
unsigned litLen = _litLenDecoder->decode(_input);
if (litLen < 256) {
_writeLiteral(litLen);
block.tokens.push_back(PreflateToken(PreflateToken::LITERAL));
curPos++;
} else if (litLen == 256) {
block.uncompressedLen = _output.cacheEndPos() - block.uncompressedStartPos;
block.contextLen = -earliest_reference;
return true;
} else {
unsigned lcode = litLen - PreflateConstants::NONLEN_CODE_COUNT;
if (lcode >= PreflateConstants::LEN_CODE_COUNT) {
return false;
}
unsigned len = PreflateConstants::MIN_MATCH
+ PreflateConstants::lengthBaseTable[lcode]
+ _readBits(PreflateConstants::lengthExtraTable[lcode]);
bool irregular258 = len == 258 && lcode != PreflateConstants::LEN_CODE_COUNT - 1;
unsigned dcode = _distDecoder->decode(_input);
if (dcode >= PreflateConstants::DIST_CODE_COUNT) {
return false;
}
unsigned dist = 1
+ PreflateConstants::distBaseTable[dcode]
+ _readBits(PreflateConstants::distExtraTable[dcode]);
if (dist > _output.cacheEndPos()) {
return false;
}
_writeReference(dist, len);
block.tokens.push_back(PreflateToken(PreflateToken::REFERENCE, len, dist, irregular258));
earliest_reference = std::min(earliest_reference, curPos - (int32_t)dist);
curPos += len;
}
}
}
}
void PreflateBlockDecoder::_setupStaticTables() {
_litLenDecoder = PreflateBlockTrees::staticLitLenTreeDecoder();
_distDecoder = PreflateBlockTrees::staticDistTreeDecoder();
}
bool PreflateBlockDecoder::_readDynamicTables(PreflateTokenBlock& block) {
block.nlen = PreflateConstants::NONLEN_CODE_COUNT + _readBits(5);
block.ndist = 1 + _readBits(5);
block.ncode = 4 + _readBits(4);
if (block.nlen > PreflateConstants::LITLEN_CODE_COUNT || block.ndist > PreflateConstants::DIST_CODE_COUNT) {
return false;
}
block.treecodes.clear();
block.treecodes.reserve(block.nlen + block.ndist + block.ncode);
unsigned char tcBitLengths[PreflateConstants::CODETREE_CODE_COUNT];
unsigned char ldBitLengths[PreflateConstants::LITLENDIST_CODE_COUNT];
memset(tcBitLengths, 0, sizeof(tcBitLengths));
memset(ldBitLengths, 0, sizeof(ldBitLengths));
for (unsigned i = 0, n = block.ncode; i < n; ++i) {
unsigned char tc = _readBits(3);
block.treecodes.push_back(tc);
tcBitLengths[PreflateConstants::treeCodeOrderTable[i]] = tc;
}
HuffmanDecoder tcTree(tcBitLengths, PreflateConstants::CODETREE_CODE_COUNT, true, 7);
if (tcTree.error()) {
return false;
}
for (unsigned i = 0, n = block.nlen + block.ndist; i < n; ++i) {
unsigned char code = tcTree.decode(_input);
if (code > 18) {
return false;
}
block.treecodes.push_back(code);
if (code < 16) {
ldBitLengths[i] = code;
continue;
}
unsigned char len = 0, tocopy = 0;
switch (code) {
case 16:
if (i == 0) {
return false;
}
tocopy = ldBitLengths[i - 1];
len = 3 + _readBits(2);
break;
case 17:
tocopy = 0;
len = 3 + _readBits(3);
break;
case 18:
tocopy = 0;
len = 11 + _readBits(7);
break;
}
if (i + len > n) {
return false;
}
block.treecodes.push_back(len);
memset(ldBitLengths + i, tocopy, len);
i += len - 1;
}
if (!ldBitLengths[256]) {
return false;
}
_dynamicLitLenDecoder = HuffmanDecoder(ldBitLengths, block.nlen, true, 15);
if (_dynamicLitLenDecoder.error()) {
return false;
}
_litLenDecoder = &_dynamicLitLenDecoder;
_dynamicDistDecoder = HuffmanDecoder(ldBitLengths + block.nlen, block.ndist, true, 15);
if (_dynamicDistDecoder.error()) {
return false;
}
_distDecoder = &_dynamicDistDecoder;
return true;
}