You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by sf...@apache.org on 2012/12/16 12:49:15 UTC

svn commit: r1422549 - in /httpd/httpd/trunk: include/ap_mmn.h include/httpd.h server/util.c server/util_md5.c

Author: sf
Date: Sun Dec 16 11:49:14 2012
New Revision: 1422549

URL: http://svn.apache.org/viewvc?rev=1422549&view=rev
Log:
add new ap_bin2hex() utility function

Modified:
    httpd/httpd/trunk/include/ap_mmn.h
    httpd/httpd/trunk/include/httpd.h
    httpd/httpd/trunk/server/util.c
    httpd/httpd/trunk/server/util_md5.c

Modified: httpd/httpd/trunk/include/ap_mmn.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_mmn.h?rev=1422549&r1=1422548&r2=1422549&view=diff
==============================================================================
--- httpd/httpd/trunk/include/ap_mmn.h (original)
+++ httpd/httpd/trunk/include/ap_mmn.h Sun Dec 16 11:49:14 2012
@@ -407,6 +407,7 @@
  * 20120724.7 (2.5.0-dev)  Add min_http_version/max_http_version to
  *                         core_server_config
  * 20120724.8 (2.5.0-dev)  Add conn_log_level to core_server_config
+ * 20120724.9 (2.5.0-dev)  Add ap_bin2hex()
  */
 
 #define MODULE_MAGIC_COOKIE 0x41503235UL /* "AP25" */
@@ -414,7 +415,7 @@
 #ifndef MODULE_MAGIC_NUMBER_MAJOR
 #define MODULE_MAGIC_NUMBER_MAJOR 20120724
 #endif
-#define MODULE_MAGIC_NUMBER_MINOR 8                   /* 0...n */
+#define MODULE_MAGIC_NUMBER_MINOR 9                   /* 0...n */
 
 /**
  * Determine if the server's current MODULE_MAGIC_NUMBER is at least a

Modified: httpd/httpd/trunk/include/httpd.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/httpd.h?rev=1422549&r1=1422548&r2=1422549&view=diff
==============================================================================
--- httpd/httpd/trunk/include/httpd.h (original)
+++ httpd/httpd/trunk/include/httpd.h Sun Dec 16 11:49:14 2012
@@ -2251,6 +2251,14 @@ AP_DECLARE(void) ap_get_sload(ap_sload_t
  */
 AP_DECLARE(void) ap_get_loadavg(ap_loadavg_t *ld);
 
+/**
+ * Convert binary data into a hex string
+ * @param src pointer to the data
+ * @param srclen length of the data
+ * @param dest pointer to buffer of length (2 * srclen + 1). The resulting
+ *        string will be NUL-terminated.
+ */
+AP_DECLARE(void) ap_bin2hex(const void *src, apr_size_t srclen, char *dest);
 
 #define AP_NORESTART APR_OS_START_USEERR + 1
 

Modified: httpd/httpd/trunk/server/util.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/util.c?rev=1422549&r1=1422548&r2=1422549&view=diff
==============================================================================
--- httpd/httpd/trunk/server/util.c (original)
+++ httpd/httpd/trunk/server/util.c Sun Dec 16 11:49:14 2012
@@ -1958,6 +1958,19 @@ AP_DECLARE(apr_size_t) ap_escape_errorlo
     return (d - (unsigned char *)dest);
 }
 
