From 3deee501d621c48ca887fab354ae4e743bb13fcd Mon Sep 17 00:00:00 2001 From: Alberto Leiva Popper Date: Wed, 15 May 2024 15:08:56 -0600 Subject: [PATCH] Merge some duplicate code --- src/asn1/asn1c/ANY.c | 7 ++++++- src/asn1/asn1c/ANY.h | 2 ++ src/asn1/asn1c/CMSAttribute.c | 18 +----------------- src/asn1/asn1c/ContentInfo.c | 18 +----------------- src/asn1/asn1c/EncapsulatedContentInfo.c | 20 ++------------------ src/asn1/asn1c/OCTET_STRING.c | 6 ++++++ src/asn1/asn1c/OCTET_STRING.h | 3 +++ src/asn1/asn1c/json_encoder.c | 16 ++++++++++++++++ src/asn1/asn1c/json_encoder.h | 3 +++ 9 files changed, 40 insertions(+), 53 deletions(-) diff --git a/src/asn1/asn1c/ANY.c b/src/asn1/asn1c/ANY.c index cde0cf9e..8edaa8e9 100644 --- a/src/asn1/asn1c/ANY.c +++ b/src/asn1/asn1c/ANY.c @@ -121,7 +121,6 @@ ANY_new_fromType(asn_TYPE_descriptor_t *td, void *sptr) { } } -/* FIXME use this more. */ int ANY_to_type(ANY_t *st, asn_TYPE_descriptor_t *td, void **struct_ptr) { asn_dec_rval_t rval; @@ -166,3 +165,9 @@ static int ANY__consume_bytes(const void *buffer, size_t size, void *key) { return 0; } + +json_t * +ANY_to_json(const asn_TYPE_descriptor_t *td, ANY_t const *ber) +{ + return ber2json(td, ber->buf, ber->size); +} diff --git a/src/asn1/asn1c/ANY.h b/src/asn1/asn1c/ANY.h index 399f70b1..01c7d41b 100644 --- a/src/asn1/asn1c/ANY.h +++ b/src/asn1/asn1c/ANY.h @@ -46,4 +46,6 @@ int ANY_to_type(ANY_t *, asn_TYPE_descriptor_t *td, void **struct_ptr); #define ANY_new_fromBuf(buf, size) OCTET_STRING_new_fromBuf( \ &asn_DEF_ANY, (buf), (size)) +json_t *ANY_to_json(const asn_TYPE_descriptor_t *, ANY_t const *); + #endif /* ASN_TYPE_ANY_H */ diff --git a/src/asn1/asn1c/CMSAttribute.c b/src/asn1/asn1c/CMSAttribute.c index 262c6792..d4425609 100644 --- a/src/asn1/asn1c/CMSAttribute.c +++ b/src/asn1/asn1c/CMSAttribute.c @@ -13,22 +13,6 @@ #include "asn1/asn1c/MessageDigest.h" #include "asn1/asn1c/SigningTime.h" -static json_t * -attr2json(asn_TYPE_descriptor_t const *td, CMSAttributeValue_t const *ber) -{ - void *attr; - asn_dec_rval_t rval; - json_t *json; - - attr = NULL; - rval = ber_decode(td, &attr, ber->buf, ber->size); - - json = (rval.code == RC_OK) ? td->op->json_encoder(td, attr) : NULL; - - ASN_STRUCT_FREE(*td, attr); - return json; -} - json_t * CMSAttribute_encode_json(const asn_TYPE_descriptor_t *td, const void *sptr) { @@ -67,7 +51,7 @@ CMSAttribute_encode_json(const asn_TYPE_descriptor_t *td, const void *sptr) } for (a = 0; a < cattr->attrValues.list.count; a++) { - tmp = attr2json(td, cattr->attrValues.list.array[a]); + tmp = ANY_to_json(td, cattr->attrValues.list.array[a]); if (json_array_add(array, tmp)) goto fail; } diff --git a/src/asn1/asn1c/ContentInfo.c b/src/asn1/asn1c/ContentInfo.c index 55f554e1..0a0b2d7e 100644 --- a/src/asn1/asn1c/ContentInfo.c +++ b/src/asn1/asn1c/ContentInfo.c @@ -11,22 +11,6 @@ #include "json_util.h" #include "asn1/asn1c/SignedData.h" -json_t * -content2json(const asn_TYPE_descriptor_t *td, ANY_t const *ber) -{ - void *decoded; - asn_dec_rval_t rval; - json_t *json; - - decoded = NULL; - rval = ber_decode(td, &decoded, ber->buf, ber->size); - - json = (rval.code == RC_OK) ? td->op->json_encoder(td, decoded) : NULL; - - ASN_STRUCT_FREE(*td, decoded); - return json; -} - json_t * ContentInfo_encode_json(const asn_TYPE_descriptor_t *td, const void *sptr) { @@ -48,7 +32,7 @@ ContentInfo_encode_json(const asn_TYPE_descriptor_t *td, const void *sptr) if (OBJECT_IDENTIFIER_to_nid(&ci->contentType) == NID_pkcs7_signed) { td = &asn_DEF_SignedData; - child = content2json(td, &ci->content); + child = ANY_to_json(td, &ci->content); } else { td = &asn_DEF_ANY; child = td->op->json_encoder(td, &ci->content); diff --git a/src/asn1/asn1c/EncapsulatedContentInfo.c b/src/asn1/asn1c/EncapsulatedContentInfo.c index 54ae8c2f..2d0b259d 100644 --- a/src/asn1/asn1c/EncapsulatedContentInfo.c +++ b/src/asn1/asn1c/EncapsulatedContentInfo.c @@ -12,22 +12,6 @@ #include "asn1/asn1c/Manifest.h" #include "asn1/asn1c/RouteOriginAttestation.h" -static json_t * -econtent2json(asn_TYPE_descriptor_t const *td, OCTET_STRING_t *eContent) -{ - void *decoded; - asn_dec_rval_t rval; - json_t *json; - - decoded = NULL; - rval = ber_decode(td, &decoded, eContent->buf, eContent->size); - - json = (rval.code == RC_OK) ? td->op->json_encoder(td, decoded) : NULL; - - ASN_STRUCT_FREE(*td, decoded); - return json; -} - json_t * EncapsulatedContentInfo_encode_json(const asn_TYPE_descriptor_t *td, const void *sptr) @@ -52,10 +36,10 @@ EncapsulatedContentInfo_encode_json(const asn_TYPE_descriptor_t *td, nid = OBJECT_IDENTIFIER_to_nid(&eci->eContentType); if (nid == nid_ct_mft()) { td = &asn_DEF_Manifest; - child = econtent2json(td, eci->eContent); + child = OCTET_STRING_to_json(td, eci->eContent); } else if (nid == nid_ct_roa()) { td = &asn_DEF_RouteOriginAttestation; - child = econtent2json(td, eci->eContent); + child = OCTET_STRING_to_json(td, eci->eContent); } else if (nid == nid_ct_gbr()) { td = &asn_DEF_OCTET_STRING; child = OCTET_STRING_encode_json_utf8(td, eci->eContent); diff --git a/src/asn1/asn1c/OCTET_STRING.c b/src/asn1/asn1c/OCTET_STRING.c index bf081c82..3994bf59 100644 --- a/src/asn1/asn1c/OCTET_STRING.c +++ b/src/asn1/asn1c/OCTET_STRING.c @@ -941,6 +941,12 @@ OCTET_STRING_new_fromBuf(const asn_TYPE_descriptor_t *td, const char *str, return st; } +json_t * +OCTET_STRING_to_json(const asn_TYPE_descriptor_t *td, OCTET_STRING_t const *ber) +{ + return ber2json(td, ber->buf, ber->size); +} + /* * Lexicographically compare the common prefix of both strings, * and if it is the same return -1 for the smallest string. diff --git a/src/asn1/asn1c/OCTET_STRING.h b/src/asn1/asn1c/OCTET_STRING.h index 4647a803..28b0872a 100644 --- a/src/asn1/asn1c/OCTET_STRING.h +++ b/src/asn1/asn1c/OCTET_STRING.h @@ -64,6 +64,9 @@ int OCTET_STRING_fromBuf(OCTET_STRING_t *s, const char *str, int size); OCTET_STRING_t *OCTET_STRING_new_fromBuf(const asn_TYPE_descriptor_t *td, const char *str, int size); +json_t *OCTET_STRING_to_json(const asn_TYPE_descriptor_t *, + OCTET_STRING_t const *); + /**************************** * Internally useful stuff. * ****************************/ diff --git a/src/asn1/asn1c/json_encoder.c b/src/asn1/asn1c/json_encoder.c index c7e7e2bd..fce881c8 100644 --- a/src/asn1/asn1c/json_encoder.c +++ b/src/asn1/asn1c/json_encoder.c @@ -12,3 +12,19 @@ json_encode(const asn_TYPE_descriptor_t *td, const void *sptr) return td->op->json_encoder(td, sptr); } + +json_t * +ber2json(struct asn_TYPE_descriptor_s const *td, uint8_t *buf, size_t size) +{ + void *decoded; + asn_dec_rval_t rval; + json_t *json; + + decoded = NULL; + rval = ber_decode(td, &decoded, buf, size); + + json = (rval.code == RC_OK) ? td->op->json_encoder(td, decoded) : NULL; + + ASN_STRUCT_FREE(*td, decoded); + return json; +} diff --git a/src/asn1/asn1c/json_encoder.h b/src/asn1/asn1c/json_encoder.h index 0ecd83fe..e5e6c8b0 100644 --- a/src/asn1/asn1c/json_encoder.h +++ b/src/asn1/asn1c/json_encoder.h @@ -2,6 +2,7 @@ #define SRC_ASN1_ASN1C_JSON_ENCODER_H_ #include +#include struct asn_TYPE_descriptor_s; /* Forward declaration */ @@ -10,6 +11,8 @@ json_t *json_encode( const void *struct_ptr /* Structure to be encoded */ ); +json_t *ber2json(struct asn_TYPE_descriptor_s const *, uint8_t *, size_t); + /* * Type of the generic JSON encoder. */ -- 2.47.2