More VMS synchronisation
[openssl.git] / crypto / asn1 / a_strex.c
index 02fe2bad1c90e40b515d0d1f1c0803ecd645f42e..569b811998584a8612e8ea5040d4ec96407d5581 100644 (file)
@@ -57,6 +57,7 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <openssl/crypto.h>
 #include <openssl/x509.h>
 #include <openssl/asn1.h>
@@ -509,3 +510,24 @@ int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
 {
        return do_print_ex(send_fp_chars, fp, flags, str);
 }
+
+/* Utility function: convert any string type to UTF8, returns number of bytes
+ * in output string or a negative error code
+ */
+
+int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
+{
+       ASN1_STRING stmp, *str = &stmp;
+       int mbflag, type, ret;
+       if(!*out || !in) return -1;
+       type = in->type;
+       if((type < 0) || (type > 30)) return -1;
+       mbflag = tag2nbyte[type];
+       if(mbflag == -1) return -1;
+       mbflag |= MBSTRING_FLAG;
+       stmp.data = NULL;
+       ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
+       if(ret < 0) return ret;
+       if(out) *out = stmp.data;
+       return stmp.length;
+}