Deflate.cpp 41 KB

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