MCPcopy Create free account
hub / github.com/DavidColson/Polybox / CborEncode

Function CborEncode

source/serialization.cpp:182–228  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

180// ***********************************************************************
181
182void CborEncode(ResizableArray<u8>& output, u8 majorType, u8* pData, i64 dataSize, bool indefinite = false) {
183 u8 additionalInfo;
184 u8 followingBytes;
185
186 // check if upper 32 bits are set, therefore needs 8 bytes of space
187 if (dataSize & ~(0x100000000ull - 1)) {
188 additionalInfo = 27;
189 followingBytes = 8;
190 // upper 16 bits this time (4 bytes)
191 } else if (dataSize & ~(0x10000ull - 1)) {
192 additionalInfo = 26;
193 followingBytes = 4;
194 // upper 8 bits (2 bytes)
195 } else if (dataSize & ~(0x100ull - 1)) {
196 additionalInfo = 25;
197 followingBytes = 2;
198 // upper 4 bits set (1 byte)
199 } else if (dataSize >= 24) {
200 additionalInfo = 24;
201 followingBytes = 1;
202 // remainder 0 - 23
203 } else {
204 additionalInfo = (u8)dataSize;
205 followingBytes = 0;
206 }
207
208 if (indefinite) {
209 additionalInfo = 31;
210 followingBytes = 0;
211 }
212
213 i64 requiredSize = (i64)dataSize + followingBytes + 1; // extra 1 for header
214 if (!(majorType == 2 || majorType == 3)) {
215 requiredSize -= (i64)dataSize;
216 }
217
218 // Allocate more space in output if needed
219 output.Reserve(output.GrowCapacity(output.count + requiredSize));
220
221 output.pData[output.count] = (u8)majorType << 5 | additionalInfo;
222 MemcpyBE(&output.pData[output.count+1], (u8*)&dataSize, followingBytes);
223 if (pData != nullptr) {
224 MemcpyLE(&output.pData[output.count+1+followingBytes], pData, dataSize);
225 }
226
227 output.count += requiredSize;
228}
229
230// ***********************************************************************
231

Callers 1

SerializeCborRecursiveFunction · 0.85

Calls 2

MemcpyBEFunction · 0.85
MemcpyLEFunction · 0.85

Tested by

no test coverage detected