summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2021-09-18 17:15:14 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-09-20 17:39:36 +0430
commitd344253b086b14fd712736db5a9408e56198c418 (patch)
tree01ec6f4e44fb4fe05e4232a404e67dac71a33c89 /Userland/Libraries/LibPDF
parentda170997d5754880255e2527a861aed304ed76ee (diff)
downloadserenity-d344253b086b14fd712736db5a9408e56198c418.zip
LibPDF: Extract reference bitpacking into dedicated class
Diffstat (limited to 'Userland/Libraries/LibPDF')
-rw-r--r--Userland/Libraries/LibPDF/Parser.cpp2
-rw-r--r--Userland/Libraries/LibPDF/Reference.h45
-rw-r--r--Userland/Libraries/LibPDF/Value.h21
3 files changed, 52 insertions, 16 deletions
diff --git a/Userland/Libraries/LibPDF/Parser.cpp b/Userland/Libraries/LibPDF/Parser.cpp
index 3892576c4c..d0c54afb2c 100644
--- a/Userland/Libraries/LibPDF/Parser.cpp
+++ b/Userland/Libraries/LibPDF/Parser.cpp
@@ -660,7 +660,7 @@ Value Parser::parse_possible_indirect_value_or_ref()
m_reader.discard();
consume();
consume_whitespace();
- return Value(first_number.as_int(), second_number.as_int());
+ return Value(Reference(first_number.as_int(), second_number.as_int()));
}
if (m_reader.matches("obj")) {
diff --git a/Userland/Libraries/LibPDF/Reference.h b/Userland/Libraries/LibPDF/Reference.h
new file mode 100644
index 0000000000..d1765cc3ec
--- /dev/null
+++ b/Userland/Libraries/LibPDF/Reference.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
+ * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Types.h>
+
+namespace PDF {
+
+class Reference {
+ // We store refs as u32, with 18 bits for the index and 14 bits for the
+ // generation index. The generation index is stored in the higher bits.
+ // This may need to be rethought later, as the max generation index is
+ // 2^16 and the max for the object index is probably 2^32 (I don't know
+ // exactly)
+ static constexpr auto MAX_REF_INDEX = (1 << 19) - 1; // 2 ^ 19 - 1
+ static constexpr auto MAX_REF_GENERATION_INDEX = (1 << 15) - 1; // 2 ^ 15 - 1
+
+public:
+ Reference(u32 index, u32 generation_index)
+ {
+ VERIFY(index < MAX_REF_INDEX);
+ VERIFY(generation_index < MAX_REF_GENERATION_INDEX);
+ m_combined = (generation_index << 14) | index;
+ }
+
+ [[nodiscard]] ALWAYS_INLINE u32 as_ref_index() const
+ {
+ return m_combined & 0x3ffff;
+ }
+
+ [[nodiscard]] ALWAYS_INLINE u32 as_ref_generation_index() const
+ {
+ return m_combined >> 18;
+ }
+
+private:
+ u32 m_combined;
+};
+
+}
diff --git a/Userland/Libraries/LibPDF/Value.h b/Userland/Libraries/LibPDF/Value.h
index 2f6e0b1b63..e0e3e740c0 100644
--- a/Userland/Libraries/LibPDF/Value.h
+++ b/Userland/Libraries/LibPDF/Value.h
@@ -11,19 +11,12 @@
#include <AK/String.h>
#include <LibPDF/Forward.h>
#include <LibPDF/Object.h>
+#include <LibPDF/Reference.h>
namespace PDF {
class Value {
public:
- // We store refs as u32, with 18 bits for the index and 14 bits for the
- // generation index. The generation index is stored in the higher bits.
- // This may need to be rethought later, as the max generation index is
- // 2^16 and the max for the object index is probably 2^32 (I don't know
- // exactly)
- static constexpr auto max_ref_index = (1 << 19) - 1; // 2 ^ 19 - 1
- static constexpr auto max_ref_generation_index = (1 << 15) - 1; // 2 ^ 15 - 1
-
Value()
: m_type(Type::Empty)
{
@@ -55,12 +48,10 @@ public:
m_as_float = f;
}
- Value(u32 index, u32 generation_index)
+ Value(Reference ref)
: m_type(Type::Ref)
{
- VERIFY(index < max_ref_index);
- VERIFY(generation_index < max_ref_generation_index);
- m_as_ref = (generation_index << 14) | index;
+ m_as_ref = ref;
}
template<IsObject T>
@@ -148,13 +139,13 @@ public:
[[nodiscard]] ALWAYS_INLINE u32 as_ref_index() const
{
VERIFY(is_ref());
- return m_as_ref & 0x3ffff;
+ return m_as_ref.as_ref_index();
}
[[nodiscard]] ALWAYS_INLINE u32 as_ref_generation_index() const
{
VERIFY(is_ref());
- return m_as_ref >> 18;
+ return m_as_ref.as_ref_generation_index();
}
[[nodiscard]] ALWAYS_INLINE NonnullRefPtr<Object> as_object() const { return *m_as_object; }
@@ -177,7 +168,7 @@ private:
union {
bool m_as_bool;
int m_as_int;
- u32 m_as_ref;
+ Reference m_as_ref;
float m_as_float;
};