Serenity Operating System
1/*
2 * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Debug.h>
10#include <AK/DeprecatedString.h>
11#include <AK/Function.h>
12#include <AK/ScopeGuard.h>
13#include <AK/Span.h>
14#include <AK/Vector.h>
15#include <LibPDF/Error.h>
16
17namespace PDF {
18
19class Reader {
20public:
21 explicit Reader(ReadonlyBytes bytes)
22 : m_bytes(bytes)
23 {
24 }
25
26 ALWAYS_INLINE ReadonlyBytes bytes() const { return m_bytes; }
27 ALWAYS_INLINE size_t offset() const { return m_offset; }
28
29 bool done() const
30 {
31 if (m_forwards)
32 return offset() >= bytes().size();
33 return m_offset < 0;
34 }
35
36 size_t remaining() const
37 {
38 if (done())
39 return 0;
40
41 if (m_forwards)
42 return bytes().size() - offset();
43 return offset() + 1;
44 }
45
46 void move_by(size_t count)
47 {
48 if (m_forwards) {
49 m_offset += static_cast<ssize_t>(count);
50 } else {
51 m_offset -= static_cast<ssize_t>(count);
52 }
53 }
54
55 template<typename T = char>
56 T read()
57 {
58 T value = reinterpret_cast<T const*>(m_bytes.offset(m_offset))[0];
59 move_by(sizeof(T));
60 return value;
61 }
62
63 template<typename T = char>
64 PDFErrorOr<T> try_read()
65 {
66 if (sizeof(T) + m_offset >= m_bytes.size()) {
67 auto message = DeprecatedString::formatted("Cannot read {} bytes at offset {} of ReadonlyBytes of size {}", sizeof(T), m_offset, m_bytes.size());
68 return Error { Error::Type::Parse, message };
69 }
70 return read<T>();
71 }
72
73 char peek(size_t shift = 0) const
74 {
75 auto offset = m_offset + shift * (m_forwards ? 1 : -1);
76 return static_cast<char>(m_bytes.at(offset));
77 }
78
79 template<typename... T>
80 bool matches_any(T... elements) const
81 {
82 if (done())
83 return false;
84 auto ch = peek();
85 return ((ch == elements) || ...);
86 }
87
88 bool matches(char ch) const
89 {
90 return !done() && peek() == ch;
91 }
92
93 bool matches(char const* chars) const
94 {
95 DeprecatedString string(chars);
96 if (remaining() < string.length())
97 return false;
98
99 if (!m_forwards)
100 string = string.reverse();
101
102 for (size_t i = 0; i < string.length(); i++) {
103 if (peek(i) != string[i])
104 return false;
105 }
106
107 return true;
108 }
109
110 template<typename T = char>
111 void move_to(size_t offset)
112 {
113 VERIFY(offset < m_bytes.size());
114 m_offset = static_cast<ssize_t>(offset);
115 }
116
117 void move_until(char ch)
118 {
119 while (!done() && peek() != ch)
120 move_by(1);
121 }
122
123 void move_until(Function<bool(char)> predicate)
124 {
125 while (!done() && !predicate(peek()))
126 move_by(1);
127 }
128
129 ALWAYS_INLINE void move_while(Function<bool(char)> predicate)
130 {
131 move_until([&predicate](char t) { return !predicate(t); });
132 }
133
134 bool matches_eol() const;
135 bool matches_whitespace() const;
136 bool matches_number() const;
137 bool matches_delimiter() const;
138 bool matches_regular_character() const;
139
140 bool consume_eol();
141 bool consume_whitespace();
142 char consume();
143 void consume(int amount);
144 bool consume(char);
145
146 ALWAYS_INLINE void set_reading_forwards() { m_forwards = true; }
147 ALWAYS_INLINE void set_reading_backwards() { m_forwards = false; }
148
149 ALWAYS_INLINE void save() { m_saved_offsets.append(m_offset); }
150 ALWAYS_INLINE void load() { m_offset = m_saved_offsets.take_last(); }
151 ALWAYS_INLINE void discard() { m_saved_offsets.take_last(); }
152
153#ifdef PDF_DEBUG
154 void dump_state() const
155 {
156 dbgln("Reader State (offset={} size={})", offset(), bytes().size());
157
158 size_t from = max(0, static_cast<int>(offset()) - 10);
159 size_t to = min(bytes().size() - 1, offset() + 10);
160
161 for (auto i = from; i <= to; i++) {
162 char value = static_cast<char>(bytes().at(i));
163 auto line = DeprecatedString::formatted(" {}: '{}' (value={:3d}) ", i, value, static_cast<u8>(value));
164 if (i == offset()) {
165 dbgln("{} <<< current location, forwards={}", line, m_forwards);
166 } else {
167 dbgln("{}", line);
168 }
169 }
170 dbgln();
171 }
172#endif
173
174private:
175 ReadonlyBytes m_bytes;
176 ssize_t m_offset { 0 };
177 Vector<ssize_t> m_saved_offsets;
178 bool m_forwards { true };
179};
180
181}