From 1634d68dd0761c285516a7db67934d89cd722930 Mon Sep 17 00:00:00 2001 From: Andrew Bettison Date: Wed, 13 Nov 2013 12:45:02 +1030 Subject: [PATCH] Format UUID strings as lower case hex --- str.c | 5 +++-- str.h | 3 ++- strbuf.c | 4 ++-- strbuf_helpers.c | 4 ++-- uuid.c | 4 ++-- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/str.c b/str.c index 182a7842..d5af0349 100644 --- a/str.c +++ b/str.c @@ -30,14 +30,15 @@ #include #include -const char hexdigit[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; +const char hexdigit_upper[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; +const char hexdigit_lower[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; char *tohex(char *dstHex, size_t dstStrLen, const unsigned char *srcBinary) { char *p; size_t i; for (p = dstHex, i = 0; i < dstStrLen; ++i) - *p++ = (i & 1) ? hexdigit[*srcBinary++ & 0xf] : hexdigit[*srcBinary >> 4]; + *p++ = (i & 1) ? hexdigit_upper[*srcBinary++ & 0xf] : hexdigit_upper[*srcBinary >> 4]; *p = '\0'; return dstHex; } diff --git a/str.h b/str.h index 7f9827be..e84a8649 100644 --- a/str.h +++ b/str.h @@ -60,7 +60,8 @@ __STR_INLINE int is_xstring(const char *text, int len) return *text == '\0'; } -extern const char hexdigit[16]; +extern const char hexdigit_upper[16]; +extern const char hexdigit_lower[16]; char *tohex(char *dstHex, size_t dstStrlen, const unsigned char *srcBinary); size_t fromhex(unsigned char *dstBinary, const char *srcHex, size_t nbinary); int fromhexstr(unsigned char *dstBinary, const char *srcHex, size_t nbinary); diff --git a/strbuf.c b/strbuf.c index df37a0a1..23efa133 100644 --- a/strbuf.c +++ b/strbuf.c @@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #define __STRBUF_INLINE #include "strbuf.h" +#include "str.h" static inline size_t min(size_t a, size_t b) { return a < b ? a : b; @@ -76,7 +77,6 @@ strbuf strbuf_puts(strbuf sb, const char *text) strbuf strbuf_tohex(strbuf sb, size_t strlen, const unsigned char *data) { - static char hexdigit[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char *p = sb->current; sb->current += strlen; if (sb->start) { @@ -84,7 +84,7 @@ strbuf strbuf_tohex(strbuf sb, size_t strlen, const unsigned char *data) // The following loop could overwrite the '\0' at *sp->end. size_t i; for (i = 0; i < strlen && p < e; ++i) - *p++ = (i & 1) ? hexdigit[*data++ & 0xf] : hexdigit[*data >> 4]; + *p++ = (i & 1) ? hexdigit_upper[*data++ & 0xf] : hexdigit_upper[*data >> 4]; // This will restore the '\0' at *sp->end if it was overwritten. *e = '\0'; } diff --git a/strbuf_helpers.c b/strbuf_helpers.c index 530be30d..093c5cc8 100644 --- a/strbuf_helpers.c +++ b/strbuf_helpers.c @@ -457,8 +457,8 @@ strbuf strbuf_json_hex(strbuf sb, const unsigned char *buf, size_t len) strbuf_putc(sb, '"'); size_t i; for (i = 0; i != len; ++i) { - strbuf_putc(sb, hexdigit[*buf >> 4]); - strbuf_putc(sb, hexdigit[*buf++ & 0xf]); + strbuf_putc(sb, hexdigit_upper[*buf >> 4]); + strbuf_putc(sb, hexdigit_upper[*buf++ & 0xf]); } strbuf_putc(sb, '"'); } else diff --git a/uuid.c b/uuid.c index 8666fbbe..8b629591 100644 --- a/uuid.c +++ b/uuid.c @@ -76,8 +76,8 @@ char *uuid_to_str(const uuid_t *uuid, char *const dst) case 4: case 6: case 8: case 10: *p++ = '-'; default: - *p++ = hexdigit[uuid->u.binary[i] >> 4]; - *p++ = hexdigit[uuid->u.binary[i] & 0xf]; + *p++ = hexdigit_lower[uuid->u.binary[i] >> 4]; + *p++ = hexdigit_lower[uuid->u.binary[i] & 0xf]; } } *p = '\0';