From 6111a9f9d0ba462e233f739ce949aba6d4c01503 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 10 Jul 2023 11:02:36 -0400 Subject: [PATCH] LibPDF: Make Reference store two u32s instead of one Reference used to be clever and stored the index of a ref in 18 bits and the generation in 14 bits, so that both fit into a single u32. However: - It set MAX_REF_INDEX incorrectly (the max value of an 18-bit number is `(1 << 18) - 1`, not `(1 << 19) - 1` - pdf_reference_1-7.pdf has 349223 objects, and that's larger than `(1 << 18) - 1` (which is 262143) Since a Reference is stored in Value which is a Variant that also stores a pointer, the size of Value is already 64-bit. So just don't be clever here. Makes pdf_reference_1-7.pdf get a bit further during decryption. --- Userland/Libraries/LibPDF/Reference.h | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibPDF/Reference.h b/Userland/Libraries/LibPDF/Reference.h index d1765cc3ec..79c058fdf4 100644 --- a/Userland/Libraries/LibPDF/Reference.h +++ b/Userland/Libraries/LibPDF/Reference.h @@ -12,34 +12,26 @@ 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) + : m_ref_index(index) + , m_generation_index(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; + return m_ref_index; } [[nodiscard]] ALWAYS_INLINE u32 as_ref_generation_index() const { - return m_combined >> 18; + return m_generation_index; } private: - u32 m_combined; + u32 m_ref_index; + u32 m_generation_index; }; }