conversion of base types

ENGINE-898
Volker Birk 2 years ago
parent 7de726962a
commit 052e23d2c6

@ -190,3 +190,214 @@ enomem:
return NULL;
}
StringPair_t *StringPair_from_Struct(
const stringpair_t *value,
StringPair_t *result
)
{
bool allocated = !result;
assert(value);
if (!value)
return NULL;
if (allocated)
result = (StringPair_t *) calloc(1, sizeof(StringPair_t));
assert(result);
if (!result)
return NULL;
if (value->key) {
int r = OCTET_STRING_fromBuf(&result->key, value->key, -1);
if (r)
goto enomem;
}
if (value->value) {
int r = OCTET_STRING_fromBuf(&result->value, value->value, -1);
if (r)
goto enomem;
}
return result;
enomem:
if (allocated)
ASN_STRUCT_FREE(asn_DEF_StringPair, result);
return NULL;
}
stringpair_t *StringPair_to_Struct(StringPair_t *value, stringpair_t *result)
{
bool allocated = !result;
assert(value);
if (!value)
return NULL;
if (allocated)
result = new_stringpair(NULL, NULL);
if (!result)
return NULL;
result->key = strndup((char *) value->key.buf,
value->key.size);
assert(result->key);
if (!result->key)
goto enomem;
result->value = strndup((char *) value->value.buf,
value->value.size);
assert(result->value);
if (!result->value)
goto enomem;
return result;
enomem:
if (allocated)
free_stringpair(result);
return NULL;
}
StringPairList_t *StringPairList_from_stringpair_list(
const stringpair_list_t *list,
StringPairList_t *result
)
{
bool allocated = !result;
assert(list);
if (!list)
return NULL;
if (allocated) {
result = (StringPairList_t *) calloc(1, sizeof(StringPairList_t));
assert(result);
if (!result)
return NULL;
}
else {
asn_sequence_empty(result);
}
for (const stringpair_list_t *l = list; l && l->value; l=l->next) {
StringPair_t *value = StringPair_from_Struct(l->value, NULL);
if (ASN_SEQUENCE_ADD(&result->list, value)) {
ASN_STRUCT_FREE(asn_DEF_StringPair, value);
goto enomem;
}
}
return result;
enomem:
if (allocated)
ASN_STRUCT_FREE(asn_DEF_StringPairList, result);
return NULL;
}
stringpair_list_t *StringPairList_to_stringpair_list(
StringPairList_t *list,
stringpair_list_t *result
)
{
bool allocated = !result;
assert(list);
if (!list)
return NULL;
if (allocated)
result = new_stringpair_list(NULL);
if (!result)
return NULL;
stringpair_list_t *r = result;
for (int i=0; i<list->list.count; i++) {
stringpair_t *value = StringPair_to_Struct(list->list.array[i], NULL);
r = stringpair_list_add(r, value);
if (!r)
goto enomem;
}
return result;
enomem:
if (allocated)
free_stringpair_list(result);
return NULL;
}
PStringList_t *PStringList_from_stringlist(
const stringlist_t *list,
PStringList_t *result
)
{
bool allocated = !result;
assert(list);
if (!list)
return NULL;
if (allocated) {
result = (PStringList_t *) calloc(1, sizeof(PStringList_t));
assert(result);
if (!result)
return NULL;
}
else {
asn_sequence_empty(result);
}
for (const stringlist_t *l = list; l && l->value; l=l->next) {
PString_t *element = NULL;
int r = OCTET_STRING_fromBuf(element, l->value, -1);
if (r)
goto enomem;
if (ASN_SEQUENCE_ADD(&result->list, element)) {
ASN_STRUCT_FREE(asn_DEF_PString, element);
goto enomem;
}
}
return result;
enomem:
if (allocated)
ASN_STRUCT_FREE(asn_DEF_PStringList, result);
return NULL;
}
stringlist_t *PStringList_to_stringlist(
PStringList_t *list,
stringlist_t *result
)
{
bool allocated = !result;
assert(list);
if (!list)
return NULL;
if (allocated)
result = new_stringlist(NULL);
if (!result)
return NULL;
for (int i=0; i<list->list.count; i++) {
result->value = strndup((char *) list->list.array[i]->buf,
list->list.array[i]->size);
assert(result->value);
if (!result->value)
goto enomem;
}
return result;
enomem:
if (allocated)
free_stringlist(result);
return NULL;
}

