Deflate.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Array.h>
  8. #include <AK/Assertions.h>
  9. #include <AK/BinarySearch.h>
  10. #include <AK/MemoryStream.h>
  11. #include <string.h>
  12. #include <LibCompress/Deflate.h>
  13. #include <LibCompress/Huffman.h>
  14. namespace Compress {
  15. static constexpr u8 deflate_special_code_length_copy = 16;
  16. static constexpr u8 deflate_special_code_length_zeros = 17;
  17. static constexpr u8 deflate_special_code_length_long_zeros = 18;
  18. static constexpr int EndOfBlock = 256;
  19. CanonicalCode const& CanonicalCode::fixed_literal_codes()
  20. {
  21. static CanonicalCode code;
  22. static bool initialized = false;
  23. if (initialized)
  24. return code;
  25. code = MUST(CanonicalCode::from_bytes(fixed_literal_bit_lengths));
  26. initialized = true;
  27. return code;
  28. }
  29. CanonicalCode const& CanonicalCode::fixed_distance_codes()
  30. {
  31. static CanonicalCode code;
  32. static bool initialized = false;
  33. if (initialized)
  34. return code;
  35. code = MUST(CanonicalCode::from_bytes(fixed_distance_bit_lengths));
  36. initialized = true;
  37. return code;
  38. }
  39. ErrorOr<CanonicalCode> CanonicalCode::from_bytes(ReadonlyBytes bytes)
  40. {
  41. CanonicalCode code;
  42. auto non_zero_symbols = 0;
  43. auto last_non_zero = -1;
  44. for (size_t i = 0; i < bytes.size(); i++) {
  45. if (bytes[i] != 0) {
  46. non_zero_symbols++;
  47. last_non_zero = i;
  48. }
  49. }
  50. if (non_zero_symbols == 1) { // special case - only 1 symbol
  51. code.m_prefix_table[0] = PrefixTableEntry { static_cast<u16>(last_non_zero), 1u };
  52. code.m_prefix_table[1] = code.m_prefix_table[0];
  53. code.m_max_prefixed_code_length = 1;
  54. if (code.m_bit_codes.size() < static_cast<size_t>(last_non_zero + 1)) {
  55. TRY(code.m_bit_codes.try_resize(last_non_zero + 1));
  56. TRY(code.m_bit_code_lengths.try_resize(last_non_zero + 1));
  57. }
  58. code.m_bit_codes[last_non_zero] = 0;
  59. code.m_bit_code_lengths[last_non_zero] = 1;
  60. return code;
  61. }
  62. struct PrefixCode {
  63. u16 symbol_code { 0 };
  64. u16 symbol_value { 0 };
  65. u16 code_length { 0 };
  66. };
  67. Array<PrefixCode, 1 << CanonicalCode::max_allowed_prefixed_code_length> prefix_codes;
  68. size_t number_of_prefix_codes = 0;
  69. auto next_code = 0;
  70. for (size_t code_length = 1; code_length <= 15; ++code_length) {
  71. next_code <<= 1;
  72. auto start_bit = 1 << code_length;
  73. for (size_t symbol = 0; symbol < bytes.size(); ++symbol) {
  74. if (bytes[symbol] != code_length)
  75. continue;
  76. if (next_code > start_bit)
  77. return Error::from_string_literal("Failed to decode code lengths");
  78. if (code_length <= CanonicalCode::max_allowed_prefixed_code_length) {
  79. if (number_of_prefix_codes >= prefix_codes.size())
  80. return Error::from_string_literal("Invalid canonical Huffman code");
  81. auto& prefix_code = prefix_codes[number_of_prefix_codes++];
  82. prefix_code.symbol_code = next_code;
  83. prefix_code.symbol_value = symbol;
  84. prefix_code.code_length = code_length;
  85. code.m_max_prefixed_code_length = code_length;
  86. } else {
  87. code.m_symbol_codes.append(start_bit | next_code);
  88. code.m_symbol_values.append(symbol);
  89. }
  90. if (code.m_bit_codes.size() < symbol + 1) {
  91. TRY(code.m_bit_codes.try_resize(symbol + 1));
  92. TRY(code.m_bit_code_lengths.try_resize(symbol + 1));
  93. }
  94. code.m_bit_codes[symbol] = fast_reverse16(start_bit | next_code, code_length); // DEFLATE writes huffman encoded symbols as lsb-first
  95. code.m_bit_code_lengths[symbol] = code_length;
  96. next_code++;
  97. }
  98. }
  99. if (next_code != (1 << 15))
  100. return Error::from_string_literal("Failed to decode code lengths");
  101. for (auto [symbol_code, symbol_value, code_length] : prefix_codes) {
  102. if (code_length == 0 || code_length > CanonicalCode::max_allowed_prefixed_code_length)
  103. break;
  104. auto shift = code.m_max_prefixed_code_length - code_length;
  105. symbol_code <<= shift;
  106. for (size_t j = 0; j < (1u << shift); ++j) {
  107. auto index = fast_reverse16(symbol_code + j, code.m_max_prefixed_code_length);
  108. code.m_prefix_table[index] = PrefixTableEntry { symbol_value, code_length };
  109. }
  110. }
  111. return code;
  112. }
  113. ErrorOr<u32> CanonicalCode::read_symbol(LittleEndianInputBitStream& stream) const
  114. {
  115. auto prefix = TRY(stream.peek_bits<size_t>(m_max_prefixed_code_length));
  116. if (auto [symbol_value, code_length] = m_prefix_table[prefix]; code_length != 0) {
  117. stream.discard_previously_peeked_bits(code_length);
  118. return symbol_value;
  119. }
  120. auto code_bits = TRY(stream.read_bits<u16>(m_max_prefixed_code_length));
  121. code_bits = fast_reverse16(code_bits, m_max_prefixed_code_length);
  122. code_bits |= 1 << m_max_prefixed_code_length;
  123. for (size_t i = m_max_prefixed_code_length; i < 16; ++i) {
  124. size_t index;
  125. if (binary_search(m_symbol_codes.span(), code_bits, &index))
  126. return m_symbol_values[index];
  127. code_bits = code_bits << 1 | TRY(stream.read_bit());
  128. }
  129. return Error::from_string_literal("Symbol exceeds maximum symbol number");
  130. }
  131. DeflateDecompressor::CompressedBlock::CompressedBlock(DeflateDecompressor& decompressor, CanonicalCode literal_codes, Optional<CanonicalCode> distance_codes)
  132. : m_decompressor(decompressor)
  133. , m_literal_codes(literal_codes)
  134. , m_distance_codes(distance_codes)
  135. {
  136. }
  137. ErrorOr<bool> DeflateDecompressor::CompressedBlock::try_read_more()
  138. {
  139. if (m_eof == true)
  140. return false;
  141. auto const symbol = TRY(m_literal_codes.read_symbol(*m_decompressor.m_input_stream));
  142. if (symbol >= 286)
  143. return Error::from_string_literal("Invalid deflate literal/length symbol");
  144. if (symbol < EndOfBlock) {
  145. u8 byte_symbol = symbol;
  146. m_decompressor.m_output_buffer.write({ &byte_symbol, sizeof(byte_symbol) });
  147. return true;
  148. }
  149. if (symbol == EndOfBlock) {
  150. m_eof = true;
  151. return false;
  152. }
  153. if (!m_distance_codes.has_value())
  154. return Error::from_string_literal("Distance codes have not been initialized");
  155. auto const length = TRY(m_decompressor.decode_length(symbol));
  156. auto const distance_symbol = TRY(m_distance_codes.value().read_symbol(*m_decompressor.m_input_stream));
  157. if (distance_symbol >= 30)
  158. return Error::from_string_literal("Invalid deflate distance symbol");
  159. auto const distance = TRY(m_decompressor.decode_distance(distance_symbol));
  160. auto copied_length = TRY(m_decompressor.m_output_buffer.copy_from_seekback(distance, length));
  161. // TODO: What should we do if the output buffer is full?
  162. VERIFY(copied_length == length);
  163. return true;
  164. }
  165. DeflateDecompressor::UncompressedBlock::UncompressedBlock(DeflateDecompressor& decompressor, size_t length)
  166. : m_decompressor(decompressor)
  167. , m_bytes_remaining(length)
  168. {
  169. }
  170. ErrorOr<bool> DeflateDecompressor::UncompressedBlock::try_read_more()
  171. {
  172. if (m_bytes_remaining == 0)
  173. return false;
  174. if (m_decompressor.m_input_stream->is_eof())
  175. return Error::from_string_literal("Input data ends in the middle of an uncompressed DEFLATE block");
  176. Array<u8, 4096> temporary_buffer;
  177. auto readable_bytes = temporary_buffer.span().trim(min(m_bytes_remaining, m_decompressor.m_output_buffer.empty_space()));
  178. auto read_bytes = TRY(m_decompressor.m_input_stream->read_some(readable_bytes));
  179. auto written_bytes = m_decompressor.m_output_buffer.write(read_bytes);
  180. VERIFY(read_bytes.size() == written_bytes);
  181. m_bytes_remaining -= written_bytes;
  182. return true;
  183. }
  184. ErrorOr<NonnullOwnPtr<DeflateDecompressor>> DeflateDecompressor::construct(MaybeOwned<LittleEndianInputBitStream> stream)
  185. {
  186. auto output_buffer = TRY(CircularBuffer::create_empty(32 * KiB));
  187. return TRY(adopt_nonnull_own_or_enomem(new (nothrow) DeflateDecompressor(move(stream), move(output_buffer))));
  188. }
  189. DeflateDecompressor::DeflateDecompressor(MaybeOwned<LittleEndianInputBitStream> stream, CircularBuffer output_buffer)
  190. : m_input_stream(move(stream))
  191. , m_output_buffer(move(output_buffer))
  192. {
  193. }
  194. DeflateDecompressor::~DeflateDecompressor()
  195. {
  196. if (m_state == State::ReadingCompressedBlock)
  197. m_compressed_block.~CompressedBlock();
  198. if (m_state == State::ReadingUncompressedBlock)
  199. m_uncompressed_block.~UncompressedBlock();
  200. }
  201. ErrorOr<Bytes> DeflateDecompressor::read_some(Bytes bytes)
  202. {
  203. size_t total_read = 0;
  204. while (total_read < bytes.size()) {
  205. auto slice = bytes.slice(total_read);
  206. if (m_state == State::Idle) {
  207. if (m_read_final_block)
  208. break;
  209. m_read_final_block = TRY(m_input_stream->read_bit());
  210. auto const block_type = TRY(m_input_stream->read_bits(2));
  211. if (block_type == 0b00) {
  212. m_input_stream->align_to_byte_boundary();
  213. u16 length = TRY(m_input_stream->read_value<LittleEndian<u16>>());
  214. u16 negated_length = TRY(m_input_stream->read_value<LittleEndian<u16>>());
  215. if ((length ^ 0xffff) != negated_length)
  216. return Error::from_string_literal("Calculated negated length does not equal stored negated length");
  217. m_state = State::ReadingUncompressedBlock;
  218. new (&m_uncompressed_block) UncompressedBlock(*this, length);
  219. continue;
  220. }
  221. if (block_type == 0b01) {
  222. m_state = State::ReadingCompressedBlock;
  223. new (&m_compressed_block) CompressedBlock(*this, CanonicalCode::fixed_literal_codes(), CanonicalCode::fixed_distance_codes());
  224. continue;
  225. }
  226. if (block_type == 0b10) {
  227. CanonicalCode literal_codes;
  228. Optional<CanonicalCode> distance_codes;
  229. TRY(decode_codes(literal_codes, distance_codes));
  230. m_state = State::ReadingCompressedBlock;
  231. new (&m_compressed_block) CompressedBlock(*this, literal_codes, distance_codes);
  232. continue;
  233. }
  234. return Error::from_string_literal("Unhandled block type for Idle state");
  235. }
  236. if (m_state == State::ReadingCompressedBlock) {
  237. auto nread = m_output_buffer.read(slice).size();
  238. while (nread < slice.size() && TRY(m_compressed_block.try_read_more())) {
  239. nread += m_output_buffer.read(slice.slice(nread)).size();
  240. }
  241. total_read += nread;
  242. if (nread == slice.size())
  243. break;
  244. m_compressed_block.~CompressedBlock();
  245. m_state = State::Idle;
  246. continue;
  247. }
  248. if (m_state == State::ReadingUncompressedBlock) {
  249. auto nread = m_output_buffer.read(slice).size();
  250. while (nread < slice.size() && TRY(m_uncompressed_block.try_read_more())) {
  251. nread += m_output_buffer.read(slice.slice(nread)).size();
  252. }
  253. total_read += nread;
  254. if (nread == slice.size())
  255. break;
  256. m_uncompressed_block.~UncompressedBlock();
  257. m_state = State::Idle;
  258. continue;
  259. }
  260. VERIFY_NOT_REACHED();
  261. }
  262. return bytes.slice(0, total_read);
  263. }
  264. bool DeflateDecompressor::is_eof() const { return m_state == State::Idle && m_read_final_block; }
  265. ErrorOr<size_t> DeflateDecompressor::write_some(ReadonlyBytes)
  266. {
  267. return Error::from_errno(EBADF);
  268. }
  269. bool DeflateDecompressor::is_open() const
  270. {
  271. return true;
  272. }
  273. void DeflateDecompressor::close()
  274. {
  275. }
  276. ErrorOr<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)
  277. {
  278. FixedMemoryStream memory_stream { bytes };
  279. LittleEndianInputBitStream bit_stream { MaybeOwned<Stream>(memory_stream) };
  280. auto deflate_stream = TRY(DeflateDecompressor::construct(MaybeOwned<LittleEndianInputBitStream>(bit_stream)));
  281. return deflate_stream->read_until_eof(4096);
  282. }
  283. ErrorOr<u32> DeflateDecompressor::decode_length(u32 symbol)
  284. {
  285. if (symbol <= 264)
  286. return symbol - 254;
  287. if (symbol <= 284) {
  288. auto extra_bits = (symbol - 261) / 4;
  289. return (((symbol - 265) % 4 + 4) << extra_bits) + 3 + TRY(m_input_stream->read_bits(extra_bits));
  290. }
  291. if (symbol == 285)
  292. return DeflateDecompressor::max_back_reference_length;
  293. VERIFY_NOT_REACHED();
  294. }
  295. ErrorOr<u32> DeflateDecompressor::decode_distance(u32 symbol)
  296. {
  297. if (symbol <= 3)
  298. return symbol + 1;
  299. if (symbol <= 29) {
  300. auto extra_bits = (symbol / 2) - 1;
  301. return ((symbol % 2 + 2) << extra_bits) + 1 + TRY(m_input_stream->read_bits(extra_bits));
  302. }
  303. VERIFY_NOT_REACHED();
  304. }
  305. ErrorOr<void> DeflateDecompressor::decode_codes(CanonicalCode& literal_code, Optional<CanonicalCode>& distance_code)
  306. {
  307. auto literal_code_count = TRY(m_input_stream->read_bits(5)) + 257;
  308. auto distance_code_count = TRY(m_input_stream->read_bits(5)) + 1;
  309. auto code_length_count = TRY(m_input_stream->read_bits(4)) + 4;
  310. // First we have to extract the code lengths of the code that was used to encode the code lengths of
  311. // the code that was used to encode the block.
  312. u8 code_lengths_code_lengths[19] = { 0 };
  313. for (size_t i = 0; i < code_length_count; ++i) {
  314. code_lengths_code_lengths[code_lengths_code_lengths_order[i]] = TRY(m_input_stream->read_bits(3));
  315. }
  316. // Now we can extract the code that was used to encode the code lengths of the code that was used to
  317. // encode the block.
  318. auto const code_length_code = TRY(CanonicalCode::from_bytes({ code_lengths_code_lengths, sizeof(code_lengths_code_lengths) }));
  319. // Next we extract the code lengths of the code that was used to encode the block.
  320. Vector<u8, 286> code_lengths;
  321. while (code_lengths.size() < literal_code_count + distance_code_count) {
  322. auto symbol = TRY(code_length_code.read_symbol(*m_input_stream));
  323. if (symbol < deflate_special_code_length_copy) {
  324. code_lengths.append(static_cast<u8>(symbol));
  325. } else if (symbol == deflate_special_code_length_copy) {
  326. if (code_lengths.is_empty())
  327. return Error::from_string_literal("Found no codes to copy before a copy block");
  328. auto nrepeat = 3 + TRY(m_input_stream->read_bits(2));
  329. for (size_t j = 0; j < nrepeat; ++j)
  330. code_lengths.append(code_lengths.last());
  331. } else if (symbol == deflate_special_code_length_zeros) {
  332. auto nrepeat = 3 + TRY(m_input_stream->read_bits(3));
  333. for (size_t j = 0; j < nrepeat; ++j)
  334. code_lengths.append(0);
  335. } else {
  336. VERIFY(symbol == deflate_special_code_length_long_zeros);
  337. auto nrepeat = 11 + TRY(m_input_stream->read_bits(7));
  338. for (size_t j = 0; j < nrepeat; ++j)
  339. code_lengths.append(0);
  340. }
  341. }
  342. if (code_lengths.size() != literal_code_count + distance_code_count)
  343. return Error::from_string_literal("Number of code lengths does not match the sum of codes");
  344. // Now we extract the code that was used to encode literals and lengths in the block.
  345. literal_code = TRY(CanonicalCode::from_bytes(code_lengths.span().trim(literal_code_count)));
  346. // Now we extract the code that was used to encode distances in the block.
  347. if (distance_code_count == 1) {
  348. auto length = code_lengths[literal_code_count];
  349. if (length == 0)
  350. return {};
  351. else if (length != 1)
  352. return Error::from_string_literal("Length for a single distance code is longer than 1");
  353. }
  354. distance_code = TRY(CanonicalCode::from_bytes(code_lengths.span().slice(literal_code_count)));
  355. return {};
  356. }
  357. ErrorOr<NonnullOwnPtr<DeflateCompressor>> DeflateCompressor::construct(MaybeOwned<Stream> stream, CompressionLevel compression_level)
  358. {
  359. auto bit_stream = TRY(try_make<LittleEndianOutputBitStream>(move(stream)));
  360. auto deflate_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DeflateCompressor(move(bit_stream), compression_level)));
  361. return deflate_compressor;
  362. }
  363. DeflateCompressor::DeflateCompressor(NonnullOwnPtr<LittleEndianOutputBitStream> stream, CompressionLevel compression_level)
  364. : m_compression_level(compression_level)
  365. , m_compression_constants(compression_constants[static_cast<int>(m_compression_level)])
  366. , m_output_stream(move(stream))
  367. {
  368. m_symbol_frequencies.fill(0);
  369. m_distance_frequencies.fill(0);
  370. }
  371. DeflateCompressor::~DeflateCompressor()
  372. {
  373. VERIFY(m_finished);
  374. }
  375. ErrorOr<Bytes> DeflateCompressor::read_some(Bytes)
  376. {
  377. return Error::from_errno(EBADF);
  378. }
  379. ErrorOr<size_t> DeflateCompressor::write_some(ReadonlyBytes bytes)
  380. {
  381. VERIFY(!m_finished);
  382. size_t total_written = 0;
  383. while (!bytes.is_empty()) {
  384. auto n_written = bytes.copy_trimmed_to(pending_block().slice(m_pending_block_size));
  385. m_pending_block_size += n_written;
  386. if (m_pending_block_size == block_size)
  387. TRY(flush());
  388. bytes = bytes.slice(n_written);
  389. total_written += n_written;
  390. }
  391. return total_written;
  392. }
  393. bool DeflateCompressor::is_eof() const
  394. {
  395. return true;
  396. }
  397. bool DeflateCompressor::is_open() const
  398. {
  399. return m_output_stream->is_open();
  400. }
  401. void DeflateCompressor::close()
  402. {
  403. }
  404. // Knuth's multiplicative hash on 4 bytes
  405. u16 DeflateCompressor::hash_sequence(u8 const* bytes)
  406. {
  407. constexpr u32 const knuth_constant = 2654435761; // shares no common factors with 2^32
  408. return ((bytes[0] | bytes[1] << 8 | bytes[2] << 16 | bytes[3] << 24) * knuth_constant) >> (32 - hash_bits);
  409. }
  410. size_t DeflateCompressor::compare_match_candidate(size_t start, size_t candidate, size_t previous_match_length, size_t maximum_match_length)
  411. {
  412. VERIFY(previous_match_length < maximum_match_length);
  413. // We firstly check that the match is at least (prev_match_length + 1) long, we check backwards as there's a higher chance the end mismatches
  414. for (ssize_t i = previous_match_length; i >= 0; i--) {
  415. if (m_rolling_window[start + i] != m_rolling_window[candidate + i])
  416. return 0;
  417. }
  418. // Find the actual length
  419. auto match_length = previous_match_length + 1;
  420. while (match_length < maximum_match_length && m_rolling_window[start + match_length] == m_rolling_window[candidate + match_length]) {
  421. match_length++;
  422. }
  423. VERIFY(match_length > previous_match_length);
  424. VERIFY(match_length <= maximum_match_length);
  425. return match_length;
  426. }
  427. size_t DeflateCompressor::find_back_match(size_t start, u16 hash, size_t previous_match_length, size_t maximum_match_length, size_t& match_position)
  428. {
  429. auto max_chain_length = m_compression_constants.max_chain;
  430. if (previous_match_length == 0)
  431. previous_match_length = min_match_length - 1; // we only care about matches that are at least min_match_length long
  432. if (previous_match_length >= maximum_match_length)
  433. return 0; // we can't improve a maximum length match
  434. if (previous_match_length >= m_compression_constants.max_lazy_length)
  435. return 0; // the previous match is already pretty, we shouldn't waste another full search
  436. if (previous_match_length >= m_compression_constants.good_match_length)
  437. max_chain_length /= 4; // we already have a pretty good much, so do a shorter search
  438. auto candidate = m_hash_head[hash];
  439. auto match_found = false;
  440. while (max_chain_length--) {
  441. if (candidate == empty_slot)
  442. break; // no remaining candidates
  443. VERIFY(candidate < start);
  444. if (start - candidate > window_size)
  445. break; // outside the window
  446. auto match_length = compare_match_candidate(start, candidate, previous_match_length, maximum_match_length);
  447. if (match_length != 0) {
  448. match_found = true;
  449. match_position = candidate;
  450. previous_match_length = match_length;
  451. if (match_length == maximum_match_length)
  452. return match_length; // bail if we got the maximum possible length
  453. }
  454. candidate = m_hash_prev[candidate % window_size];
  455. }
  456. if (!match_found)
  457. return 0; // we didn't find any matches
  458. return previous_match_length; // we found matches, but they were at most previous_match_length long
  459. }
  460. ALWAYS_INLINE u8 DeflateCompressor::distance_to_base(u16 distance)
  461. {
  462. return (distance <= 256) ? distance_to_base_lo[distance - 1] : distance_to_base_hi[(distance - 1) >> 7];
  463. }
  464. void DeflateCompressor::lz77_compress_block()
  465. {
  466. for (auto& slot : m_hash_head) { // initialize chained hash table
  467. slot = empty_slot;
  468. }
  469. auto insert_hash = [&](auto pos, auto hash) {
  470. auto window_pos = pos % window_size;
  471. m_hash_prev[window_pos] = m_hash_head[hash];
  472. m_hash_head[hash] = window_pos;
  473. };
  474. auto emit_literal = [&](auto literal) {
  475. VERIFY(m_pending_symbol_size <= block_size + 1);
  476. auto index = m_pending_symbol_size++;
  477. m_symbol_buffer[index].distance = 0;
  478. m_symbol_buffer[index].literal = literal;
  479. m_symbol_frequencies[literal]++;
  480. };
  481. auto emit_back_reference = [&](auto distance, auto length) {
  482. VERIFY(m_pending_symbol_size <= block_size + 1);
  483. auto index = m_pending_symbol_size++;
  484. m_symbol_buffer[index].distance = distance;
  485. m_symbol_buffer[index].length = length;
  486. m_symbol_frequencies[length_to_symbol[length]]++;
  487. m_distance_frequencies[distance_to_base(distance)]++;
  488. };
  489. size_t previous_match_length = 0;
  490. size_t previous_match_position = 0;
  491. VERIFY(m_compression_constants.great_match_length <= max_match_length);
  492. // our block starts at block_size and is m_pending_block_size in length
  493. auto block_end = block_size + m_pending_block_size;
  494. size_t current_position;
  495. for (current_position = block_size; current_position < block_end - min_match_length + 1; current_position++) {
  496. auto hash = hash_sequence(&m_rolling_window[current_position]);
  497. size_t match_position;
  498. auto match_length = find_back_match(current_position, hash, previous_match_length,
  499. min(m_compression_constants.great_match_length, block_end - current_position), match_position);
  500. insert_hash(current_position, hash);
  501. // if the previous match is as good as the new match, just use it
  502. if (previous_match_length != 0 && previous_match_length >= match_length) {
  503. emit_back_reference((current_position - 1) - previous_match_position, previous_match_length);
  504. // skip all the bytes that are included in this match
  505. for (size_t j = current_position + 1; j < min(current_position - 1 + previous_match_length, block_end - min_match_length + 1); j++) {
  506. insert_hash(j, hash_sequence(&m_rolling_window[j]));
  507. }
  508. current_position = (current_position - 1) + previous_match_length - 1;
  509. previous_match_length = 0;
  510. continue;
  511. }
  512. if (match_length == 0) {
  513. VERIFY(previous_match_length == 0);
  514. emit_literal(m_rolling_window[current_position]);
  515. continue;
  516. }
  517. // if this is a lazy match, and the new match is better than the old one, output previous as literal
  518. if (previous_match_length != 0) {
  519. emit_literal(m_rolling_window[current_position - 1]);
  520. }
  521. previous_match_length = match_length;
  522. previous_match_position = match_position;
  523. }
  524. // clean up leftover lazy match
  525. if (previous_match_length != 0) {
  526. emit_back_reference((current_position - 1) - previous_match_position, previous_match_length);
  527. current_position = (current_position - 1) + previous_match_length;
  528. }
  529. // output remaining literals
  530. while (current_position < block_end) {
  531. emit_literal(m_rolling_window[current_position++]);
  532. }
  533. }
  534. size_t DeflateCompressor::huffman_block_length(Array<u8, max_huffman_literals> const& literal_bit_lengths, Array<u8, max_huffman_distances> const& distance_bit_lengths)
  535. {
  536. size_t length = 0;
  537. for (size_t i = 0; i < 286; i++) {
  538. auto frequency = m_symbol_frequencies[i];
  539. length += literal_bit_lengths[i] * frequency;
  540. if (i >= 257) // back reference length symbols
  541. length += packed_length_symbols[i - 257].extra_bits * frequency;
  542. }
  543. for (size_t i = 0; i < 30; i++) {
  544. auto frequency = m_distance_frequencies[i];
  545. length += distance_bit_lengths[i] * frequency;
  546. length += packed_distances[i].extra_bits * frequency;
  547. }
  548. return length;
  549. }
  550. size_t DeflateCompressor::uncompressed_block_length()
  551. {
  552. auto padding = 8 - ((m_output_stream->bit_offset() + 3) % 8);
  553. // 3 bit block header + align to byte + 2 * 16 bit length fields + block contents
  554. return 3 + padding + (2 * 16) + m_pending_block_size * 8;
  555. }
  556. size_t DeflateCompressor::fixed_block_length()
  557. {
  558. // block header + fixed huffman encoded block contents
  559. return 3 + huffman_block_length(fixed_literal_bit_lengths, fixed_distance_bit_lengths);
  560. }
  561. size_t DeflateCompressor::dynamic_block_length(Array<u8, max_huffman_literals> const& literal_bit_lengths, Array<u8, max_huffman_distances> const& distance_bit_lengths, Array<u8, 19> const& code_lengths_bit_lengths, Array<u16, 19> const& code_lengths_frequencies, size_t code_lengths_count)
  562. {
  563. // block header + literal code count + distance code count + code length count
  564. auto length = 3 + 5 + 5 + 4;
  565. // 3 bits per code_length
  566. length += 3 * code_lengths_count;
  567. for (size_t i = 0; i < code_lengths_frequencies.size(); i++) {
  568. auto frequency = code_lengths_frequencies[i];
  569. length += code_lengths_bit_lengths[i] * frequency;
  570. if (i == deflate_special_code_length_copy) {
  571. length += 2 * frequency;
  572. } else if (i == deflate_special_code_length_zeros) {
  573. length += 3 * frequency;
  574. } else if (i == deflate_special_code_length_long_zeros) {
  575. length += 7 * frequency;
  576. }
  577. }
  578. return length + huffman_block_length(literal_bit_lengths, distance_bit_lengths);
  579. }
  580. ErrorOr<void> DeflateCompressor::write_huffman(CanonicalCode const& literal_code, Optional<CanonicalCode> const& distance_code)
  581. {
  582. auto has_distances = distance_code.has_value();
  583. for (size_t i = 0; i < m_pending_symbol_size; i++) {
  584. if (m_symbol_buffer[i].distance == 0) {
  585. TRY(literal_code.write_symbol(*m_output_stream, m_symbol_buffer[i].literal));
  586. continue;
  587. }
  588. VERIFY(has_distances);
  589. auto symbol = length_to_symbol[m_symbol_buffer[i].length];
  590. TRY(literal_code.write_symbol(*m_output_stream, symbol));
  591. // Emit extra bits if needed
  592. TRY(m_output_stream->write_bits<u16>(m_symbol_buffer[i].length - packed_length_symbols[symbol - 257].base_length, packed_length_symbols[symbol - 257].extra_bits));
  593. auto base_distance = distance_to_base(m_symbol_buffer[i].distance);
  594. TRY(distance_code.value().write_symbol(*m_output_stream, base_distance));
  595. // Emit extra bits if needed
  596. TRY(m_output_stream->write_bits<u16>(m_symbol_buffer[i].distance - packed_distances[base_distance].base_distance, packed_distances[base_distance].extra_bits));
  597. }
  598. return {};
  599. }
  600. size_t DeflateCompressor::encode_huffman_lengths(ReadonlyBytes lengths, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths)
  601. {
  602. size_t encoded_count = 0;
  603. size_t i = 0;
  604. while (i < lengths.size()) {
  605. if (lengths[i] == 0) {
  606. auto zero_count = 0;
  607. for (size_t j = i; j < min(lengths.size(), i + 138) && lengths[j] == 0; j++)
  608. zero_count++;
  609. if (zero_count < 3) { // below minimum repeated zero count
  610. encoded_lengths[encoded_count++].symbol = 0;
  611. i++;
  612. continue;
  613. }
  614. if (zero_count <= 10) {
  615. encoded_lengths[encoded_count].symbol = deflate_special_code_length_zeros;
  616. encoded_lengths[encoded_count++].count = zero_count;
  617. } else {
  618. encoded_lengths[encoded_count].symbol = deflate_special_code_length_long_zeros;
  619. encoded_lengths[encoded_count++].count = zero_count;
  620. }
  621. i += zero_count;
  622. continue;
  623. }
  624. encoded_lengths[encoded_count++].symbol = lengths[i++];
  625. auto copy_count = 0;
  626. for (size_t j = i; j < min(lengths.size(), i + 6) && lengths[j] == lengths[i - 1]; j++)
  627. copy_count++;
  628. if (copy_count >= 3) {
  629. encoded_lengths[encoded_count].symbol = deflate_special_code_length_copy;
  630. encoded_lengths[encoded_count++].count = copy_count;
  631. i += copy_count;
  632. continue;
  633. }
  634. }
  635. return encoded_count;
  636. }
  637. size_t DeflateCompressor::encode_block_lengths(Array<u8, max_huffman_literals> const& literal_bit_lengths, Array<u8, max_huffman_distances> const& distance_bit_lengths, Array<code_length_symbol, max_huffman_literals + max_huffman_distances>& encoded_lengths, size_t& literal_code_count, size_t& distance_code_count)
  638. {
  639. literal_code_count = max_huffman_literals;
  640. distance_code_count = max_huffman_distances;
  641. VERIFY(literal_bit_lengths[EndOfBlock] != 0); // Make sure at least the EndOfBlock marker is present
  642. while (literal_bit_lengths[literal_code_count - 1] == 0)
  643. literal_code_count--;
  644. // Drop trailing zero lengths, keeping at least one
  645. while (distance_bit_lengths[distance_code_count - 1] == 0 && distance_code_count > 1)
  646. distance_code_count--;
  647. Array<u8, max_huffman_literals + max_huffman_distances> all_lengths {};
  648. for (size_t i = 0; i < literal_code_count; i++)
  649. all_lengths[i] = literal_bit_lengths[i];
  650. for (size_t i = 0; i < distance_code_count; i++)
  651. all_lengths[literal_code_count + i] = distance_bit_lengths[i];
  652. return encode_huffman_lengths(all_lengths.span().trim(literal_code_count + distance_code_count), encoded_lengths);
  653. }
  654. ErrorOr<void> DeflateCompressor::write_dynamic_huffman(CanonicalCode const& literal_code, size_t literal_code_count, Optional<CanonicalCode> const& distance_code, size_t distance_code_count, Array<u8, 19> const& code_lengths_bit_lengths, size_t code_length_count, Array<code_length_symbol, max_huffman_literals + max_huffman_distances> const& encoded_lengths, size_t encoded_lengths_count)
  655. {
  656. TRY(m_output_stream->write_bits(literal_code_count - 257, 5));
  657. TRY(m_output_stream->write_bits(distance_code_count - 1, 5));
  658. TRY(m_output_stream->write_bits(code_length_count - 4, 4));
  659. for (size_t i = 0; i < code_length_count; i++) {
  660. TRY(m_output_stream->write_bits(code_lengths_bit_lengths[code_lengths_code_lengths_order[i]], 3));
  661. }
  662. auto code_lengths_code = MUST(CanonicalCode::from_bytes(code_lengths_bit_lengths));
  663. for (size_t i = 0; i < encoded_lengths_count; i++) {
  664. auto encoded_length = encoded_lengths[i];
  665. TRY(code_lengths_code.write_symbol(*m_output_stream, encoded_length.symbol));
  666. if (encoded_length.symbol == deflate_special_code_length_copy) {
  667. TRY(m_output_stream->write_bits<u8>(encoded_length.count - 3, 2));
  668. } else if (encoded_length.symbol == deflate_special_code_length_zeros) {
  669. TRY(m_output_stream->write_bits<u8>(encoded_length.count - 3, 3));
  670. } else if (encoded_length.symbol == deflate_special_code_length_long_zeros) {
  671. TRY(m_output_stream->write_bits<u8>(encoded_length.count - 11, 7));
  672. }
  673. }
  674. TRY(write_huffman(literal_code, distance_code));
  675. return {};
  676. }
  677. ErrorOr<void> DeflateCompressor::flush()
  678. {
  679. TRY(m_output_stream->write_bits(m_finished, 1));
  680. // if this is just an empty block to signify the end of the deflate stream use the smallest block possible (10 bits total)
  681. if (m_pending_block_size == 0) {
  682. VERIFY(m_finished); // we shouldn't be writing empty blocks unless this is the final one
  683. TRY(m_output_stream->write_bits(0b01u, 2)); // fixed huffman codes
  684. TRY(m_output_stream->write_bits(0b0000000u, 7)); // end of block symbol
  685. TRY(m_output_stream->align_to_byte_boundary());
  686. return {};
  687. }
  688. auto write_uncompressed = [&]() -> ErrorOr<void> {
  689. TRY(m_output_stream->write_bits(0b00u, 2)); // no compression
  690. TRY(m_output_stream->align_to_byte_boundary());
  691. TRY(m_output_stream->write_value<LittleEndian<u16>>(m_pending_block_size));
  692. TRY(m_output_stream->write_value<LittleEndian<u16>>(~m_pending_block_size));
  693. TRY(m_output_stream->write_until_depleted(pending_block().slice(0, m_pending_block_size)));
  694. return {};
  695. };
  696. if (m_compression_level == CompressionLevel::STORE) { // disabled compression fast path
  697. TRY(write_uncompressed());
  698. m_pending_block_size = 0;
  699. return {};
  700. }
  701. // The following implementation of lz77 compression and huffman encoding is based on the reference implementation by Hans Wennborg https://www.hanshq.net/zip.html
  702. // this reads from the pending block and writes to m_symbol_buffer
  703. lz77_compress_block();
  704. // insert EndOfBlock marker to the symbol buffer
  705. m_symbol_buffer[m_pending_symbol_size].distance = 0;
  706. m_symbol_buffer[m_pending_symbol_size++].literal = EndOfBlock;
  707. m_symbol_frequencies[EndOfBlock]++;
  708. // generate optimal dynamic huffman code lengths
  709. Array<u8, max_huffman_literals> dynamic_literal_bit_lengths {};
  710. Array<u8, max_huffman_distances> dynamic_distance_bit_lengths {};
  711. generate_huffman_lengths(dynamic_literal_bit_lengths, m_symbol_frequencies, 15); // deflate data huffman can use up to 15 bits per symbol
  712. generate_huffman_lengths(dynamic_distance_bit_lengths, m_distance_frequencies, 15);
  713. // encode literal and distance lengths together in deflate format
  714. Array<code_length_symbol, max_huffman_literals + max_huffman_distances> encoded_lengths {};
  715. size_t literal_code_count;
  716. size_t distance_code_count;
  717. auto encoded_lengths_count = encode_block_lengths(dynamic_literal_bit_lengths, dynamic_distance_bit_lengths, encoded_lengths, literal_code_count, distance_code_count);
  718. // count code length frequencies
  719. Array<u16, 19> code_lengths_frequencies { 0 };
  720. for (size_t i = 0; i < encoded_lengths_count; i++) {
  721. code_lengths_frequencies[encoded_lengths[i].symbol]++;
  722. }
  723. // generate optimal huffman code lengths code lengths
  724. Array<u8, 19> code_lengths_bit_lengths {};
  725. generate_huffman_lengths(code_lengths_bit_lengths, code_lengths_frequencies, 7); // deflate code length huffman can use up to 7 bits per symbol
  726. // calculate actual code length code lengths count (without trailing zeros)
  727. auto code_lengths_count = code_lengths_bit_lengths.size();
  728. while (code_lengths_bit_lengths[code_lengths_code_lengths_order[code_lengths_count - 1]] == 0)
  729. code_lengths_count--;
  730. auto uncompressed_size = uncompressed_block_length();
  731. auto fixed_huffman_size = fixed_block_length();
  732. auto dynamic_huffman_size = dynamic_block_length(dynamic_literal_bit_lengths, dynamic_distance_bit_lengths, code_lengths_bit_lengths, code_lengths_frequencies, code_lengths_count);
  733. // If the compression somehow didn't reduce the size enough, just write out the block uncompressed as it allows for much faster decompression
  734. if (uncompressed_size <= min(fixed_huffman_size, dynamic_huffman_size)) {
  735. TRY(write_uncompressed());
  736. } else if (fixed_huffman_size <= dynamic_huffman_size) {
  737. // If the fixed and dynamic huffman codes come out the same size, prefer the fixed version, as it takes less time to decode fixed huffman codes.
  738. TRY(m_output_stream->write_bits(0b01u, 2));
  739. TRY(write_huffman(CanonicalCode::fixed_literal_codes(), CanonicalCode::fixed_distance_codes()));
  740. } else {
  741. // dynamic huffman codes
  742. TRY(m_output_stream->write_bits(0b10u, 2));
  743. auto literal_code = MUST(CanonicalCode::from_bytes(dynamic_literal_bit_lengths));
  744. auto distance_code_or_error = CanonicalCode::from_bytes(dynamic_distance_bit_lengths);
  745. Optional<CanonicalCode> distance_code;
  746. if (!distance_code_or_error.is_error())
  747. distance_code = distance_code_or_error.release_value();
  748. TRY(write_dynamic_huffman(literal_code, literal_code_count, distance_code, distance_code_count, code_lengths_bit_lengths, code_lengths_count, encoded_lengths, encoded_lengths_count));
  749. }
  750. if (m_finished)
  751. TRY(m_output_stream->align_to_byte_boundary());
  752. // reset all block specific members
  753. m_pending_block_size = 0;
  754. m_pending_symbol_size = 0;
  755. m_symbol_frequencies.fill(0);
  756. m_distance_frequencies.fill(0);
  757. // On the final block this copy will potentially produce an invalid search window, but since its the final block we dont care
  758. pending_block().copy_trimmed_to({ m_rolling_window, block_size });
  759. return {};
  760. }
  761. ErrorOr<void> DeflateCompressor::final_flush()
  762. {
  763. VERIFY(!m_finished);
  764. m_finished = true;
  765. TRY(flush());
  766. TRY(m_output_stream->flush_buffer_to_stream());
  767. return {};
  768. }
  769. ErrorOr<ByteBuffer> DeflateCompressor::compress_all(ReadonlyBytes bytes, CompressionLevel compression_level)
  770. {
  771. auto output_stream = TRY(try_make<AllocatingMemoryStream>());
  772. auto deflate_stream = TRY(DeflateCompressor::construct(MaybeOwned<Stream>(*output_stream), compression_level));
  773. TRY(deflate_stream->write_until_depleted(bytes));
  774. TRY(deflate_stream->final_flush());
  775. auto buffer = TRY(ByteBuffer::create_uninitialized(output_stream->used_buffer_size()));
  776. TRY(output_stream->read_until_filled(buffer));
  777. return buffer;
  778. }
  779. }