1 #pragma once
2 
3 #include "bej_common.h"
4 
5 #include <stdint.h>
6 
7 #ifdef __cplusplus
8 extern "C"
9 {
10 #endif
11 
12 /**
13  * @brief Mask for the type of the dictionary within a bejTupleS.
14  */
15 #define DICTIONARY_TYPE_MASK 0x01
16 
17 /**
18  * @brief Number of bits needed to shift to get the sequence number from a
19  * bejTupleS nnint value.
20  */
21 #define DICTIONARY_SEQ_NUM_SHIFT 1
22 
23 /**
24  * @brief BEJ dictionary type.
25  */
26 enum BejDictionaryType
27 {
28     bejPrimary = 0,
29     bejAnnotation = 1,
30 };
31 
32 /**
33  * @brief Dictionary property header.
34  */
35 struct BejDictionaryProperty
36 {
37     struct BejTupleF format;
38     uint16_t sequenceNumber;
39     uint16_t childPointerOffset;
40     uint16_t childCount;
41     uint8_t nameLength;
42     uint16_t nameOffset;
43 } __attribute__((__packed__));
44 
45 struct BejDictionaryHeader
46 {
47     uint8_t versionTag;
48     uint8_t truncationFlag:1;
49     uint8_t reservedFlags:7;
50     uint16_t entryCount;
51     uint32_t schemaVersion;
52     uint32_t dictionarySize;
53 } __attribute__((__packed__));
54 
55 /**
56  * @brief Get the offset of the first property in a dictionary.
57  *
58  * @return the offset to the first property.
59  */
60 uint16_t bejDictGetPropertyHeadOffset();
61 
62 /**
63  * @brief Get the offset of the first annotated property in an annoation
64  * dictionary.
65  *
66  * @return the offset to the first annotated property in an annoation
67  * dictionary.
68  */
69 uint16_t bejDictGetFirstAnnotatedPropertyOffset();
70 
71 /**
72  * @brief Get the property related to the given sequence number.
73  *
74  * @param[in] dictionary - dictionary containing the sequence number.
75  * @param[in] startingPropertyOffset - offset of the starting property for
76  * the search.
77  * @param[in] sequenceNumber - sequence number of the property.
78  * @param[out] property - if the search is successful, this will point to a
79  * valid property.
80  * @return 0 if successful.
81  */
82 int bejDictGetProperty(const uint8_t* dictionary,
83                        uint16_t startingPropertyOffset, uint16_t sequenceNumber,
84                        const struct BejDictionaryProperty** property);
85 
86 /**
87  * @brief Get the name of a property.
88  *
89  * @param[in] dictionary - dictionary containing the property.
90  * @param[in] nameOffset - dictionary offset of the name.
91  * @param[in] nameLength - length of the name.
92  * @return a NULL terminated string. If the nameLength is 0, this will
93  * return an empty string.
94  */
95 const char* bejDictGetPropertyName(const uint8_t* dictionary,
96                                    uint16_t nameOffset, uint8_t nameLength);
97 
98 /**
99  * @brief Get the property related to the given property name.
100  *
101  * @param[in] dictionary - dictionary containing the property.
102  * @param[in] startingPropertyOffset - offset of the starting property for
103  * the search.
104  * @param[in] propertyName - name of the searched property.
105  * @param[out] property - if the search is successful, this will point to a
106  * valid property.
107  * @param[out] propertyOffset - if the search is successful, this will point
108  * to the offset of the property within the dictionary. Can provide a NULL
109  * pointer if this is not needed.
110  * @return 0 if successful.
111  */
112 int bejDictGetPropertyByName(const uint8_t* dictionary,
113                              uint16_t startingPropertyOffset,
114                              const char* propertyName,
115                              const struct BejDictionaryProperty** property,
116                              uint16_t* propertyOffset);
117 
118 #ifdef __cplusplus
119 }
120 #endif
121