[PATCH v1.5 1/5] lib: hex2bin converts ascii hexadecimal string to binary

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Mimi Zohar
Date: Tuesday, November 23, 2010 - 4:43 pm

Similar to the kgdb_hex2mem() code, hex2bin converts a string
to binary using the hex_to_bin() library call.

Changelog:
- Replace parameter names with src/dst (based on David Howell's comment)
- Add 'const' where needed (based on David Howell's comment)
- Replace int with size_t (based on David Howell's comment)

Signed-off-by: Mimi Zohar <zohar@us.ibm.com>
Acked-by: Serge E. Hallyn <serge@hallyn.com>
Acked-by: David Howells <dhowells@redhat.com>
---
 include/linux/kernel.h |    1 +
 lib/hexdump.c          |   16 ++++++++++++++++
 2 files changed, 17 insertions(+), 0 deletions(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 3f648d2..fbca217 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -419,6 +419,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte)
 }
 
 extern int hex_to_bin(char ch);
+extern void hex2bin(u8 *dst, const char *src, size_t count);
 
 #ifndef pr_fmt
 #define pr_fmt(fmt) fmt
diff --git a/lib/hexdump.c b/lib/hexdump.c
index 5d7a480..b66b2bd 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -34,6 +34,22 @@ int hex_to_bin(char ch)
 EXPORT_SYMBOL(hex_to_bin);
 
 /**
+ * hex2bin - convert an ascii hexadecimal string to its binary representation
+ * @dst: binary result
+ * @src: ascii hexadecimal string
+ * @count: result length
+ */
+void hex2bin(u8 *dst, const char *src, size_t count)
+{
+	while (count--) {
+		*dst = hex_to_bin(*src++) << 4;
+		*dst += hex_to_bin(*src++);
+		dst++;
+	}
+}
+EXPORT_SYMBOL(hex2bin);
+
+/**
  * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
  * @buf: data blob to dump
  * @len: number of bytes in the @buf
-- 
1.7.2.2



--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH v1.5 0/5] keys: trusted and encrypted keys, Mimi Zohar, (Tue Nov 23, 3:50 pm)
[PATCH v1.5 2/5] tpm: add module_put wrapper, Mimi Zohar, (Tue Nov 23, 3:50 pm)
[PATCH v1.5 4/5] keys: add new trusted key-type, Mimi Zohar, (Tue Nov 23, 3:50 pm)
[PATCH v1.5 1/5] lib: hex2bin converts ascii hexadecimal s ..., Mimi Zohar, (Tue Nov 23, 4:43 pm)
[PATCH v1.5 3/5] key: add tpm_send command, Mimi Zohar, (Tue Nov 23, 4:54 pm)
Re: [PATCH v1.5 2/5] tpm: add module_put wrapper, Serge Hallyn, (Tue Nov 23, 7:19 pm)
Re: [PATCH v1.5 3/5] key: add tpm_send command, Serge Hallyn, (Tue Nov 23, 7:32 pm)
Re: [PATCH v1.5 3/5] key: add tpm_send command, David Safford, (Wed Nov 24, 5:46 am)
Re: [PATCH v1.5 3/5] key: add tpm_send command, Serge Hallyn, (Wed Nov 24, 7:59 am)
[PATCH v1.5 5/5] keys: add new key-type encrypted, Mimi Zohar, (Wed Nov 24, 9:21 am)
Re: [PATCH v1.5 3/5] key: add tpm_send command, David Safford, (Wed Nov 24, 9:31 am)
Re: [PATCH v1.5 0/5] keys: trusted and encrypted keys, James Morris, (Sun Nov 28, 2:56 pm)
Re: [PATCH v1.5 3/5] key: add tpm_send command, David Howells, (Tue Nov 30, 7:32 am)
Re: [PATCH v1.5 3/5] key: add tpm_send command, David Safford, (Tue Nov 30, 8:22 am)
Re: [PATCH v1.5 4/5] keys: add new trusted key-type, David Howells, (Wed Dec 1, 10:48 am)
Re: [PATCH v1.5 4/5] keys: add new trusted key-type, David Safford, (Wed Dec 1, 2:18 pm)
Re: [PATCH v1.5 5/5] keys: add new key-type encrypted, David Howells, (Fri Dec 3, 6:42 am)
Re: [PATCH v1.5 5/5] keys: add new key-type encrypted, David Howells, (Wed Dec 8, 3:54 am)