UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
CborTypes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
9
14enum class ECborCode : uint8
15{
16 None = 0, // no code
17 // Major Types
18 Uint = 0 << 5, // positive/unsigned int
19 Int = 1 << 5, // negative number
20 ByteString = 2 << 5, // byte string
21 TextString = 3 << 5, // text string
22 Array = 4 << 5, // array
23 Map = 5 << 5, // map
24 Tag = 6 << 5, // semantic tag
25 Prim = 7 << 5, // bool, null, char, half-float, float, double, break code
26
27 // Additional Value Info
28 Value_1Byte = 0x18, // Additional value in next byte
29 Value_2Bytes = 0x19, // Additional value in next 2 bytes
30 Value_4Bytes = 0x1A, // Additional value in next 4 bytes
31 Value_8Bytes = 0x1B, // Additional value in next 8 bytes
32 Unused_28 = 0x1C, // Unused value in protocol
33 Unused_29 = 0x1D, // Unused value in protocol
34 Unused_30 = 0x1E, // Unused value in protocol
35 Indefinite = 0x1F, // Indicate indefinite containers
36
37 // Prim type codes
38 False = 0x14, // boolean
39 True = 0x15, // boolean
40 Null = 0x16, // null value
41 Undefined = 0x17, // undefined, unused in the writer
42
43 // Special values
44 Break = 0xFF, // break code (Prim | 31)
45
46 // Protocol unused values, used to report context or errors
47 // State
48 Dummy = 0x1C, // mark a dummy (Uint | 28)
49 StreamEnd = 0x3C, // stream end (Int | 28)
50 // Errors
51 ErrorReservedItem = 0x1D, // reserved value (Uint | 29)
52 ErrorStreamFailure = 0x1E, // stream error (Uint | 30)
53 ErrorBreak = 0x3D, // break not allowed (Int | 29)
54 ErrorMapContainer = 0x3E, // odd item number in map (Int | 30)
55 ErrorContext = 0x5E, // reader/writer context error (ByteString | 30)
56 ErrorStringNesting = 0x7D, // infinite string wrong type (TextString | 29)
57 ErrorStringLength = 0x7E, // invalid string length (TextString | 30)
58};
60
61class FCborReader;
62class FCborWriter;
63
68{
69public:
71 : Header(InHeader)
72 {}
76
78 void Set(ECborCode Code)
79 {
80 Header = (uint8)Code;
81 }
82
84 void Set(uint8 Code)
85 {
86 Header = Code;
87 }
88
90 uint8 Raw() const
91 {
92 return Header;
93 }
94
97 {
98 return (ECborCode)Header;
99 }
100
103 {
104 return (ECborCode)(Header & (7 << 5));
105 }
106
109 {
110 return (ECborCode)(Header & 0x1F);
111 }
112
115 {
116 return Ar << InHeader.Header;
117 }
118
119private:
121 uint8 Header;
122};
123
129{
131 : Header(ECborCode::Dummy)
132 , IntValue(0)
133 {}
134
136 void Reset()
137 {
138 *this = FCborContext();
139 }
140
143 {
144 return Header.RawCode();
145 }
146
149 {
150 return Header.MajorType();
151 }
152
155 {
156 return Header.AdditionalValue();
157 }
158
160 bool IsDummy()
161 {
162 return Header.RawCode() == ECborCode::Dummy;
163 }
164
166 bool IsError() const
167 {
168 // All error code have their additional value set to those 2 protocol unused values.
170 }
171
173 bool IsBreak() const
174 {
175 return Header.RawCode() == ECborCode::Break;
176 }
177
179 bool IsString() const
180 {
182 }
183
185 bool IsContainer() const
186 {
188 }
189
196
198 bool IsFiniteContainer() const
199 {
202 }
203
206 {
207 check(Header.RawCode() == ECborCode::Break && RawTextValue.Num() == 1);
208 return (ECborCode)RawTextValue[0];
209 }
210
217
220 {
222 return UIntValue;
223 }
224
226 int64 AsInt() const
227 {
229 return IntValue;
230 }
231
233 bool AsBool() const
234 {
236 return BoolValue;
237 }
238
240 float AsFloat() const
241 {
243 return FloatValue;
244 }
245
247 double AsDouble() const
248 {
250 return DoubleValue;
251 }
252
254 FString AsString() const
255 {
257 return FString(FUTF8ToTCHAR(RawTextValue.GetData()).Get());
258 }
259
262 {
263 // Data loss is possible here
265 return FAnsiString(RawTextValue.GetData());
266 }
267
270 {
272 return FUtf8String(RawTextValue.GetData());
273 }
274
276 const char* AsCString() const
277 {
279 return RawTextValue.GetData();
280 }
281
284 {
286 return MakeArrayView(reinterpret_cast<const uint8*>(RawTextValue.GetData()), RawTextValue.Num() - 1); // -1 to exclude the null terminated added by the reader.
287 }
288
289private:
290 friend class FCborReader;
291 friend class FCborWriter;
292
294 : Header(Code)
295 , IntValue(0)
296 {}
297
298 // Holds the context header.
300
302 union
303 {
310 };
311 // Hold text value separately since, non trivial type are a mess in union, also used to report container type for break code
312 TArray<char> RawTextValue;
313};
314
317{
319 Platform,
320
322 BigEndian,
323
326
329};
330
336{
337public:
339 ScopedCborArchiveEndianness(FArchive& InArchive, ECborEndianness Endianness) : Archive(InArchive), bOldByteSwappingState(InArchive.IsByteSwapping())
340 {
341 constexpr bool bLittleEndianPlatform = PLATFORM_LITTLE_ENDIAN != 0;
343 }
344
346 {
347 Archive.SetByteSwapping(bOldByteSwappingState); // Restore the original state whatever it was.
348 }
349
350private:
351 FArchive& Archive;
352 bool bOldByteSwappingState;
353};
#define PLATFORM_LITTLE_ENDIAN
Definition AndroidPlatform.h:38
constexpr auto MakeArrayView(OtherRangeType &&Other)
Definition ArrayView.h:873
#define check(expr)
Definition AssertionMacros.h:314
ECborEndianness
Definition CborTypes.h:317
ECborCode
Definition CborTypes.h:15
@ ErrorReservedItem
@ ErrorStringNesting
@ ErrorStringLength
@ ErrorStreamFailure
@ ErrorMapContainer
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
TStringConversion< FUTF8ToTCHAR_Convert > FUTF8ToTCHAR
Definition StringConv.h:1017
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition Archive.h:1208
void SetByteSwapping(bool Enabled)
Definition Archive.h:546
Definition CborTypes.h:68
void Set(uint8 Code)
Definition CborTypes.h:84
ECborCode MajorType() const
Definition CborTypes.h:102
FCborHeader(uint8 InHeader=0)
Definition CborTypes.h:70
ECborCode RawCode() const
Definition CborTypes.h:96
FCborHeader(ECborCode InHeader)
Definition CborTypes.h:73
uint8 Raw() const
Definition CborTypes.h:90
ECborCode AdditionalValue() const
Definition CborTypes.h:108
void Set(ECborCode Code)
Definition CborTypes.h:78
friend FArchive & operator<<(FArchive &Ar, FCborHeader &InHeader)
Definition CborTypes.h:114
Definition CborReader.h:17
Definition CborWriter.h:20
Definition CborTypes.h:336
~ScopedCborArchiveEndianness()
Definition CborTypes.h:345
ScopedCborArchiveEndianness(FArchive &InArchive, ECborEndianness Endianness)
Definition CborTypes.h:339
Definition ArrayView.h:139
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
Definition CborTypes.h:129
uint64 UIntValue
Definition CborTypes.h:305
double AsDouble() const
Definition CborTypes.h:247
FUtf8String AsUtf8String() const
Definition CborTypes.h:269
TArrayView< const uint8 > AsByteArray() const
Definition CborTypes.h:283
bool IsDummy()
Definition CborTypes.h:160
float AsFloat() const
Definition CborTypes.h:240
double DoubleValue
Definition CborTypes.h:308
int64 AsInt() const
Definition CborTypes.h:226
bool AsBool() const
Definition CborTypes.h:233
uint64 AsUInt() const
Definition CborTypes.h:219
uint64 Length
Definition CborTypes.h:309
const char * AsCString() const
Definition CborTypes.h:276
float FloatValue
Definition CborTypes.h:307
void Reset()
Definition CborTypes.h:136
bool IsString() const
Definition CborTypes.h:179
ECborCode RawCode() const
Definition CborTypes.h:142
bool IsContainer() const
Definition CborTypes.h:185
bool IsIndefiniteContainer() const
Definition CborTypes.h:191
ECborCode AdditionalValue() const
Definition CborTypes.h:154
bool IsError() const
Definition CborTypes.h:166
bool IsBreak() const
Definition CborTypes.h:173
ECborCode AsBreak() const
Definition CborTypes.h:205
bool BoolValue
Definition CborTypes.h:306
FCborContext()
Definition CborTypes.h:130
bool IsFiniteContainer() const
Definition CborTypes.h:198
ECborCode MajorType() const
Definition CborTypes.h:148
FAnsiString AsAnsiString() const
Definition CborTypes.h:261
uint64 AsLength() const
Definition CborTypes.h:212
int64 IntValue
Definition CborTypes.h:304
FString AsString() const
Definition CborTypes.h:254