Migrate to libfdt
Replace our custom FDT parser implementation with libfdt while retaining
the original API as a thin wrapper around libfdt. This minimizes the
changes to the rest of our code base and hides differences in coding
styles.
As a byproduct, this fixes an issue with unaligned memory accesses while
parsing as libfdt handles these correctly.
Bug: 150587116
Change-Id: I8d305d7094b1be04608048009d73d7c448a578a0
diff --git a/src/string.c b/src/string.c
index 4876a3f..f64d87c 100644
--- a/src/string.c
+++ b/src/string.c
@@ -30,14 +30,17 @@
* The constructor checks that it fits into the internal buffer and copies
* the string there.
*/
-enum string_return_code string_init(struct string *str, const char *data,
- size_t size)
+enum string_return_code string_init(struct string *str,
+ const struct memiter *data)
{
+ const char *base = memiter_base(data);
+ size_t size = memiter_size(data);
+
/*
* Require that the value contains exactly one NULL character and that
* it is the last byte.
*/
- if (size < 1 || memchr(data, '\0', size) != &data[size - 1]) {
+ if (size < 1 || memchr(base, '\0', size) != &base[size - 1]) {
return STRING_ERROR_INVALID_INPUT;
}
@@ -45,7 +48,7 @@
return STRING_ERROR_TOO_LONG;
}
- memcpy_s(str->data, sizeof(str->data), data, size);
+ memcpy_s(str->data, sizeof(str->data), base, size);
return STRING_SUCCESS;
}
@@ -58,3 +61,16 @@
{
return str->data;
}
+
+/**
+ * Returns true if the iterator `data` contains string `str`.
+ * Only characters until the first null terminator are compared.
+ */
+bool string_eq(const struct string *str, const struct memiter *data)
+{
+ const char *base = memiter_base(data);
+ size_t len = memiter_size(data);
+
+ return (len <= sizeof(str->data)) &&
+ (strncmp(str->data, base, len) == 0);
+}