From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] lib: Add hex2bin and bin2hex implementations
Date: Fri, 13 Mar 2015 08:23:30 +0100 [thread overview]
Message-ID: <1426231410-14049-1-git-send-email-s.hauer@pengutronix.de> (raw)
Taken from the Kernel, put into the same place as in the kernel,
although the hexdump.c does not actually contain hexdum functions.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/linux/kernel.h | 4 +++
lib/Makefile | 1 +
lib/hexdump.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++
lib/misc.c | 2 --
4 files changed, 79 insertions(+), 2 deletions(-)
create mode 100644 lib/hexdump.c
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 5b6b448..945e063 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -246,6 +246,10 @@ static inline char *hex_byte_pack_upper(char *buf, u8 byte)
return buf;
}
+extern int hex_to_bin(char ch);
+extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
+extern char *bin2hex(char *dst, const void *src, size_t count);
+
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
diff --git a/lib/Makefile b/lib/Makefile
index f08ac59..6a3e9fd 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -53,3 +53,4 @@ obj-y += wchar.o
obj-y += libfile.o
obj-y += bitmap.o
obj-y += gcd.o
+obj-y += hexdump.o
diff --git a/lib/hexdump.c b/lib/hexdump.c
new file mode 100644
index 0000000..3b1d5e6
--- /dev/null
+++ b/lib/hexdump.c
@@ -0,0 +1,74 @@
+/*
+ * lib/hexdump.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation. See README and COPYING for
+ * more details.
+ */
+
+#include <common.h>
+#include <linux/kernel.h>
+#include <linux/ctype.h>
+
+const char hex_asc[] = "0123456789abcdef";
+EXPORT_SYMBOL(hex_asc);
+const char hex_asc_upper[] = "0123456789ABCDEF";
+EXPORT_SYMBOL(hex_asc_upper);
+
+/**
+ * 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);
+
+/**
+ * hex2bin - convert an ascii hexadecimal string to its binary representation
+ * @dst: binary result
+ * @src: ascii hexadecimal string
+ * @count: result length
+ *
+ * Return 0 on success, -1 in case of bad input.
+ */
+int hex2bin(u8 *dst, const char *src, size_t count)
+{
+ while (count--) {
+ int hi = hex_to_bin(*src++);
+ int lo = hex_to_bin(*src++);
+
+ if ((hi < 0) || (lo < 0))
+ return -1;
+
+ *dst++ = (hi << 4) | lo;
+ }
+ return 0;
+}
+EXPORT_SYMBOL(hex2bin);
+
+/**
+ * bin2hex - convert binary data to an ascii hexadecimal string
+ * @dst: ascii hexadecimal result
+ * @src: binary data
+ * @count: binary data length
+ */
+char *bin2hex(char *dst, const void *src, size_t count)
+{
+ const unsigned char *_src = src;
+
+ while (count--)
+ dst = hex_byte_pack(dst, *_src++);
+ return dst;
+}
+EXPORT_SYMBOL(bin2hex);
diff --git a/lib/misc.c b/lib/misc.c
index 87626c1..62ddd66 100644
--- a/lib/misc.c
+++ b/lib/misc.c
@@ -114,5 +114,3 @@ int parse_area_spec(const char *str, loff_t *start, loff_t *size)
return -1;
}
EXPORT_SYMBOL(parse_area_spec);
-
-const char hex_asc[] = "0123456789abcdef";
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
reply other threads:[~2015-03-13 7:23 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1426231410-14049-1-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox