From 8dd1609b1d67c57498a25c445189e9ac2a4bc2ed Mon Sep 17 00:00:00 2001 From: Thomas Kowalski Date: Mon, 18 May 2026 15:28:46 +0200 Subject: [PATCH] Add ddwaf_object_stringl and ddwaf_object_map_addl --- include/ddwaf.h | 25 +++++++++++++++++++++++++ libddwaf.def | 2 ++ src/interface.cpp | 21 +++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/include/ddwaf.h b/include/ddwaf.h index 043d21d29..0a155c39e 100644 --- a/include/ddwaf.h +++ b/include/ddwaf.h @@ -673,6 +673,17 @@ ddwaf_object* ddwaf_object_set_null(ddwaf_object *object); **/ ddwaf_object* ddwaf_object_set_string(ddwaf_object *object, const char *string, uint32_t length, ddwaf_allocator alloc); +/** + * Backward-compatible wrapper: creates a string object using the default allocator. + * + * @param object Object to perform the operation on. (nonnull) + * @param string String to initialise the object with, this string will be copied. (nonnull) + * @param length Length of the string. + * + * @return A pointer to the passed object or NULL if the operation failed. + **/ +ddwaf_object* ddwaf_object_stringl(ddwaf_object *object, const char *string, uint64_t length); + /** * Creates an object from a literal string and its length. * @@ -788,6 +799,20 @@ ddwaf_object *ddwaf_object_insert(ddwaf_object *array, ddwaf_allocator alloc); **/ ddwaf_object *ddwaf_object_insert_key(ddwaf_object *map, const char *key, uint32_t length, ddwaf_allocator alloc); +/** + * Backward-compatible wrapper: inserts a pre-built object into a map using the + * default allocator. The value is moved into the map and the source object is + * invalidated. + * + * @param map Map in which to insert the object. (nonnull) + * @param key The key for indexing purposes, this string will be copied. (nonnull) + * @param key_length Length of the key. + * @param object Object to move into the map. (nonnull) + * + * @return true if the operation succeeded, false otherwise. + **/ +bool ddwaf_object_map_addl(ddwaf_object *map, const char *key, uint64_t key_length, ddwaf_object *object); + /** * Inserts a new object into a map object, using a literal key. * diff --git a/libddwaf.def b/libddwaf.def index aff52982c..48ff02d18 100644 --- a/libddwaf.def +++ b/libddwaf.def @@ -34,12 +34,14 @@ EXPORTS ddwaf_object_set_unsigned ddwaf_object_set_float ddwaf_object_set_string + ddwaf_object_stringl ddwaf_object_set_string_literal ddwaf_object_set_array ddwaf_object_set_map ddwaf_object_from_json ddwaf_object_insert ddwaf_object_insert_key + ddwaf_object_map_addl ddwaf_object_insert_key_nocopy ddwaf_object_insert_literal_key ddwaf_object_get_type diff --git a/src/interface.cpp b/src/interface.cpp index cd773fdce..c2da196e2 100644 --- a/src/interface.cpp +++ b/src/interface.cpp @@ -630,6 +630,12 @@ ddwaf_object *ddwaf_object_set_null(ddwaf_object *object) ddwaf_object *ddwaf_object_null(ddwaf_object *object) { return ddwaf_object_set_null(object); } +ddwaf_object *ddwaf_object_stringl(ddwaf_object *object, const char *string, uint64_t length) +{ + return ddwaf_object_set_string( + object, string, static_cast(length), ddwaf_get_default_allocator()); +} + ddwaf_object *ddwaf_object_set_string( ddwaf_object *object, const char *string, uint32_t length, ddwaf_allocator alloc) { @@ -854,6 +860,21 @@ ddwaf_object *ddwaf_object_insert_literal_key( return nullptr; } +bool ddwaf_object_map_addl( + ddwaf_object *map, const char *key, uint64_t key_length, ddwaf_object *object) +{ + ddwaf_allocator alloc = ddwaf_get_default_allocator(); + ddwaf_object *slot = + ddwaf_object_insert_key(map, key, static_cast(key_length), alloc); + if (slot == nullptr) { + return false; + } + // Move the value into the map slot and invalidate the source + std::memcpy(slot, object, sizeof(ddwaf_object)); + std::memset(object, 0, sizeof(ddwaf_object)); + return true; +} + void ddwaf_object_destroy(ddwaf_object *object, ddwaf_allocator alloc) { if (object == nullptr || alloc == nullptr) {