Add `struct string` for strings parsed from FDT
Just a small wrapper around a char array to make things simpler.
Bug: 117551352
Change-Id: I0880ebbb81258830290ce49bf3772280551d3483
diff --git a/inc/hf/cpio.h b/inc/hf/cpio.h
index 1adc98f..d67f8d4 100644
--- a/inc/hf/cpio.h
+++ b/inc/hf/cpio.h
@@ -20,6 +20,7 @@
#include <stddef.h>
#include "hf/memiter.h"
+#include "hf/string.h"
-bool cpio_next(struct memiter *iter, const char **name, const void **contents,
- size_t *size);
+bool cpio_get_file(const struct memiter *cpio, const struct string *name,
+ struct memiter *it);
diff --git a/inc/hf/manifest.h b/inc/hf/manifest.h
index cdbdd39..23689d9 100644
--- a/inc/hf/manifest.h
+++ b/inc/hf/manifest.h
@@ -19,19 +19,15 @@
#include "hf/fdt.h"
#include "hf/memiter.h"
#include "hf/spci.h"
-
-/**
- * Maximum length of a string parsed from the FDT, including NULL terminator.
- */
-#define MANIFEST_MAX_STRING_LENGTH 32
+#include "hf/string.h"
/**
* Holds information about one of the VMs described in the manifest.
*/
struct manifest_vm {
/* Properties defined for both primary and secondary VMs. */
- char debug_name[MANIFEST_MAX_STRING_LENGTH];
- char kernel_filename[MANIFEST_MAX_STRING_LENGTH];
+ struct string debug_name;
+ struct string kernel_filename;
/* Properties specific to secondary VMs. */
struct {
diff --git a/inc/hf/string.h b/inc/hf/string.h
new file mode 100644
index 0000000..9c64632
--- /dev/null
+++ b/inc/hf/string.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2019 The Hafnium Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <stdbool.h>
+#include <stddef.h>
+
+/**
+ * Maximum length of a string including the NULL terminator.
+ * This is an arbitrary number and can be adjusted to fit use cases.
+ */
+#define STRING_MAX_SIZE 32
+
+enum string_return_code {
+ STRING_SUCCESS,
+ STRING_ERROR_INVALID_INPUT,
+ STRING_ERROR_TOO_LONG,
+};
+
+/**
+ * Statically-allocated string data structure with input validation to ensure
+ * strings are properly NULL-terminated.
+ *
+ * This is intentionally kept as simple as possible and should not be extended
+ * to perform complex string operations without a good use case.
+ */
+struct string {
+ char data[STRING_MAX_SIZE];
+};
+
+/**
+ * Macro to initialize `struct string` from a string constant.
+ * Triggers a compilation error if the string does not fit into the buffer.
+ */
+#define STRING_INIT(str) ((struct string){.data = str})
+
+enum string_return_code string_init(struct string *str, const char *data,
+ size_t size);
+void string_init_empty(struct string *str);
+bool string_is_empty(const struct string *str);
+const char *string_data(const struct string *str);