Merge from the ASN1 branch of new ASN1 code
[openssl.git] / crypto / asn1 / a_strex.c
index f94ae2751b302bc8dd930591b8ee72e16090ca2b..5335538ae03f74fc4765c42cb474ff61e25b14e1 100644 (file)
@@ -57,6 +57,7 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <openssl/crypto.h>
 #include <openssl/x509.h>
 #include <openssl/asn1.h>
@@ -96,7 +97,7 @@ int send_bio_chars(void *arg, const void *buf, int len)
 int send_fp_chars(void *arg, const void *buf, int len)
 {
        if(!arg) return 1;
-       if(fwrite(buf, 1, len, arg) != len) return 0;
+       if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0;
        return 1;
 }
 
@@ -123,7 +124,7 @@ static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, ch
                if(!io_ch(arg, tmphex, 6)) return -1;
                return 6;
        }
-       chtmp = c;
+       chtmp = (unsigned char)c;
        if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB;
        else chflgs = char_type[chtmp] & flags;
        if(chflgs & CHARTYPE_BS_ESC) {
@@ -200,12 +201,12 @@ static int do_buf(unsigned char *buf, int buflen,
                                 * otherwise each character will be > 0x7f and so the 
                                 * character will never be escaped on first and last.
                                 */
-                               len = do_esc_char(utfbuf[i], flags | orflags, quotes, io_ch, arg);
+                               len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg);
                                if(len < 0) return -1;
                                outlen += len;
                        }
                } else {
-                       len = do_esc_char(c, flags | orflags, quotes, io_ch, arg);
+                       len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg);
                        if(len < 0) return -1;
                        outlen += len;
                }
@@ -304,7 +305,7 @@ static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STR
        unsigned char flags;
        quotes = 0;
        /* Keep a copy of escape flags */
-       flags = lflags & ESC_FLAGS;
+       flags = (unsigned char)(lflags & ESC_FLAGS);
 
        type = str->type;
 
@@ -490,12 +491,24 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
 
 int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
 {
+       if(flags == XN_FLAG_COMPAT)
+               return X509_NAME_print(out, nm, indent);
        return do_name_ex(send_bio_chars, out, nm, indent, flags);
 }
 
 
 int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
 {
+       if(flags == XN_FLAG_COMPAT)
+               {
+               BIO *btmp;
+               int ret;
+               btmp = BIO_new_fp(fp, BIO_NOCLOSE);
+               if(!btmp) return -1;
+               ret = X509_NAME_print(btmp, nm, indent);
+               BIO_free(btmp);
+               return ret;
+               }
        return do_name_ex(send_fp_chars, fp, nm, indent, flags);
 }
 
@@ -509,3 +522,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;
+}