[openssl-commits] [openssl] master update

Rich Salz rsalz at openssl.org
Mon May 16 19:21:20 UTC 2016


The branch master has been updated
       via  49445f21da5ad436a117d0d4cc6220c4bbbbf8a7 (commit)
      from  589902b2cbc667564642a0fdedfb2ef176dba0e8 (commit)


- Log -----------------------------------------------------------------
commit 49445f21da5ad436a117d0d4cc6220c4bbbbf8a7
Author: Rich Salz <rsalz at openssl.org>
Date:   Thu May 12 15:52:58 2016 -0400

    Use OPENSSL_hexchar2int
    
    Reviewed-by: Richard Levitte <levitte at openssl.org>

-----------------------------------------------------------------------

Summary of changes:
 apps/s_client.c            | 11 ++++-------
 crypto/asn1/f_int.c        | 10 ++--------
 crypto/asn1/f_string.c     | 10 ++--------
 crypto/bn/bn_print.c       |  9 ++-------
 crypto/pem/pem_lib.c       |  9 ++-------
 crypto/poly1305/poly1305.c | 11 ++++-------
 crypto/x509v3/v3_utl.c     | 12 +++++-------
 test/danetest.c            | 13 +++++--------
 8 files changed, 26 insertions(+), 59 deletions(-)

