Deflate.cpp 38 KB

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