CGzip.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "CGzip.h"
  2. #include <AK/ByteBuffer.h>
  3. #include <AK/Optional.h>
  4. #include <LibCore/puff.h>
  5. #include <LibCore/puff.c>
  6. #include <limits.h>
  7. #include <stddef.h>
  8. bool CGzip::is_compressed(const ByteBuffer& data)
  9. {
  10. return data.size() > 2 && data[0] == 0x1F && data[1] == 0x8b;
  11. }
  12. // skips the gzip header
  13. // see: https://tools.ietf.org/html/rfc1952#page-5
  14. static Optional<ByteBuffer> get_gzip_payload(const ByteBuffer& data)
  15. {
  16. int current = 0;
  17. auto read_byte = [&]() {
  18. if (current >= data.size()) {
  19. ASSERT_NOT_REACHED();
  20. return (u8)0;
  21. }
  22. // dbg() << "read_byte: " << String::format("%x", data[current]);
  23. return data[current++];
  24. };
  25. dbg() << "get_gzip_payload: Skipping over gzip header.";
  26. // Magic Header
  27. if (read_byte() != 0x1F || read_byte() != 0x8B) {
  28. dbg() << "get_gzip_payload: Wrong magic number.";
  29. return Optional<ByteBuffer>();
  30. }
  31. // Compression method
  32. auto method = read_byte();
  33. if (method != 8) {
  34. dbg() << "get_gzip_payload: Wrong compression method = " << method;
  35. return Optional<ByteBuffer>();
  36. }
  37. u8 flags = read_byte();
  38. // Timestamp, Extra flags, OS
  39. current += 6;
  40. // FEXTRA
  41. if (flags & 4) {
  42. u16 length = read_byte() & read_byte() << 8;
  43. dbg() << "get_gzip_payload: Header has FEXTRA flag set. Length = " << length;
  44. current += length;
  45. }
  46. // FNAME
  47. if (flags & 8) {
  48. dbg() << "get_gzip_payload: Header has FNAME flag set.";
  49. while (read_byte() != '\0')
  50. ;
  51. }
  52. // FCOMMENT
  53. if (flags & 16) {
  54. dbg() << "get_gzip_payload: Header has FCOMMENT flag set.";
  55. while (read_byte() != '\0')
  56. ;
  57. }
  58. // FHCRC
  59. if (flags & 2) {
  60. dbg() << "get_gzip_payload: Header has FHCRC flag set.";
  61. current += 2;
  62. }
  63. auto new_size = data.size() - current;
  64. dbg() << "get_gzip_payload: Returning slice from " << current << " with size " << new_size;
  65. return data.slice(current, new_size);
  66. }
  67. Optional<ByteBuffer> CGzip::decompress(const ByteBuffer& data)
  68. {
  69. ASSERT(is_compressed(data));
  70. dbg() << "Gzip::decompress: Decompressing gzip compressed data. Size = " << data.size();
  71. auto optional_payload = get_gzip_payload(data);
  72. if (!optional_payload.has_value()) {
  73. return Optional<ByteBuffer>();
  74. }
  75. auto source = optional_payload.value();
  76. unsigned long source_len = source.size();
  77. auto destination = ByteBuffer::create_uninitialized(1024);
  78. while (true) {
  79. unsigned long destination_len = destination.size();
  80. // FIXME: dbg() cannot take ulong?
  81. // dbg() << "Gzip::decompress: Calling puff()\n"
  82. // << " destination_data = " << destination.data() << "\n"
  83. // << " destination_len = " << (int)destination_len << "\n"
  84. // << " source_data = " << source.data() << "\n"
  85. // << " source_len = " << (int)source_len;
  86. auto puff_ret = puff(
  87. destination.data(), &destination_len,
  88. source.data(), &source_len);
  89. if (puff_ret == 0) {
  90. dbg() << "Gzip::decompress: Decompression success.";
  91. break;
  92. }
  93. if (puff_ret == 1) {
  94. // FIXME: Find a better way of decompressing without needing to try over and over again.
  95. dbg() << "Gzip::decompress: Output buffer exhausted. Growing.";
  96. destination.grow(destination.size() * 2);
  97. } else {
  98. dbg() << "Gzip::decompress: Error. puff() returned: " << puff_ret;
  99. ASSERT_NOT_REACHED();
  100. }
  101. }
  102. return destination;
  103. }