diff --git a/apps/s_client.c b/apps/s_client.c
index fada838..d8d5501 100644
--- a/apps/s_client.c
+++ b/apps/s_client.c
@@ -518,19 +518,16 @@ static ossl_ssize_t hexdecode(const char **inptr, void *result)
         return -1;
 
     for (byte = 0; *in; ++in) {
-        char c;
+        int x;
 
         if (isspace(_UC(*in)))
             continue;
-        c = tolower(_UC(*in));
-        if ('0' <= c && c <= '9') {
-            byte |= c - '0';
-        } else if ('a' <= c && c <= 'f') {
-            byte |= c - 'a' + 10;
-        } else {
+        x = OPENSSL_hexchar2int(*in);
+        if (x < 0) {
             OPENSSL_free(ret);
             return 0;
         }
+        byte |= (char)x;
         if ((nibble ^= 1) == 0) {
             *cp++ = byte;
             byte = 0;
diff --git a/crypto/asn1/f_int.c b/crypto/asn1/f_int.c
index e0e49de..99932aa 100644
--- a/crypto/asn1/f_int.c
+++ b/crypto/asn1/f_int.c
@@ -175,14 +175,8 @@ int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size)
         }
         for (j = 0; j < i; j++, k += 2) {
             for (n = 0; n < 2; n++) {
-                m = bufp[k + n];
-                if ((m >= '0') && (m <= '9'))
-                    m -= '0';
-                else if ((m >= 'a') && (m <= 'f'))
-                    m = m - 'a' + 10;
-                else if ((m >= 'A') && (m <= 'F'))
-                    m = m - 'A' + 10;
-                else {
+                m = OPENSSL_hexchar2int(bufp[k + n]);
+                if (m < 0) {
                     ASN1err(ASN1_F_A2I_ASN1_INTEGER,
                             ASN1_R_NON_HEX_CHARACTERS);
                     goto err;
diff --git a/crypto/asn1/f_string.c b/crypto/asn1/f_string.c
index 2b2b545..892e011 100644
--- a/crypto/asn1/f_string.c
+++ b/crypto/asn1/f_string.c
@@ -167,14 +167,8 @@ int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
         }
         for (j = 0; j < i; j++, k += 2) {
             for (n = 0; n < 2; n++) {
-                m = bufp[k + n];
-                if ((m >= '0') && (m <= '9'))
-                    m -= '0';
-                else if ((m >= 'a') && (m <= 'f'))
-                    m = m - 'a' + 10;
-                else if ((m >= 'A') && (m <= 'F'))
-                    m = m - 'A' + 10;
-                else {
+                m = OPENSSL_hexchar2int(bufp[k + n]);
+                if (m < 0) {
                     ASN1err(ASN1_F_A2I_ASN1_STRING,
                             ASN1_R_NON_HEX_CHARACTERS);
                     return 0;
diff --git a/crypto/bn/bn_print.c b/crypto/bn/bn_print.c
index 0c3b214..c00b724 100644
--- a/crypto/bn/bn_print.c
+++ b/crypto/bn/bn_print.c
@@ -215,13 +215,8 @@ int BN_hex2bn(BIGNUM **bn, const char *a)
         l = 0;
         for (;;) {
             c = a[j - m];
-            if ((c >= '0') && (c <= '9'))
-                k = c - '0';
-            else if ((c >= 'a') && (c <= 'f'))
-                k = c - 'a' + 10;
-            else if ((c >= 'A') && (c <= 'F'))
-                k = c - 'A' + 10;
-            else
+            k = OPENSSL_hexchar2int(c);
+            if (k < 0)
                 k = 0;          /* paranoia */
             l = (l << 4) | k;
 
diff --git a/crypto/pem/pem_lib.c b/crypto/pem/pem_lib.c
index 4ca6187..fce8a3a 100644
--- a/crypto/pem/pem_lib.c
+++ b/crypto/pem/pem_lib.c
@@ -558,13 +558,8 @@ static int load_iv(char **fromp, unsigned char *to, int num)
         to[i] = 0;
     num *= 2;
     for (i = 0; i < num; i++) {
-        if ((*from >= '0') && (*from <= '9'))
-            v = *from - '0';
-        else if ((*from >= 'A') && (*from <= 'F'))
-            v = *from - 'A' + 10;
-        else if ((*from >= 'a') && (*from <= 'f'))
-            v = *from - 'a' + 10;
-        else {
+        v = OPENSSL_hexchar2int(*from);
+        if (v < 0) {
             PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS);
             return (0);
         }
diff --git a/crypto/poly1305/poly1305.c b/crypto/poly1305/poly1305.c
index a7c4a08..fd6d320 100644
--- a/crypto/poly1305/poly1305.c
+++ b/crypto/poly1305/poly1305.c
@@ -871,14 +871,11 @@ static const struct poly1305_test poly1305_tests[] = {
 
 static unsigned char hex_digit(char h)
 {
-    if (h >= '0' && h <= '9')
-        return h - '0';
-    else if (h >= 'a' && h <= 'f')
-        return h - 'a' + 10;
-    else if (h >= 'A' && h <= 'F')
-        return h - 'A' + 10;
-    else
+    int i = OPENSSL_hexchar2int(h);
+
+    if (i < 0)
         abort();
+    return i;
 }
 
 static void hex_decode(unsigned char *out, const char *hex)
diff --git a/crypto/x509v3/v3_utl.c b/crypto/x509v3/v3_utl.c
index ae9645d..a5016ba 100644
--- a/crypto/x509v3/v3_utl.c
+++ b/crypto/x509v3/v3_utl.c
@@ -1177,19 +1177,17 @@ static int ipv6_hex(unsigned char *out, const char *in, int inlen)
 {
     unsigned char c;
     unsigned int num = 0;
+    int x;
+
     if (inlen > 4)
         return 0;
     while (inlen--) {
         c = *in++;
         num <<= 4;
-        if ((c >= '0') && (c <= '9'))
-            num |= c - '0';
-        else if ((c >= 'A') && (c <= 'F'))
-            num |= c - 'A' + 10;
-        else if ((c >= 'a') && (c <= 'f'))
-            num |= c - 'a' + 10;
-        else
+        x = OPENSSL_hexchar2int(c);
+        if (x < 0)
             return 0;
+        num |= (char)x;
     }
     out[0] = num >> 8;
     out[1] = num & 0xff;
diff --git a/test/danetest.c b/test/danetest.c
index 3bcc02e..75bcb58 100644
--- a/test/danetest.c
+++ b/test/danetest.c
@@ -198,7 +198,7 @@ static STACK_OF(X509) *load_chain(BIO *fp, int nelem)
         fprintf(stderr, "error reading: malformed %s\n", errtype);
         goto err;
     }
-    
+
     if (count == nelem) {
         ERR_clear_error();
         return chain;
@@ -252,19 +252,16 @@ static ossl_ssize_t hexdecode(const char *in, void *result)
         return -1;
 
     for (byte = 0; *in; ++in) {
-        char c;
+        int x;
 
         if (isspace(_UC(*in)))
             continue;
-        c = tolower(_UC(*in));
-        if ('0' <= c && c <= '9') {
-            byte |= c - '0';
-        } else if ('a' <= c && c <= 'f') {
-            byte |= c - 'a' + 10;
-        } else {
+        x = OPENSSL_hexchar2int(*in);
+        if (x < 0) {
             OPENSSL_free(ret);
             return 0;
         }
+        byte |= (char)x;
         if ((nibble ^= 1) == 0) {
             *cp++ = byte;
             byte = 0;


More information about the openssl-commits mailing list