From: Mimi Zohar Subject: [PATCH v1.2 1/4] lib: hex2bin converts ascii hexadecimal string to binary Date: Mon, 8 Nov 2010 10:30:43 -0500 Message-ID: <1289230246-3856-2-git-send-email-zohar@linux.vnet.ibm.com> References: <1289230246-3856-1-git-send-email-zohar@linux.vnet.ibm.com> Cc: Mimi Zohar , linux-security-module@vger.kernel.org, keyrings@linux-nfs.org, linux-crypto@vger.kernel.org, David Howells , Jason Gunthorpe , James Morris , David Safford , Rajiv Andrade , Mimi Zohar To: linux-kernel@vger.kernel.org Return-path: Received: from e2.ny.us.ibm.com ([32.97.182.142]:48841 "EHLO e2.ny.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751147Ab0KHPa4 (ORCPT ); Mon, 8 Nov 2010 10:30:56 -0500 In-Reply-To: <1289230246-3856-1-git-send-email-zohar@linux.vnet.ibm.com> Sender: linux-crypto-owner@vger.kernel.org List-ID: Similar to the kgdb_hex2mem() code, hex2bin converts a string to binary using the hex_to_bin() library call. Signed-off-by: Mimi Zohar Acked-by: Serge E. Hallyn --- 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 450092c..efe0e1e 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -417,6 +417,7 @@ static inline char *pack_hex_byte(char *buf, u8 byte) } extern int hex_to_bin(char ch); +extern void hex2bin(unsigned char *mem, char *buf, int count); #ifndef pr_fmt #define pr_fmt(fmt) fmt diff --git a/lib/hexdump.c b/lib/hexdump.c index 5d7a480..66f96bb 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 + * @mem: result + * @buf: ascii hexadecimal string + * @count: result length + */ +void hex2bin(unsigned char *mem, char *buf, int count) +{ + while (count--) { + *mem = hex_to_bin(*buf++) << 4; + *mem += hex_to_bin(*buf++); + mem++; + } +} +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