From: Baruch Siach <baruch@tkos.co.il>
To: barebox@lists.infradead.org
Subject: [PATCH 1/2] misc: introduce string_to_bin
Date: Thu, 5 Aug 2010 14:23:17 +0300 [thread overview]
Message-ID: <f375ed4cd64b27529dd717f6f39518eb261e1853.1281007209.git.baruch@tkos.co.il> (raw)
Factor out string_to_bin from string_to_ethaddr. string_to_bin is useful for
other hex data strings.
Signed-off-by: Baruch Siach <baruch@tkos.co.il>
---
include/common.h | 1 +
lib/misc.c | 29 +++++++++++++++++++++++++++++
net/net.c | 17 +----------------
3 files changed, 31 insertions(+), 16 deletions(-)
diff --git a/include/common.h b/include/common.h
index 0edc778..f600eb0 100644
--- a/include/common.h
+++ b/include/common.h
@@ -127,6 +127,7 @@ struct memarea_info {
int spec_str_to_info(const char *str, struct memarea_info *info);
int parse_area_spec(const char *str, ulong *start, ulong *size);
+int string_to_bin(const char *str, u8 *buf, unsigned int buflen);
/* Just like simple_strtoul(), but this one honors a K/M/G suffix */
unsigned long strtoul_suffix(const char *str, char **endp, int base);
diff --git a/lib/misc.c b/lib/misc.c
index 549b960..7735725 100644
--- a/lib/misc.c
+++ b/lib/misc.c
@@ -107,3 +107,32 @@ int parse_area_spec(const char *str, ulong *start, ulong *size)
return -1;
}
EXPORT_SYMBOL(parse_area_spec);
+
+/*
+ * This function parses strings in the form "xx:xx:xx...", where x is an
+ * hexadecimal digit.
+ */
+int string_to_bin(const char *str, u8 *buf, unsigned int buflen)
+{
+ int i;
+ char *e;
+
+ if (!str || strlen(str) != buflen*3 - 1)
+ return -1;
+
+ for (i = 0; i < buflen-1; i++)
+ if (str[i*3 + 2] != ':')
+ return -1;
+
+ for (i = 0; str[i]; i++)
+ if (!isxdigit(str[i]) && !(str[i] == ':'))
+ return -1;
+
+ for (i = 0; i < buflen; i++) {
+ buf[i] = simple_strtoul(str, &e, 16);
+ str = e + 1;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL(string_to_bin);
diff --git a/net/net.c b/net/net.c
index 8d99595..017de8b 100644
--- a/net/net.c
+++ b/net/net.c
@@ -161,22 +161,7 @@ void print_IPaddr (IPaddr_t x)
int string_to_ethaddr(const char *str, char *enetaddr)
{
- int reg;
- char *e;
-
- if (!str || strlen(str) != 17)
- return -1;
-
- if (str[2] != ':' || str[5] != ':' || str[8] != ':' ||
- str[11] != ':' || str[14] != ':')
- return -1;
-
- for (reg = 0; reg < 6; ++reg) {
- enetaddr[reg] = simple_strtoul (str, &e, 16);
- str = e + 1;
- }
-
- return 0;
+ return string_to_bin(str, enetaddr, 6);
}
void ethaddr_to_string(const unsigned char *enetaddr, char *str)
--
1.7.1
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next reply other threads:[~2010-08-05 11:23 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-08-05 11:23 Baruch Siach [this message]
2010-08-05 11:23 ` [PATCH 2/2] setser: new command to set serial number on ARM Baruch Siach
2010-08-18 4:49 ` Baruch Siach
2010-08-05 12:23 ` [PATCH 1/2] misc: introduce string_to_bin Jean-Christophe PLAGNIOL-VILLARD
2010-08-05 12:43 ` Baruch Siach
2010-08-05 13:41 ` Jean-Christophe PLAGNIOL-VILLARD
2010-08-05 14:13 ` Baruch Siach
2010-08-19 6:43 ` Jean-Christophe PLAGNIOL-VILLARD
2010-08-19 12:37 ` Baruch Siach
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=f375ed4cd64b27529dd717f6f39518eb261e1853.1281007209.git.baruch@tkos.co.il \
--to=baruch@tkos.co.il \
--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