Gzip.cpp 5.2 KB

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