Gzip.cpp 4.9 KB

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