Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 1 | /* 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 15 | |
| 16 | #include <stdlib.h> |
| 17 | #include <stdio.h> |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 18 | #include <string.h> |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 19 | #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) |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | #ifdef BROTLI_DECODE_DEBUG |
| 31 | #define BROTLI_LOG_UINT(name) \ |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 32 | printf("[%s] %s = %lu\n", __func__, #name, (unsigned long)(name)) |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 33 | #define BROTLI_LOG_ARRAY_INDEX(array_name, idx) \ |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 34 | printf("[%s] %s[%lu] = %lu\n", __func__, #array_name, \ |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 35 | (unsigned long)(idx), (unsigned long)array_name[idx]) |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 36 | #else |
| 37 | #define BROTLI_LOG_UINT(name) |
| 38 | #define BROTLI_LOG_ARRAY_INDEX(array_name, idx) |
| 39 | #endif |
| 40 | |
| 41 | static const int kDefaultCodeLength = 8; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 42 | static const int kCodeLengthRepeatCode = 16; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 43 | static const int kNumLiteralCodes = 256; |
| 44 | static const int kNumInsertAndCopyCodes = 704; |
| 45 | static const int kNumBlockLengthCodes = 26; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 46 | static const int kLiteralContextBits = 6; |
| 47 | static const int kDistanceContextBits = 2; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 48 | |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 49 | #define CODE_LENGTH_CODES 18 |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 50 | static const uint8_t kCodeLengthCodeOrder[CODE_LENGTH_CODES] = { |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 51 | 1, 2, 3, 4, 0, 17, 5, 6, 16, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #define NUM_DISTANCE_SHORT_CODES 16 |
| 55 | static 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 | |
| 59 | static 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 Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 63 | static BROTLI_INLINE int DecodeWindowBits(BrotliBitReader* br) { |
| 64 | if (BrotliReadBits(br, 1)) { |
| 65 | return 17 + BrotliReadBits(br, 3); |
| 66 | } else { |
| 67 | return 16; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 68 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 71 | /* Decodes a number in the range [0..255], by reading 1 - 11 bits. */ |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 72 | static 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 Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 84 | static void DecodeMetaBlockLength(BrotliBitReader* br, |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 85 | size_t* meta_block_length, |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 86 | int* input_end, |
| 87 | int* is_uncompressed) { |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 88 | int size_nibbles; |
| 89 | int i; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 90 | *input_end = BrotliReadBits(br, 1); |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 91 | *meta_block_length = 0; |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 92 | *is_uncompressed = 0; |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 93 | if (*input_end && BrotliReadBits(br, 1)) { |
| 94 | return; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 95 | } |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 96 | size_nibbles = BrotliReadBits(br, 2) + 4; |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 97 | for (i = 0; i < size_nibbles; ++i) { |
| 98 | *meta_block_length |= BrotliReadBits(br, 4) << (i * 4); |
| 99 | } |
| 100 | ++(*meta_block_length); |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 101 | if (!*input_end) { |
| 102 | *is_uncompressed = BrotliReadBits(br, 1); |
| 103 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 104 | } |
| 105 | |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 106 | /* Decodes the next Huffman code from bit-stream. */ |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 107 | static BROTLI_INLINE int ReadSymbol(const HuffmanTree* tree, |
| 108 | BrotliBitReader* br) { |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 109 | uint32_t bits; |
| 110 | int bitpos; |
| 111 | int lut_ix; |
| 112 | int lut_bits; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 113 | const HuffmanTreeNode* node = tree->root_; |
| 114 | BrotliFillBitWindow(br); |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 115 | bits = BrotliPrefetchBits(br); |
| 116 | bitpos = br->bit_pos_; |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 117 | /* Check if we find the bit combination from the Huffman lookup table. */ |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 118 | lut_ix = bits & (HUFF_LUT - 1); |
| 119 | lut_bits = tree->lut_bits_[lut_ix]; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 120 | if (lut_bits <= HUFF_LUT_BITS) { |
| 121 | BrotliSetBitPos(br, bitpos + lut_bits); |
| 122 | return tree->lut_symbol_[lut_ix]; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 123 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 124 | node += tree->lut_jump_[lut_ix]; |
| 125 | bitpos += HUFF_LUT_BITS; |
| 126 | bits >>= HUFF_LUT_BITS; |
| 127 | |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 128 | /* Decode the value from a binary tree. */ |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 129 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 139 | static void PrintUcharVector(const uint8_t* v, int len) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 140 | while (len-- > 0) printf(" %d", *v++); |
| 141 | printf("\n"); |
| 142 | } |
| 143 | |
| 144 | static int ReadHuffmanCodeLengths( |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 145 | const uint8_t* code_length_code_lengths, |
| 146 | int num_symbols, uint8_t* code_lengths, |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 147 | BrotliBitReader* br) { |
| 148 | int ok = 0; |
| 149 | int symbol; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 150 | int prev_code_len = kDefaultCodeLength; |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 151 | int repeat = 0; |
| 152 | int repeat_length = 0; |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 153 | int space = 32768; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 154 | HuffmanTree tree; |
| 155 | |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 156 | if (!BrotliHuffmanTreeBuildImplicit(&tree, code_length_code_lengths, |
| 157 | CODE_LENGTH_CODES)) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 158 | printf("[ReadHuffmanCodeLengths] Building code length tree failed: "); |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 159 | PrintUcharVector(code_length_code_lengths, CODE_LENGTH_CODES); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 163 | if (!BrotliReadMoreInput(br)) { |
| 164 | printf("[ReadHuffmanCodeLengths] Unexpected end of input.\n"); |
| 165 | return 0; |
| 166 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 167 | |
| 168 | symbol = 0; |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 169 | while (symbol + repeat < num_symbols && space > 0) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 170 | int code_len; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 171 | if (!BrotliReadMoreInput(br)) { |
| 172 | printf("[ReadHuffmanCodeLengths] Unexpected end of input.\n"); |
| 173 | goto End; |
| 174 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 175 | code_len = ReadSymbol(&tree, br); |
| 176 | BROTLI_LOG_UINT(symbol); |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 177 | BROTLI_LOG_UINT(repeat); |
| 178 | BROTLI_LOG_UINT(repeat_length); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 179 | BROTLI_LOG_UINT(code_len); |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 180 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 189 | code_lengths[symbol++] = code_len; |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 190 | if (code_len != 0) { |
| 191 | prev_code_len = code_len; |
| 192 | space -= 32768 >> code_len; |
| 193 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 194 | } else { |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 195 | const int extra_bits = code_len - 14; |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 196 | int i = repeat; |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 197 | if (repeat > 0) { |
| 198 | repeat -= 2; |
| 199 | repeat <<= extra_bits; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 200 | } |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 201 | repeat += BrotliReadBits(br, extra_bits) + 3; |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 202 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 210 | } |
| 211 | } |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 212 | if (space != 0) { |
| 213 | printf("[ReadHuffmanCodeLengths] space = %d\n", space); |
| 214 | goto End; |
| 215 | } |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 216 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 222 | while (symbol < num_symbols) code_lengths[symbol++] = 0; |
| 223 | ok = 1; |
| 224 | |
| 225 | End: |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 226 | BrotliHuffmanTreeRelease(&tree); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 227 | return ok; |
| 228 | } |
| 229 | |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 230 | static int ReadHuffmanCode(int alphabet_size, |
| 231 | HuffmanTree* tree, |
| 232 | BrotliBitReader* br) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 233 | int ok = 1; |
| 234 | int simple_code; |
| 235 | uint8_t* code_lengths = NULL; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 236 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 237 | 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 Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 249 | if (simple_code) { /* Read symbols, codes & code lengths directly. */ |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 250 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 280 | } |
| 281 | BROTLI_LOG_UINT(num_symbols); |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 282 | } else { /* Decode Huffman-coded code lengths. */ |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 283 | int i; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 284 | uint8_t code_length_code_lengths[CODE_LENGTH_CODES] = { 0 }; |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 285 | int space = 32; |
| 286 | for (i = BrotliReadBits(br, 1) * 2; |
| 287 | i < CODE_LENGTH_CODES && space > 0; ++i) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 288 | int code_len_idx = kCodeLengthCodeOrder[i]; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 289 | int v = BrotliReadBits(br, 2); |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 290 | if (v == 1) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 291 | 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 302 | } else if (v == 2) { |
| 303 | v = 4; |
| 304 | } |
| 305 | code_length_code_lengths[code_len_idx] = v; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 306 | BROTLI_LOG_ARRAY_INDEX(code_length_code_lengths, code_len_idx); |
Zoltan Szabadka | b89f3be | 2013-12-17 17:17:57 +0100 | [diff] [blame^] | 307 | if (v != 0) { |
| 308 | space -= (32 >> v); |
| 309 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 310 | } |
| 311 | ok = ReadHuffmanCodeLengths(code_length_code_lengths, alphabet_size, |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 312 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 319 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 320 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 321 | free(code_lengths); |
| 322 | return ok; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static int ReadCopyDistance(const HuffmanTree* tree, |
| 326 | int num_direct_codes, |
| 327 | int postfix_bits, |
| 328 | uint32_t postfix_mask, |
| 329 | BrotliBitReader* br) { |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 330 | int code; |
| 331 | int nbits; |
| 332 | int postfix; |
| 333 | int offset; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 334 | code = ReadSymbol(tree, br); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 335 | if (code < num_direct_codes) { |
| 336 | return code; |
| 337 | } |
| 338 | code -= num_direct_codes; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 339 | postfix = code & postfix_mask; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 340 | code >>= postfix_bits; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 341 | nbits = (code >> 1) + 1; |
| 342 | offset = ((2 + (code & 1)) << nbits) - 4; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 343 | return (num_direct_codes + |
| 344 | ((offset + BrotliReadBits(br, nbits)) << postfix_bits) + |
| 345 | postfix); |
| 346 | } |
| 347 | |
| 348 | static int ReadBlockLength(const HuffmanTree* tree, BrotliBitReader* br) { |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 349 | int code; |
| 350 | int nbits; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 351 | code = ReadSymbol(tree, br); |
| 352 | nbits = kBlockLengthPrefixCode[code].nbits; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 353 | return kBlockLengthPrefixCode[code].offset + BrotliReadBits(br, nbits); |
| 354 | } |
| 355 | |
| 356 | static void ReadInsertAndCopy(const HuffmanTree* tree, |
| 357 | int* insert_len, |
| 358 | int* copy_len, |
| 359 | int* copy_dist, |
| 360 | BrotliBitReader* br) { |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 361 | int code; |
| 362 | int range_idx; |
| 363 | int insert_code; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 364 | int insert_extra_bits; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 365 | int copy_code; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 366 | int copy_extra_bits; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 367 | code = ReadSymbol(tree, br); |
| 368 | range_idx = code >> 6; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 369 | if (range_idx >= 2) { |
| 370 | range_idx -= 2; |
| 371 | *copy_dist = -1; |
| 372 | } else { |
| 373 | *copy_dist = 0; |
| 374 | } |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 375 | insert_code = kInsertRangeLut[range_idx] + ((code >> 3) & 7); |
| 376 | copy_code = kCopyRangeLut[range_idx] + (code & 7); |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 377 | *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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 389 | static int TranslateShortCodes(int code, int* ringbuffer, size_t index) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 390 | int val; |
| 391 | if (code < NUM_DISTANCE_SHORT_CODES) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 392 | index += kDistanceShortCodeIndexOffset[code]; |
| 393 | index &= 3; |
| 394 | val = ringbuffer[index] + kDistanceShortCodeValueOffset[code]; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 395 | } else { |
| 396 | val = code - NUM_DISTANCE_SHORT_CODES + 1; |
| 397 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 398 | return val; |
| 399 | } |
| 400 | |
| 401 | static 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 | |
| 408 | static 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 Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 421 | /* Contains a collection of huffman trees with the same alphabet size. */ |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 422 | typedef struct { |
| 423 | int alphabet_size; |
| 424 | int num_htrees; |
| 425 | HuffmanTree* htrees; |
| 426 | } HuffmanTreeGroup; |
| 427 | |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 428 | static void HuffmanTreeGroupInit(HuffmanTreeGroup* group, int alphabet_size, |
| 429 | int ntrees) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 430 | group->alphabet_size = alphabet_size; |
| 431 | group->num_htrees = ntrees; |
| 432 | group->htrees = (HuffmanTree*)malloc(sizeof(HuffmanTree) * ntrees); |
| 433 | } |
| 434 | |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 435 | static void HuffmanTreeGroupRelease(HuffmanTreeGroup* group) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 436 | int i; |
| 437 | for (i = 0; i < group->num_htrees; ++i) { |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 438 | BrotliHuffmanTreeRelease(&group->htrees[i]); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 439 | } |
| 440 | free(group->htrees); |
| 441 | } |
| 442 | |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 443 | static int HuffmanTreeGroupDecode(HuffmanTreeGroup* group, |
| 444 | BrotliBitReader* br) { |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 445 | int i; |
| 446 | for (i = 0; i < group->num_htrees; ++i) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 447 | if (!ReadHuffmanCode(group->alphabet_size, &group->htrees[i], br)) { |
| 448 | return 0; |
| 449 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 450 | } |
| 451 | return 1; |
| 452 | } |
| 453 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 454 | static int DecodeContextMap(int context_map_size, |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 455 | int* num_htrees, |
| 456 | uint8_t** context_map, |
| 457 | BrotliBitReader* br) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 458 | int ok = 1; |
| 459 | if (!BrotliReadMoreInput(br)) { |
| 460 | printf("[DecodeContextMap] Unexpected end of input.\n"); |
| 461 | return 0; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 462 | } |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 463 | *num_htrees = DecodeVarLenUint8(br) + 1; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 464 | |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 465 | 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 Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 474 | { |
| 475 | HuffmanTree tree_index_htree; |
| 476 | int use_rle_for_zeros = BrotliReadBits(br, 1); |
| 477 | int max_run_length_prefix = 0; |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 478 | int i; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 479 | if (use_rle_for_zeros) { |
| 480 | max_run_length_prefix = BrotliReadBits(br, 4) + 1; |
| 481 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 482 | if (!ReadHuffmanCode(*num_htrees + max_run_length_prefix, |
| 483 | &tree_index_htree, br)) { |
| 484 | return 0; |
| 485 | } |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 486 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 500 | (*context_map)[i] = 0; |
| 501 | ++i; |
| 502 | } |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 503 | } else { |
| 504 | (*context_map)[i] = code - max_run_length_prefix; |
| 505 | ++i; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 506 | } |
| 507 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 508 | End: |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 509 | BrotliHuffmanTreeRelease(&tree_index_htree); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 510 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 511 | if (BrotliReadBits(br, 1)) { |
| 512 | InverseMoveToFrontTransform(*context_map, context_map_size); |
| 513 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 514 | return ok; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static 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 Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 539 | /* 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 570 | static 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 587 | int BrotliDecompressedSize(size_t encoded_size, |
| 588 | const uint8_t* encoded_buffer, |
| 589 | size_t* decoded_size) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 590 | BrotliMemInput memin; |
| 591 | BrotliInput input = BrotliInitMemInput(encoded_buffer, encoded_size, &memin); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 592 | BrotliBitReader br; |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 593 | size_t meta_block_len; |
| 594 | int input_end; |
| 595 | int is_uncompressed; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 596 | if (!BrotliInitBitReader(&br, input)) { |
| 597 | return 0; |
| 598 | } |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 599 | DecodeWindowBits(&br); |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 600 | DecodeMetaBlockLength(&br, &meta_block_len, &input_end, &is_uncompressed); |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 601 | if (!input_end) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 602 | return 0; |
| 603 | } |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 604 | *decoded_size = meta_block_len; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 605 | return 1; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | int BrotliDecompressBuffer(size_t encoded_size, |
| 609 | const uint8_t* encoded_buffer, |
| 610 | size_t* decoded_size, |
| 611 | uint8_t* decoded_buffer) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 612 | 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 | |
| 621 | int BrotliDecompress(BrotliInput input, BrotliOutput output) { |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 622 | int ok = 1; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 623 | int i; |
| 624 | size_t pos = 0; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 625 | int input_end = 0; |
| 626 | int window_bits = 0; |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 627 | size_t max_backward_distance; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 628 | size_t ringbuffer_size; |
| 629 | size_t ringbuffer_mask; |
| 630 | uint8_t* ringbuffer; |
| 631 | uint8_t* ringbuffer_end; |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 632 | /* This ring buffer holds a few past copy distances that will be used by */ |
| 633 | /* some special distance codes. */ |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 634 | int dist_rb[4] = { 16, 15, 11, 4 }; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 635 | size_t dist_rb_idx = 0; |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 636 | /* The previous 2 bytes used for context. */ |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 637 | uint8_t prev_byte1 = 0; |
| 638 | uint8_t prev_byte2 = 0; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 639 | HuffmanTreeGroup hgroup[3]; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 640 | BrotliBitReader br; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 641 | |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 642 | static const int kRingBufferWriteAheadSlack = 16; |
| 643 | |
| 644 | static const int kMaxDictionaryWordLength = 0; |
| 645 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 646 | if (!BrotliInitBitReader(&br, input)) { |
| 647 | return 0; |
| 648 | } |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 649 | |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 650 | /* Decode window size. */ |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 651 | window_bits = DecodeWindowBits(&br); |
Zoltan Szabadka | 931479d | 2013-12-13 15:30:20 +0100 | [diff] [blame] | 652 | max_backward_distance = (1ULL << window_bits) - 16; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 653 | |
Zoltan Szabadka | 931479d | 2013-12-13 15:30:20 +0100 | [diff] [blame] | 654 | ringbuffer_size = 1ULL << window_bits; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 655 | ringbuffer_mask = ringbuffer_size - 1; |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 656 | ringbuffer = (uint8_t*)malloc(ringbuffer_size + |
| 657 | kRingBufferWriteAheadSlack + |
| 658 | kMaxDictionaryWordLength); |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 659 | ringbuffer_end = ringbuffer + ringbuffer_size; |
| 660 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 661 | while (!input_end && ok) { |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 662 | size_t meta_block_len = 0; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 663 | size_t meta_block_end_pos; |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 664 | int is_uncompressed; |
| 665 | uint32_t block_length[3] = { 1 << 28, 1 << 28, 1 << 28 }; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 666 | int block_type[3] = { 0 }; |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 667 | int num_block_types[3] = { 1, 1, 1 }; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 668 | 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 Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 672 | 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 677 | uint8_t* context_modes = NULL; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 678 | int num_literal_htrees; |
| 679 | uint8_t* dist_context_map = NULL; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 680 | 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 687 | int context_lookup_offset1 = 0; |
| 688 | int context_lookup_offset2 = 0; |
| 689 | uint8_t context_mode; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 690 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 691 | 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 Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 700 | ok = 0; |
| 701 | goto End; |
| 702 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 703 | BROTLI_LOG_UINT(pos); |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 704 | DecodeMetaBlockLength(&br, &meta_block_len, &input_end, &is_uncompressed); |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 705 | BROTLI_LOG_UINT(meta_block_len); |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 706 | if (meta_block_len == 0) { |
| 707 | goto End; |
| 708 | } |
| 709 | meta_block_end_pos = pos + meta_block_len; |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 710 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 723 | for (i = 0; i < 3; ++i) { |
| 724 | block_type_trees[i].root_ = NULL; |
| 725 | block_len_trees[i].root_ = NULL; |
Zoltan Szabadka | e709491 | 2013-12-12 13:18:04 +0100 | [diff] [blame] | 726 | num_block_types[i] = DecodeVarLenUint8(&br) + 1; |
| 727 | if (num_block_types[i] >= 2) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 728 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 734 | block_length[i] = ReadBlockLength(&block_len_trees[i], &br); |
| 735 | block_type_rb_index[i] = 1; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 736 | } |
| 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 746 | if (!BrotliReadMoreInput(&br)) { |
| 747 | printf("[BrotliDecompress] Unexpected end of input.\n"); |
| 748 | ok = 0; |
| 749 | goto End; |
| 750 | } |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 751 | distance_postfix_bits = BrotliReadBits(&br, 2); |
| 752 | num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES + |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 753 | (BrotliReadBits(&br, 4) << distance_postfix_bits); |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 754 | distance_postfix_mask = (1 << distance_postfix_bits) - 1; |
| 755 | num_distance_codes = (num_direct_distance_codes + |
| 756 | (48 << distance_postfix_bits)); |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 757 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 762 | BROTLI_LOG_UINT(num_direct_distance_codes); |
| 763 | BROTLI_LOG_UINT(distance_postfix_bits); |
| 764 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 765 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 772 | |
| 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 779 | if (!HuffmanTreeGroupDecode(&hgroup[i], &br)) { |
| 780 | ok = 0; |
| 781 | goto End; |
| 782 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 785 | context_map_slice = context_map; |
| 786 | dist_context_map_slice = dist_context_map; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 787 | context_mode = context_modes[block_type[0]]; |
| 788 | context_lookup_offset1 = kContextLookupOffsets[context_mode]; |
| 789 | context_lookup_offset2 = kContextLookupOffsets[context_mode + 1]; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 790 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 791 | while (pos < meta_block_end_pos) { |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 792 | int insert_length; |
| 793 | int copy_length; |
| 794 | int distance_code; |
| 795 | int distance; |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 796 | size_t max_distance; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 797 | uint8_t context; |
Zoltan Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 798 | int j; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 799 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 806 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 812 | ReadInsertAndCopy(&hgroup[1].htrees[block_type[1]], |
| 813 | &insert_length, ©_length, &distance_code, &br); |
| 814 | BROTLI_LOG_UINT(insert_length); |
| 815 | BROTLI_LOG_UINT(copy_length); |
| 816 | BROTLI_LOG_UINT(distance_code); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 817 | for (j = 0; j < insert_length; ++j) { |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 818 | if (!BrotliReadMoreInput(&br)) { |
| 819 | printf("[BrotliDecompress] Unexpected end of input.\n"); |
| 820 | ok = 0; |
| 821 | goto End; |
| 822 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 823 | 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 827 | context_offset = block_type[0] << kLiteralContextBits; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 828 | context_map_slice = context_map + context_offset; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 829 | context_mode = context_modes[block_type[0]]; |
| 830 | context_lookup_offset1 = kContextLookupOffsets[context_mode]; |
| 831 | context_lookup_offset2 = kContextLookupOffsets[context_mode + 1]; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 832 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 833 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 837 | --block_length[0]; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 838 | prev_byte2 = prev_byte1; |
| 839 | prev_byte1 = ReadSymbol(&hgroup[0].htrees[literal_htree_index], &br); |
| 840 | ringbuffer[pos & ringbuffer_mask] = prev_byte1; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 841 | BROTLI_LOG_UINT(literal_htree_index); |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 842 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 849 | ++pos; |
| 850 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 851 | if (pos == meta_block_end_pos) break; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 852 | |
| 853 | if (distance_code < 0) { |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 854 | uint8_t context; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 855 | if (!BrotliReadMoreInput(&br)) { |
| 856 | printf("[BrotliDecompress] Unexpected end of input.\n"); |
| 857 | ok = 0; |
| 858 | goto End; |
| 859 | } |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 860 | 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 865 | dist_context_offset = block_type[2] << kDistanceContextBits; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 866 | dist_context_map_slice = dist_context_map + dist_context_offset; |
| 867 | } |
| 868 | --block_length[2]; |
Zoltan Szabadka | 1932055 | 2013-12-13 10:39:46 +0100 | [diff] [blame] | 869 | context = copy_length > 4 ? 3 : copy_length - 2; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 870 | dist_htree_index = dist_context_map_slice[context]; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 871 | 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 Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 876 | } |
| 877 | |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 878 | /* Convert the distance code to the actual distance by possibly looking */ |
| 879 | /* up past distnaces from the ringbuffer. */ |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 880 | 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 Szabadka | b35077c | 2013-10-22 15:02:54 +0200 | [diff] [blame] | 885 | BROTLI_LOG_UINT(distance); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 886 | |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 887 | max_distance = max_backward_distance; |
| 888 | if (pos < max_distance) { |
| 889 | max_distance = pos; |
| 890 | } |
| 891 | |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 892 | copy_dst = &ringbuffer[pos & ringbuffer_mask]; |
| 893 | |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 894 | if ((size_t)distance > max_distance) { |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 895 | printf("Invalid backward reference. pos: %lu distance: %d " |
| 896 | "len: %d end: %lu\n", (unsigned long)pos, distance, copy_length, |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 897 | (unsigned long)meta_block_end_pos); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 898 | ok = 0; |
| 899 | goto End; |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 900 | } else { |
| 901 | if (pos + copy_length > meta_block_end_pos) { |
Zoltan Szabadka | fe79fac | 2013-11-28 17:37:13 +0100 | [diff] [blame] | 902 | 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 Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 905 | ok = 0; |
| 906 | goto End; |
| 907 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 908 | |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 909 | copy_src = &ringbuffer[(pos - distance) & ringbuffer_mask]; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 910 | |
| 911 | #if (defined(__x86_64__) || defined(_M_X64)) |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 912 | 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 922 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 923 | #endif |
| 924 | |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 925 | 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 933 | } |
Roderick Sheeter | 437bbad | 2013-11-19 14:32:56 -0800 | [diff] [blame] | 934 | ++pos; |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 935 | } |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 936 | } |
| 937 | |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 938 | /* 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 Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 941 | prev_byte1 = ringbuffer[(pos - 1) & ringbuffer_mask]; |
| 942 | prev_byte2 = ringbuffer[(pos - 2) & ringbuffer_mask]; |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 943 | } |
| 944 | End: |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 945 | free(context_modes); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 946 | free(context_map); |
| 947 | free(dist_context_map); |
| 948 | for (i = 0; i < 3; ++i) { |
| 949 | HuffmanTreeGroupRelease(&hgroup[i]); |
Zoltan Szabadka | 9c62eb3 | 2013-10-17 12:41:36 +0200 | [diff] [blame] | 950 | BrotliHuffmanTreeRelease(&block_type_trees[i]); |
| 951 | BrotliHuffmanTreeRelease(&block_len_trees[i]); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
Zoltan Szabadka | 1571db3 | 2013-11-15 19:02:17 +0100 | [diff] [blame] | 955 | if (BrotliWrite(output, ringbuffer, pos & ringbuffer_mask) < 0) { |
| 956 | ok = 0; |
| 957 | } |
| 958 | free(ringbuffer); |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 959 | return ok; |
| 960 | } |
| 961 | |
| 962 | #if defined(__cplusplus) || defined(c_plusplus) |
Zoltan Szabadka | 30625ba | 2013-12-16 14:45:57 +0100 | [diff] [blame] | 963 | } /* extern "C" */ |
Zoltan Szabadka | 04163a8 | 2013-10-11 10:26:07 +0200 | [diff] [blame] | 964 | #endif |