Format UUID strings as lower case hex

This commit is contained in:
Andrew Bettison 2013-11-13 12:45:02 +10:30
parent 64db53a092
commit 1634d68dd0
5 changed files with 11 additions and 9 deletions

5
str.c
View File

@ -30,14 +30,15 @@
#include <limits.h>
#include <errno.h>
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;
}

3
str.h
View File

@ -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);

View File

@ -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';
}

View File

@ -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

4
uuid.c
View File

@ -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';