Add helper functions for BEJ dictionaries
Signed-off-by: Kasun Athukorala <kasunath@google.com>
Change-Id: I50f2ccec155434639dfecd96193608cf6c5a1787
diff --git a/include/bej_dictionary.h b/include/bej_dictionary.h
new file mode 100644
index 0000000..13d979e
--- /dev/null
+++ b/include/bej_dictionary.h
@@ -0,0 +1,101 @@
+#pragma once
+
+#include "rde_common.h"
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/**
+ * @brief Mask for the type of the dictionary within a bejTupleS.
+ */
+#define DICTIONARY_TYPE_MASK 0x01
+
+/**
+ * @brief Number of bits needed to shift to get the sequence number from a
+ * bejTupleS nnint value.
+ */
+#define DICTIONARY_SEQ_NUM_SHIFT 1
+
+ /**
+ * @brief BEJ dictionary type.
+ */
+ enum BejDictionaryType
+ {
+ bejPrimary = 0,
+ bejAnnotation = 1,
+ };
+
+ /**
+ * @brief Dictionary property header.
+ */
+ struct BejDictionaryProperty
+ {
+ struct BejTupleF format;
+ uint16_t sequenceNumber;
+ uint16_t childPointerOffset;
+ uint16_t childCount;
+ uint8_t nameLength;
+ uint16_t nameOffset;
+ } __attribute__((__packed__));
+
+ struct BejDictionaryHeader
+ {
+ uint8_t versionTag;
+ uint8_t truncationFlag : 1;
+ uint8_t reservedFlags : 7;
+ uint16_t entryCount;
+ uint32_t schemaVersion;
+ uint32_t dictionarySize;
+ } __attribute__((__packed__));
+
+ /**
+ * @brief Get the offset of the first property in a dictionary.
+ *
+ * @return the offset to the first property.
+ */
+ uint16_t bejDictGetPropertyHeadOffset();
+
+ /**
+ * @brief Get the offset of the first annotated property in an annoation
+ * dictionary.
+ *
+ * @return the offset to the first annotated property in an annoation
+ * dictionary.
+ */
+ uint16_t bejDictGetFirstAnnotatedPropertyOffset();
+
+ /**
+ * @brief Get the property related to the given sequence number.
+ *
+ * @param[in] dictionary - dictionary containing the sequence number.
+ * @param[in] startingPropertyOffset - offset of the starting property for
+ * the search.
+ * @param[in] sequenceNumber - sequence number of the property.
+ * @param[out] property - if the search is successful, this will point to a
+ * valid property.
+ * @return 0 if successful.
+ */
+ int bejDictGetProperty(const uint8_t* dictionary,
+ uint16_t startingPropertyOffset,
+ uint16_t sequenceNumber,
+ const struct BejDictionaryProperty** property);
+
+ /**
+ * @brief Get the name of a property.
+ *
+ * @param[in] dictionary - dictionary containing the property.
+ * @param[in] nameOffset - dictionary offset of the name.
+ * @param[in] nameLength - length of the name.
+ * @return a NULL terminated string. If the nameLength is 0, this will
+ * return an empty string.
+ */
+ const char* bejDictGetPropertyName(const uint8_t* dictionary,
+ uint16_t nameOffset, uint8_t nameLength);
+
+#ifdef __cplusplus
+}
+#endif