Preliminary streaming ASN1 encode support.
[openssl.git] / crypto / asn1 / tasn_new.c
index d6f115592823a22fdb38cd0d1b04741f3d2058f0..11a90f739edce0f7b3e6ea084cf19066f33a3dfa 100644 (file)
@@ -62,6 +62,7 @@
 #include <openssl/objects.h>
 #include <openssl/err.h>
 #include <openssl/asn1t.h>
+#include <string.h>
 
 static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine);
 static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
@@ -96,6 +97,10 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
 
        if(!combine) *pval = NULL;
 
+#ifdef CRYPTO_MDEBUG
+       if(it->sname) CRYPTO_push_info(it->sname);
+#endif
+
        switch(it->itype) {
 
                case ASN1_ITYPE_EXTERN:
@@ -133,7 +138,12 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
                if(asn1_cb) {
                        i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
                        if(!i) goto auxerr;
-                       if(i==2) return 1;
+                       if(i==2) {
+#ifdef CRYPTO_MDEBUG
+                               if(it->sname) CRYPTO_pop_info();
+#endif
+                               return 1;
+                       }
                }
                if(!combine) {
                        *pval = OPENSSL_malloc(it->size);
@@ -145,11 +155,17 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
                                goto auxerr;
                break;
 
+               case ASN1_ITYPE_NDEF_SEQUENCE:
                case ASN1_ITYPE_SEQUENCE:
                if(asn1_cb) {
                        i = asn1_cb(ASN1_OP_NEW_PRE, pval, it);
                        if(!i) goto auxerr;
-                       if(i==2) return 1;
+                       if(i==2) {
+#ifdef CRYPTO_MDEBUG
+                               if(it->sname) CRYPTO_pop_info();
+#endif
+                               return 1;
+                       }
                }
                if(!combine) {
                        *pval = OPENSSL_malloc(it->size);
@@ -166,15 +182,24 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int
                                goto auxerr;
                break;
        }
+#ifdef CRYPTO_MDEBUG
+       if(it->sname) CRYPTO_pop_info();
+#endif
        return 1;
 
        memerr:
        ASN1err(ASN1_F_ASN1_ITEM_NEW, ERR_R_MALLOC_FAILURE);
+#ifdef CRYPTO_MDEBUG
+       if(it->sname) CRYPTO_pop_info();
+#endif
        return 0;
 
        auxerr:
        ASN1err(ASN1_F_ASN1_ITEM_NEW, ASN1_R_AUX_ERROR);
        ASN1_item_ex_free(pval, it);
+#ifdef CRYPTO_MDEBUG
+       if(it->sname) CRYPTO_pop_info();
+#endif
        return 0;
 
 }
@@ -207,6 +232,7 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
                case ASN1_ITYPE_COMPAT:
                case ASN1_ITYPE_CHOICE:
                case ASN1_ITYPE_SEQUENCE:
+               case ASN1_ITYPE_NDEF_SEQUENCE:
                *pval = NULL;
                break;
        }
@@ -215,7 +241,8 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
 
 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
 {
-       const ASN1_ITEM *it = tt->item;
+       const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
+       int ret;
        if(tt->flags & ASN1_TFLG_OPTIONAL) {
                asn1_template_clear(pval, tt);
                return 1;
@@ -226,28 +253,38 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
                *pval = NULL;
                return 1;
        }
+#ifdef CRYPTO_MDEBUG
+       if(tt->field_name) CRYPTO_push_info(tt->field_name);
+#endif
        /* If SET OF or SEQUENCE OF, its a STACK */
        if(tt->flags & ASN1_TFLG_SK_MASK) {
                STACK_OF(ASN1_VALUE) *skval;
                skval = sk_ASN1_VALUE_new_null();
                if(!skval) {
                        ASN1err(ASN1_F_ASN1_TEMPLATE_NEW, ERR_R_MALLOC_FAILURE);
-                       return 0;
+                       ret = 0;
+                       goto done;
                }
                *pval = (ASN1_VALUE *)skval;
-               return 1;
+               ret = 1;
+               goto done;
        }
        /* Otherwise pass it back to the item routine */
-       return asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
+       ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
+       done:
+#ifdef CRYPTO_MDEBUG
+       if(it->sname) CRYPTO_pop_info();
+#endif
+       return ret;
 }
 
-void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
+static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
 {
        /* If ADB or STACK just NULL the field */
        if(tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK)) 
                *pval = NULL;
        else
-               asn1_item_clear(pval, tt->item);
+               asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
 }