Gzip.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 <AK/ByteBuffer.h>
  27. #include <AK/Optional.h>
  28. #include <LibCore/Gzip.h>
  29. #include <LibCore/puff.h>
  30. #include <limits.h>
  31. #include <stddef.h>
  32. //#define DEBUG_GZIP
  33. namespace Core {
  34. bool Gzip::is_compressed(const ByteBuffer& data)
  35. {
  36. return data.size() > 2 && data[0] == 0x1F && data[1] == 0x8b;
  37. }
  38. // skips the gzip header
  39. // see: https://tools.ietf.org/html/rfc1952#page-5
  40. static Optional<ByteBuffer> get_gzip_payload(const ByteBuffer& data)
  41. {
  42. size_t current = 0;
  43. auto read_byte = [&]() {
  44. if (current >= data.size()) {
  45. ASSERT_NOT_REACHED();
  46. return (u8)0;
  47. }
  48. return data[current++];
  49. };
  50. #ifdef DEBUG_GZIP
  51. dbgln("get_gzip_payload: Skipping over gzip header.");
  52. #endif
  53. // Magic Header
  54. if (read_byte() != 0x1F || read_byte() != 0x8B) {
  55. dbgln("get_gzip_payload: Wrong magic number.");
  56. return Optional<ByteBuffer>();
  57. }
  58. // Compression method
  59. auto method = read_byte();
  60. if (method != 8) {
  61. dbgln("get_gzip_payload: Wrong compression method={}", method);
  62. return Optional<ByteBuffer>();
  63. }
  64. u8 flags = read_byte();
  65. // Timestamp, Extra flags, OS
  66. current += 6;
  67. // FEXTRA
  68. if (flags & 4) {
  69. u16 length = read_byte() & read_byte() << 8;
  70. dbgln("get_gzip_payload: Header has FEXTRA flag set. length={}", length);
  71. current += length;
  72. }
  73. // FNAME
  74. if (flags & 8) {
  75. dbgln("get_gzip_payload: Header has FNAME flag set.");
  76. while (read_byte() != '\0')
  77. ;
  78. }
  79. // FCOMMENT
  80. if (flags & 16) {
  81. dbgln("get_gzip_payload: Header has FCOMMENT flag set.");
  82. while (read_byte() != '\0')
  83. ;
  84. }
  85. // FHCRC
  86. if (flags & 2) {
  87. dbgln("get_gzip_payload: Header has FHCRC flag set.");
  88. current += 2;
  89. }
  90. auto new_size = data.size() - current;
  91. #ifdef DEBUG_GZIP
  92. dbg() << "get_gzip_payload: Returning slice from " << current << " with size " << new_size;
  93. #endif
  94. return data.slice(current, new_size);
  95. }
  96. Optional<ByteBuffer> Gzip::decompress(const ByteBuffer& data)
  97. {
  98. ASSERT(is_compressed(data));
  99. #ifdef DEBUG_GZIP
  100. dbg() << "Gzip::decompress: Decompressing gzip compressed data. Size = " << data.size();
  101. #endif
  102. auto optional_payload = get_gzip_payload(data);
  103. if (!optional_payload.has_value()) {
  104. return Optional<ByteBuffer>();
  105. }
  106. auto source = optional_payload.value();
  107. unsigned long source_len = source.size();
  108. auto destination = ByteBuffer::create_uninitialized(1024);
  109. while (true) {
  110. unsigned long destination_len = destination.size();
  111. #ifdef DEBUG_GZIP
  112. dbg() << "Gzip::decompress: Calling puff()\n"
  113. << " destination_data = " << destination.data() << "\n"
  114. << " destination_len = " << destination_len << "\n"
  115. << " source_data = " << source.data() << "\n"
  116. << " source_len = " << source_len;
  117. #endif
  118. auto puff_ret = puff(
  119. destination.data(), &destination_len,
  120. source.data(), &source_len);
  121. if (puff_ret == 0) {
  122. #ifdef DEBUG_GZIP
  123. dbgln("Gzip::decompress: Decompression success.");
  124. #endif
  125. destination.trim(destination_len);
  126. break;
  127. }
  128. if (puff_ret == 1) {
  129. // FIXME: Find a better way of decompressing without needing to try over and over again.
  130. #ifdef DEBUG_GZIP
  131. dbgln("Gzip::decompress: Output buffer exhausted. Growing.");
  132. #endif
  133. destination.grow(destination.size() * 2);
  134. } else {
  135. dbgln("Gzip::decompress: Error. puff() returned: {}", puff_ret);
  136. ASSERT_NOT_REACHED();
  137. }
  138. }
  139. return destination;
  140. }
  141. }