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