From c660341f900d5ca9944248b6a8c6599341b77a6f Mon Sep 17 00:00:00 2001 From: offtkp Date: Fri, 1 Nov 2024 19:22:16 +0200 Subject: [PATCH] assembler: Allow cursor to be moved forwards --- include/biscuit/assembler.hpp | 12 ++++++++++++ include/biscuit/code_buffer.hpp | 24 ++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/include/biscuit/assembler.hpp b/include/biscuit/assembler.hpp index 30db402..069656a 100644 --- a/include/biscuit/assembler.hpp +++ b/include/biscuit/assembler.hpp @@ -112,6 +112,18 @@ class Assembler { m_buffer.RewindCursor(offset); } + /** + * Allows advancing of the code buffer cursor. + * + * @param offset The offset to advance the cursor by. + * + * @note The offset may not be smaller than the current cursor offset + * and may not be larger than the current buffer capacity. + */ + void AdvanceBuffer(ptrdiff_t offset) { + m_buffer.AdvanceCursor(offset); + } + /// Retrieves the cursor pointer for the underlying code buffer. [[nodiscard]] uint8_t* GetCursorPointer() noexcept { return m_buffer.GetCursorPointer(); diff --git a/include/biscuit/code_buffer.hpp b/include/biscuit/code_buffer.hpp index 46314e4..9998236 100644 --- a/include/biscuit/code_buffer.hpp +++ b/include/biscuit/code_buffer.hpp @@ -88,14 +88,16 @@ class CodeBuffer { /// Retrieves the pointer to an arbitrary location within the buffer. [[nodiscard]] uint8_t* GetOffsetPointer(ptrdiff_t offset) noexcept { - BISCUIT_ASSERT(offset >= 0 && offset <= GetCursorOffset()); - return m_buffer + offset; + auto pointer = m_buffer + offset; + BISCUIT_ASSERT(pointer >= m_buffer && pointer < m_buffer + m_capacity); + return pointer; } /// Retrieves the pointer to an arbitrary location within the buffer. [[nodiscard]] const uint8_t* GetOffsetPointer(ptrdiff_t offset) const noexcept { - BISCUIT_ASSERT(offset >= 0 && offset <= GetCursorOffset()); - return m_buffer + offset; + auto pointer = m_buffer + offset; + BISCUIT_ASSERT(pointer >= m_buffer && pointer < m_buffer + m_capacity); + return pointer; } /** @@ -115,6 +117,20 @@ class CodeBuffer { m_cursor = rewound; } + /** + * Allows advancing of the code buffer cursor. + * + * @param offset The offset to advance the cursor by. + * + * @note The offset may not be smaller than the current cursor offset + * and may not be larger than the current buffer capacity. + */ + void AdvanceCursor(ptrdiff_t offset) noexcept { + auto* forward = m_buffer + offset; + BISCUIT_ASSERT(m_cursor <= forward && forward < m_buffer + m_capacity); + m_cursor = forward; + } + /** * Whether or not the underlying buffer has enough room for the * given number of bytes.