From: Andy Shevchenko Date: Mon, 24 May 2010 21:33:23 +0000 (-0700) Subject: lib: introduce common method to convert hex digits X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=903788892ea0fc7fcaf7e8e5fac9a77379fc215b;p=GitHub%2FLineageOS%2Fandroid_kernel_samsung_universal7580.git lib: introduce common method to convert hex digits hex_to_bin() is a little method which converts hex digit to its actual value. There are plenty of places where such functionality is needed. [akpm@linux-foundation.org: use tolower(), saving 3 bytes, test the more common case first - it's quicker] [akpm@linux-foundation.org: relocate tolower to make it even faster! (Joe)] Signed-off-by: Andy Shevchenko Cc: Tilman Schmidt Cc: Duncan Sands Cc: Eric W. Biederman Cc: Greg Kroah-Hartman Cc: "Richard Russon (FlatCap)" Cc: John W. Linville Cc: Len Brown Cc: Joe Perches Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- diff --git a/include/linux/kernel.h b/include/linux/kernel.h index 05f332afc9e..8317ec4b9f3 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -375,6 +375,8 @@ static inline char *pack_hex_byte(char *buf, u8 byte) return buf; } +extern int hex_to_bin(char ch); + #ifndef pr_fmt #define pr_fmt(fmt) fmt #endif diff --git a/lib/hexdump.c b/lib/hexdump.c index 1bd6a977977..5d7a4802c56 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -15,6 +15,24 @@ const char hex_asc[] = "0123456789abcdef"; EXPORT_SYMBOL(hex_asc); +/** + * hex_to_bin - convert a hex digit to its real value + * @ch: ascii character represents hex digit + * + * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad + * input. + */ +int hex_to_bin(char ch) +{ + if ((ch >= '0') && (ch <= '9')) + return ch - '0'; + ch = tolower(ch); + if ((ch >= 'a') && (ch <= 'f')) + return ch - 'a' + 10; + return -1; +} +EXPORT_SYMBOL(hex_to_bin); + /** * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory * @buf: data blob to dump