Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions include/ddwaf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions libddwaf.def
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(length), ddwaf_get_default_allocator());
}

ddwaf_object *ddwaf_object_set_string(
ddwaf_object *object, const char *string, uint32_t length, ddwaf_allocator alloc)
{
Expand Down Expand Up @@ -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<uint32_t>(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) {
Expand Down
Loading