+AP_DECLARE(void) ap_bin2hex(const void *src, apr_size_t srclen, char *dest)
+{
+    const unsigned char *in = src;
+    unsigned char *out = (unsigned char *)dest;
+    apr_size_t i;
+
+    for (i = 0; i < srclen; i++) {
+        *out++ = c2x_table[in[i] >> 4];
+        *out++ = c2x_table[in[i] & 0xf];
+    }
+    *out = '\0';
+}
+
 AP_DECLARE(int) ap_is_directory(apr_pool_t *p, const char *path)
 {
     apr_finfo_t finfo;

Modified: httpd/httpd/trunk/server/util_md5.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/util_md5.c?rev=1422549&r1=1422548&r2=1422549&view=diff
==============================================================================
--- httpd/httpd/trunk/server/util_md5.c (original)
+++ httpd/httpd/trunk/server/util_md5.c Sun Dec 16 11:49:14 2012
@@ -52,11 +52,9 @@
 
 AP_DECLARE(char *) ap_md5_binary(apr_pool_t *p, const unsigned char *buf, int length)
 {
-    const char *hex = "0123456789abcdef";
     apr_md5_ctx_t my_md5;
     unsigned char hash[APR_MD5_DIGESTSIZE];
-    char *r, result[33]; /* (MD5_DIGESTSIZE * 2) + 1 */
-    int i;
+    char result[2 * APR_MD5_DIGESTSIZE + 1];
 
     /*
      * Take the MD5 hash of the string argument.
@@ -69,11 +67,7 @@ AP_DECLARE(char *) ap_md5_binary(apr_poo
     apr_md5_update(&my_md5, buf, (unsigned int)length);
     apr_md5_final(hash, &my_md5);
 
-    for (i = 0, r = result; i < APR_MD5_DIGESTSIZE; i++) {
-        *r++ = hex[hash[i] >> 4];
-        *r++ = hex[hash[i] & 0xF];
-    }
-    *r = '\0';
+    ap_bin2hex(hash, APR_MD5_DIGESTSIZE, result);
 
     return apr_pstrndup(p, result, APR_MD5_DIGESTSIZE*2);
 }



Re: svn commit: r1422549 - in /httpd/httpd/trunk: include/ap_mmn.h include/httpd.h server/util.c server/util_md5.c

Posted by Marion & Christophe JAILLET <ch...@wanadoo.fr>.
Le 17/12/2012 00:20, Stefan Fritsch a écrit :
> +AP_DECLARE(void) ap_bin2hex(const void *src, apr_size_t srclen,
> char *dest) +{
> +    const unsigned char *in = src;
> +    unsigned char *out = (unsigned char *)dest;
> +    apr_size_t i;
> +
> +    for (i = 0; i < srclen; i++) {
> +        *out++ = c2x_table[in[i] >> 4];
> +        *out++ = c2x_table[in[i] & 0xf];
> +    }
> +    *out = '\0';
> +}
> +
The following functions:
     SSL_SESSION_id2sz, in ssl_util_ssl.c, line 487
     socache_mc_id2key, in mod_socache_memcache.c, line 193
     log_xlate_error, in mod_charset_lite.c, line 498
     add_password, in htdigest.c, line 157
could also benefit from it.

However, some use uppercase HEX representation.

add_password is not an issue.
socache_mc_id2key should not be an issue, the generated string is only 
used with apr_memcache_[set|getp|delete].
log_xlate_error should not be an issue, the generated string is only 
used for logging.
*But* SSL_SESSION_id2sz could be an issue.

Do you think it could be a win ?
Do you think it would be useful to pass an extra parameter to ap_bin2hex 
in order to tell if the conversion should be done using abcdef or ABCDEF ?

Thanks for your comment.

CJ

Re: svn commit: r1422549 - in /httpd/httpd/trunk: include/ap_mmn.h include/httpd.h server/util.c server/util_md5.c

Posted by Christophe JAILLET <ch...@wanadoo.fr>.
Le 16/12/2012 12:49, sf@apache.org a écrit :
> +AP_DECLARE(void) ap_bin2hex(const void *src, apr_size_t srclen, char *dest)
> +{
> +    const unsigned char *in = src;
> +    unsigned char *out = (unsigned char *)dest;
> +    apr_size_t i;
> +
> +    for (i = 0; i < srclen; i++) {
> +        *out++ = c2x_table[in[i] >> 4];
> +        *out++ = c2x_table[in[i] & 0xf];
> +    }
> +    *out = '\0';
> +}
> +
This will be useful for PR 50919.

BTW, why these casts to unsigned char ?
I don't see what it brings here. Moreover c2x_table is just 'static 
const char[]'

CJ