@ -11,6 +11,10 @@
#include "identity_list.h"
#include "../asn.1/Identity.h"
#include "../asn.1/IdentityList.h"
#include "../asn.1/StringPair.h"
#include "../asn.1/StringPair.h"
#include "../asn.1/StringPairList.h"
#include "../asn.1/PStringList.h"
#ifdef __cplusplus
extern "C" {
@ -89,6 +93,122 @@ IdentityList_t *IdentityList_from_identity_list(
identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result);
/**
* <!-- StringPair_from_Struct() -->
*
* @brief Convert stringpair_t into ASN.1 StringPair_t
*
* @param value[in] stringpair_t to convert
* @param result[in,out] StringPair_t to update or NULL to alloc a new one
*
* @retval pointer to updated or allocated result
*
* @warning if a new struct is allocated, the ownership goes to the caller
*
*/
StringPair_t *StringPair_from_Struct(
const stringpair_t *value,
StringPair_t *result
);
/**
* <!-- StringPair_to_Struct() -->
*
* @brief Convert ASN.1 StringPair_t into stringpair_t
*
* @param value[in] StringPair_t to convert
* @param result[inout] stringpair_t to update or NULL to alloc a new one
*
* @retval pointer to updated or allocated result
*
* @warning if a new struct is allocated, the ownership goes to the caller
*
*/
stringpair_t *StringPair_to_Struct(StringPair_t *value, stringpair_t *result);
/**
* <!-- StringPairList_from_stringpair_list() -->
*
* @brief Convert stringpair_list_t into ASN.1 StringPairList_t
*
* @param list[in] stringpair_list to convert
* @param result[inout] StringPairList_t to update or NULL to alloc a new one
*
* @retval pointer to updated or allocated result
*
* @warning if a new struct is allocated, the ownership goes to the caller
*
*/
StringPairList_t *StringPairList_from_stringpair_list(
const stringpair_list_t *list,
StringPairList_t *result
);
/**
* <!-- StringPairList_to_stringpair_list() -->
*
* @brief Convert ASN.1 StringPairList_t to stringpair_list_t
*
* @param list[in] ASN.1 StringPairList_t to convert
* @param result[inout] stringpair_list_t to update or NULL to alloc a new one
*
* @retval pointer to updated or allocated result
*
* @warning if a new struct is allocated, the ownership goes to the caller
*
*/
stringpair_list_t *StringPairList_to_stringpair_list(
StringPairList_t *list,
stringpair_list_t *result
);
/**
* <!-- PStringList_from_stringlist() -->
*
* @brief Convert stringlist_t into ASN.1 PStringList_t
*
* @param list[in] stringlist to convert
* @param result[inout] PStringList_t to update or NULL to alloc a new one
*
* @retval pointer to updated or allocated result
*
* @warning if a new struct is allocated, the ownership goes to the caller
*
*/
PStringList_t *PStringList_from_stringlist(
const stringlist_t *list,
PStringList_t *result
);
/**
* <!-- PStringList_to_stringlist() -->
*
* @brief Convert ASN.1 PStringList_t to stringlist_t
*
* @param list[in] ASN.1 PStringList_t to convert
* @param result[inout] stringlist_t to update or NULL to alloc a new one
*
* @retval pointer to updated or allocated result
*
* @warning if a new struct is allocated, the ownership goes to the caller
*
*/
stringlist_t *StringPairList_to_stringlist(
StringPairList_t *list,
stringlist_t *result
);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save