diff --git a/src/condition/scalar_condition.cpp b/src/condition/scalar_condition.cpp index 8c419e60..00ce131a 100644 --- a/src/condition/scalar_condition.cpp +++ b/src/condition/scalar_condition.cpp @@ -20,6 +20,7 @@ #include "log.hpp" #include "matcher/base.hpp" #include "object_store.hpp" +#include "object_type.hpp" #include "scalar_condition.hpp" #include "transformer/base.hpp" #include "transformer/manager.hpp" diff --git a/src/iterator.cpp b/src/iterator.cpp index 850f1b10..9d141da7 100644 --- a/src/iterator.cpp +++ b/src/iterator.cpp @@ -8,10 +8,13 @@ #include #include #include +#include #include #include "exclusion/common.hpp" #include "iterator.hpp" +#include "object_type.hpp" +#include "object_view.hpp" #include "utils.hpp" namespace ddwaf { @@ -100,7 +103,8 @@ void value_iterator::initialise_cursor(object_view obj, const std::span 0) { - stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0}); + const stack_node node{.key = {}, .value = obj, .index = 0}; + stack_.emplace_back(node); set_cursor_to_next_object(); } } else { @@ -121,7 +125,8 @@ void value_iterator::initialise_cursor_with_path( } // Add container to stack and find next scalar within the given path - stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0}); + const stack_node node{.key = {}, .value = obj, .index = 0}; + stack_.emplace_back(node); for (std::size_t i = 0; i < path.size(); i++) { const std::string_view key = path[i]; @@ -238,7 +243,8 @@ void key_iterator::initialise_cursor(object_view obj, const std::span 0) { - stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0}); + const stack_node node{.key = {}, .value = obj, .index = 0}; + stack_.emplace_back(node); set_cursor_to_next_object(); } } else { @@ -254,7 +260,8 @@ void key_iterator::initialise_cursor_with_path( } // Add container to stack and find next scalar within the given path - stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0}); + const stack_node node{.key = {}, .value = obj, .index = 0}; + stack_.emplace_back(node); for (std::size_t i = 0; i < path.size(); i++) { const std::string_view key = path[i]; @@ -373,7 +380,8 @@ void kv_iterator::initialise_cursor(object_view obj, const std::span 0) { - stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0}); + const stack_node node{.key = {}, .value = obj, .index = 0}; + stack_.emplace_back(node); set_cursor_to_next_object(); } } else { @@ -389,7 +397,8 @@ void kv_iterator::initialise_cursor_with_path( } // Add container to stack and find next scalar within the given path - stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0}); + const stack_node node{.key = {}, .value = obj, .index = 0}; + stack_.emplace_back(node); for (std::size_t i = 0; i < path.size(); i++) { const std::string_view key = path[i]; diff --git a/src/iterator.hpp b/src/iterator.hpp index f442d6fc..77ba7b1d 100644 --- a/src/iterator.hpp +++ b/src/iterator.hpp @@ -14,6 +14,7 @@ #include "context_allocator.hpp" #include "exclusion/common.hpp" +#include "object_type.hpp" #include "object_view.hpp" #include "utils.hpp" diff --git a/src/object_view.hpp b/src/object_view.hpp index 7b86b789..5dead87a 100644 --- a/src/object_view.hpp +++ b/src/object_view.hpp @@ -19,95 +19,6 @@ namespace ddwaf { namespace detail { using object = ddwaf_object; -/*struct object*/ -/*{*/ -/*const char* parameterName;*/ -/*uint64_t parameterNameLength;*/ -/*// uintValue should be at least as wide as the widest type on the platform.*/ -/*union*/ -/*{*/ -/*const char* stringValue;*/ -/*uint64_t uintValue;*/ -/*int64_t intValue;*/ -/*object* array;*/ -/*bool boolean;*/ -/*double f64;*/ -/*};*/ -/*uint64_t nbEntries;*/ -/*object_type type;*/ -/*};*/ - -struct object_iterator { - const object *ptr; - std::size_t index; - std::size_t size; - DDWAF_OBJ_TYPE type; - // 1 byte of padding to spare - - // NOLINTNEXTLINE(bugprone-easily-swappable-parameters) - static object_iterator construct( - const detail::object *obj, std::size_t index, std::size_t max_size) - { - if (obj->type == DDWAF_OBJ_MAP || obj->type == DDWAF_OBJ_ARRAY) { - return { - .ptr = obj->array, - .index = index < obj->nbEntries ? index : static_cast(obj->nbEntries), - .size = - obj->nbEntries < max_size ? static_cast(obj->nbEntries) : max_size, - .type = obj->type, - }; - } - - return { - .ptr = nullptr, - .index = 0, - .size = 0, - .type = DDWAF_OBJ_INVALID, - }; - } - - object_iterator &operator++() noexcept - { - if (index < size) { - ++index; - } - return *this; - } - - std::pair operator*() const noexcept - { - if (index < size) { - const auto &slot = ptr[index]; - std::string_view key; - if (type == DDWAF_OBJ_MAP) { - key = {slot.parameterName, static_cast(slot.parameterNameLength)}; - } - - return {key, &slot}; - } - - [[unlikely]] return {}; - } - - [[nodiscard]] bool is_valid() const noexcept { return index < size; } - - [[nodiscard]] std::string_view key() const noexcept - { - if (type != DDWAF_OBJ_MAP || index >= size) { - [[unlikely]] return {}; - } - const auto &slot = ptr[index]; - return {slot.parameterName, static_cast(slot.parameterNameLength)}; - } - - [[nodiscard]] const object *value() const noexcept - { - if (index >= size) { - [[unlikely]] return nullptr; - } - return &ptr[index]; - } -}; } // namespace detail template struct converter; @@ -130,26 +41,20 @@ class object_view { [[nodiscard]] object_type type() const noexcept { - if (obj_ == nullptr) { - [[unlikely]] return object_type::invalid; - } - return static_cast(obj_->type); + return obj_ != nullptr ? static_cast(obj_->type) : object_type::invalid; } [[nodiscard]] std::size_t size() const noexcept { - if (!is_container()) { - [[unlikely]] return 0; - } - return static_cast(obj_->nbEntries); + return obj_ != nullptr ? static_cast(obj_->nbEntries) : 0; } [[nodiscard]] std::size_t capacity() const noexcept { - if (!is_container()) { - [[unlikely]] return 0; - } - return static_cast(obj_->nbEntries); + return obj_ != nullptr ? static_cast(obj_->nbEntries) : 0; + } + [[nodiscard]] bool empty() const noexcept + { + return obj_ != nullptr ? obj_->nbEntries == 0 : true; } - [[nodiscard]] bool empty() const noexcept { return size() == 0; } template [[nodiscard]] bool is() const noexcept { return is_compatible_type(type()); @@ -172,7 +77,7 @@ class object_view { [[nodiscard]] std::pair at(std::size_t index) const noexcept { - if (!is_container() || index > size()) { + if (!is_container() || index > static_cast(obj_->nbEntries)) { [[unlikely]] return {}; } return at_unchecked(index); @@ -187,7 +92,6 @@ class object_view { slot.parameterName, static_cast(slot.parameterNameLength)}; return {key, object_view{&slot}}; } - return {{}, object_view{&slot}}; } @@ -588,6 +492,7 @@ class object_view { }; } // namespace ddwaf + namespace std { template <> struct hash {