Revision: 14357 Author: adrian.chadd Date: Wed Nov 4 14:56:03 2009 Log: Break out the hex string generation code into a library function. It is about to be reused elsewhere.
http://code.google.com/p/lusca-cache/source/detail?r=14357 Added: /branches/LUSCA_HEAD/include/hex.h /branches/LUSCA_HEAD/lib/hex.c Modified: /branches/LUSCA_HEAD/contrib/build_wrap /branches/LUSCA_HEAD/lib/Makefile.am /branches/LUSCA_HEAD/lib/rfc1738.c /branches/LUSCA_HEAD/src/store_key_md5.c ======================================= --- /dev/null +++ /branches/LUSCA_HEAD/include/hex.h Wed Nov 4 14:56:03 2009 @@ -0,0 +1,6 @@ +#ifndef __INCLUDE_HEX_H__ +#define __INCLUDE_HEX_H__ + +extern void hex_from_byte_array(char *dst, const char *src, int srclen); + +#endif ======================================= --- /dev/null +++ /branches/LUSCA_HEAD/lib/hex.c Wed Nov 4 14:56:03 2009 @@ -0,0 +1,20 @@ +#include "../include/config.h" + +/* XXX this should be uhm, a library function! [ahc] */ +static char tohex[] = { '0','1','2','3','4','5','6','7','8','9', 'A', 'B', 'C', 'D', 'E', 'F' }; + +/* + * Create a hex string from the given byte array. + * + * It is up to the caller to ensure that there is at least 2x "srclen" bytes + * available in dst. + */ +void +hex_from_byte_array(char *dst, const char *src, int srclen) +{ + int i; + for (i = 0; i < srclen; i++) { + *(dst++) = tohex[(*src >> 4) & 0x0f]; + *(dst++) = tohex[*(src++) & 0x0f]; + } +} ======================================= --- /branches/LUSCA_HEAD/contrib/build_wrap Tue Jan 6 11:23:23 2009 +++ /branches/LUSCA_HEAD/contrib/build_wrap Wed Nov 4 14:56:03 2009 @@ -1,3 +1,3 @@ #!/bin/sh -gcc -Wall -fPIC -shared -Wl,-soname,libwrap_printf.so.1.0 -ldl -o libwrap_printf.so.1.0 wrap_printf.c - +gcc -I/usr/local/include -L/usr/local/lib -Wall -fPIC -shared -Wl,-soname,libwrap_printf.so.1.0 -o libwrap_printf.so.1.0 wrap_printf.c -lexecinfo + ======================================= --- /branches/LUSCA_HEAD/lib/Makefile.am Sun Jan 11 16:56:50 2009 +++ /branches/LUSCA_HEAD/lib/Makefile.am Wed Nov 4 14:56:03 2009 @@ -40,6 +40,7 @@ getfullhostname.c \ hash.c \ heap.c \ + hex.c \ html_quote.c \ iso3307.c \ md5.c \ ======================================= --- /branches/LUSCA_HEAD/lib/rfc1738.c Mon Jan 5 07:58:31 2009 +++ /branches/LUSCA_HEAD/lib/rfc1738.c Wed Nov 4 14:56:03 2009 @@ -43,9 +43,50 @@ #include "util.h" +static char rfc1738_unsafe_char_map[] = +{ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; + +static char rfc1738_reserved_char_map[] = +{ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +#if 0 /* * RFC 1738 defines that these characters should be escaped, as well - * any non-US-ASCII character or anything between 0x00 - 0x1F. + * any non-US-ASCII character and anything between 0x00 and 0x1F. */ static char rfc1738_unsafe_chars[] = { @@ -79,6 +120,7 @@ (char) 0x3d, /* = */ (char) 0x26 /* & */ }; +#endif /* * rfc1738_escape - Returns a static buffer contains the RFC 1738 @@ -91,7 +133,7 @@ static size_t bufsize = 0; const char *p; char *q; - unsigned int i, do_escape; + unsigned int do_escape; if (buf == NULL || strlen(url) * 3 > bufsize) { xfree(buf); @@ -101,35 +143,16 @@ for (p = url, q = buf; *p != '\0'; p++, q++) { do_escape = 0; - /* RFC 1738 defines these chars as unsafe */ - for (i = 0; i < sizeof(rfc1738_unsafe_chars); i++) { - if (*p == rfc1738_unsafe_chars[i]) { - do_escape = 1; - break; - } - } + if (rfc1738_unsafe_char_map[(int) *p] > 0) + do_escape = 1; + /* Handle % separately */ if (encode_reserved >= 0 && *p == '%') do_escape = 1; - /* RFC 1738 defines these chars as reserved */ - for (i = 0; i < sizeof(rfc1738_reserved_chars) && encode_reserved > 0; i++) { - if (*p == rfc1738_reserved_chars[i]) { + + if (encode_reserved > 0 && rfc1738_reserved_char_map[ (int) *p ] > 0) do_escape = 1; - break; - } - } - /* RFC 1738 says any control chars (0x00-0x1F) are encoded */ - if ((unsigned char) *p <= (unsigned char) 0x1F) { - do_escape = 1; - } - /* RFC 1738 says 0x7f is encoded */ - if (*p == (char) 0x7F) { - do_escape = 1; - } - /* RFC 1738 says any non-US-ASCII are encoded */ - if (((unsigned char) *p >= (unsigned char) 0x80)) { - do_escape = 1; - } + /* Do the triplet encoding, or just copy the char */ /* note: we do not need snprintf here as q is appropriately * allocated - KA */ ======================================= --- /branches/LUSCA_HEAD/src/store_key_md5.c Wed Nov 4 06:13:00 2009 +++ /branches/LUSCA_HEAD/src/store_key_md5.c Wed Nov 4 14:56:03 2009 @@ -35,24 +35,18 @@ #include "squid.h" +#include "../include/hex.h" + static cache_key null_key[SQUID_MD5_DIGEST_LENGTH]; static MemPool * pool_md5_key = NULL; -/* XXX this should be uhm, a library function! [ahc] */ -static char tohex[] = { '0','1','2','3','4','5','6','7','8','9', 'A', 'B', 'C', 'D', 'E', 'F' }; - const char * storeKeyText(const unsigned char *key) { static char buf[SQUID_MD5_DIGEST_LENGTH*2+1]; - char *b = buf; - int i; - - for (i = 0; i < SQUID_MD5_DIGEST_LENGTH; i++) { - *(b++) = tohex[(key[i] >> 4) & 0x0f]; - *(b++) = tohex[key[i] & 0x0f]; - } - *b = '\0'; + + hex_from_byte_array(buf, (char *) key, SQUID_MD5_DIGEST_LENGTH); + buf[SQUID_MD5_DIGEST_LENGTH*2] = '\0'; return buf; } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "lusca-commit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/lusca-commit?hl=en -~----------~----~----~----~------~----~------~--~---
