Gzip.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 (m_current_member.has_value()) {
  61. size_t nread = current_member().m_stream.read(bytes);
  62. current_member().m_checksum.update(bytes.trim(nread));
  63. current_member().m_nread += nread;
  64. if (nread < bytes.size()) {
  65. LittleEndian<u32> crc32, input_size;
  66. m_input_stream >> crc32 >> input_size;
  67. if (crc32 != current_member().m_checksum.digest()) {
  68. // FIXME: Somehow the checksum is incorrect?
  69. set_fatal_error();
  70. return 0;
  71. }
  72. if (input_size != current_member().m_nread) {
  73. set_fatal_error();
  74. return 0;
  75. }
  76. m_current_member.clear();
  77. return nread + read(bytes.slice(nread));
  78. }
  79. return nread;
  80. } else {
  81. if (m_input_stream.eof())
  82. return 0;
  83. BlockHeader header;
  84. m_input_stream >> Bytes { &header, sizeof(header) };
  85. if (!header.valid_magic_number() || !header.supported_by_implementation()) {
  86. set_fatal_error();
  87. return 0;
  88. }
  89. if (header.flags & Flags::FEXTRA) {
  90. LittleEndian<u16> subfield_id, length;
  91. m_input_stream >> subfield_id >> length;
  92. m_input_stream.discard_or_error(length);
  93. }
  94. if (header.flags & Flags::FNAME) {
  95. String original_filename;
  96. m_input_stream >> original_filename;
  97. }
  98. if (header.flags & Flags::FCOMMENT) {
  99. String comment;
  100. m_input_stream >> comment;
  101. }
  102. m_current_member.emplace(header, m_input_stream);
  103. return read(bytes);
  104. }
  105. }
  106. bool GzipDecompressor::read_or_error(Bytes bytes)
  107. {
  108. if (read(bytes) < bytes.size()) {
  109. set_fatal_error();
  110. return false;
  111. }
  112. return true;
  113. }
  114. bool GzipDecompressor::discard_or_error(size_t count)
  115. {
  116. u8 buffer[4096];
  117. size_t ndiscarded = 0;
  118. while (ndiscarded < count) {
  119. if (eof()) {
  120. set_fatal_error();
  121. return false;
  122. }
  123. ndiscarded += read({ buffer, min<size_t>(count - ndiscarded, sizeof(buffer)) });
  124. }
  125. return true;
  126. }
  127. ByteBuffer GzipDecompressor::decompress_all(ReadonlyBytes bytes)
  128. {
  129. InputMemoryStream memory_stream { bytes };
  130. GzipDecompressor gzip_stream { memory_stream };
  131. auto buffer = ByteBuffer::create_uninitialized(4096);
  132. size_t nread = 0;
  133. while (!gzip_stream.eof()) {
  134. nread += gzip_stream.read(buffer.bytes().slice(nread));
  135. if (buffer.size() - nread < 4096)
  136. buffer.grow(buffer.size() + 4096);
  137. }
  138. buffer.trim(nread);
  139. return buffer;
  140. }
  141. bool GzipDecompressor::eof() const
  142. {
  143. if (m_current_member.has_value()) {
  144. // FIXME: There is an ugly edge case where we read the whole deflate block
  145. // but haven't read CRC32 and ISIZE.
  146. return current_member().m_stream.eof() && m_input_stream.eof();
  147. } else {
  148. return m_input_stream.eof();
  149. }
  150. }
  151. }