From 448a8b8efb95f653460f150e676bb6b43d5e6af0 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Sun, 20 Nov 2022 20:40:50 -0600 Subject: [PATCH] LibVideo/VP9: Create Vector2DView to limit writable ranges of contexts --- Userland/Libraries/LibVideo/VP9/Context.h | 62 ++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibVideo/VP9/Context.h b/Userland/Libraries/LibVideo/VP9/Context.h index 51053f0d75..7443a96d4b 100644 --- a/Userland/Libraries/LibVideo/VP9/Context.h +++ b/Userland/Libraries/LibVideo/VP9/Context.h @@ -42,6 +42,59 @@ struct Pair { typedef Pair ReferenceFramePair; typedef Pair MotionVectorPair; +template +class Vector2D; + +template +class Vector2DView { +public: + u32 top() const { return m_top; } + u32 left() const { return m_left; } + u32 height() const { return m_height; } + u32 width() const { return m_width; } + + T const& operator[](size_t index) const { return m_storage[index]; } + size_t size() const { return m_storage->size(); } + + T& at(u32 relative_row, u32 relative_column) + { + VERIFY(relative_row < height()); + VERIFY(relative_column < width()); + return m_storage->at(top() + relative_row, left() + relative_column); + } + T const& at(u32 relative_row, u32 relative_column) const + { + VERIFY(relative_row < height()); + VERIFY(relative_column < width()); + return m_storage->at(top() + relative_row, left() + relative_column); + } + + Vector2DView view(u32 top, u32 left, u32 height, u32 width) + { + VERIFY(top + height <= this->height()); + VERIFY(left + width <= this->width()); + return Vector2DView(m_storage, this->top() + top, this->left() + left, height, width); + } + +private: + friend class Vector2D; + + Vector2DView(Vector2D* const storage, u32 top, u32 left, u32 height, u32 width) + : m_storage(storage) + , m_top(top) + , m_left(left) + , m_height(height) + , m_width(width) + { + } + + Vector2D* const m_storage; + u32 const m_top { 0 }; + u32 const m_left { 0 }; + u32 const m_height { 0 }; + u32 const m_width { 0 }; +}; + template class Vector2D { public: @@ -72,7 +125,7 @@ public: u32 height() const { return m_height; } u32 width() const { return m_width; } - + size_t index_at(u32 row, u32 column) const { VERIFY(row < height()); @@ -126,6 +179,13 @@ public: m_storage[i] = T(); } + Vector2DView view(u32 top, u32 left, u32 height, u32 width) + { + VERIFY(top + height <= this->height()); + VERIFY(left + width <= this->width()); + return Vector2DView(this, top, left, height, width); + } + private: u32 m_height { 0 }; u32 m_width { 0 };