Use uint32_t for unicode code points.

The newer clang-cl is unhappy about the tautological comparison on
Windows, but the comparison itself is unnecessary anyway, since the
values will never exceed uint32_t.

I think the reason it's not firing elsewhere is because on other 64-bit
platforms, it is not tautological because long is 64-bit. On other
32-bit platforms, I'm not sure we actually have a standalone trunk clang
builder right now.

Update-Note: UTF8_getc and UTF8_putc were unexported. No one appears to
    be calling them. (We're a crypto library, not a Unicode library.)
Change-Id: I0949ddea3131dca5f55d04e672c3ccf2915c41ab
Reviewed-on: https://boringssl-review.googlesource.com/23844
Commit-Queue: Adam Langley <agl@google.com>
Reviewed-by: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/crypto/asn1/a_mbstr.c b/crypto/asn1/a_mbstr.c
index 30fff82..a2789ed 100644
--- a/crypto/asn1/a_mbstr.c
+++ b/crypto/asn1/a_mbstr.c
@@ -61,17 +61,19 @@
 #include <openssl/err.h>
 #include <openssl/mem.h>
 
+#include "asn1_locl.h"
+
 static int traverse_string(const unsigned char *p, int len, int inform,
-                           int (*rfunc) (unsigned long value, void *in),
+                           int (*rfunc) (uint32_t value, void *in),
                            void *arg);
-static int in_utf8(unsigned long value, void *arg);
-static int out_utf8(unsigned long value, void *arg);
-static int type_str(unsigned long value, void *arg);
-static int cpy_asc(unsigned long value, void *arg);
-static int cpy_bmp(unsigned long value, void *arg);
-static int cpy_univ(unsigned long value, void *arg);
-static int cpy_utf8(unsigned long value, void *arg);
-static int is_printable(unsigned long value);
+static int in_utf8(uint32_t value, void *arg);
+static int out_utf8(uint32_t value, void *arg);
+static int type_str(uint32_t value, void *arg);
+static int cpy_asc(uint32_t value, void *arg);
+static int cpy_bmp(uint32_t value, void *arg);
+static int cpy_univ(uint32_t value, void *arg);
+static int cpy_utf8(uint32_t value, void *arg);
+static int is_printable(uint32_t value);
 
 /*
  * These functions take a string in UTF8, ASCII or multibyte form and a mask
@@ -100,7 +102,7 @@
     unsigned char *p;
     int nchar;
     char strbuf[32];
-    int (*cpyfunc) (unsigned long, void *) = NULL;
+    int (*cpyfunc) (uint32_t, void *) = NULL;
     if (len == -1)
         len = strlen((const char *)in);
     if (!mask)
@@ -253,10 +255,10 @@
  */
 
 static int traverse_string(const unsigned char *p, int len, int inform,
-                           int (*rfunc) (unsigned long value, void *in),
+                           int (*rfunc) (uint32_t value, void *in),
                            void *arg)
 {
-    unsigned long value;
+    uint32_t value;
     int ret;
     while (len) {
         if (inform == MBSTRING_ASC) {
@@ -267,8 +269,8 @@
             value |= *p++;
             len -= 2;
         } else if (inform == MBSTRING_UNIV) {
-            value = ((unsigned long)*p++) << 24;
-            value |= ((unsigned long)*p++) << 16;
+            value = ((uint32_t)*p++) << 24;
+            value |= ((uint32_t)*p++) << 16;
             value |= *p++ << 8;
             value |= *p++;
             len -= 4;
@@ -292,7 +294,7 @@
 
 /* Just count number of characters */
 
-static int in_utf8(unsigned long value, void *arg)
+static int in_utf8(uint32_t value, void *arg)
 {
     int *nchar;
     nchar = arg;
@@ -302,7 +304,7 @@
 
 /* Determine size of output as a UTF8 String */
 
-static int out_utf8(unsigned long value, void *arg)
+static int out_utf8(uint32_t value, void *arg)
 {
     int *outlen;
     outlen = arg;
@@ -315,7 +317,7 @@
  * "mask".
  */
 
-static int type_str(unsigned long value, void *arg)
+static int type_str(uint32_t value, void *arg)
 {
     unsigned long types;
     types = *((unsigned long *)arg);
@@ -335,7 +337,7 @@
 
 /* Copy one byte per character ASCII like strings */
 
-static int cpy_asc(unsigned long value, void *arg)
+static int cpy_asc(uint32_t value, void *arg)
 {
     unsigned char **p, *q;
     p = arg;
@@ -347,7 +349,7 @@
 
 /* Copy two byte per character BMPStrings */
 
-static int cpy_bmp(unsigned long value, void *arg)
+static int cpy_bmp(uint32_t value, void *arg)
 {
     unsigned char **p, *q;
     p = arg;
@@ -360,7 +362,7 @@
 
 /* Copy four byte per character UniversalStrings */
 
-static int cpy_univ(unsigned long value, void *arg)
+static int cpy_univ(uint32_t value, void *arg)
 {
     unsigned char **p, *q;
     p = arg;
@@ -375,7 +377,7 @@
 
 /* Copy to a UTF8String */
 
-static int cpy_utf8(unsigned long value, void *arg)
+static int cpy_utf8(uint32_t value, void *arg)
 {
     unsigned char **p;
     int ret;
@@ -387,7 +389,7 @@
 }
 
 /* Return 1 if the character is permitted in a PrintableString */
-static int is_printable(unsigned long value)
+static int is_printable(uint32_t value)
 {
     int ch;
     if (value > 0x7f)
diff --git a/crypto/asn1/a_utf8.c b/crypto/asn1/a_utf8.c
index 1702768..119ccf9 100644
--- a/crypto/asn1/a_utf8.c
+++ b/crypto/asn1/a_utf8.c
@@ -59,6 +59,8 @@
 #include <openssl/err.h>
 #include <openssl/mem.h>
 
+#include "asn1_locl.h"
+
 /* UTF8 utilities */
 
 /*
@@ -70,10 +72,10 @@
  * incorrectly (not minimal length).
  */
 
-int UTF8_getc(const unsigned char *str, int len, unsigned long *val)
+int UTF8_getc(const unsigned char *str, int len, uint32_t *val)
 {
     const unsigned char *p;
-    unsigned long value;
+    uint32_t value;
     int ret;
     if (len <= 0)
         return 0;
@@ -112,7 +114,7 @@
             || ((p[2] & 0xc0) != 0x80)
             || ((p[3] & 0xc0) != 0x80))
             return -3;
-        value = ((unsigned long)(*p++ & 0x7)) << 18;
+        value = ((uint32_t)(*p++ & 0x7)) << 18;
         value |= (*p++ & 0x3f) << 12;
         value |= (*p++ & 0x3f) << 6;
         value |= *p++ & 0x3f;
@@ -127,9 +129,9 @@
             || ((p[3] & 0xc0) != 0x80)
             || ((p[4] & 0xc0) != 0x80))
             return -3;
-        value = ((unsigned long)(*p++ & 0x3)) << 24;
-        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
-        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
+        value = ((uint32_t)(*p++ & 0x3)) << 24;
+        value |= ((uint32_t)(*p++ & 0x3f)) << 18;
+        value |= ((uint32_t)(*p++ & 0x3f)) << 12;
         value |= (*p++ & 0x3f) << 6;
         value |= *p++ & 0x3f;
         if (value < 0x200000)
@@ -144,10 +146,10 @@
             || ((p[4] & 0xc0) != 0x80)
             || ((p[5] & 0xc0) != 0x80))
             return -3;
-        value = ((unsigned long)(*p++ & 0x1)) << 30;
-        value |= ((unsigned long)(*p++ & 0x3f)) << 24;
-        value |= ((unsigned long)(*p++ & 0x3f)) << 18;
-        value |= ((unsigned long)(*p++ & 0x3f)) << 12;
+        value = ((uint32_t)(*p++ & 0x1)) << 30;
+        value |= ((uint32_t)(*p++ & 0x3f)) << 24;
+        value |= ((uint32_t)(*p++ & 0x3f)) << 18;
+        value |= ((uint32_t)(*p++ & 0x3f)) << 12;
         value |= (*p++ & 0x3f) << 6;
         value |= *p++ & 0x3f;
         if (value < 0x4000000)
@@ -167,7 +169,7 @@
  * most 6 characters.
  */
 
-int UTF8_putc(unsigned char *str, int len, unsigned long value)
+int UTF8_putc(unsigned char *str, int len, uint32_t value)
 {
     if (!str)
         len = 6;                /* Maximum we will need */
diff --git a/crypto/asn1/asn1_locl.h b/crypto/asn1/asn1_locl.h
index 10a832c..8cef246 100644
--- a/crypto/asn1/asn1_locl.h
+++ b/crypto/asn1/asn1_locl.h
@@ -93,6 +93,9 @@
 void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
                             int combine);
 
+int UTF8_getc(const unsigned char *str, int len, uint32_t *val);
+int UTF8_putc(unsigned char *str, int len, uint32_t value);
+
 
 #if defined(__cplusplus)
 }  /* extern C */
diff --git a/crypto/x509/a_strex.c b/crypto/x509/a_strex.c
index d0d8356..465ad08 100644
--- a/crypto/x509/a_strex.c
+++ b/crypto/x509/a_strex.c
@@ -56,6 +56,7 @@
 
 #include <openssl/x509.h>
 
+#include <inttypes.h>
 #include <string.h>
 
 #include <openssl/asn1.h>
@@ -63,6 +64,7 @@
 #include <openssl/obj.h>
 
 #include "charmap.h"
+#include "../asn1/asn1_locl.h"
 
 /*
  * ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
@@ -105,22 +107,20 @@
 
 #define HEX_SIZE(type) (sizeof(type)*2)
 
-static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes,
+static int do_esc_char(uint32_t c, unsigned char flags, char *do_quotes,
                        char_io *io_ch, void *arg)
 {
     unsigned char chflgs, chtmp;
-    char tmphex[HEX_SIZE(long) + 3];
+    char tmphex[HEX_SIZE(uint32_t) + 3];
 
-    if (c > 0xffffffffL)
-        return -1;
     if (c > 0xffff) {
-        BIO_snprintf(tmphex, sizeof tmphex, "\\W%08lX", c);
+        BIO_snprintf(tmphex, sizeof tmphex, "\\W%08" PRIX32, c);
         if (!io_ch(arg, tmphex, 10))
             return -1;
         return 10;
     }
     if (c > 0xff) {
-        BIO_snprintf(tmphex, sizeof tmphex, "\\U%04lX", c);
+        BIO_snprintf(tmphex, sizeof tmphex, "\\U%04" PRIX32, c);
         if (!io_ch(arg, tmphex, 6))
             return -1;
         return 6;
@@ -180,7 +180,7 @@
 {
     int i, outlen, len;
     unsigned char orflags, *p, *q;
-    unsigned long c;
+    uint32_t c;
     p = buf;
     q = buf + buflen;
     outlen = 0;
@@ -191,14 +191,14 @@
             orflags = 0;
         switch (type & BUF_TYPE_WIDTH_MASK) {
         case 4:
-            c = ((unsigned long)*p++) << 24;
-            c |= ((unsigned long)*p++) << 16;
-            c |= ((unsigned long)*p++) << 8;
+            c = ((uint32_t)*p++) << 24;
+            c |= ((uint32_t)*p++) << 16;
+            c |= ((uint32_t)*p++) << 8;
             c |= *p++;
             break;
 
         case 2:
-            c = ((unsigned long)*p++) << 8;
+            c = ((uint32_t)*p++) << 8;
             c |= *p++;
             break;
 
diff --git a/include/openssl/asn1.h b/include/openssl/asn1.h
index 5c8bf4c..6572995 100644
--- a/include/openssl/asn1.h
+++ b/include/openssl/asn1.h
@@ -708,9 +708,6 @@
 DECLARE_ASN1_FUNCTIONS(ASN1_NULL)
 DECLARE_ASN1_FUNCTIONS(ASN1_BMPSTRING)
 
-OPENSSL_EXPORT int UTF8_getc(const unsigned char *str, int len, unsigned long *val);
-OPENSSL_EXPORT int UTF8_putc(unsigned char *str, int len, unsigned long value);
-
 DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, ASN1_PRINTABLE)
 
 DECLARE_ASN1_FUNCTIONS_name(ASN1_STRING, DIRECTORYSTRING)