mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +00:00
LibPDF: Improve error support for Filter class
The Filter class had a few TODO()s that resulted in crashes at runtime. Since we now have a better way to report errors back to the user let's use that instead.
This commit is contained in:
parent
7bd78d40e9
commit
2a8e0da71c
2 changed files with 33 additions and 38 deletions
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
namespace PDF {
|
namespace PDF {
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode(ReadonlyBytes bytes, DeprecatedFlyString const& encoding_type, RefPtr<DictObject> decode_parms)
|
PDFErrorOr<ByteBuffer> Filter::decode(ReadonlyBytes bytes, DeprecatedFlyString const& encoding_type, RefPtr<DictObject> decode_parms)
|
||||||
{
|
{
|
||||||
int predictor = 1;
|
int predictor = 1;
|
||||||
int columns = 1;
|
int columns = 1;
|
||||||
|
@ -51,13 +51,13 @@ ErrorOr<ByteBuffer> Filter::decode(ReadonlyBytes bytes, DeprecatedFlyString cons
|
||||||
if (encoding_type == CommonNames::Crypt)
|
if (encoding_type == CommonNames::Crypt)
|
||||||
return decode_crypt(bytes);
|
return decode_crypt(bytes);
|
||||||
|
|
||||||
return AK::Error::from_string_literal("Unrecognized filter encoding");
|
return Error::malformed_error("Unrecognized filter encoding {}", encoding_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_ascii_hex(ReadonlyBytes bytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_ascii_hex(ReadonlyBytes bytes)
|
||||||
{
|
{
|
||||||
if (bytes.size() % 2 == 0)
|
if (bytes.size() % 2 == 0)
|
||||||
return decode_hex(bytes);
|
return TRY(decode_hex(bytes));
|
||||||
|
|
||||||
// FIXME: Integrate this padding into AK/Hex?
|
// FIXME: Integrate this padding into AK/Hex?
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ ErrorOr<ByteBuffer> Filter::decode_ascii_hex(ReadonlyBytes bytes)
|
||||||
return { move(output) };
|
return { move(output) };
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes bytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes bytes)
|
||||||
{
|
{
|
||||||
Vector<u8> buff;
|
Vector<u8> buff;
|
||||||
buff.ensure_capacity(bytes.size());
|
buff.ensure_capacity(bytes.size());
|
||||||
|
@ -133,10 +133,10 @@ ErrorOr<ByteBuffer> Filter::decode_ascii85(ReadonlyBytes bytes)
|
||||||
buff.append(reinterpret_cast<u8*>(&number)[3 - i]);
|
buff.append(reinterpret_cast<u8*>(&number)[3 - i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ByteBuffer::copy(buff.span());
|
return TRY(ByteBuffer::copy(buff.span()));
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_png_prediction(Bytes bytes, int bytes_per_row)
|
PDFErrorOr<ByteBuffer> Filter::decode_png_prediction(Bytes bytes, int bytes_per_row)
|
||||||
{
|
{
|
||||||
int number_of_rows = bytes.size() / bytes_per_row;
|
int number_of_rows = bytes.size() / bytes_per_row;
|
||||||
|
|
||||||
|
@ -201,13 +201,12 @@ ErrorOr<ByteBuffer> Filter::decode_png_prediction(Bytes bytes, int bytes_per_row
|
||||||
return decoded;
|
return decoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_lzw(ReadonlyBytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_lzw(ReadonlyBytes)
|
||||||
{
|
{
|
||||||
dbgln("LZW decoding is not supported");
|
return Error::rendering_unsupported_error("LZW Filter is not supported");
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component)
|
PDFErrorOr<ByteBuffer> Filter::decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component)
|
||||||
{
|
{
|
||||||
auto buff = Compress::DeflateDecompressor::decompress_all(bytes.slice(2)).value();
|
auto buff = Compress::DeflateDecompressor::decompress_all(bytes.slice(2)).value();
|
||||||
if (predictor == 1)
|
if (predictor == 1)
|
||||||
|
@ -227,7 +226,7 @@ ErrorOr<ByteBuffer> Filter::decode_flate(ReadonlyBytes bytes, int predictor, int
|
||||||
return decode_png_prediction(buff, bytes_per_row);
|
return decode_png_prediction(buff, bytes_per_row);
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_run_length(ReadonlyBytes bytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_run_length(ReadonlyBytes bytes)
|
||||||
{
|
{
|
||||||
constexpr size_t END_OF_DECODING = 128;
|
constexpr size_t END_OF_DECODING = 128;
|
||||||
ByteBuffer buffer {};
|
ByteBuffer buffer {};
|
||||||
|
@ -255,40 +254,36 @@ ErrorOr<ByteBuffer> Filter::decode_run_length(ReadonlyBytes bytes)
|
||||||
return buffer;
|
return buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_ccitt(ReadonlyBytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_ccitt(ReadonlyBytes)
|
||||||
{
|
{
|
||||||
// FIXME: Support CCITT decoding
|
return Error::rendering_unsupported_error("CCITTFaxDecode Filter is unsupported");
|
||||||
TODO();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_jbig2(ReadonlyBytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_jbig2(ReadonlyBytes)
|
||||||
{
|
{
|
||||||
// FIXME: Support JBIG2 decoding
|
return Error::rendering_unsupported_error("JBIG2 Filter is unsupported");
|
||||||
TODO();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_dct(ReadonlyBytes bytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_dct(ReadonlyBytes bytes)
|
||||||
{
|
{
|
||||||
if (Gfx::JPEGImageDecoderPlugin::sniff({ bytes.data(), bytes.size() })) {
|
if (Gfx::JPEGImageDecoderPlugin::sniff({ bytes.data(), bytes.size() })) {
|
||||||
auto decoder = Gfx::JPEGImageDecoderPlugin::create({ bytes.data(), bytes.size() }).release_value_but_fixme_should_propagate_errors();
|
auto decoder = Gfx::JPEGImageDecoderPlugin::create({ bytes.data(), bytes.size() }).release_value_but_fixme_should_propagate_errors();
|
||||||
if (decoder->initialize()) {
|
if (decoder->initialize()) {
|
||||||
auto frame = TRY(decoder->frame(0));
|
auto frame = TRY(decoder->frame(0));
|
||||||
return frame.image->serialize_to_byte_buffer();
|
return TRY(frame.image->serialize_to_byte_buffer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return AK::Error::from_string_literal("Not a JPEG image!");
|
return AK::Error::from_string_literal("Not a JPEG image!");
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_jpx(ReadonlyBytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_jpx(ReadonlyBytes)
|
||||||
{
|
{
|
||||||
// FIXME: Support JPX decoding
|
return Error::rendering_unsupported_error("JPX Filter is not supported");
|
||||||
TODO();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ErrorOr<ByteBuffer> Filter::decode_crypt(ReadonlyBytes)
|
PDFErrorOr<ByteBuffer> Filter::decode_crypt(ReadonlyBytes)
|
||||||
{
|
{
|
||||||
// FIXME: Support Crypt decoding
|
return Error::rendering_unsupported_error("Crypt Filter is not supported");
|
||||||
TODO();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,20 +15,20 @@ namespace PDF {
|
||||||
|
|
||||||
class Filter {
|
class Filter {
|
||||||
public:
|
public:
|
||||||
static ErrorOr<ByteBuffer> decode(ReadonlyBytes bytes, DeprecatedFlyString const& encoding_type, RefPtr<DictObject> decode_parms);
|
static PDFErrorOr<ByteBuffer> decode(ReadonlyBytes bytes, DeprecatedFlyString const& encoding_type, RefPtr<DictObject> decode_parms);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static ErrorOr<ByteBuffer> decode_ascii_hex(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_ascii_hex(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_ascii85(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_ascii85(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_png_prediction(Bytes bytes, int bytes_per_row);
|
static PDFErrorOr<ByteBuffer> decode_png_prediction(Bytes bytes, int bytes_per_row);
|
||||||
static ErrorOr<ByteBuffer> decode_lzw(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_lzw(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component);
|
static PDFErrorOr<ByteBuffer> decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component);
|
||||||
static ErrorOr<ByteBuffer> decode_run_length(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_run_length(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_ccitt(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_ccitt(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_jbig2(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_jbig2(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_dct(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_dct(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_jpx(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_jpx(ReadonlyBytes bytes);
|
||||||
static ErrorOr<ByteBuffer> decode_crypt(ReadonlyBytes bytes);
|
static PDFErrorOr<ByteBuffer> decode_crypt(ReadonlyBytes bytes);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue