1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

LibPDF: Don't treat a broken document header as a fatal error

As the current goal is to make our best effort loading documents, we
might as well ignore a broken header and power through, giving the user
a warning.
This commit is contained in:
Julian Offenhäuser 2023-02-21 23:41:23 +01:00 committed by Andreas Kling
parent f66de973ff
commit 6c0f7d83bb

View file

@ -23,7 +23,15 @@ DocumentParser::DocumentParser(Document* document, ReadonlyBytes bytes)
PDFErrorOr<void> DocumentParser::initialize()
{
TRY(parse_header());
m_reader.set_reading_forwards();
if (m_reader.remaining() == 0)
return error("Empty PDF document");
auto maybe_error = parse_header();
if (maybe_error.is_error()) {
warnln("{}", maybe_error.error().message());
warnln("No valid PDF header detected, continuing anyway.");
}
auto const linearization_result = TRY(initialize_linearization_dict());
@ -68,10 +76,6 @@ PDFErrorOr<Value> DocumentParser::parse_object_with_index(u32 index)
PDFErrorOr<void> DocumentParser::parse_header()
{
// FIXME: Do something with the version?
m_reader.set_reading_forwards();
if (m_reader.remaining() == 0)
return error("Empty PDF document");
m_reader.move_to(0);
if (m_reader.remaining() < 8 || !m_reader.matches("%PDF-"))
return error("Not a PDF document");