As ftruncate is not availabe on all platforms, switch back to
[oweals/openssl.git] / crypto / asn1 / a_bytes.c
index fdfe9ea8efda48f25d20e54714256d6e933926c2..c894f5664ce59c6098486186f74c8f11d9f34112 100644 (file)
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "asn1_mac.h"
-
-/* ASN1err(ASN1_F_D2I_ASN1_TYPE_BYTES,ASN1_R_WRONG_TYPE);
- * ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,ASN1_R_WRONG_TAG);
- */
+#include <openssl/asn1_mac.h>
 
 static unsigned long tag2bit[32]={
 0,     0,      0,      B_ASN1_BIT_STRING,      /* tags  0 -  3 */
@@ -75,12 +71,7 @@ B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING,0,
 B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN,
        };
 
-#ifndef NOPROTO
 static int asn1_collate_primative(ASN1_STRING *a, ASN1_CTX *c);
-#else
-static int asn1_collate_primative();
-#endif
-
 /* type is a 'bitmap' of acceptable string types.
  */
 ASN1_STRING *d2i_ASN1_type_bytes(ASN1_STRING **a, unsigned char **pp,
@@ -228,7 +219,7 @@ ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, unsigned char **pp, long length,
                        if ((ret->length < len) || (ret->data == NULL))
                                {
                                if (ret->data != NULL) Free((char *)ret->data);
-                               s=(unsigned char *)Malloc((int)len);
+                               s=(unsigned char *)Malloc((int)len + 1);
                                if (s == NULL)
                                        {
                                        i=ERR_R_MALLOC_FAILURE;
@@ -238,6 +229,7 @@ ASN1_STRING *d2i_ASN1_bytes(ASN1_STRING **a, unsigned char **pp, long length,
                        else
                                s=ret->data;
                        memcpy(s,p,(int)len);
+                       s[len] = '\0';
                        p+=len;
                        }
                else
@@ -323,7 +315,7 @@ static int asn1_collate_primative(ASN1_STRING *a, ASN1_CTX *c)
        if (os != NULL) ASN1_STRING_free(os);
        return(1);
 err:
-       ASN1err(ASN1_F_ASN1_COLLATE_PRIMATIVE,c->error);
+       ASN1err(ASN1_F_ASN1_COLLATE_PRIMITIVE,c->error);
        if (os != NULL) ASN1_STRING_free(os);
        if (b.data != NULL) Free(b.data);
        return(0);