Filter.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Hex.h>
  7. #include <LibCompress/Deflate.h>
  8. #include <LibPDF/CommonNames.h>
  9. #include <LibPDF/Filter.h>
  10. namespace PDF {
  11. Optional<ByteBuffer> Filter::decode(ReadonlyBytes const& bytes, FlyString const& encoding_type)
  12. {
  13. if (encoding_type == CommonNames::ASCIIHexDecode)
  14. return decode_ascii_hex(bytes);
  15. if (encoding_type == CommonNames::ASCII85Decode)
  16. return decode_ascii85(bytes);
  17. if (encoding_type == CommonNames::LZWDecode)
  18. return decode_lzw(bytes);
  19. if (encoding_type == CommonNames::FlateDecode)
  20. return decode_flate(bytes);
  21. if (encoding_type == CommonNames::RunLengthDecode)
  22. return decode_run_length(bytes);
  23. if (encoding_type == CommonNames::CCITTFaxDecode)
  24. return decode_ccitt(bytes);
  25. if (encoding_type == CommonNames::JBIG2Decode)
  26. return decode_jbig2(bytes);
  27. if (encoding_type == CommonNames::DCTDecode)
  28. return decode_dct(bytes);
  29. if (encoding_type == CommonNames::JPXDecode)
  30. return decode_jpx(bytes);
  31. if (encoding_type == CommonNames::Crypt)
  32. return decode_crypt(bytes);
  33. return {};
  34. }
  35. Optional<ByteBuffer> Filter::decode_ascii_hex(ReadonlyBytes const& bytes)
  36. {
  37. if (bytes.size() % 2 == 0)
  38. return decode_hex(bytes);
  39. // FIXME: Integrate this padding into AK/Hex?
  40. auto output_result = ByteBuffer::create_zeroed(bytes.size() / 2 + 1);
  41. if (!output_result.has_value())
  42. return output_result;
  43. auto output = output_result.release_value();
  44. for (size_t i = 0; i < bytes.size() / 2; ++i) {
  45. const auto c1 = decode_hex_digit(static_cast<char>(bytes[i * 2]));
  46. if (c1 >= 16)
  47. return {};
  48. const auto c2 = decode_hex_digit(static_cast<char>(bytes[i * 2 + 1]));
  49. if (c2 >= 16)
  50. return {};
  51. output[i] = (c1 << 4) + c2;
  52. }
  53. // Process last byte with a padded zero
  54. output[output.size() - 1] = decode_hex_digit(static_cast<char>(bytes[bytes.size() - 1])) * 16;
  55. return { move(output) };
  56. };
  57. Optional<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes const& bytes)
  58. {
  59. Vector<u8> buff;
  60. buff.ensure_capacity(bytes.size());
  61. size_t byte_index = 0;
  62. while (byte_index < bytes.size()) {
  63. if (bytes[byte_index] == ' ') {
  64. byte_index++;
  65. continue;
  66. }
  67. if (bytes[byte_index] == 'z') {
  68. byte_index++;
  69. for (int i = 0; i < 4; i++)
  70. buff.append(0);
  71. continue;
  72. }
  73. u32 number = 0;
  74. if (byte_index + 5 >= bytes.size()) {
  75. auto to_write = bytes.size() - byte_index;
  76. for (int i = 0; i < 5; i++) {
  77. auto byte = byte_index >= bytes.size() ? 'u' : bytes[byte_index++];
  78. if (byte == ' ') {
  79. i--;
  80. continue;
  81. }
  82. number = number * 85 + byte - 33;
  83. }
  84. for (size_t i = 0; i < to_write - 1; i++)
  85. buff.append(reinterpret_cast<u8*>(&number)[3 - i]);
  86. break;
  87. } else {
  88. for (int i = 0; i < 5; i++) {
  89. auto byte = bytes[byte_index++];
  90. if (byte == ' ') {
  91. i--;
  92. continue;
  93. }
  94. number = number * 85 + byte - 33;
  95. }
  96. }
  97. for (int i = 0; i < 4; i++)
  98. buff.append(reinterpret_cast<u8*>(&number)[3 - i]);
  99. }
  100. return ByteBuffer::copy(buff.span());
  101. };
  102. Optional<ByteBuffer> Filter::decode_lzw(ReadonlyBytes const&)
  103. {
  104. dbgln("LZW decoding is not supported");
  105. VERIFY_NOT_REACHED();
  106. };
  107. Optional<ByteBuffer> Filter::decode_flate(ReadonlyBytes const& bytes)
  108. {
  109. // FIXME: The spec says Flate decoding is "based on" zlib, does that mean they
  110. // aren't exactly the same?
  111. auto buff = Compress::DeflateDecompressor::decompress_all(bytes.slice(2));
  112. VERIFY(buff.has_value());
  113. return buff.value();
  114. };
  115. Optional<ByteBuffer> Filter::decode_run_length(ReadonlyBytes const&)
  116. {
  117. // FIXME: Support RunLength decoding
  118. TODO();
  119. };
  120. Optional<ByteBuffer> Filter::decode_ccitt(ReadonlyBytes const&)
  121. {
  122. // FIXME: Support CCITT decoding
  123. TODO();
  124. };
  125. Optional<ByteBuffer> Filter::decode_jbig2(ReadonlyBytes const&)
  126. {
  127. // FIXME: Support JBIG2 decoding
  128. TODO();
  129. };
  130. Optional<ByteBuffer> Filter::decode_dct(ReadonlyBytes const&)
  131. {
  132. // FIXME: Support dct decoding
  133. TODO();
  134. };
  135. Optional<ByteBuffer> Filter::decode_jpx(ReadonlyBytes const&)
  136. {
  137. // FIXME: Support JPX decoding
  138. TODO();
  139. };
  140. Optional<ByteBuffer> Filter::decode_crypt(ReadonlyBytes const&)
  141. {
  142. // FIXME: Support Crypt decoding
  143. TODO();
  144. };
  145. }