Gzip.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <LibCompress/Gzip.h>
  28. #include <AK/MemoryStream.h>
  29. #include <AK/String.h>
  30. namespace Compress {
  31. bool GzipDecompressor::is_likely_compressed(ReadonlyBytes bytes)
  32. {
  33. return bytes.size() >= 2 && bytes[0] == gzip_magic_1 && bytes[1] == gzip_magic_2;
  34. }
  35. bool BlockHeader::valid_magic_number() const
  36. {
  37. return identification_1 == gzip_magic_1 && identification_2 == gzip_magic_2;
  38. }
  39. bool BlockHeader::supported_by_implementation() const
  40. {
  41. if (compression_method != 0x08) {
  42. // RFC 1952 does not define any compression methods other than deflate.
  43. return false;
  44. }
  45. if (flags > Flags::MAX) {
  46. // RFC 1952 does not define any more flags.
  47. return false;
  48. }
  49. return true;
  50. }
  51. GzipDecompressor::GzipDecompressor(InputStream& stream)
  52. : m_input_stream(stream)
  53. {
  54. }
  55. GzipDecompressor::~GzipDecompressor()
  56. {
  57. m_current_member.clear();
  58. }
  59. // FIXME: Again, there are surely a ton of bugs because the code doesn't check for read errors.
  60. size_t GzipDecompressor::read(Bytes bytes)
  61. {
  62. size_t total_read = 0;
  63. while (total_read < bytes.size()) {
  64. if (has_any_error() || m_eof)
  65. break;
  66. auto slice = bytes.slice(total_read);
  67. if (m_current_member.has_value()) {
  68. size_t nread = current_member().m_stream.read(slice);
  69. current_member().m_checksum.update(slice.trim(nread));
  70. current_member().m_nread += nread;
  71. if (current_member().m_stream.handle_any_error()) {
  72. set_fatal_error();
  73. break;
  74. }
  75. if (nread < slice.size()) {
  76. LittleEndian<u32> crc32, input_size;
  77. m_input_stream >> crc32 >> input_size;
  78. if (crc32 != current_member().m_checksum.digest()) {
  79. // FIXME: Somehow the checksum is incorrect?
  80. set_fatal_error();
  81. break;
  82. }
  83. if (input_size != current_member().m_nread) {
  84. set_fatal_error();
  85. break;
  86. }
  87. m_current_member.clear();
  88. total_read += nread;
  89. continue;
  90. }
  91. total_read += nread;
  92. continue;
  93. } else {
  94. m_partial_header_offset += m_input_stream.read(Bytes { m_partial_header, sizeof(BlockHeader) }.slice(m_partial_header_offset));
  95. if (m_input_stream.handle_any_error() || m_input_stream.unreliable_eof()) {
  96. m_eof = true;
  97. break;
  98. }
  99. if (m_partial_header_offset < sizeof(BlockHeader)) {
  100. break; // partial header read
  101. }
  102. m_partial_header_offset = 0;
  103. BlockHeader header = *(reinterpret_cast<BlockHeader*>(m_partial_header));
  104. if (!header.valid_magic_number() || !header.supported_by_implementation()) {
  105. set_fatal_error();
  106. break;
  107. }
  108. if (header.flags & Flags::FEXTRA) {
  109. LittleEndian<u16> subfield_id, length;
  110. m_input_stream >> subfield_id >> length;
  111. m_input_stream.discard_or_error(length);
  112. }
  113. if (header.flags & Flags::FNAME) {
  114. String original_filename;
  115. m_input_stream >> original_filename;
  116. }
  117. if (header.flags & Flags::FCOMMENT) {
  118. String comment;
  119. m_input_stream >> comment;
  120. }
  121. if (header.flags & Flags::FHCRC) {
  122. LittleEndian<u16> crc16;
  123. m_input_stream >> crc16;
  124. // FIXME: we should probably verify this instead of just assuming it matches
  125. }
  126. m_current_member.emplace(header, m_input_stream);
  127. continue;
  128. }
  129. }
  130. return total_read;
  131. }
  132. bool GzipDecompressor::read_or_error(Bytes bytes)
  133. {
  134. if (read(bytes) < bytes.size()) {
  135. set_fatal_error();
  136. return false;
  137. }
  138. return true;
  139. }
  140. bool GzipDecompressor::discard_or_error(size_t count)
  141. {
  142. u8 buffer[4096];
  143. size_t ndiscarded = 0;
  144. while (ndiscarded < count) {
  145. if (unreliable_eof()) {
  146. set_fatal_error();
  147. return false;
  148. }
  149. ndiscarded += read({ buffer, min<size_t>(count - ndiscarded, sizeof(buffer)) });
  150. }
  151. return true;
  152. }
  153. Optional<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
  154. {
  155. InputMemoryStream memory_stream { bytes };
  156. GzipDecompressor gzip_stream { memory_stream };
  157. DuplexMemoryStream output_stream;
  158. u8 buffer[4096];
  159. while (!gzip_stream.has_any_error() && !gzip_stream.unreliable_eof()) {
  160. const auto nread = gzip_stream.read({ buffer, sizeof(buffer) });
  161. output_stream.write_or_error({ buffer, nread });
  162. }
  163. if (gzip_stream.handle_any_error())
  164. return {};
  165. return output_stream.copy_into_contiguous_buffer();
  166. }
  167. bool GzipDecompressor::unreliable_eof() const { return m_eof; }
  168. bool GzipDecompressor::handle_any_error()
  169. {
  170. bool handled_errors = m_input_stream.handle_any_error();
  171. return Stream::handle_any_error() || handled_errors;
  172. }
  173. GzipCompressor::GzipCompressor(OutputStream& stream)
  174. : m_output_stream(stream)
  175. {
  176. }
  177. GzipCompressor::~GzipCompressor()
  178. {
  179. }
  180. size_t GzipCompressor::write(ReadonlyBytes bytes)
  181. {
  182. BlockHeader header;
  183. header.identification_1 = 0x1f;
  184. header.identification_2 = 0x8b;
  185. header.compression_method = 0x08;
  186. header.flags = 0;
  187. header.modification_time = 0;
  188. header.extra_flags = 3; // DEFLATE sets 2 for maximum compression and 4 for minimum compression
  189. header.operating_system = 3; // unix
  190. m_output_stream << Bytes { &header, sizeof(header) };
  191. DeflateCompressor compressed_stream { m_output_stream };
  192. VERIFY(compressed_stream.write_or_error(bytes));
  193. compressed_stream.final_flush();
  194. Crypto::Checksum::CRC32 crc32;
  195. crc32.update(bytes);
  196. LittleEndian<u32> digest = crc32.digest();
  197. LittleEndian<u32> size = bytes.size();
  198. m_output_stream << digest << size;
  199. return bytes.size();
  200. }
  201. bool GzipCompressor::write_or_error(ReadonlyBytes bytes)
  202. {
  203. if (write(bytes) < bytes.size()) {
  204. set_fatal_error();
  205. return false;
  206. }
  207. return true;
  208. }
  209. Optional<ByteBuffer> GzipCompressor::compress_all(const ReadonlyBytes& bytes)
  210. {
  211. DuplexMemoryStream output_stream;
  212. GzipCompressor gzip_stream { output_stream };
  213. gzip_stream.write_or_error(bytes);
  214. if (gzip_stream.handle_any_error())
  215. return {};
  216. return output_stream.copy_into_contiguous_buffer();
  217. }
  218. }