]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add fr_value_box_memcpy_in()
authorAlan T. DeKok <aland@freeradius.org>
Thu, 29 Sep 2022 13:34:38 +0000 (09:34 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 29 Sep 2022 22:09:34 +0000 (18:09 -0400)
which mirrors fr_value_box_memcpy_out()

src/lib/util/value.h

index e1c32f6e332f63620da0db3cfca61cd93e81edef..413a523e40c6c32f23d3260b08fcdb8204df3356 100644 (file)
@@ -601,6 +601,40 @@ int fr_value_box_memcpy_out(void *out, fr_value_box_t const *vb)
        return 0;
 }
 
+/** Copy a C value value to a value box.
+ *
+ * This is useful when interacting with 3rd party libraries, and doing configuration parsing
+ * as it allows us to use standard parsing and casting functions and then emit the result
+ * as a C value.
+ *
+ * The field pointed to by in must be of the same type as we use to represent the value boxe's
+ * value in its datum union, or at least the same size.
+ *
+ * No checks are done to ensure this is the case, so if you get this wrong it'll lead to silent
+ * memory corruption.
+ *
+ * @param[in] vb       destination value box, MUST already be initialized
+ * @param[out] in      C variable to read from
+ * @return
+ *     - 0 on success.
+ *     - -1 on failure.
+ */
+static inline CC_HINT(always_inline)
+int fr_value_box_memcpy_in(fr_value_box_t *vb, void const *in)
+{
+       size_t len;
+
+       len = fr_value_box_field_sizes[vb->type];
+       if (len == 0) {
+               fr_strerror_printf("Type %s not supported for conversion to C type", fr_type_to_str(vb->type));
+               return -1;
+       }
+
+       memcpy(((uint8_t *)vb) + fr_value_box_offsets[vb->type], in, len);
+
+       return 0;
+}
+
 
 /** Box an ethernet value (6 bytes, network byte order)
  *