@@ -9,37 +9,41 @@ namespace YAML {
99template <> struct as_if <ddwaf_object, void > {
1010 explicit as_if (const Node &node_) : node(node_) {}
1111
12- static ddwaf_object yaml_to_object_helper (const Node &node)
12+ static ddwaf_object yaml_to_object_helper (const Node &node, ddwaf_allocator alloc )
1313 {
1414 ddwaf_object arg;
1515 switch (node.Type ()) {
1616 case NodeType::Sequence:
17- ddwaf_object_array (&arg);
17+ ddwaf_object_set_array (&arg, 0 , alloc );
1818 break ;
1919 case NodeType::Map:
20- ddwaf_object_map (&arg);
20+ ddwaf_object_set_map (&arg, 0 , alloc );
2121 break ;
2222 case NodeType::Scalar:
23- ddwaf_object_string (&arg, node.Scalar ().c_str ());
23+ {
24+ auto scalar = node.Scalar ();
25+ ddwaf_object_set_string (&arg, scalar.c_str (), scalar.size (), alloc);
26+ }
2427 break ;
2528 case NodeType::Null:
26- ddwaf_object_null (&arg);
29+ ddwaf_object_set_null (&arg);
2730 break ;
2831 case NodeType::Undefined:
2932 default :
30- ddwaf_object_invalid (&arg);
33+ ddwaf_object_set_invalid (&arg);
3134 break ;
3235 }
3336 return arg;
3437 }
3538
3639 ddwaf_object operator ()() const
3740 {
38- std::list<std::tuple<ddwaf_object &, YAML::Node, YAML::Node::const_iterator>> stack;
41+ auto alloc = ddwaf_get_default_allocator ();
42+ std::list<std::tuple<ddwaf_object *, YAML::Node, YAML::Node::const_iterator>> stack;
3943
40- ddwaf_object root = yaml_to_object_helper (node);
44+ ddwaf_object root = yaml_to_object_helper (node, alloc );
4145 if (root.type == DDWAF_OBJ_MAP || root.type == DDWAF_OBJ_ARRAY) {
42- stack.emplace_back (root, node, node.begin ());
46+ stack.emplace_back (& root, node, node.begin ());
4347 }
4448
4549 while (!stack.empty ()) {
@@ -48,16 +52,18 @@ template <> struct as_if<ddwaf_object, void> {
4852
4953 for (; it != parent_node.end (); ++it) {
5054 YAML::Node child_node = parent_node.IsMap () ? it->second : *it;
51- auto child_obj = yaml_to_object_helper (child_node);
52- if (parent_obj.type == DDWAF_OBJ_MAP) {
55+ auto child_obj = yaml_to_object_helper (child_node, alloc);
56+ ddwaf_object *child_ptr = nullptr ;
57+ if (parent_obj->type == DDWAF_OBJ_MAP) {
5358 auto key = it->first .as <std::string>();
54- ddwaf_object_map_add (&parent_obj, key.c_str (), &child_obj);
55- } else if (parent_obj.type == DDWAF_OBJ_ARRAY) {
56- ddwaf_object_array_add (&parent_obj, &child_obj);
59+ child_ptr = ddwaf_object_insert_key (parent_obj, key.c_str (), key.size (), alloc);
60+ *child_ptr = child_obj;
61+ } else if (parent_obj->type == DDWAF_OBJ_ARRAY) {
62+ child_ptr = ddwaf_object_insert (parent_obj, alloc);
63+ *child_ptr = child_obj;
5764 }
5865
5966 if (child_obj.type == DDWAF_OBJ_MAP || child_obj.type == DDWAF_OBJ_ARRAY) {
60- auto &child_ptr = parent_obj.array [parent_obj.nbEntries - 1 ];
6167 stack.emplace_back (child_ptr, child_node, child_node.begin ());
6268 ++it;
6369 break ;
@@ -83,19 +89,25 @@ YAML::Node object_to_yaml_helper(const ddwaf_object &obj)
8389 YAML::Node output;
8490 switch (obj.type ) {
8591 case DDWAF_OBJ_BOOL:
86- output = obj. boolean ;
92+ output = ddwaf_object_get_bool (& obj) ;
8793 break ;
8894 case DDWAF_OBJ_SIGNED:
89- output = obj. intValue ;
95+ output = ddwaf_object_get_signed (& obj) ;
9096 break ;
9197 case DDWAF_OBJ_UNSIGNED:
92- output = obj. uintValue ;
98+ output = ddwaf_object_get_unsigned (& obj) ;
9399 break ;
94100 case DDWAF_OBJ_FLOAT:
95- output = obj. f64 ;
101+ output = ddwaf_object_get_float (& obj) ;
96102 break ;
97103 case DDWAF_OBJ_STRING:
98- output = std::string{obj.stringValue , obj.nbEntries };
104+ case DDWAF_OBJ_LITERAL_STRING:
105+ case DDWAF_OBJ_SMALL_STRING:
106+ {
107+ size_t length;
108+ const char * str = ddwaf_object_get_string (&obj, &length);
109+ output = std::string{str, length};
110+ }
99111 break ;
100112 case DDWAF_OBJ_MAP:
101113 output = YAML::Load (" {}" );
@@ -126,21 +138,34 @@ YAML::Node object_to_yaml(const ddwaf_object &obj)
126138 auto current_depth = stack.size ();
127139 auto &[parent_obj, parent_node, index] = stack.back ();
128140
129- for (; index < parent_obj.nbEntries ; ++index) {
130- auto &child_obj = parent_obj.array [index];
131- auto child_node = object_to_yaml_helper (child_obj);
132-
141+ size_t size = ddwaf_object_get_size (&parent_obj);
142+ for (; index < size; ++index) {
143+ const ddwaf_object *child_obj = nullptr ;
133144 if (parent_obj.type == DDWAF_OBJ_MAP) {
134- std::string key{child_obj.parameterName , child_obj.parameterNameLength };
145+ child_obj = ddwaf_object_at_value (&parent_obj, index);
146+ auto *key_obj = ddwaf_object_at_key (&parent_obj, index);
147+ size_t key_len;
148+ const char * key_str = ddwaf_object_get_string (key_obj, &key_len);
149+ std::string key{key_str, key_len};
150+
151+ auto child_node = object_to_yaml_helper (*child_obj);
135152 parent_node[key] = child_node;
153+
154+ if (child_obj->type == DDWAF_OBJ_MAP || child_obj->type == DDWAF_OBJ_ARRAY) {
155+ stack.emplace_back (*child_obj, child_node, 0 );
156+ ++index;
157+ break ;
158+ }
136159 } else if (parent_obj.type == DDWAF_OBJ_ARRAY) {
160+ child_obj = ddwaf_object_at_value (&parent_obj, index);
161+ auto child_node = object_to_yaml_helper (*child_obj);
137162 parent_node.push_back (child_node);
138- }
139163
140- if (child_obj.type == DDWAF_OBJ_MAP || child_obj.type == DDWAF_OBJ_ARRAY) {
141- stack.emplace_back (child_obj, child_node, 0 );
142- ++index;
143- break ;
164+ if (child_obj->type == DDWAF_OBJ_MAP || child_obj->type == DDWAF_OBJ_ARRAY) {
165+ stack.emplace_back (*child_obj, child_node, 0 );
166+ ++index;
167+ break ;
168+ }
144169 }
145170 }
146171
@@ -175,29 +200,34 @@ version: "2.1"
175200
176201int main ()
177202{
178- YAML::Node doc = YAML::Load (waf_rule. data (), waf_rule. size () );
203+ auto alloc = ddwaf_get_default_allocator ( );
179204
180- auto rule = doc.as <ddwaf_object>(); // = convert_yaml_to_args(doc);
181- ddwaf_handle handle = ddwaf_init (&rule, nullptr , nullptr );
205+ YAML::Node doc = YAML::Load (waf_rule.data ());
206+
207+ auto rule = doc.as <ddwaf_object>();
208+ ddwaf_handle handle = ddwaf_init (&rule, nullptr );
182209 ddwaf_object_destroy (&rule, alloc);
183210 if (handle == nullptr ) {
184211 return EXIT_FAILURE;
185212 }
186213
187- ddwaf_context context = ddwaf_context_init (handle);
214+ ddwaf_context context = ddwaf_context_init (handle, alloc );
188215 if (context == nullptr ) {
189216 ddwaf_destroy (handle);
190217 return EXIT_FAILURE;
191218 }
192219
193220 ddwaf_object root;
194- ddwaf_object tmp;
195- ddwaf_object_map (&root);
196- ddwaf_object_map_add (&root, " arg1" , ddwaf_object_string (&tmp, " string 1" ));
197- ddwaf_object_map_add (&root, " arg2" , ddwaf_object_string (&tmp, " string 2" ));
221+ ddwaf_object_set_map (&root, 2 , alloc);
222+
223+ ddwaf_object *arg1 = ddwaf_object_insert_literal_key (&root, " arg1" , 4 , alloc);
224+ ddwaf_object_set_string_literal (arg1, " string 1" , 8 );
225+
226+ ddwaf_object *arg2 = ddwaf_object_insert_literal_key (&root, " arg2" , 4 , alloc);
227+ ddwaf_object_set_string_literal (arg2, " string 2" , 8 );
198228
199229 ddwaf_object ret;
200- auto code = ddwaf_context_eval (context, &root, nullptr , &ret, LONG_TIME);
230+ auto code = ddwaf_context_eval (context, &root, alloc , &ret, LONG_TIME);
201231 std::cout << " Output second run: " << code << ' \n ' ;
202232 if (code == DDWAF_MATCH) {
203233 YAML::Emitter out (std::cout);
0 commit comments