blob: 4dcaf9cdf8108300ab430d66c9161da8639c1974 [file] [log] [blame]
Zoltan Szabadka30625ba2013-12-16 14:45:57 +01001/* Copyright 2013 Google Inc. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14*/
Zoltan Szabadka04163a82013-10-11 10:26:07 +020015
16#include <stdlib.h>
17#include <stdio.h>
Roderick Sheeter437bbad2013-11-19 14:32:56 -080018#include <string.h>
Zoltan Szabadka04163a82013-10-11 10:26:07 +020019#include "./bit_reader.h"
20#include "./context.h"
21#include "./decode.h"
22#include "./huffman.h"
23#include "./prefix.h"
24#include "./safe_malloc.h"
25
26#if defined(__cplusplus) || defined(c_plusplus)
27extern "C" {
28#endif
29
30#ifdef BROTLI_DECODE_DEBUG
31#define BROTLI_LOG_UINT(name) \
Zoltan Szabadka1571db32013-11-15 19:02:17 +010032 printf("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name))
Zoltan Szabadka04163a82013-10-11 10:26:07 +020033#define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \
Zoltan Szabadkab35077c2013-10-22 15:02:54 +020034 printf("[%s] %s[%lu] = %lu\n", __func__, #array_name, \
Zoltan Szabadka1571db32013-11-15 19:02:17 +010035 (unsigned long)(idx), (unsigned long)array_name[idx])
Zoltan Szabadka04163a82013-10-11 10:26:07 +020036#else
37#define BROTLI_LOG_UINT(name)
38#define BROTLI_LOG_ARRAY_INDEX(array_name, idx)
39#endif
40
41static const int kDefaultCodeLength = 8;
Zoltan Szabadka04163a82013-10-11 10:26:07 +020042static const int kCodeLengthRepeatCode = 16;
Zoltan Szabadka04163a82013-10-11 10:26:07 +020043static const int kNumLiteralCodes = 256;
44static const int kNumInsertAndCopyCodes = 704;
45static const int kNumBlockLengthCodes = 26;
Zoltan Szabadka1571db32013-11-15 19:02:17 +010046static const int kLiteralContextBits = 6;
47static const int kDistanceContextBits = 2;
Zoltan Szabadka04163a82013-10-11 10:26:07 +020048
Zoltan Szabadkae7094912013-12-12 13:18:04 +010049#define CODE_LENGTH_CODES 18
Zoltan Szabadka04163a82013-10-11 10:26:07 +020050static const uint8_t kCodeLengthCodeOrder[CODE_LENGTH_CODES] = {
Zoltan Szabadkae7094912013-12-12 13:18:04 +010051 1, 2, 3, 4, 0, 17, 5, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15,
Zoltan Szabadka04163a82013-10-11 10:26:07 +020052};
53
54#define NUM_DISTANCE_SHORT_CODES 16
55static const int kDistanceShortCodeIndexOffset[NUM_DISTANCE_SHORT_CODES] = {
56 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2
57};
58
59static const int kDistanceShortCodeValueOffset[NUM_DISTANCE_SHORT_CODES] = {
60 0, 0, 0, 0, -1, 1, -2, 2, -3, 3, -1, 1, -2, 2, -3, 3
61};
62
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +010063static BROTLI_INLINE int DecodeWindowBits(BrotliBitReader* br) {
64 if (BrotliReadBits(br, 1)) {
65 return 17 + BrotliReadBits(br, 3);
66 } else {
67 return 16;
Zoltan Szabadka04163a82013-10-11 10:26:07 +020068 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +020069}
70
Zoltan Szabadka30625ba2013-12-16 14:45:57 +010071/* Decodes a number in the range [0..255], by reading 1 - 11 bits. */
Zoltan Szabadkae7094912013-12-12 13:18:04 +010072static BROTLI_INLINE int DecodeVarLenUint8(BrotliBitReader* br) {
73 if (BrotliReadBits(br, 1)) {
74 int nbits = BrotliReadBits(br, 3);
75 if (nbits == 0) {
76 return 1;
77 } else {
78 return BrotliReadBits(br, nbits) + (1 << nbits);
79 }
80 }
81 return 0;
82}
83
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +010084static void DecodeMetaBlockLength(BrotliBitReader* br,
Zoltan Szabadka1571db32013-11-15 19:02:17 +010085 size_t* meta_block_length,
Zoltan Szabadkae7094912013-12-12 13:18:04 +010086 int* input_end,
87 int* is_uncompressed) {
Zoltan Szabadka19320552013-12-13 10:39:46 +010088 int size_nibbles;
89 int i;
Zoltan Szabadka1571db32013-11-15 19:02:17 +010090 *input_end = BrotliReadBits(br, 1);
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +010091 *meta_block_length = 0;
Zoltan Szabadkae7094912013-12-12 13:18:04 +010092 *is_uncompressed = 0;
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +010093 if (*input_end && BrotliReadBits(br, 1)) {
94 return;
Zoltan Szabadka04163a82013-10-11 10:26:07 +020095 }
Zoltan Szabadka19320552013-12-13 10:39:46 +010096 size_nibbles = BrotliReadBits(br, 2) + 4;
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +010097 for (i = 0; i < size_nibbles; ++i) {
98 *meta_block_length |= BrotliReadBits(br, 4) << (i * 4);
99 }
100 ++(*meta_block_length);
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100101 if (!*input_end) {
102 *is_uncompressed = BrotliReadBits(br, 1);
103 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200104}
105
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100106/* Decodes the next Huffman code from bit-stream. */
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200107static BROTLI_INLINE int ReadSymbol(const HuffmanTree* tree,
108 BrotliBitReader* br) {
Zoltan Szabadka19320552013-12-13 10:39:46 +0100109 uint32_t bits;
110 int bitpos;
111 int lut_ix;
112 int lut_bits;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100113 const HuffmanTreeNode* node = tree->root_;
114 BrotliFillBitWindow(br);
Zoltan Szabadka19320552013-12-13 10:39:46 +0100115 bits = BrotliPrefetchBits(br);
116 bitpos = br->bit_pos_;
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100117 /* Check if we find the bit combination from the Huffman lookup table. */
Zoltan Szabadka19320552013-12-13 10:39:46 +0100118 lut_ix = bits & (HUFF_LUT - 1);
119 lut_bits = tree->lut_bits_[lut_ix];
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100120 if (lut_bits <= HUFF_LUT_BITS) {
121 BrotliSetBitPos(br, bitpos + lut_bits);
122 return tree->lut_symbol_[lut_ix];
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200123 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100124 node += tree->lut_jump_[lut_ix];
125 bitpos += HUFF_LUT_BITS;
126 bits >>= HUFF_LUT_BITS;
127
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100128 /* Decode the value from a binary tree. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100129 assert(node != NULL);
130 do {
131 node = HuffmanTreeNextNode(node, bits & 1);
132 bits >>= 1;
133 ++bitpos;
134 } while (HuffmanTreeNodeIsNotLeaf(node));
135 BrotliSetBitPos(br, bitpos);
136 return node->symbol_;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200137}
138
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100139static void PrintUcharVector(const uint8_t* v, int len) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200140 while (len-- > 0) printf(" %d", *v++);
141 printf("\n");
142}
143
144static int ReadHuffmanCodeLengths(
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100145 const uint8_t* code_length_code_lengths,
146 int num_symbols, uint8_t* code_lengths,
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200147 BrotliBitReader* br) {
148 int ok = 0;
149 int symbol;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200150 int prev_code_len = kDefaultCodeLength;
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100151 int repeat = 0;
152 int repeat_length = 0;
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100153 int space = 32768;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200154 HuffmanTree tree;
155
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200156 if (!BrotliHuffmanTreeBuildImplicit(&tree, code_length_code_lengths,
157 CODE_LENGTH_CODES)) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200158 printf("[ReadHuffmanCodeLengths] Building code length tree failed: ");
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100159 PrintUcharVector(code_length_code_lengths, CODE_LENGTH_CODES);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200160 return 0;
161 }
162
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100163 if (!BrotliReadMoreInput(br)) {
164 printf("[ReadHuffmanCodeLengths] Unexpected end of input.\n");
165 return 0;
166 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200167
168 symbol = 0;
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100169 while (symbol + repeat < num_symbols && space > 0) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200170 int code_len;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100171 if (!BrotliReadMoreInput(br)) {
172 printf("[ReadHuffmanCodeLengths] Unexpected end of input.\n");
173 goto End;
174 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200175 code_len = ReadSymbol(&tree, br);
176 BROTLI_LOG_UINT(symbol);
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100177 BROTLI_LOG_UINT(repeat);
178 BROTLI_LOG_UINT(repeat_length);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200179 BROTLI_LOG_UINT(code_len);
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100180 if ((code_len < kCodeLengthRepeatCode) ||
181 (code_len == kCodeLengthRepeatCode && repeat_length == 0) ||
182 (code_len > kCodeLengthRepeatCode && repeat_length > 0)) {
183 while (repeat > 0) {
184 code_lengths[symbol++] = repeat_length;
185 --repeat;
186 }
187 }
188 if (code_len < kCodeLengthRepeatCode) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200189 code_lengths[symbol++] = code_len;
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100190 if (code_len != 0) {
191 prev_code_len = code_len;
192 space -= 32768 >> code_len;
193 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200194 } else {
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100195 const int extra_bits = code_len - 14;
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100196 int i = repeat;
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100197 if (repeat > 0) {
198 repeat -= 2;
199 repeat <<= extra_bits;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200200 }
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100201 repeat += BrotliReadBits(br, extra_bits) + 3;
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100202 if (code_len == kCodeLengthRepeatCode) {
203 repeat_length = prev_code_len;
204 for (; i < repeat; ++i) {
205 space -= 32768 >> repeat_length;
206 }
207 } else {
208 repeat_length = 0;
209 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200210 }
211 }
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100212 if (space != 0) {
213 printf("[ReadHuffmanCodeLengths] space = %d\n", space);
214 goto End;
215 }
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100216 if (symbol + repeat > num_symbols) {
217 printf("[ReadHuffmanCodeLengths] symbol + repeat > num_symbols "
218 "(%d + %d vs %d)\n", symbol, repeat, num_symbols);
219 goto End;
220 }
221 while (repeat-- > 0) code_lengths[symbol++] = repeat_length;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200222 while (symbol < num_symbols) code_lengths[symbol++] = 0;
223 ok = 1;
224
225 End:
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200226 BrotliHuffmanTreeRelease(&tree);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200227 return ok;
228}
229
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200230static int ReadHuffmanCode(int alphabet_size,
231 HuffmanTree* tree,
232 BrotliBitReader* br) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100233 int ok = 1;
234 int simple_code;
235 uint8_t* code_lengths = NULL;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200236
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100237 code_lengths =
238 (uint8_t*)BrotliSafeMalloc((uint64_t)alphabet_size,
239 sizeof(*code_lengths));
240 if (code_lengths == NULL) {
241 return 0;
242 }
243 if (!BrotliReadMoreInput(br)) {
244 printf("[ReadHuffmanCode] Unexpected end of input.\n");
245 return 0;
246 }
247 simple_code = BrotliReadBits(br, 1);
248 BROTLI_LOG_UINT(simple_code);
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100249 if (simple_code) { /* Read symbols, codes & code lengths directly. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100250 int i;
251 int max_bits_counter = alphabet_size - 1;
252 int max_bits = 0;
253 int symbols[4] = { 0 };
254 const int num_symbols = BrotliReadBits(br, 2) + 1;
255 while (max_bits_counter) {
256 max_bits_counter >>= 1;
257 ++max_bits;
258 }
259 memset(code_lengths, 0, alphabet_size);
260 for (i = 0; i < num_symbols; ++i) {
261 symbols[i] = BrotliReadBits(br, max_bits);
262 code_lengths[symbols[i]] = 2;
263 }
264 code_lengths[symbols[0]] = 1;
265 switch (num_symbols) {
266 case 1:
267 case 3:
268 break;
269 case 2:
270 code_lengths[symbols[1]] = 1;
271 break;
272 case 4:
273 if (BrotliReadBits(br, 1)) {
274 code_lengths[symbols[2]] = 3;
275 code_lengths[symbols[3]] = 3;
276 } else {
277 code_lengths[symbols[0]] = 2;
278 }
279 break;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200280 }
281 BROTLI_LOG_UINT(num_symbols);
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100282 } else { /* Decode Huffman-coded code lengths. */
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200283 int i;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100284 uint8_t code_length_code_lengths[CODE_LENGTH_CODES] = { 0 };
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100285 int space = 32;
286 for (i = BrotliReadBits(br, 1) * 2;
287 i < CODE_LENGTH_CODES && space > 0; ++i) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200288 int code_len_idx = kCodeLengthCodeOrder[i];
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100289 int v = BrotliReadBits(br, 2);
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100290 if (v == 1) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100291 v = BrotliReadBits(br, 1);
292 if (v == 0) {
293 v = 2;
294 } else {
295 v = BrotliReadBits(br, 1);
296 if (v == 0) {
297 v = 1;
298 } else {
299 v = 5;
300 }
301 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100302 } else if (v == 2) {
303 v = 4;
304 }
305 code_length_code_lengths[code_len_idx] = v;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200306 BROTLI_LOG_ARRAY_INDEX(code_length_code_lengths, code_len_idx);
Zoltan Szabadkab89f3be2013-12-17 17:17:57 +0100307 if (v != 0) {
308 space -= (32 >> v);
309 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200310 }
311 ok = ReadHuffmanCodeLengths(code_length_code_lengths, alphabet_size,
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100312 code_lengths, br);
313 }
314 if (ok) {
315 ok = BrotliHuffmanTreeBuildImplicit(tree, code_lengths, alphabet_size);
316 if (!ok) {
317 printf("[ReadHuffmanCode] HuffmanTreeBuildImplicit failed: ");
318 PrintUcharVector(code_lengths, alphabet_size);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200319 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200320 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100321 free(code_lengths);
322 return ok;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200323}
324
325static int ReadCopyDistance(const HuffmanTree* tree,
326 int num_direct_codes,
327 int postfix_bits,
328 uint32_t postfix_mask,
329 BrotliBitReader* br) {
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200330 int code;
331 int nbits;
332 int postfix;
333 int offset;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200334 code = ReadSymbol(tree, br);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200335 if (code < num_direct_codes) {
336 return code;
337 }
338 code -= num_direct_codes;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200339 postfix = code & postfix_mask;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200340 code >>= postfix_bits;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200341 nbits = (code >> 1) + 1;
342 offset = ((2 + (code & 1)) << nbits) - 4;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200343 return (num_direct_codes +
344 ((offset + BrotliReadBits(br, nbits)) << postfix_bits) +
345 postfix);
346}
347
348static int ReadBlockLength(const HuffmanTree* tree, BrotliBitReader* br) {
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200349 int code;
350 int nbits;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200351 code = ReadSymbol(tree, br);
352 nbits = kBlockLengthPrefixCode[code].nbits;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200353 return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits);
354}
355
356static void ReadInsertAndCopy(const HuffmanTree* tree,
357 int* insert_len,
358 int* copy_len,
359 int* copy_dist,
360 BrotliBitReader* br) {
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200361 int code;
362 int range_idx;
363 int insert_code;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100364 int insert_extra_bits;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200365 int copy_code;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100366 int copy_extra_bits;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200367 code = ReadSymbol(tree, br);
368 range_idx = code >> 6;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200369 if (range_idx >= 2) {
370 range_idx -= 2;
371 *copy_dist = -1;
372 } else {
373 *copy_dist = 0;
374 }
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800375 insert_code = kInsertRangeLut[range_idx] + ((code >> 3) & 7);
376 copy_code = kCopyRangeLut[range_idx] + (code & 7);
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100377 *insert_len = kInsertLengthPrefixCode[insert_code].offset;
378 insert_extra_bits = kInsertLengthPrefixCode[insert_code].nbits;
379 if (insert_extra_bits > 0) {
380 *insert_len += BrotliReadBits(br, insert_extra_bits);
381 }
382 *copy_len = kCopyLengthPrefixCode[copy_code].offset;
383 copy_extra_bits = kCopyLengthPrefixCode[copy_code].nbits;
384 if (copy_extra_bits > 0) {
385 *copy_len += BrotliReadBits(br, copy_extra_bits);
386 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200387}
388
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100389static int TranslateShortCodes(int code, int* ringbuffer, size_t index) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200390 int val;
391 if (code < NUM_DISTANCE_SHORT_CODES) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100392 index += kDistanceShortCodeIndexOffset[code];
393 index &= 3;
394 val = ringbuffer[index] + kDistanceShortCodeValueOffset[code];
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200395 } else {
396 val = code - NUM_DISTANCE_SHORT_CODES + 1;
397 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200398 return val;
399}
400
401static void MoveToFront(uint8_t* v, uint8_t index) {
402 uint8_t value = v[index];
403 uint8_t i = index;
404 for (; i; --i) v[i] = v[i - 1];
405 v[0] = value;
406}
407
408static void InverseMoveToFrontTransform(uint8_t* v, int v_len) {
409 uint8_t mtf[256];
410 int i;
411 for (i = 0; i < 256; ++i) {
412 mtf[i] = i;
413 }
414 for (i = 0; i < v_len; ++i) {
415 uint8_t index = v[i];
416 v[i] = mtf[index];
417 if (index) MoveToFront(mtf, index);
418 }
419}
420
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100421/* Contains a collection of huffman trees with the same alphabet size. */
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200422typedef struct {
423 int alphabet_size;
424 int num_htrees;
425 HuffmanTree* htrees;
426} HuffmanTreeGroup;
427
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200428static void HuffmanTreeGroupInit(HuffmanTreeGroup* group, int alphabet_size,
429 int ntrees) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200430 group->alphabet_size = alphabet_size;
431 group->num_htrees = ntrees;
432 group->htrees = (HuffmanTree*)malloc(sizeof(HuffmanTree) * ntrees);
433}
434
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200435static void HuffmanTreeGroupRelease(HuffmanTreeGroup* group) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200436 int i;
437 for (i = 0; i < group->num_htrees; ++i) {
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200438 BrotliHuffmanTreeRelease(&group->htrees[i]);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200439 }
440 free(group->htrees);
441}
442
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200443static int HuffmanTreeGroupDecode(HuffmanTreeGroup* group,
444 BrotliBitReader* br) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200445 int i;
446 for (i = 0; i < group->num_htrees; ++i) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100447 if (!ReadHuffmanCode(group->alphabet_size, &group->htrees[i], br)) {
448 return 0;
449 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200450 }
451 return 1;
452}
453
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100454static int DecodeContextMap(int context_map_size,
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200455 int* num_htrees,
456 uint8_t** context_map,
457 BrotliBitReader* br) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100458 int ok = 1;
459 if (!BrotliReadMoreInput(br)) {
460 printf("[DecodeContextMap] Unexpected end of input.\n");
461 return 0;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200462 }
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100463 *num_htrees = DecodeVarLenUint8(br) + 1;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200464
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200465 BROTLI_LOG_UINT(context_map_size);
466 BROTLI_LOG_UINT(*num_htrees);
467
468 *context_map = (uint8_t*)malloc(context_map_size);
469 if (*num_htrees <= 1) {
470 memset(*context_map, 0, context_map_size);
471 return 1;
472 }
473
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200474 {
475 HuffmanTree tree_index_htree;
476 int use_rle_for_zeros = BrotliReadBits(br, 1);
477 int max_run_length_prefix = 0;
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800478 int i;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200479 if (use_rle_for_zeros) {
480 max_run_length_prefix = BrotliReadBits(br, 4) + 1;
481 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100482 if (!ReadHuffmanCode(*num_htrees + max_run_length_prefix,
483 &tree_index_htree, br)) {
484 return 0;
485 }
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800486 for (i = 0; i < context_map_size;) {
487 int code;
488 if (!BrotliReadMoreInput(br)) {
489 printf("[DecodeContextMap] Unexpected end of input.\n");
490 ok = 0;
491 goto End;
492 }
493 code = ReadSymbol(&tree_index_htree, br);
494 if (code == 0) {
495 (*context_map)[i] = 0;
496 ++i;
497 } else if (code <= max_run_length_prefix) {
498 int reps = 1 + (1 << code) + BrotliReadBits(br, code);
499 while (--reps) {
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200500 (*context_map)[i] = 0;
501 ++i;
502 }
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800503 } else {
504 (*context_map)[i] = code - max_run_length_prefix;
505 ++i;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200506 }
507 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100508 End:
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200509 BrotliHuffmanTreeRelease(&tree_index_htree);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200510 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200511 if (BrotliReadBits(br, 1)) {
512 InverseMoveToFrontTransform(*context_map, context_map_size);
513 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100514 return ok;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200515}
516
517static BROTLI_INLINE void DecodeBlockType(const HuffmanTree* trees,
518 int tree_type,
519 int* block_types,
520 int* ringbuffers,
521 size_t* indexes,
522 BrotliBitReader* br) {
523 int* ringbuffer = ringbuffers + tree_type * 2;
524 size_t* index = indexes + tree_type;
525 int type_code = ReadSymbol(trees + tree_type, br);
526 int block_type;
527 if (type_code == 0) {
528 block_type = ringbuffer[*index & 1];
529 } else if (type_code == 1) {
530 block_type = ringbuffer[(*index - 1) & 1] + 1;
531 } else {
532 block_type = type_code - 2;
533 }
534 block_types[tree_type] = block_type;
535 ringbuffer[(*index) & 1] = block_type;
536 ++(*index);
537}
538
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100539/* Copy len bytes from src to dst. It can write up to ten extra bytes
540 after the end of the copy.
541
542 The main part of this loop is a simple copy of eight bytes at a time until
543 we've copied (at least) the requested amount of bytes. However, if dst and
544 src are less than eight bytes apart (indicating a repeating pattern of
545 length < 8), we first need to expand the pattern in order to get the correct
546 results. For instance, if the buffer looks like this, with the eight-byte
547 <src> and <dst> patterns marked as intervals:
548
549 abxxxxxxxxxxxx
550 [------] src
551 [------] dst
552
553 a single eight-byte copy from <src> to <dst> will repeat the pattern once,
554 after which we can move <dst> two bytes without moving <src>:
555
556 ababxxxxxxxxxx
557 [------] src
558 [------] dst
559
560 and repeat the exercise until the two no longer overlap.
561
562 This allows us to do very well in the special case of one single byte
563 repeated many times, without taking a big hit for more general cases.
564
565 The worst case of extra writing past the end of the match occurs when
566 dst - src == 1 and len == 1; the last copy will read from byte positions
567 [0..7] and write to [4..11], whereas it was only supposed to write to
568 position 1. Thus, ten excess bytes.
569*/
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100570static BROTLI_INLINE void IncrementalCopyFastPath(
571 uint8_t* dst, const uint8_t* src, int len) {
572 if (src < dst) {
573 while (dst - src < 8) {
574 UNALIGNED_COPY64(dst, src);
575 len -= dst - src;
576 dst += dst - src;
577 }
578 }
579 while (len > 0) {
580 UNALIGNED_COPY64(dst, src);
581 src += 8;
582 dst += 8;
583 len -= 8;
584 }
585}
586
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200587int BrotliDecompressedSize(size_t encoded_size,
588 const uint8_t* encoded_buffer,
589 size_t* decoded_size) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100590 BrotliMemInput memin;
591 BrotliInput input = BrotliInitMemInput(encoded_buffer, encoded_size, &memin);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200592 BrotliBitReader br;
Zoltan Szabadka19320552013-12-13 10:39:46 +0100593 size_t meta_block_len;
594 int input_end;
595 int is_uncompressed;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100596 if (!BrotliInitBitReader(&br, input)) {
597 return 0;
598 }
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100599 DecodeWindowBits(&br);
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100600 DecodeMetaBlockLength(&br, &meta_block_len, &input_end, &is_uncompressed);
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100601 if (!input_end) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100602 return 0;
603 }
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100604 *decoded_size = meta_block_len;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100605 return 1;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200606}
607
608int BrotliDecompressBuffer(size_t encoded_size,
609 const uint8_t* encoded_buffer,
610 size_t* decoded_size,
611 uint8_t* decoded_buffer) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100612 BrotliMemInput memin;
613 BrotliInput in = BrotliInitMemInput(encoded_buffer, encoded_size, &memin);
614 BrotliMemOutput mout;
615 BrotliOutput out = BrotliInitMemOutput(decoded_buffer, *decoded_size, &mout);
616 int success = BrotliDecompress(in, out);
617 *decoded_size = mout.pos;
618 return success;
619}
620
621int BrotliDecompress(BrotliInput input, BrotliOutput output) {
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200622 int ok = 1;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200623 int i;
624 size_t pos = 0;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100625 int input_end = 0;
626 int window_bits = 0;
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800627 size_t max_backward_distance;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100628 size_t ringbuffer_size;
629 size_t ringbuffer_mask;
630 uint8_t* ringbuffer;
631 uint8_t* ringbuffer_end;
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100632 /* This ring buffer holds a few past copy distances that will be used by */
633 /* some special distance codes. */
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100634 int dist_rb[4] = { 16, 15, 11, 4 };
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200635 size_t dist_rb_idx = 0;
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100636 /* The previous 2 bytes used for context. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100637 uint8_t prev_byte1 = 0;
638 uint8_t prev_byte2 = 0;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200639 HuffmanTreeGroup hgroup[3];
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200640 BrotliBitReader br;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200641
Zoltan Szabadka19320552013-12-13 10:39:46 +0100642 static const int kRingBufferWriteAheadSlack = 16;
643
644 static const int kMaxDictionaryWordLength = 0;
645
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100646 if (!BrotliInitBitReader(&br, input)) {
647 return 0;
648 }
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200649
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100650 /* Decode window size. */
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100651 window_bits = DecodeWindowBits(&br);
Zoltan Szabadka931479d2013-12-13 15:30:20 +0100652 max_backward_distance = (1ULL << window_bits) - 16;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100653
Zoltan Szabadka931479d2013-12-13 15:30:20 +0100654 ringbuffer_size = 1ULL << window_bits;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100655 ringbuffer_mask = ringbuffer_size - 1;
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100656 ringbuffer = (uint8_t*)malloc(ringbuffer_size +
657 kRingBufferWriteAheadSlack +
658 kMaxDictionaryWordLength);
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100659 ringbuffer_end = ringbuffer + ringbuffer_size;
660
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100661 while (!input_end && ok) {
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200662 size_t meta_block_len = 0;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100663 size_t meta_block_end_pos;
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100664 int is_uncompressed;
665 uint32_t block_length[3] = { 1 << 28, 1 << 28, 1 << 28 };
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200666 int block_type[3] = { 0 };
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100667 int num_block_types[3] = { 1, 1, 1 };
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200668 int block_type_rb[6] = { 0, 1, 0, 1, 0, 1 };
669 size_t block_type_rb_index[3] = { 0 };
670 HuffmanTree block_type_trees[3];
671 HuffmanTree block_len_trees[3];
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200672 int distance_postfix_bits;
673 int num_direct_distance_codes;
674 uint32_t distance_postfix_mask;
675 int num_distance_codes;
676 uint8_t* context_map = NULL;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100677 uint8_t* context_modes = NULL;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200678 int num_literal_htrees;
679 uint8_t* dist_context_map = NULL;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200680 int num_dist_htrees;
681 int context_offset = 0;
682 uint8_t* context_map_slice = NULL;
683 uint8_t literal_htree_index = 0;
684 int dist_context_offset = 0;
685 uint8_t* dist_context_map_slice = NULL;
686 uint8_t dist_htree_index = 0;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100687 int context_lookup_offset1 = 0;
688 int context_lookup_offset2 = 0;
689 uint8_t context_mode;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200690
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100691 for (i = 0; i < 3; ++i) {
692 hgroup[i].num_htrees = 0;
693 hgroup[i].htrees = NULL;
694 block_type_trees[i].root_ = NULL;
695 block_len_trees[i].root_ = NULL;
696 }
697
698 if (!BrotliReadMoreInput(&br)) {
699 printf("[BrotliDecompress] Unexpected end of input.\n");
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200700 ok = 0;
701 goto End;
702 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100703 BROTLI_LOG_UINT(pos);
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100704 DecodeMetaBlockLength(&br, &meta_block_len, &input_end, &is_uncompressed);
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200705 BROTLI_LOG_UINT(meta_block_len);
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100706 if (meta_block_len == 0) {
707 goto End;
708 }
709 meta_block_end_pos = pos + meta_block_len;
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100710 if (is_uncompressed) {
711 BrotliSetBitPos(&br, (br.bit_pos_ + 7) & ~7);
712 for (; pos < meta_block_end_pos; ++pos) {
713 ringbuffer[pos & ringbuffer_mask] = BrotliReadBits(&br, 8);
714 if ((pos & ringbuffer_mask) == ringbuffer_mask) {
715 if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
716 ok = 0;
717 goto End;
718 }
719 }
720 }
721 goto End;
722 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200723 for (i = 0; i < 3; ++i) {
724 block_type_trees[i].root_ = NULL;
725 block_len_trees[i].root_ = NULL;
Zoltan Szabadkae7094912013-12-12 13:18:04 +0100726 num_block_types[i] = DecodeVarLenUint8(&br) + 1;
727 if (num_block_types[i] >= 2) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100728 if (!ReadHuffmanCode(
729 num_block_types[i] + 2, &block_type_trees[i], &br) ||
730 !ReadHuffmanCode(kNumBlockLengthCodes, &block_len_trees[i], &br)) {
731 ok = 0;
732 goto End;
733 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200734 block_length[i] = ReadBlockLength(&block_len_trees[i], &br);
735 block_type_rb_index[i] = 1;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200736 }
737 }
738
739 BROTLI_LOG_UINT(num_block_types[0]);
740 BROTLI_LOG_UINT(num_block_types[1]);
741 BROTLI_LOG_UINT(num_block_types[2]);
742 BROTLI_LOG_UINT(block_length[0]);
743 BROTLI_LOG_UINT(block_length[1]);
744 BROTLI_LOG_UINT(block_length[2]);
745
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100746 if (!BrotliReadMoreInput(&br)) {
747 printf("[BrotliDecompress] Unexpected end of input.\n");
748 ok = 0;
749 goto End;
750 }
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200751 distance_postfix_bits = BrotliReadBits(&br, 2);
752 num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200753 (BrotliReadBits(&br, 4) << distance_postfix_bits);
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200754 distance_postfix_mask = (1 << distance_postfix_bits) - 1;
755 num_distance_codes = (num_direct_distance_codes +
756 (48 << distance_postfix_bits));
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100757 context_modes = (uint8_t*)malloc(num_block_types[0]);
758 for (i = 0; i < num_block_types[0]; ++i) {
759 context_modes[i] = BrotliReadBits(&br, 2) << 1;
760 BROTLI_LOG_ARRAY_INDEX(context_modes, i);
761 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200762 BROTLI_LOG_UINT(num_direct_distance_codes);
763 BROTLI_LOG_UINT(distance_postfix_bits);
764
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100765 if (!DecodeContextMap(num_block_types[0] << kLiteralContextBits,
766 &num_literal_htrees, &context_map, &br) ||
767 !DecodeContextMap(num_block_types[2] << kDistanceContextBits,
768 &num_dist_htrees, &dist_context_map, &br)) {
769 ok = 0;
770 goto End;
771 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200772
773 HuffmanTreeGroupInit(&hgroup[0], kNumLiteralCodes, num_literal_htrees);
774 HuffmanTreeGroupInit(&hgroup[1], kNumInsertAndCopyCodes,
775 num_block_types[1]);
776 HuffmanTreeGroupInit(&hgroup[2], num_distance_codes, num_dist_htrees);
777
778 for (i = 0; i < 3; ++i) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100779 if (!HuffmanTreeGroupDecode(&hgroup[i], &br)) {
780 ok = 0;
781 goto End;
782 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200783 }
784
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200785 context_map_slice = context_map;
786 dist_context_map_slice = dist_context_map;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100787 context_mode = context_modes[block_type[0]];
788 context_lookup_offset1 = kContextLookupOffsets[context_mode];
789 context_lookup_offset2 = kContextLookupOffsets[context_mode + 1];
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200790
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100791 while (pos < meta_block_end_pos) {
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200792 int insert_length;
793 int copy_length;
794 int distance_code;
795 int distance;
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800796 size_t max_distance;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100797 uint8_t context;
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200798 int j;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100799 const uint8_t* copy_src;
800 uint8_t* copy_dst;
801 if (!BrotliReadMoreInput(&br)) {
802 printf("[BrotliDecompress] Unexpected end of input.\n");
803 ok = 0;
804 goto End;
805 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200806 if (block_length[1] == 0) {
807 DecodeBlockType(block_type_trees, 1, block_type, block_type_rb,
808 block_type_rb_index, &br);
809 block_length[1] = ReadBlockLength(&block_len_trees[1], &br);
810 }
811 --block_length[1];
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200812 ReadInsertAndCopy(&hgroup[1].htrees[block_type[1]],
813 &insert_length, &copy_length, &distance_code, &br);
814 BROTLI_LOG_UINT(insert_length);
815 BROTLI_LOG_UINT(copy_length);
816 BROTLI_LOG_UINT(distance_code);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200817 for (j = 0; j < insert_length; ++j) {
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100818 if (!BrotliReadMoreInput(&br)) {
819 printf("[BrotliDecompress] Unexpected end of input.\n");
820 ok = 0;
821 goto End;
822 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200823 if (block_length[0] == 0) {
824 DecodeBlockType(block_type_trees, 0, block_type, block_type_rb,
825 block_type_rb_index, &br);
826 block_length[0] = ReadBlockLength(&block_len_trees[0], &br);
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100827 context_offset = block_type[0] << kLiteralContextBits;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200828 context_map_slice = context_map + context_offset;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100829 context_mode = context_modes[block_type[0]];
830 context_lookup_offset1 = kContextLookupOffsets[context_mode];
831 context_lookup_offset2 = kContextLookupOffsets[context_mode + 1];
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200832 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100833 context = (kContextLookup[context_lookup_offset1 + prev_byte1] |
834 kContextLookup[context_lookup_offset2 + prev_byte2]);
835 BROTLI_LOG_UINT(context);
836 literal_htree_index = context_map_slice[context];
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200837 --block_length[0];
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100838 prev_byte2 = prev_byte1;
839 prev_byte1 = ReadSymbol(&hgroup[0].htrees[literal_htree_index], &br);
840 ringbuffer[pos & ringbuffer_mask] = prev_byte1;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200841 BROTLI_LOG_UINT(literal_htree_index);
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100842 BROTLI_LOG_ARRAY_INDEX(ringbuffer, pos & ringbuffer_mask);
843 if ((pos & ringbuffer_mask) == ringbuffer_mask) {
844 if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
845 ok = 0;
846 goto End;
847 }
848 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200849 ++pos;
850 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100851 if (pos == meta_block_end_pos) break;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200852
853 if (distance_code < 0) {
Zoltan Szabadka19320552013-12-13 10:39:46 +0100854 uint8_t context;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100855 if (!BrotliReadMoreInput(&br)) {
856 printf("[BrotliDecompress] Unexpected end of input.\n");
857 ok = 0;
858 goto End;
859 }
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200860 if (block_length[2] == 0) {
861 DecodeBlockType(block_type_trees, 2, block_type, block_type_rb,
862 block_type_rb_index, &br);
863 block_length[2] = ReadBlockLength(&block_len_trees[2], &br);
864 dist_htree_index = block_type[2];
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100865 dist_context_offset = block_type[2] << kDistanceContextBits;
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200866 dist_context_map_slice = dist_context_map + dist_context_offset;
867 }
868 --block_length[2];
Zoltan Szabadka19320552013-12-13 10:39:46 +0100869 context = copy_length > 4 ? 3 : copy_length - 2;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100870 dist_htree_index = dist_context_map_slice[context];
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200871 distance_code = ReadCopyDistance(&hgroup[2].htrees[dist_htree_index],
872 num_direct_distance_codes,
873 distance_postfix_bits,
874 distance_postfix_mask,
875 &br);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200876 }
877
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100878 /* Convert the distance code to the actual distance by possibly looking */
879 /* up past distnaces from the ringbuffer. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100880 distance = TranslateShortCodes(distance_code, dist_rb, dist_rb_idx);
881 if (distance_code > 0) {
882 dist_rb[dist_rb_idx & 3] = distance;
883 ++dist_rb_idx;
884 }
Zoltan Szabadkab35077c2013-10-22 15:02:54 +0200885 BROTLI_LOG_UINT(distance);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200886
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800887 max_distance = max_backward_distance;
888 if (pos < max_distance) {
889 max_distance = pos;
890 }
891
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100892 copy_dst = &ringbuffer[pos & ringbuffer_mask];
893
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800894 if ((size_t)distance > max_distance) {
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100895 printf("Invalid backward reference. pos: %lu distance: %d "
896 "len: %d end: %lu\n", (unsigned long)pos, distance, copy_length,
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100897 (unsigned long)meta_block_end_pos);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200898 ok = 0;
899 goto End;
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800900 } else {
901 if (pos + copy_length > meta_block_end_pos) {
Zoltan Szabadkafe79fac2013-11-28 17:37:13 +0100902 printf("Invalid backward reference. pos: %lu distance: %d "
903 "len: %d end: %lu\n", (unsigned long)pos, distance,
904 copy_length, (unsigned long)meta_block_end_pos);
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800905 ok = 0;
906 goto End;
907 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100908
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800909 copy_src = &ringbuffer[(pos - distance) & ringbuffer_mask];
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100910
911#if (defined(__x86_64__) || defined(_M_X64))
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800912 if (copy_src + copy_length <= ringbuffer_end &&
913 copy_dst + copy_length < ringbuffer_end) {
914 if (copy_length <= 16 && distance >= 8) {
915 UNALIGNED_COPY64(copy_dst, copy_src);
916 UNALIGNED_COPY64(copy_dst + 8, copy_src + 8);
917 } else {
918 IncrementalCopyFastPath(copy_dst, copy_src, copy_length);
919 }
920 pos += copy_length;
921 copy_length = 0;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100922 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100923#endif
924
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800925 for (j = 0; j < copy_length; ++j) {
926 ringbuffer[pos & ringbuffer_mask] =
927 ringbuffer[(pos - distance) & ringbuffer_mask];
928 if ((pos & ringbuffer_mask) == ringbuffer_mask) {
929 if (BrotliWrite(output, ringbuffer, ringbuffer_size) < 0) {
930 ok = 0;
931 goto End;
932 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100933 }
Roderick Sheeter437bbad2013-11-19 14:32:56 -0800934 ++pos;
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100935 }
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100936 }
937
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100938 /* When we get here, we must have inserted at least one literal and */
939 /* made a copy of at least length two, therefore accessing the last 2 */
940 /* bytes is valid. */
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100941 prev_byte1 = ringbuffer[(pos - 1) & ringbuffer_mask];
942 prev_byte2 = ringbuffer[(pos - 2) & ringbuffer_mask];
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200943 }
944 End:
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100945 free(context_modes);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200946 free(context_map);
947 free(dist_context_map);
948 for (i = 0; i < 3; ++i) {
949 HuffmanTreeGroupRelease(&hgroup[i]);
Zoltan Szabadka9c62eb32013-10-17 12:41:36 +0200950 BrotliHuffmanTreeRelease(&block_type_trees[i]);
951 BrotliHuffmanTreeRelease(&block_len_trees[i]);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200952 }
953 }
954
Zoltan Szabadka1571db32013-11-15 19:02:17 +0100955 if (BrotliWrite(output, ringbuffer, pos & ringbuffer_mask) < 0) {
956 ok = 0;
957 }
958 free(ringbuffer);
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200959 return ok;
960}
961
962#if defined(__cplusplus) || defined(c_plusplus)
Zoltan Szabadka30625ba2013-12-16 14:45:57 +0100963} /* extern "C" */
Zoltan Szabadka04163a82013-10-11 10:26:07 +0200964#endif