return ret;
}
-/** Append bytes from a buffer to an existing "string" type value pair
- *
- * @param[in,out] vp to update.
- * @param[in] src data to copy.
- * @param[in] len of data to copy.
- * @param[in] tainted Whether the value came from a trusted source.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_pair_value_bstrn_append(fr_pair_t *vp, char const *src, size_t len, bool tainted)
-{
- int ret;
-
- if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
-
- ret = fr_value_box_bstrn_append(vp, &vp->data, src, len, tainted);
- if (ret == 0) {
- PAIR_VERIFY(vp);
- }
-
- return ret;
-}
-
-/** Append a talloced buffer to an existing "string" type value pair
- *
- * @param[in,out] vp to update.
- * @param[in] src a talloced nul terminated buffer.
- * @param[in] tainted Whether the value came from a trusted source.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_pair_value_bstr_append_buffer(fr_pair_t *vp, char const *src, bool tainted)
-{
- int ret;
-
- if (!fr_cond_assert(vp->vp_type == FR_TYPE_STRING)) return -1;
-
- ret = fr_value_box_bstr_append_buffer(vp, &vp->data, src, tainted);
- if (ret == 0) {
- PAIR_VERIFY(vp);
- }
-
- return ret;
-}
-
/** Pre-allocate a memory buffer for a "octets" type value pair
*
* @note Will clear existing values (including buffers).
}
-/** Append bytes from a buffer to an existing "octets" type value pair
- *
- * @param[in,out] vp to update.
- * @param[in] src data to copy.
- * @param[in] len of data to copy.
- * @param[in] tainted Whether the value came from a trusted source.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_pair_value_mem_append(fr_pair_t *vp, uint8_t *src, size_t len, bool tainted)
-{
- int ret;
-
- if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
-
- ret = fr_value_box_mem_append(vp, &vp->data, src, len, tainted);
- if (ret == 0) {
- PAIR_VERIFY(vp);
- }
-
- return ret;
-}
-
-/** Append a talloced buffer to an existing "octets" type value pair
- *
- * @param[in,out] vp to update.
- * @param[in] src data to copy.
- * @param[in] tainted Whether the value came from a trusted source.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_pair_value_mem_append_buffer(fr_pair_t *vp, uint8_t *src, bool tainted)
-{
- int ret;
-
- if (!fr_cond_assert(vp->vp_type == FR_TYPE_OCTETS)) return -1;
-
- ret = fr_value_box_mem_append_buffer(vp, &vp->data, src, tainted);
- if (ret == 0) {
- PAIR_VERIFY(vp);
- }
-
- return ret;
-}
-
/** Return a const buffer for an enum type attribute
*
* Where the vp type is numeric but does not have any enumv, or its value
int fr_pair_value_bstrdup_buffer_shallow(fr_pair_t *vp, char const *src, bool tainted) CC_HINT(nonnull);
-int fr_pair_value_bstrn_append(fr_pair_t *vp, char const *src, size_t len, bool tainted) CC_HINT(nonnull(1));
-
-int fr_pair_value_bstr_append_buffer(fr_pair_t *vp, char const *src, bool tainted) CC_HINT(nonnull);
/** @} */
/** @name Assign and manipulate octets strings
int fr_pair_value_memdup_buffer_shallow(fr_pair_t *vp, uint8_t const *src, bool tainted) CC_HINT(nonnull);
-int fr_pair_value_mem_append(fr_pair_t *vp, uint8_t *src, size_t len, bool tainted) CC_HINT(nonnull(1));
-
-int fr_pair_value_mem_append_buffer(fr_pair_t *vp, uint8_t *src, bool tainted) CC_HINT(nonnull);
/** @} */
/** @name Enum functions
talloc_free(copy_test_string);
}
-static void test_fr_pair_value_bstrn_append(void)
-{
- fr_pair_t *vp;
- char *copy_test_string;
-
- TEST_CASE("Find 'Test-String'");
- TEST_CHECK((vp = fr_pair_find_by_da(&test_pairs, NULL, fr_dict_attr_test_string)) != NULL);
-
- TEST_CASE("Validating PAIR_VERIFY()");
- PAIR_VERIFY(vp);
-
- copy_test_string = talloc_strdup(vp, test_string);
- talloc_set_type(copy_test_string, char);
-
- TEST_CASE("Copy content of 'test_string' to attribute value using fr_pair_value_bstrndup()");
- TEST_CHECK(fr_pair_value_bstrndup(vp, test_string, test_string_len, false) == 0);
-
- TEST_CASE("Append the 'copy_test_string' value using fr_pair_value_bstrn_append()");
- TEST_CHECK(fr_pair_value_bstrn_append(vp, copy_test_string, test_string_len, true) == 0);
-
- // awful hack, just verify the first part of buffer and then the second part. yep, just appended twice.
- TEST_CASE("Check 1. part (vp->vp_string == test_string)");
- TEST_CHECK(vp && strncmp(vp->vp_strvalue, test_string, test_string_len) == 0);
-
- TEST_CASE("Check 2. part ((vp->vp_string+test_string_len) == test_string)");
- TEST_CHECK(vp && strncmp(vp->vp_strvalue+test_string_len, test_string, test_string_len) == 0);
-
- talloc_free(copy_test_string);
-}
-
-static void test_fr_pair_value_bstr_append_buffer(void)
-{
- fr_pair_t *vp;
- char *copy_test_string;
-
- TEST_CASE("Find 'Test-String'");
- TEST_CHECK((vp = fr_pair_find_by_da(&test_pairs, NULL, fr_dict_attr_test_string)) != NULL);
-
- TEST_CASE("Validating PAIR_VERIFY()");
- PAIR_VERIFY(vp);
-
- copy_test_string = talloc_strdup(vp, test_string);
- talloc_set_type(copy_test_string, char);
-
- TEST_CASE("Copy content of 'test_string' to attribute value using fr_pair_value_bstrndup()");
- TEST_CHECK(fr_pair_value_bstrndup(vp, test_string, test_string_len, false) == 0);
-
- TEST_CASE("Append the 'copy_test_string' value using fr_pair_value_bstr_append_buffer()");
- TEST_CHECK(fr_pair_value_bstr_append_buffer(vp, copy_test_string, true) == 0);
-
- TEST_CASE("Validating PAIR_VERIFY()");
- PAIR_VERIFY(vp);
-
- // awful hack, just verify the first part of buffer and then the second part. yep, just appended twice.
- TEST_CASE("Check 1. part (vp->vp_string == test_string)");
- TEST_CHECK(vp && strncmp(vp->vp_strvalue, test_string, test_string_len) == 0);
-
- TEST_CASE("Check 2. part ((vp->vp_string+test_string_len) == test_string)");
- TEST_CHECK(vp && strncmp(vp->vp_strvalue+test_string_len, test_string, test_string_len) == 0);
-
- talloc_free(copy_test_string);
-}
-
static void test_fr_pair_value_mem_alloc(void)
{
fr_pair_t *vp;
talloc_free(copy_test_octets);
}
-static void test_fr_pair_value_mem_append(void)
-{
- fr_pair_t *vp;
-
- TEST_CASE("Find 'Test-Octets'");
- TEST_CHECK((vp = fr_pair_find_by_da(&test_pairs, NULL, fr_dict_attr_test_octets)) != NULL);
-
- TEST_CASE("Validating PAIR_VERIFY()");
- PAIR_VERIFY(vp);
-
- TEST_CASE("Copy content of 'test_octets' to attribute value using fr_pair_value_memdup()");
- TEST_CHECK(fr_pair_value_memdup(vp, test_octets, NUM_ELEMENTS(test_octets), false) == 0);
-
- TEST_CASE("Append the 'test_octets' value using fr_pair_value_mem_append()");
- TEST_CHECK(fr_pair_value_mem_append(vp, test_octets, NUM_ELEMENTS(test_octets), true) == 0);
-
- // awful hack, just verify the first part of buffer and then the second part. yep, just appended twice.
- TEST_CASE("Check 1. part (vp->vp_octets == test_octets)");
- TEST_CHECK(vp && memcmp(vp->vp_octets, test_octets, NUM_ELEMENTS(test_octets)) == 0);
-
- TEST_CASE("Check 2. part ((vp->vp_string+NUM_ELEMENTS(test_octets)) == test_octets)");
- TEST_CHECK(vp && memcmp(vp->vp_octets+NUM_ELEMENTS(test_octets), test_octets, NUM_ELEMENTS(test_octets)) == 0);
-}
-
-static void test_fr_pair_value_mem_append_buffer(void)
-{
- fr_pair_t *vp;
- uint8_t *copy_test_octets;
-
- TEST_CASE("Find 'Test-Octets'");
- TEST_CHECK((vp = fr_pair_find_by_da(&test_pairs, NULL, fr_dict_attr_test_octets)) != NULL);
-
- TEST_CASE("Validating PAIR_VERIFY()");
- PAIR_VERIFY(vp);
-
- copy_test_octets = talloc_memdup(vp, test_octets, NUM_ELEMENTS(test_octets));
- talloc_set_type(copy_test_octets, uint8_t);
-
- TEST_CASE("Copy content of 'copy_test_octets' to attribute value using fr_pair_value_memdup()");
- TEST_CHECK(fr_pair_value_memdup(vp, copy_test_octets, NUM_ELEMENTS(test_octets), false) == 0);
-
- TEST_CASE("Append the 'copy_test_octets' value using fr_pair_value_mem_append_buffer()");
- TEST_CHECK(fr_pair_value_mem_append_buffer(vp, copy_test_octets, true) == 0);
-
- // awful hack, just verify the first part of buffer and then the second part. yep, just appended twice.
- TEST_CASE("Check 1. part (vp->vp_octets == test_octets)");
- TEST_CHECK(vp && memcmp(vp->vp_octets, test_octets, NUM_ELEMENTS(test_octets)) == 0);
-
- TEST_CASE("Check 2. part ((vp->vp_string+NUM_ELEMENTS(test_octets)) == test_octets)");
- TEST_CHECK(vp && memcmp(vp->vp_octets+NUM_ELEMENTS(test_octets), test_octets, NUM_ELEMENTS(test_octets)) == 0);
-
- talloc_free(copy_test_octets);
-}
-
static void test_fr_pair_value_enum(void)
{
fr_pair_t *vp;
{ "fr_pair_value_bstrdup_buffer", test_fr_pair_value_bstrdup_buffer },
{ "fr_pair_value_bstrndup_shallow", test_fr_pair_value_bstrndup_shallow },
{ "fr_pair_value_bstrdup_buffer_shallow", test_fr_pair_value_bstrdup_buffer_shallow },
- { "fr_pair_value_bstrn_append", test_fr_pair_value_bstrn_append },
- { "fr_pair_value_bstr_append_buffer", test_fr_pair_value_bstr_append_buffer },
/* Assign and manipulate octets strings */
{ "fr_pair_value_mem_alloc", test_fr_pair_value_mem_alloc },
{ "fr_pair_value_memdup_buffer", test_fr_pair_value_memdup_buffer },
{ "fr_pair_value_memdup_shallow", test_fr_pair_value_memdup_shallow },
{ "fr_pair_value_memdup_buffer_shallow", test_fr_pair_value_memdup_buffer_shallow },
- { "fr_pair_value_mem_append", test_fr_pair_value_mem_append },
- { "fr_pair_value_mem_append_buffer", test_fr_pair_value_mem_append_buffer },
-
+
/* Enum functions */
{ "fr_pair_value_enum", test_fr_pair_value_enum },
{ "fr_pair_value_enum_box", test_fr_pair_value_enum_box },
return 0;
}
-/** Append bytes from a buffer to an existing #fr_value_box_t
- *
- * @param[in] ctx Where to allocate any talloc buffers required.
- * @param[in] dst value box to append to.
- * @param[in] src octets data to append.
- * @param[in] len length of octets data.
- * @param[in] tainted Whether src is tainted.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_value_box_bstrn_append(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted)
-{
- char *ptr, *nptr;
- size_t nlen;
-
- if (len == 0) return 0;
-
- if (dst->type != FR_TYPE_STRING) {
- fr_strerror_printf("%s: Expected boxed value of type %s, got type %s", __FUNCTION__,
- fr_type_to_str(FR_TYPE_STRING),
- fr_type_to_str(dst->type));
- return -1;
- }
-
- ptr = dst->datum.ptr;
- if (!fr_cond_assert(ptr)) return -1;
-
- if (talloc_reference_count(ptr) > 0) {
- fr_strerror_printf("%s: Boxed value has too many references", __FUNCTION__);
- return -1;
- }
-
- nlen = dst->vb_length + len + 1;
- nptr = talloc_realloc(ctx, ptr, char, dst->vb_length + len + 1);
- if (!nptr) {
- fr_strerror_printf("%s: Realloc of %s array from %zu to %zu bytes failed",
- __FUNCTION__, talloc_get_name(ptr), talloc_array_length(ptr), nlen);
- return -1;
- }
- talloc_set_type(nptr, char);
- ptr = nptr;
-
- memcpy(ptr + dst->vb_length, src, len); /* Copy data into the realloced buffer */
-
- dst->tainted = dst->tainted || tainted;
- dst->datum.ptr = ptr;
- dst->vb_length += len;
-
- ptr[dst->vb_length] = '\0';
-
- return 0;
-}
-
-/** Append a talloced buffer to an existing fr_value_box_t
- *
- * @param[in] ctx Where to allocate any talloc buffers required.
- * @param[in] dst value box to append to.
- * @param[in] src string data to append.
- * @param[in] tainted Whether src is tainted.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_value_box_bstr_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, bool tainted)
-{
- size_t len;
-
- (void) talloc_get_type_abort_const(src, char);
-
- len = talloc_array_length(src);
- if ((len == 0) || (src[len - 1] != '\0')) {
- fr_strerror_const("Input buffer not \\0 terminated");
- return -1;
- }
-
- return fr_value_box_bstrn_append(ctx, dst, src, len - 1, tainted);
-}
-
/** Pre-allocate an octets buffer for filling by the caller
*
* @note Buffer will not be zeroed, as it's assumed the caller will be filling it.
dst->vb_length = talloc_array_length(src);
}
-/** Append data to an existing fr_value_box_t
- *
- * @param[in] ctx Where to allocate any talloc buffers required.
- * @param[in] dst value box to append to.
- * @param[in] src octets data to append.
- * @param[in] len length of octets data.
- * @param[in] tainted Whether src is tainted.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_value_box_mem_append(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, size_t len, bool tainted)
-{
- uint8_t *nptr;
- size_t nlen;
-
- if (len == 0) return 0;
-
- if (dst->type != FR_TYPE_OCTETS) {
- fr_strerror_printf("%s: Expected boxed value of type %s, got type %s", __FUNCTION__,
- fr_type_to_str(FR_TYPE_OCTETS),
- fr_type_to_str(dst->type));
- return -1;
- }
-
- if (!fr_cond_assert(dst->datum.ptr)) return -1;
-
- if (talloc_reference_count(dst->datum.ptr) > 0) {
- fr_strerror_printf("%s: Boxed value has too many references", __FUNCTION__);
- return -1;
- }
-
- nlen = dst->vb_length + len;
- nptr = talloc_realloc(ctx, dst->datum.ptr, uint8_t, dst->vb_length + len);
- if (!nptr) {
- fr_strerror_printf("%s: Realloc of %s array from %zu to %zu bytes failed",
- __FUNCTION__,
- talloc_get_name(dst->datum.ptr),
- talloc_array_length((uint8_t const *)dst->datum.ptr), nlen);
- return -1;
- }
-
- memcpy(nptr + dst->vb_length, src, len); /* Copy data into the realloced buffer */
-
- dst->tainted = dst->tainted || tainted;
- dst->datum.ptr = nptr;
- dst->vb_length += len;
-
- return 0;
-}
-
-/** Append a talloc buffer to an existing fr_value_box_t
- *
- * @param[in] ctx Where to allocate any talloc buffers required.
- * @param[in] dst value box to append to.
- * @param[in] src octets data to append.
- * @param[in] tainted Whether src is tainted.
- * @return
- * - 0 on success.
- * - -1 on failure.
- */
-int fr_value_box_mem_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, bool tainted)
-{
- return fr_value_box_mem_append(ctx, dst, src, talloc_array_length(src), tainted);
-}
-
/** Increment a boxed value
*
* Implements safe integer overflow.
char const *src, bool tainted)
CC_HINT(nonnull(2,4));
-int fr_value_box_bstrn_append(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, size_t len, bool tainted)
- CC_HINT(nonnull(2,3));
-
-int fr_value_box_bstr_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, char const *src, bool tainted)
- CC_HINT(nonnull(2,3));
/** @} */
/** @name Assign and manipulate octets strings
uint8_t const *src, bool tainted)
CC_HINT(nonnull(2,4));
-int fr_value_box_mem_append(TALLOC_CTX *ctx, fr_value_box_t *dst,
- uint8_t const *src, size_t len, bool tainted)
- CC_HINT(nonnull(2,3));
-
-int fr_value_box_mem_append_buffer(TALLOC_CTX *ctx, fr_value_box_t *dst, uint8_t const *src, bool tainted)
- CC_HINT(nonnull(2,3));
/** @} */
void fr_value_box_increment(fr_value_box_t *vb)