summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF/Parser.h
blob: f1205af6cc50c0026c568b57e07394d36e295c08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/SourceLocation.h>
#include <AK/WeakPtr.h>
#include <LibPDF/Object.h>
#include <LibPDF/Operator.h>
#include <LibPDF/Reader.h>
#include <LibPDF/XRefTable.h>

namespace PDF {

template<typename T, typename... Args>
static NonnullRefPtr<T> make_object(Args... args)
requires(IsBaseOf<Object, T>)
{
    return adopt_ref(*new T(forward<Args>(args)...));
}

class Document;

class Parser {
public:
    static PDFErrorOr<Vector<Operator>> parse_operators(Document*, ReadonlyBytes);

    Parser(ReadonlyBytes);
    Parser(Document*, ReadonlyBytes);

    void set_document(WeakPtr<Document> const&);

    DeprecatedString parse_comment();

    void move_by(size_t count) { m_reader.move_by(count); }
    void move_to(size_t offset) { m_reader.move_to(offset); }

    enum class CanBeIndirectValue {
        No,
        Yes
    };

    PDFErrorOr<Value> parse_value(CanBeIndirectValue = CanBeIndirectValue::Yes);
    PDFErrorOr<Value> parse_possible_indirect_value_or_ref();
    PDFErrorOr<NonnullRefPtr<IndirectValue>> parse_indirect_value(u32 index, u32 generation);
    PDFErrorOr<NonnullRefPtr<IndirectValue>> parse_indirect_value();
    PDFErrorOr<Value> parse_number();
    PDFErrorOr<NonnullRefPtr<NameObject>> parse_name();
    NonnullRefPtr<StringObject> parse_string();
    DeprecatedString parse_literal_string();
    DeprecatedString parse_hex_string();
    PDFErrorOr<NonnullRefPtr<ArrayObject>> parse_array();
    PDFErrorOr<NonnullRefPtr<DictObject>> parse_dict();
    PDFErrorOr<NonnullRefPtr<StreamObject>> parse_stream(NonnullRefPtr<DictObject> dict);
    PDFErrorOr<Vector<Operator>> parse_operators();

protected:
    void push_reference(Reference const& ref) { m_current_reference_stack.append(ref); }
    void pop_reference() { m_current_reference_stack.take_last(); }

    Error error(
        DeprecatedString const& message
#ifdef PDF_DEBUG
        ,
        SourceLocation loc = SourceLocation::current()
#endif
    ) const;

    Reader m_reader;
    WeakPtr<Document> m_document;
    Vector<Reference> m_current_reference_stack;
    bool m_disable_encryption { false };
};

};