From: Timo Sirainen Date: Wed, 28 Jul 2004 15:21:28 +0000 (+0300) Subject: Added binary_to_hex_ucase() X-Git-Tag: 1.1.alpha1~3730 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=edccd59381fdad36efac17838714da4adfe1edfc;p=thirdparty%2Fdovecot%2Fcore.git Added binary_to_hex_ucase() --HG-- branch : HEAD --- diff --git a/src/lib/hex-binary.c b/src/lib/hex-binary.c index 30be2ad32a..36ebf958d9 100644 --- a/src/lib/hex-binary.c +++ b/src/lib/hex-binary.c @@ -4,27 +4,39 @@ #include "buffer.h" #include "hex-binary.h" -const char *binary_to_hex(const unsigned char *data, size_t size) +static const char * +binary_to_hex_case(const unsigned char *data, size_t size, int ucase) { - char *buf, *p; + char *buf, *p, base_char; size_t i; int value; /* @UNSAFE */ + base_char = ucase ? 'A' : 'a'; buf = p = t_malloc(size * 2 + 1); for (i = 0; i < size; i++) { value = data[i] >> 4; - *p++ = value < 10 ? value + '0' : value - 10 + 'a'; + *p++ = value < 10 ? value + '0' : value - 10 + base_char; value = data[i] & 0x0f; - *p++ = value < 10 ? value + '0' : value - 10 + 'a'; + *p++ = value < 10 ? value + '0' : value - 10 + base_char; } *p = '\0'; return buf; } +const char *binary_to_hex(const unsigned char *data, size_t size) +{ + return binary_to_hex_case(data, size, FALSE); +} + +const char *binary_to_hex_ucase(const unsigned char *data, size_t size) +{ + return binary_to_hex_case(data, size, TRUE); +} + int hex_to_binary(const char *data, buffer_t *dest) { int value; diff --git a/src/lib/hex-binary.h b/src/lib/hex-binary.h index ba035c1a6a..58ba2d8350 100644 --- a/src/lib/hex-binary.h +++ b/src/lib/hex-binary.h @@ -1,9 +1,9 @@ #ifndef __HEX_BINARY_H #define __HEX_BINARY_H -/* Convert binary to lowercased hex digits allocating return value from - data stack */ +/* Convert binary to hex digits allocating return value from data stack */ const char *binary_to_hex(const unsigned char *data, size_t size); +const char *binary_to_hex_ucase(const unsigned char *data, size_t size); /* Convert hex to binary. data and dest may point to same value. Returns TRUE if successful. Returns 1 if all ok, 0 if dest buffer got full