Filter.cpp 4.8 KB

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