[coreboot-gerrit] New patch to review for coreboot: strtobin: Add a library function to decode a string into binary

Duncan Laurie (dlaurie@google.com) gerrit at coreboot.org
Mon May 16 21:29:34 CEST 2016


Duncan Laurie (dlaurie at google.com) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/14837

-gerrit

commit 51c22955e156d3130276b8cd3861b213fb894f52
Author: Duncan Laurie <dlaurie at chromium.org>
Date:   Mon May 9 10:58:03 2016 -0700

    strtobin: Add a library function to decode a string into binary
    
    This function will turn a string of ASCII hex characters into an array of
    bytes.  It will ignore any non-ASCII-hex characters in the input string and
    decode up to len bytes of data from it.
    
    This can be used for turning MAC addresses or UUID strings into binary for
    for storage or further processing.
    
    Sample usage:
      uint8_t buf[6];
      strtohex("00:0e:c6:81:72:01", buf, sizeof(buf));
      acpigen_emit_stream(buf, sizeof(buf));
    
    Change-Id: I2de9bd28ae8c42cdca09eec11a3bba497a52988c
    Signed-off-by: Duncan Laurie <dlaurie at chromium.org>
---
 src/include/lib.h    | 13 +++++++++++++
 src/lib/Makefile.inc |  1 +
 src/lib/strtobin.c   | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)

diff --git a/src/include/lib.h b/src/include/lib.h
index 737ab36..6713628 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -44,6 +44,19 @@ int checkstack(void *top_of_stack, int core);
 void hexdump(const void *memory, size_t length);
 void hexdump32(char LEVEL, const void *d, size_t len);
 
+/*
+ * strtobin - Turn a string of ASCII hex characters into binary
+ *
+ * @str: String to parse, accepted input is [0-9][a-f][A-F]
+ * @buf: Buffer to store the resulting bytes into
+ * @len: Maximum length of buffer to fill
+ *
+ * Defined in src/lib/strtobin.c
+ * Ignores non-hex characters in the string.
+ * Returns the number of bytes that have been put in the buffer.
+ */
+size_t strtobin(const char *str, uint8_t *buf, size_t len);
+
 #if !defined(__ROMCC__)
 /* Count Leading Zeroes: clz(0) == 32, clz(0xf) == 28, clz(1 << 31) == 0 */
 static inline int clz(u32 x) { return x ? __builtin_clz(x) : sizeof(x) * 8; }
diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc
index 734b517..2a291eb 100644
--- a/src/lib/Makefile.inc
+++ b/src/lib/Makefile.inc
@@ -114,6 +114,7 @@ ramstage-y += cbfs.c
 ramstage-$(CONFIG_COMMON_CBFS_SPI_WRAPPER) += cbfs_spi.c
 ramstage-y += lzma.c lzmadecode.c
 ramstage-y += stack.c
+ramstage-y += strtobin.c
 ramstage-y += wrdd.c
 ramstage-$(CONFIG_CONSOLE_CBMEM) += cbmem_console.c
 ramstage-$(CONFIG_BOOTSPLASH) += jpeg.c
diff --git a/src/lib/strtobin.c b/src/lib/strtobin.c
new file mode 100644
index 0000000..71c6632
--- /dev/null
+++ b/src/lib/strtobin.c
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but without any warranty; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <lib.h>
+#include <string.h>
+
+/*
+ * strtobin - Turn a string of ASCII hex characters into binary
+ *
+ * @str: String to parse, accepted input is [0-9][a-f][A-F]
+ * @buf: Buffer to store the resulting bytes into
+ * @len: Maximum length of buffer to fill
+ *
+ * Ignores non-hex characters in the string.
+ * Returns the number of bytes that have been put in the buffer.
+ */
+size_t strtobin(const char *str, uint8_t *buf, size_t len)
+{
+	size_t count, ptr = 0;
+	uint8_t byte;
+
+	for (byte = count = 0; str && *str; str++) {
+		uint8_t c = *str;
+
+		if (!isxdigit(c))
+			continue;
+		if (isdigit(c))
+			c -= '0';
+		else
+			c = tolower(c) - 'a' + 10;
+
+		byte <<= 4;
+		byte |= c;
+
+		if (++count > 1) {
+			if (ptr > len)
+				return ptr;
+			buf[ptr++] = byte;
+			byte = count = 0;
+		}
+	}
+
+	return ptr;
+}



More information about the coreboot-gerrit mailing list