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
diff --git a/include/rde_common.h b/include/rde_common.h
index bb55a37..f377507 100644
--- a/include/rde_common.h
+++ b/include/rde_common.h
@@ -27,13 +27,13 @@
      */
     enum BejError
     {
-        BejErrorNoError = 0,
-        BejErrorUnknown,
-        BejErrorInvalidSize,
-        BejErrorNotSuppoted,
-        BejErrorUnknownProperty,
-        BejErrorInvalidSchemaType,
-        BejErrorInvalidPropertyOffset,
+        bejErrorNoError = 0,
+        bejErrorUnknown,
+        bejErrorInvalidSize,
+        bejErrorNotSuppoted,
+        bejErrorUnknownProperty,
+        bejErrorInvalidSchemaType,
+        bejErrorInvalidPropertyOffset,
     };
 
     /**
@@ -41,11 +41,11 @@
      */
     enum BejSchemaClass
     {
-        BejMajorSchemaClass = 0,
-        BejEventSchemaClass = 1,
-        BejAnnotationSchemaClass = 2,
-        BejCollectionMemberTypeSchemaClass = 3,
-        BejErrorSchemaClass = 4,
+        bejMajorSchemaClass = 0,
+        bejEventSchemaClass = 1,
+        bejAnnotationSchemaClass = 2,
+        bejCollectionMemberTypeSchemaClass = 3,
+        bejErrorSchemaClass = 4,
     };
 
     /**
@@ -53,22 +53,22 @@
      */
     enum BejPrincipalDataType
     {
-        BejSet = 0,
-        BejArray = 1,
-        BejNull = 2,
-        BejInteger = 3,
-        BejEnum = 4,
-        BejString = 5,
-        BejReal = 6,
-        BejBoolean = 7,
-        BejBytestring = 8,
-        BejChoice = 9,
-        BejPropertyAnnotation = 10,
-        Reserved1 = 11,
-        Reserved2 = 12,
-        Reserved3 = 13,
-        BejResourceLink = 14,
-        BejResourceLinkExpansion = 15,
+        bejSet = 0,
+        bejArray = 1,
+        bejNull = 2,
+        bejInteger = 3,
+        bejEnum = 4,
+        bejString = 5,
+        bejReal = 6,
+        bejBoolean = 7,
+        bejBytestring = 8,
+        bejChoice = 9,
+        bejPropertyAnnotation = 10,
+        bejPrincipalDataReserved1 = 11,
+        bejPrincipalDataReserved2 = 12,
+        bejPrincipalDataReserved3 = 13,
+        bejResourceLink = 14,
+        bejResourceLinkExpansion = 15,
     };
 
     /**
@@ -145,21 +145,21 @@
 
     enum RdeOperationInitType
     {
-        RdeOpInitOperationHead = 0,
-        RdeOpInitOperationRead = 1,
-        RdeOpInitOperationCreate = 2,
-        RdeOpInitOperationDelete = 3,
-        RdeOpInitOperationUpdate = 4,
-        RdeOpInitOperationReplace = 5,
-        RdeOpInitOperationAction = 6,
+        rdeOpInitOperationHead = 0,
+        rdeOpInitOperationRead = 1,
+        rdeOpInitOperationCreate = 2,
+        rdeOpInitOperationDelete = 3,
+        rdeOpInitOperationUpdate = 4,
+        rdeOpInitOperationReplace = 5,
+        rdeOpInitOperationAction = 6,
     };
 
     enum RdeMultiReceiveTransferFlag
     {
-        RdeMRecFlagStart = 0,
-        RdeMRecFlagMiddle = 1,
-        RdeMRecFlagEnd = 2,
-        RdeMRecFlagStartAndEnd = 3,
+        rdeMRecFlagStart = 0,
+        rdeMRecFlagMiddle = 1,
+        rdeMRecFlagEnd = 2,
+        rdeMRecFlagStartAndEnd = 3,
     };
 
     struct RdeOperationInitReqHeader
@@ -190,9 +190,11 @@
     /**
      * @brief Get the unsigned integer value from provided bytes.
      *
-     * @param bytes - valid pointer to a byte stream in little-endian format.
-     * @param numOfBytes - number of bytes belongs to the value. Maximum number
-     * of bytes supported is 8. If numOfBytes > 8, the result is undefined.
+     * @param[in] bytes - valid pointer to a byte stream in little-endian
+     * format.
+     * @param[in] numOfBytes - number of bytes belongs to the value. Maximum
+     * number of bytes supported is 8. If numOfBytes > 8, the result is
+     * undefined.
      * @return unsigend 64bit representation of the value.
      */
     uint64_t rdeGetUnsignedInteger(const uint8_t* bytes, uint8_t numOfBytes);
@@ -200,7 +202,7 @@
     /**
      * @brief Get the value from nnint type.
      *
-     * @param  nnint - nnint should be pointing to a valid nnint.
+     * @param[in] nnint - nnint should be pointing to a valid nnint.
      * @return unsigend 64bit representation of the value.
      */
     uint64_t rdeGetNnint(const uint8_t* nnint);
@@ -208,7 +210,7 @@
     /**
      * @brief Get the size of the complete nnint.
      *
-     * @param nnint - pointer to a valid nnint.
+     * @param[in] nnint - pointer to a valid nnint.
      * @return size of the complete nnint.
      */
     uint8_t rdeGetNnintSize(const uint8_t* nnint);