2021-04-30 18:33:13 -07:00
|
|
|
/*
|
2022-03-04 19:53:38 -07:00
|
|
|
* Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
|
2021-04-30 18:33:13 -07:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-03-05 17:30:55 -07:00
|
|
|
#include <AK/SourceLocation.h>
|
2021-11-28 11:52:48 +01:00
|
|
|
#include <AK/WeakPtr.h>
|
2021-04-30 18:33:13 -07:00
|
|
|
#include <LibPDF/Object.h>
|
2022-03-25 15:00:11 -07:00
|
|
|
#include <LibPDF/Operator.h>
|
2021-04-30 18:33:13 -07:00
|
|
|
#include <LibPDF/Reader.h>
|
|
|
|
|
#include <LibPDF/XRefTable.h>
|
|
|
|
|
|
|
|
|
|
namespace PDF {
|
|
|
|
|
|
2022-08-15 11:45:24 +02:00
|
|
|
template<typename T, typename... Args>
|
2022-10-17 00:06:11 +02:00
|
|
|
static NonnullRefPtr<T> make_object(Args... args)
|
|
|
|
|
requires(IsBaseOf<Object, T>)
|
2022-08-15 11:45:24 +02:00
|
|
|
{
|
|
|
|
|
return adopt_ref(*new T(forward<Args>(args)...));
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 18:33:13 -07:00
|
|
|
class Document;
|
|
|
|
|
|
2022-08-15 11:45:24 +02:00
|
|
|
class Parser {
|
2021-04-30 18:33:13 -07:00
|
|
|
public:
|
2022-03-25 15:00:11 -07:00
|
|
|
static PDFErrorOr<Vector<Operator>> parse_operators(Document*, ReadonlyBytes);
|
2021-05-10 10:39:19 -07:00
|
|
|
|
2022-08-15 11:45:24 +02:00
|
|
|
Parser(ReadonlyBytes);
|
2022-03-22 19:22:45 -07:00
|
|
|
Parser(Document*, ReadonlyBytes);
|
2021-04-30 18:33:13 -07:00
|
|
|
|
2021-11-28 11:52:48 +01:00
|
|
|
void set_document(WeakPtr<Document> const&);
|
2021-05-10 10:33:32 -07:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString parse_comment();
|
2021-04-30 18:33:13 -07:00
|
|
|
|
2022-08-15 13:34:08 +02:00
|
|
|
void move_by(size_t count) { m_reader.move_by(count); }
|
|
|
|
|
void move_to(size_t offset) { m_reader.move_to(offset); }
|
|
|
|
|
|
2022-08-20 09:00:51 +02:00
|
|
|
enum class CanBeIndirectValue {
|
|
|
|
|
No,
|
|
|
|
|
Yes
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PDFErrorOr<Value> parse_value(CanBeIndirectValue = CanBeIndirectValue::Yes);
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<Value> parse_possible_indirect_value_or_ref();
|
2022-03-21 11:26:31 -07:00
|
|
|
PDFErrorOr<NonnullRefPtr<IndirectValue>> parse_indirect_value(u32 index, u32 generation);
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<NonnullRefPtr<IndirectValue>> parse_indirect_value();
|
|
|
|
|
PDFErrorOr<Value> parse_number();
|
|
|
|
|
PDFErrorOr<NonnullRefPtr<NameObject>> parse_name();
|
2023-10-25 00:02:55 -07:00
|
|
|
PDFErrorOr<NonnullRefPtr<StringObject>> parse_string();
|
|
|
|
|
PDFErrorOr<DeprecatedString> parse_literal_string();
|
|
|
|
|
PDFErrorOr<DeprecatedString> parse_hex_string();
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<NonnullRefPtr<ArrayObject>> parse_array();
|
2023-12-09 22:06:51 -05:00
|
|
|
PDFErrorOr<HashMap<DeprecatedFlyString, Value>> parse_dict_contents_until(char const*);
|
2022-03-05 17:30:55 -07:00
|
|
|
PDFErrorOr<NonnullRefPtr<DictObject>> parse_dict();
|
|
|
|
|
PDFErrorOr<NonnullRefPtr<StreamObject>> parse_stream(NonnullRefPtr<DictObject> dict);
|
2022-03-25 15:00:11 -07:00
|
|
|
PDFErrorOr<Vector<Operator>> parse_operators();
|
2021-05-10 10:39:19 -07:00
|
|
|
|
2023-07-10 15:49:48 -04:00
|
|
|
void set_filters_enabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
m_enable_filters = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-11 12:02:16 -04:00
|
|
|
void set_encryption_enabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
m_enable_encryption = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-21 11:26:31 -07:00
|
|
|
void push_reference(Reference const& ref) { m_current_reference_stack.append(ref); }
|
|
|
|
|
void pop_reference() { m_current_reference_stack.take_last(); }
|
|
|
|
|
|
2023-07-11 12:02:16 -04:00
|
|
|
protected:
|
2022-03-05 17:30:55 -07:00
|
|
|
Error error(
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString const& message
|
2022-03-05 17:30:55 -07:00
|
|
|
#ifdef PDF_DEBUG
|
|
|
|
|
,
|
|
|
|
|
SourceLocation loc = SourceLocation::current()
|
|
|
|
|
#endif
|
|
|
|
|
) const;
|
|
|
|
|
|
2021-04-30 18:33:13 -07:00
|
|
|
Reader m_reader;
|
2021-11-28 11:52:48 +01:00
|
|
|
WeakPtr<Document> m_document;
|
2022-03-21 11:26:31 -07:00
|
|
|
Vector<Reference> m_current_reference_stack;
|
2023-07-11 10:16:16 -04:00
|
|
|
bool m_enable_encryption { true };
|
2023-10-20 22:39:36 -04:00
|
|
|
bool m_enable_filters { true };
|
2021-04-30 18:33:13 -07:00
|
|
|
};
|
|
|
|
|
|
2021-05-26 22:52:05 -07:00
|
|
|
};
|