Deflate.cpp 40 KB

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