Gzip.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCompress/Gzip.h>
  27. #include <AK/MemoryStream.h>
  28. #include <AK/String.h>
  29. namespace Compress {
  30. bool GzipDecompressor::BlockHeader::valid_magic_number() const
  31. {
  32. return identification_1 == 0x1f && identification_2 == 0x8b;
  33. }
  34. bool GzipDecompressor::BlockHeader::supported_by_implementation() const
  35. {
  36. if (compression_method != 0x08) {
  37. // RFC 1952 does not define any compression methods other than deflate.
  38. return false;
  39. }
  40. if (flags > Flags::MAX) {
  41. // RFC 1952 does not define any more flags.
  42. return false;
  43. }
  44. if (flags & Flags::FHCRC) {
  45. TODO();
  46. }
  47. return true;
  48. }
  49. GzipDecompressor::GzipDecompressor(InputStream& stream)
  50. : m_input_stream(stream)
  51. {
  52. }
  53. GzipDecompressor::~GzipDecompressor()
  54. {
  55. m_current_member.clear();
  56. }
  57. // FIXME: Again, there are surely a ton of bugs because the code doesn't check for read errors.
  58. size_t GzipDecompressor::read(Bytes bytes)
  59. {
  60. if (has_any_error())
  61. return 0;
  62. if (m_current_member.has_value()) {
  63. size_t nread = current_member().m_stream.read(bytes);
  64. current_member().m_checksum.update(bytes.trim(nread));
  65. current_member().m_nread += nread;
  66. if (nread < bytes.size()) {
  67. LittleEndian<u32> crc32, input_size;
  68. m_input_stream >> crc32 >> input_size;
  69. if (crc32 != current_member().m_checksum.digest()) {
  70. // FIXME: Somehow the checksum is incorrect?
  71. set_fatal_error();
  72. return 0;
  73. }
  74. if (input_size != current_member().m_nread) {
  75. set_fatal_error();
  76. return 0;
  77. }
  78. m_current_member.clear();
  79. return nread + read(bytes.slice(nread));
  80. }
  81. return nread;
  82. } else {
  83. if (m_input_stream.eof())
  84. return 0;
  85. BlockHeader header;
  86. m_input_stream >> Bytes { &header, sizeof(header) };
  87. if (!header.valid_magic_number() || !header.supported_by_implementation()) {
  88. set_fatal_error();
  89. return 0;
  90. }
  91. if (header.flags & Flags::FEXTRA) {
  92. LittleEndian<u16> subfield_id, length;
  93. m_input_stream >> subfield_id >> length;
  94. m_input_stream.discard_or_error(length);
  95. }
  96. if (header.flags & Flags::FNAME) {
  97. String original_filename;
  98. m_input_stream >> original_filename;
  99. }
  100. if (header.flags & Flags::FCOMMENT) {
  101. String comment;
  102. m_input_stream >> comment;
  103. }
  104. m_current_member.emplace(header, m_input_stream);
  105. return read(bytes);
  106. }
  107. }
  108. bool GzipDecompressor::read_or_error(Bytes bytes)
  109. {
  110. if (read(bytes) < bytes.size()) {
  111. set_fatal_error();
  112. return false;
  113. }
  114. return true;
  115. }
  116. bool GzipDecompressor::discard_or_error(size_t count)
  117. {
  118. u8 buffer[4096];
  119. size_t ndiscarded = 0;
  120. while (ndiscarded < count) {
  121. if (eof()) {
  122. set_fatal_error();
  123. return false;
  124. }
  125. ndiscarded += read({ buffer, min<size_t>(count - ndiscarded, sizeof(buffer)) });
  126. }
  127. return true;
  128. }
  129. ByteBuffer GzipDecompressor::decompress_all(ReadonlyBytes bytes)
  130. {
  131. InputMemoryStream memory_stream { bytes };
  132. GzipDecompressor gzip_stream { memory_stream };
  133. auto buffer = ByteBuffer::create_uninitialized(4096);
  134. size_t nread = 0;
  135. while (!gzip_stream.eof()) {
  136. nread += gzip_stream.read(buffer.bytes().slice(nread));
  137. if (buffer.size() - nread < 4096)
  138. buffer.grow(buffer.size() + 4096);
  139. }
  140. buffer.trim(nread);
  141. return buffer;
  142. }
  143. bool GzipDecompressor::eof() const
  144. {
  145. if (m_current_member.has_value()) {
  146. // FIXME: There is an ugly edge case where we read the whole deflate block
  147. // but haven't read CRC32 and ISIZE.
  148. return current_member().m_stream.eof() && m_input_stream.eof();
  149. } else {
  150. return m_input_stream.eof();
  151. }
  152. }
  153. }