Mercurial > dovecot > core-2.2
changeset 9634:7fc9e93fe745 HEAD
Added binary_to_hex_append().
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 15 Jul 2009 18:28:59 -0400 |
parents | 4f9267f13944 |
children | c8be44ff1090 |
files | src/lib/Makefile.am src/lib/hex-binary.c src/lib/hex-binary.h src/lib/test-lib.c src/lib/test-lib.h |
diffstat | 5 files changed, 31 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/Makefile.am Wed Jul 15 16:09:09 2009 -0400 +++ b/src/lib/Makefile.am Wed Jul 15 18:28:59 2009 -0400 @@ -216,6 +216,7 @@ test-base64.c \ test-bsearch-insert-pos.c \ test-buffer.c \ + test-hex-binary.c \ test-istream.c \ test-mempool-alloconly.c \ test-network.c \
--- a/src/lib/hex-binary.c Wed Jul 15 16:09:09 2009 -0400 +++ b/src/lib/hex-binary.c Wed Jul 15 18:28:59 2009 -0400 @@ -4,17 +4,19 @@ #include "buffer.h" #include "hex-binary.h" -static const char * -binary_to_hex_case(const unsigned char *data, size_t size, bool ucase) +static void +binary_to_hex_case(unsigned char *dest, const unsigned char *data, + size_t size, bool ucase) { - char *buf, *p, base_char; + unsigned char *p; + char base_char; size_t i; int value; /* @UNSAFE */ base_char = ucase ? 'A' : 'a'; - buf = p = t_malloc(size * 2 + 1); + p = dest; for (i = 0; i < size; i++) { value = data[i] >> 4; *p++ = value < 10 ? value + '0' : value - 10 + base_char; @@ -22,19 +24,33 @@ value = data[i] & 0x0f; *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); + unsigned char *dest = t_malloc(size * 2 + 1); + + binary_to_hex_case(dest, data, size, FALSE); + dest[size*2] = '\0'; + return (char *)dest; } const char *binary_to_hex_ucase(const unsigned char *data, size_t size) { - return binary_to_hex_case(data, size, TRUE); + unsigned char *dest = t_malloc(size * 2 + 1); + + binary_to_hex_case(dest, data, size, TRUE); + dest[size*2] = '\0'; + return (char *)dest; +} + +void binary_to_hex_append(string_t *dest, const unsigned char *data, + size_t size) +{ + unsigned char *buf; + + buf = buffer_append_space_unsafe(dest, size * 2); + binary_to_hex_case(buf, data, size, FALSE); } int hex_to_binary(const char *data, buffer_t *dest)
--- a/src/lib/hex-binary.h Wed Jul 15 16:09:09 2009 -0400 +++ b/src/lib/hex-binary.h Wed Jul 15 18:28:59 2009 -0400 @@ -5,6 +5,9 @@ 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); +void binary_to_hex_append(string_t *dest, const unsigned char *data, + size_t size); + /* Convert hex to binary. data and dest may point to same value. Returns 0 if all ok, -1 if data is invalid. */ int hex_to_binary(const char *data, buffer_t *dest);
--- a/src/lib/test-lib.c Wed Jul 15 16:09:09 2009 -0400 +++ b/src/lib/test-lib.c Wed Jul 15 18:28:59 2009 -0400 @@ -10,6 +10,7 @@ test_base64, test_bsearch_insert_pos, test_buffer, + test_hex_binary, test_istream, test_mempool_alloconly, test_network,
--- a/src/lib/test-lib.h Wed Jul 15 16:09:09 2009 -0400 +++ b/src/lib/test-lib.h Wed Jul 15 18:28:59 2009 -0400 @@ -9,6 +9,7 @@ void test_base64(void); void test_bsearch_insert_pos(void); void test_buffer(void); +void test_hex_binary(void); void test_istream(void); void test_mempool_alloconly(void); void test_network(void);