From a1048a772fc19a1c3b5572b780b7df174b231a7c Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Wed, 11 Feb 2015 14:27:21 -0800 Subject: Add sk_deep_copy and its macro. The next change imported from upstream needs this function. Change-Id: I547efa1f7f46f0558e88047837a26ede32b19275 --- crypto/stack/make_macros.sh | 3 +++ crypto/stack/stack.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) (limited to 'crypto') diff --git a/crypto/stack/make_macros.sh b/crypto/stack/make_macros.sh index f72aa334..4837e449 100644 --- a/crypto/stack/make_macros.sh +++ b/crypto/stack/make_macros.sh @@ -86,6 +86,9 @@ output_stack () { #define sk_${type}_set_cmp_func(sk, comp)\\ ((int (*) (const ${type} **a, const ${type} **b)) sk_set_cmp_func(CHECKED_CAST(_STACK*, STACK_OF(${type})*, sk), CHECKED_CAST(stack_cmp_func, int (*) (const ${type} **a, const ${type} **b), comp))) +#define sk_${type}_deep_copy(sk, copy_func, free_func)\\ +((STACK_OF(${type})*) sk_deep_copy(CHECKED_CAST(const _STACK*, const STACK_OF(${type})*, sk), CHECKED_CAST(void* (*) (void*), ${ptrtype} (*) (${ptrtype}), copy_func), CHECKED_CAST(void (*) (void*), void (*) (${ptrtype}), free_func))) + EOF } diff --git a/crypto/stack/stack.c b/crypto/stack/stack.c index 0b336ba4..c8332de4 100644 --- a/crypto/stack/stack.c +++ b/crypto/stack/stack.c @@ -360,3 +360,31 @@ stack_cmp_func sk_set_cmp_func(_STACK *sk, stack_cmp_func comp) { return old; } + +_STACK *sk_deep_copy(const _STACK *sk, void *(*copy_func)(void *), + void (*free_func)(void *)) { + _STACK *ret = sk_dup(sk); + if (ret == NULL) { + return NULL; + } + + size_t i; + for (i = 0; i < ret->num; i++) { + if (ret->data[i] == NULL) { + continue; + } + ret->data[i] = copy_func(ret->data[i]); + if (ret->data[i] == NULL) { + size_t j; + for (j = 0; j < i; j++) { + if (ret->data[j] != NULL) { + free_func(ret->data[j]); + } + } + sk_free(ret); + return NULL; + } + } + + return ret; +} -- cgit v1.2.3