CGzip.cpp 3.6 KB

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