]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Merge some duplicate code
authorAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 15 May 2024 21:08:56 +0000 (15:08 -0600)
committerAlberto Leiva Popper <ydahhrk@gmail.com>
Wed, 15 May 2024 21:08:56 +0000 (15:08 -0600)
src/asn1/asn1c/ANY.c
src/asn1/asn1c/ANY.h
src/asn1/asn1c/CMSAttribute.c
src/asn1/asn1c/ContentInfo.c
src/asn1/asn1c/EncapsulatedContentInfo.c
src/asn1/asn1c/OCTET_STRING.c
src/asn1/asn1c/OCTET_STRING.h
src/asn1/asn1c/json_encoder.c
src/asn1/asn1c/json_encoder.h

index cde0cf9e673ee01d3012801c2a22dc382e1fae35..8edaa8e99933e5772535de7798115053dbb58f41 100644 (file)
@@ -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);
+}
index 399f70b11fe30d8afedbfb753285fecf07b4acec..01c7d41b86313391ca4a8ba5ffc049d89314a8a3 100644 (file)
@@ -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 */
index 262c6792d83f2727de627a94c26cc25a0942ef9a..d4425609e0074d1ee08efc0c0edf4890c4b7731e 100644 (file)
 #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;
        }
index 55f554e181423296819a8cf2940d8f486c7aa2d1..0a0b2d7ececfdbceec30ac0d67b023afe245e85d 100644 (file)
 #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);
index 54ae8c2f7b9786768afd63d3ee52103961d9aad5..2d0b259d1f5340a5ae93aaa0b281de9fb018c467 100644 (file)
 #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);
index bf081c82708cee648c4d11cd8b0cb994cb2fcea2..3994bf59c69b2f6efa8cfcd44d3b31b90b6346ea 100644 (file)
@@ -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.
index 4647a803457b73f4b9be7c01fb4a44ef32233386..28b0872a34284f5b28edfa6ec5ab59ccc577eb8c 100644 (file)
@@ -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. *
  ****************************/
index c7e7e2bd2c525146aa917cbcd69ee85fe969231f..fce881c80f77f75bb1a320bf4ef9831a4ebe3dc9 100644 (file)
@@ -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;
+}
index 0ecd83fe22408c4a4f6cbd0cbfeef02b80c4f4e3..e5e6c8b01e7a69d73edd08a77c71d8751478786a 100644 (file)
@@ -2,6 +2,7 @@
 #define SRC_ASN1_ASN1C_JSON_ENCODER_H_
 
 #include <jansson.h>
+#include <stdint.h>
 
 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.
  */