dovecot-2.0: Added binary_to_hex_append().

dovecot at dovecot.org dovecot at dovecot.org
Thu Jul 16 01:32:05 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/7fc9e93fe745
changeset: 9634:7fc9e93fe745
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jul 15 18:28:59 2009 -0400
description:
Added binary_to_hex_append().

diffstat:

5 files changed, 31 insertions(+), 9 deletions(-)
src/lib/Makefile.am  |    1 +
src/lib/hex-binary.c |   34 +++++++++++++++++++++++++---------
src/lib/hex-binary.h |    3 +++
src/lib/test-lib.c   |    1 +
src/lib/test-lib.h   |    1 +

diffs (112 lines):

diff -r 4f9267f13944 -r 7fc9e93fe745 src/lib/Makefile.am
--- 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_lib_SOURCES = \
 	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 \
diff -r 4f9267f13944 -r 7fc9e93fe745 src/lib/hex-binary.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 @@ binary_to_hex_case(const unsigned char *
 		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)
diff -r 4f9267f13944 -r 7fc9e93fe745 src/lib/hex-binary.h
--- 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
 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);
diff -r 4f9267f13944 -r 7fc9e93fe745 src/lib/test-lib.c
--- 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 @@ int main(void)
 		test_base64,
 		test_bsearch_insert_pos,
 		test_buffer,
+		test_hex_binary,
 		test_istream,
 		test_mempool_alloconly,
 		test_network,
diff -r 4f9267f13944 -r 7fc9e93fe745 src/lib/test-lib.h
--- 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_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);


More information about the dovecot-cvs mailing list