xref: /xnu-12377.41.6/EXTERNAL_HEADERS/CoreEntitlements/Serialization.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions //
2*bbb1b6f9SApple OSS Distributions //  Serialization.h
3*bbb1b6f9SApple OSS Distributions //  CoreEntitlements
4*bbb1b6f9SApple OSS Distributions //
5*bbb1b6f9SApple OSS Distributions //
6*bbb1b6f9SApple OSS Distributions 
7*bbb1b6f9SApple OSS Distributions #ifndef CORE_ENTITLEMENTS_SERIALIZATION_H
8*bbb1b6f9SApple OSS Distributions #define CORE_ENTITLEMENTS_SERIALIZATION_H
9*bbb1b6f9SApple OSS Distributions 
10*bbb1b6f9SApple OSS Distributions #ifndef _CE_INDIRECT
11*bbb1b6f9SApple OSS Distributions #error "Please include <CoreEntitlements/CoreEntitlements.h> instead of this file"
12*bbb1b6f9SApple OSS Distributions #endif
13*bbb1b6f9SApple OSS Distributions 
14*bbb1b6f9SApple OSS Distributions #include <CoreEntitlements/Result.h>
15*bbb1b6f9SApple OSS Distributions #include <CoreEntitlements/Runtime.h>
16*bbb1b6f9SApple OSS Distributions #include <CoreEntitlements/Entitlements.h>
17*bbb1b6f9SApple OSS Distributions 
18*bbb1b6f9SApple OSS Distributions __ptrcheck_abi_assume_single();
19*bbb1b6f9SApple OSS Distributions 
20*bbb1b6f9SApple OSS Distributions /*!
21*bbb1b6f9SApple OSS Distributions  * @enum CESerializedElementType_t
22*bbb1b6f9SApple OSS Distributions  * These are the primitive types that CoreEntitlements can serialize
23*bbb1b6f9SApple OSS Distributions  * Depending on the underlying representation some of these elements may be "virtual" or zero-sized.
24*bbb1b6f9SApple OSS Distributions  * However, they must still be included.
25*bbb1b6f9SApple OSS Distributions  */
26*bbb1b6f9SApple OSS Distributions OS_CLOSED_ENUM(CESerializedElementType, int64_t,
27*bbb1b6f9SApple OSS Distributions                /* A boolean element with a true / false value */
28*bbb1b6f9SApple OSS Distributions                kCESerializedBool = 1,
29*bbb1b6f9SApple OSS Distributions                /* A string element with a definite length */
30*bbb1b6f9SApple OSS Distributions                kCESerializedString = 2,
31*bbb1b6f9SApple OSS Distributions                /* A key string element with a definite length */
32*bbb1b6f9SApple OSS Distributions                kCESerializedKey = 3,
33*bbb1b6f9SApple OSS Distributions                /* An integer element, must be representable as int64_t */
34*bbb1b6f9SApple OSS Distributions                kCESerializedInteger = 4,
35*bbb1b6f9SApple OSS Distributions                /* Marks the start of an array / ordered sequence */
36*bbb1b6f9SApple OSS Distributions                kCESerializedArrayBegin = 5,
37*bbb1b6f9SApple OSS Distributions                /* Marks the end of an array / ordered sequence */
38*bbb1b6f9SApple OSS Distributions                kCESerializedArrayEnd = 6,
39*bbb1b6f9SApple OSS Distributions                /* Marks the start of a dictionary */
40*bbb1b6f9SApple OSS Distributions                /* The only valid elements contained in a dictionary are tuples (represented as an ordered sequence) */
41*bbb1b6f9SApple OSS Distributions                /* The first element of the ordered sequence must be kCESerializedString value */
42*bbb1b6f9SApple OSS Distributions                /* No restrictions are placed on the contents of the second element*/
43*bbb1b6f9SApple OSS Distributions                kCESerializedDictionaryBegin = 7,
44*bbb1b6f9SApple OSS Distributions                /* Marks the end of a dictionary */
45*bbb1b6f9SApple OSS Distributions                kCESerializedDictionaryEnd = 8,
46*bbb1b6f9SApple OSS Distributions                /* A data element with a definite length*/
47*bbb1b6f9SApple OSS Distributions                kCESerializedData = 9,
48*bbb1b6f9SApple OSS Distributions                );
49*bbb1b6f9SApple OSS Distributions 
50*bbb1b6f9SApple OSS Distributions /*!
51*bbb1b6f9SApple OSS Distributions  * @typedef CESerializedElement_t
52*bbb1b6f9SApple OSS Distributions  * This structure represents an encodable piece of data, along with its type and length (in bytes).
53*bbb1b6f9SApple OSS Distributions  * For the most part you will not need to use this structure manually, and instead you should use the helpers below
54*bbb1b6f9SApple OSS Distributions  */
55*bbb1b6f9SApple OSS Distributions typedef struct CESerializedElement {
56*bbb1b6f9SApple OSS Distributions     CESerializedElementType_t type;
57*bbb1b6f9SApple OSS Distributions     union {
58*bbb1b6f9SApple OSS Distributions #if !__has_ptrcheck
59*bbb1b6f9SApple OSS Distributions         void* bytes;
60*bbb1b6f9SApple OSS Distributions #endif
61*bbb1b6f9SApple OSS Distributions         int64_t value;
62*bbb1b6f9SApple OSS Distributions     } data;
63*bbb1b6f9SApple OSS Distributions     size_t length;
64*bbb1b6f9SApple OSS Distributions     bool pair;
65*bbb1b6f9SApple OSS Distributions } CESerializedElement_t;
66*bbb1b6f9SApple OSS Distributions 
CESerializedElementGetData(const CESerializedElement_t * element)67*bbb1b6f9SApple OSS Distributions static inline void *CE_HEADER_INDEXABLE CESerializedElementGetData(const CESerializedElement_t *element) {
68*bbb1b6f9SApple OSS Distributions     return __unsafe_forge_bidi_indexable(void *, element->data.value, element->length);
69*bbb1b6f9SApple OSS Distributions }
70*bbb1b6f9SApple OSS Distributions 
CESerializedElementSetData(CESerializedElement_t * element,void * __sized_by (length)bytes,size_t length)71*bbb1b6f9SApple OSS Distributions static inline void CESerializedElementSetData(CESerializedElement_t *element, void *__sized_by(length) bytes, size_t length) {
72*bbb1b6f9SApple OSS Distributions     element->data.value = (intptr_t)bytes;
73*bbb1b6f9SApple OSS Distributions     element->length = length;
74*bbb1b6f9SApple OSS Distributions }
75*bbb1b6f9SApple OSS Distributions 
76*bbb1b6f9SApple OSS Distributions /*!
77*bbb1b6f9SApple OSS Distributions  * @function CESizeSerialization
78*bbb1b6f9SApple OSS Distributions  * This function will iterate over the elements that are to be serialized and compute the size of an allocation that needs to be made
79*bbb1b6f9SApple OSS Distributions  * for a successful serialization.
80*bbb1b6f9SApple OSS Distributions  *
81*bbb1b6f9SApple OSS Distributions  * @note
82*bbb1b6f9SApple OSS Distributions  * This function may modify the length field of the CESerializedElements that are passed in. This must be done for serialization to succeed
83*bbb1b6f9SApple OSS Distributions  *
84*bbb1b6f9SApple OSS Distributions  * @returns
85*bbb1b6f9SApple OSS Distributions  * kCENoError if the requiredSize has been successfully populated and contains a valid value
86*bbb1b6f9SApple OSS Distributions  */
87*bbb1b6f9SApple OSS Distributions CEError_t CESizeSerialization(CESerializedElement_t elements[__counted_by(elementsCount)], size_t elementsCount, size_t* requiredSize) __result_use_check;
88*bbb1b6f9SApple OSS Distributions 
89*bbb1b6f9SApple OSS Distributions /*!
90*bbb1b6f9SApple OSS Distributions  * @function CESizeXMLSerialization
91*bbb1b6f9SApple OSS Distributions  * This function will iterate over the elements that are to be serialized and compute the size of an allocation that needs to be made
92*bbb1b6f9SApple OSS Distributions  * for a successful serialization to XML.
93*bbb1b6f9SApple OSS Distributions  *
94*bbb1b6f9SApple OSS Distributions  * @note
95*bbb1b6f9SApple OSS Distributions  * This function may modify the length field of the CESerializedElements that are passed in. This must be done for serialization to succeed
96*bbb1b6f9SApple OSS Distributions  *
97*bbb1b6f9SApple OSS Distributions  * @returns
98*bbb1b6f9SApple OSS Distributions  * kCENoError if the requiredSize has been successfully populated and contains a valid value
99*bbb1b6f9SApple OSS Distributions  */
100*bbb1b6f9SApple OSS Distributions CEError_t CESizeXMLSerialization(CESerializedElement_t elements[__counted_by(elementsCount)], size_t elementsCount, size_t* requiredSize) __result_use_check;
101*bbb1b6f9SApple OSS Distributions 
102*bbb1b6f9SApple OSS Distributions /*!
103*bbb1b6f9SApple OSS Distributions  * @function CESerializeWithOptions
104*bbb1b6f9SApple OSS Distributions  * Serializes the array of elements that contains the underlying data. The elements must have been sized with CESizeSerialization before calling this function.
105*bbb1b6f9SApple OSS Distributions  *
106*bbb1b6f9SApple OSS Distributions  * @param runtime
107*bbb1b6f9SApple OSS Distributions  * The runtime to use for this operation
108*bbb1b6f9SApple OSS Distributions  *
109*bbb1b6f9SApple OSS Distributions  * @param options
110*bbb1b6f9SApple OSS Distributions  * Options that modify what can be serialized.
111*bbb1b6f9SApple OSS Distributions  *
112*bbb1b6f9SApple OSS Distributions  * @param elements
113*bbb1b6f9SApple OSS Distributions  * The list of elements to serialize
114*bbb1b6f9SApple OSS Distributions  *
115*bbb1b6f9SApple OSS Distributions  * @param elementsCount
116*bbb1b6f9SApple OSS Distributions  * How many elements are in that list
117*bbb1b6f9SApple OSS Distributions  *
118*bbb1b6f9SApple OSS Distributions  * @param start
119*bbb1b6f9SApple OSS Distributions  * A pointer to the first byte into a buffer that will be filled with the serialized representation
120*bbb1b6f9SApple OSS Distributions  *
121*bbb1b6f9SApple OSS Distributions  * @param end
122*bbb1b6f9SApple OSS Distributions  * A pointer 1 byte past the end of the buffer to be used for serialization
123*bbb1b6f9SApple OSS Distributions  */
124*bbb1b6f9SApple OSS Distributions CEError_t CESerializeWithOptions(const CERuntime_t runtime, CEValidationOptions* options, CESerializedElement_t elements[__counted_by(elementsCount)], size_t elementsCount, uint8_t *__ended_by(end) start, uint8_t* end) __result_use_check;
125*bbb1b6f9SApple OSS Distributions 
126*bbb1b6f9SApple OSS Distributions /*!
127*bbb1b6f9SApple OSS Distributions  * @function CESerialize
128*bbb1b6f9SApple OSS Distributions  * Serializes the array of elements that contains the underlying data. The elements must have been sized with CESizeSerialization before calling this function.
129*bbb1b6f9SApple OSS Distributions  *
130*bbb1b6f9SApple OSS Distributions  * @param runtime
131*bbb1b6f9SApple OSS Distributions  * The runtime to use for this operation
132*bbb1b6f9SApple OSS Distributions  *
133*bbb1b6f9SApple OSS Distributions  * @param elements
134*bbb1b6f9SApple OSS Distributions  * The list of elements to serialize
135*bbb1b6f9SApple OSS Distributions  *
136*bbb1b6f9SApple OSS Distributions  * @param elementsCount
137*bbb1b6f9SApple OSS Distributions  * How many elements are in that list
138*bbb1b6f9SApple OSS Distributions  *
139*bbb1b6f9SApple OSS Distributions  * @param start
140*bbb1b6f9SApple OSS Distributions  * A pointer to the first byte into a buffer that will be filled with the serialized representation
141*bbb1b6f9SApple OSS Distributions  *
142*bbb1b6f9SApple OSS Distributions  * @param end
143*bbb1b6f9SApple OSS Distributions  * A pointer 1 byte past the end of the buffer to be used for serialization
144*bbb1b6f9SApple OSS Distributions  */
145*bbb1b6f9SApple OSS Distributions CEError_t CESerialize(const CERuntime_t runtime, CESerializedElement_t elements[__counted_by(elementsCount)], size_t elementsCount, uint8_t *__ended_by(end) start, uint8_t* end) __result_use_check;
146*bbb1b6f9SApple OSS Distributions 
147*bbb1b6f9SApple OSS Distributions /*!
148*bbb1b6f9SApple OSS Distributions  * @function CESerializeXML
149*bbb1b6f9SApple OSS Distributions  * Serializes the array of elements that contains the underlying data. The elements must have been sized with CESizeXMLSerialization before calling this function.
150*bbb1b6f9SApple OSS Distributions  *
151*bbb1b6f9SApple OSS Distributions  * @param runtime
152*bbb1b6f9SApple OSS Distributions  * The runtime to use for this operation
153*bbb1b6f9SApple OSS Distributions  *
154*bbb1b6f9SApple OSS Distributions  * @param elements
155*bbb1b6f9SApple OSS Distributions  * The list of elements to serialize
156*bbb1b6f9SApple OSS Distributions  *
157*bbb1b6f9SApple OSS Distributions  * @param elementsCount
158*bbb1b6f9SApple OSS Distributions  * How many elements are in that list
159*bbb1b6f9SApple OSS Distributions  *
160*bbb1b6f9SApple OSS Distributions  * @param start
161*bbb1b6f9SApple OSS Distributions  * A pointer to the first byte into a buffer that will be filled with the serialized representation
162*bbb1b6f9SApple OSS Distributions  *
163*bbb1b6f9SApple OSS Distributions  * @param end
164*bbb1b6f9SApple OSS Distributions  * A pointer 1 byte past the end of the buffer to be used for serialization
165*bbb1b6f9SApple OSS Distributions  */
166*bbb1b6f9SApple OSS Distributions CEError_t CESerializeXML(const CERuntime_t runtime, CESerializedElement_t elements[__counted_by(elementsCount)], size_t elementsCount, uint8_t *__ended_by(end) start, uint8_t* end) __result_use_check;
167*bbb1b6f9SApple OSS Distributions 
168*bbb1b6f9SApple OSS Distributions // Helpers
169*bbb1b6f9SApple OSS Distributions // These automatically construct CESerializedElements for you
170*bbb1b6f9SApple OSS Distributions #define CESerializeInteger(intv) (CESerializedElement_t){.type = kCESerializedInteger, .data.value = intv}
171*bbb1b6f9SApple OSS Distributions #define CESerializeBool(boolVal) (CESerializedElement_t){.type = kCESerializedBool, .data.value = !!boolVal}
172*bbb1b6f9SApple OSS Distributions #define CESerializeStaticString(strVal) (CESerializedElement_t){.type = kCESerializedString, .data.value = (intptr_t)strVal, .length = sizeof(strVal) - 1}
173*bbb1b6f9SApple OSS Distributions #define CESerializeKey(strVal) (CESerializedElement_t){.type = kCESerializedKey, .data.value = (intptr_t)strVal, .length = sizeof(strVal) - 1}
174*bbb1b6f9SApple OSS Distributions #define CESerializeDynamicKey(strVal, len) (CESerializedElement_t){.type = kCESerializedKey, .data.value = (intptr_t)strVal, .length = len}
175*bbb1b6f9SApple OSS Distributions #define CESerializeString(strVal, len) (CESerializedElement_t){.type = kCESerializedString, .data.value = (intptr_t)strVal, .length = len}
176*bbb1b6f9SApple OSS Distributions #define CESerializeData(dataVal, len) (CESerializedElement_t){.type = kCESerializedData, .data.value = (intptr_t)dataVal, .length = len}
177*bbb1b6f9SApple OSS Distributions #define CESerializeArray(...) (CESerializedElement_t){.type = kCESerializedArrayBegin}, __VA_ARGS__ , (CESerializedElement_t){.type = kCESerializedArrayEnd}
178*bbb1b6f9SApple OSS Distributions #define CESerializeDictionary(...) (CESerializedElement_t){.type = kCESerializedDictionaryBegin}, __VA_ARGS__ , (CESerializedElement_t){.type = kCESerializedDictionaryEnd}
179*bbb1b6f9SApple OSS Distributions #define CESerializeDictionaryPair(a, b) CESerializeArray(a, b)
180*bbb1b6f9SApple OSS Distributions 
181*bbb1b6f9SApple OSS Distributions #endif
182