UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Crc.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
8#include "Misc/CString.h"
9#include "Misc/Char.h"
11#include "Traits/IsCharType.h"
12
13template <typename... Types>
14struct TTuple;
15
19struct FCrc
20{
22 static CORE_API uint32 CRCTablesSB8[8][256];
23
26 static CORE_API void Init();
27
29 typedef uint32 (*MemCrc32Functor)( const void* Data, int32 Length, uint32 CRC );
31 [[nodiscard]] static UE_FORCEINLINE_HINT uint32 MemCrc32(const void* Data, int32 Length, uint32 CRC = 0)
32 {
33 return MemCrc32Func(Data, Length, CRC);
34 }
35
37 template <typename T>
38 [[nodiscard]] static uint32 TypeCrc32( const T& Data, uint32 CRC=0 )
39 {
40 return MemCrc32(&Data, sizeof(T), CRC);
41 }
42
44 template <typename CharType>
45 [[nodiscard]] static uint32 StrCrc32(const CharType* Data, uint32 CRC = 0)
46 {
47 // We ensure that we never try to do a StrCrc32 with a CharType of more than 4 bytes. This is because
48 // we always want to treat every CRC as if it was based on 4 byte chars, even if it's less, because we
49 // want consistency between equivalent strings with different character types.
50 static_assert(sizeof(CharType) <= 4, "StrCrc32 only works with CharType up to 32 bits.");
51
52 CRC = ~CRC;
53 while (CharType Ch = *Data++)
54 {
55 uint32 V = CRC ^ Ch;
56 CRC =
57 CRCTablesSB8[3][ V & 0xFF] ^
58 CRCTablesSB8[2][(V >> 8) & 0xFF] ^
59 CRCTablesSB8[1][(V >> 16) & 0xFF] ^
60 CRCTablesSB8[0][ V >> 24 ];
61 }
62 return ~CRC;
63 }
64
66 template <typename CharType>
67 [[nodiscard]] static uint32 StrCrc32Len(const CharType* Data, int32 Length, uint32 CRC = 0)
68 {
69 // We ensure that we never try to do a StrCrc32 with a CharType of more than 4 bytes. This is because
70 // we always want to treat every CRC as if it was based on 4 byte chars, even if it's less, because we
71 // want consistency between equivalent strings with different character types.
72 static_assert(sizeof(CharType) <= 4, "StrCrc32Len only works with CharType up to 32 bits.");
73
74 CRC = ~CRC;
75 for (int32 Idx = 0; Idx < Length; ++Idx)
76 {
77 uint32 V = CRC ^ Data[Idx];
78 CRC =
79 CRCTablesSB8[3][ V & 0xFF] ^
80 CRCTablesSB8[2][(V >> 8) & 0xFF] ^
81 CRCTablesSB8[1][(V >> 16) & 0xFF] ^
82 CRCTablesSB8[0][ V >> 24 ];
83 }
84 return ~CRC;
85 }
86
97
99 template <typename CharType>
100 static inline uint32 StrCrc_DEPRECATED(const CharType* Data)
101 {
102 // make sure table is initialized
103 check(CRCTable_DEPRECATED[1] != 0);
104
105 uint32 CRC = 0xFFFFFFFF;
106 while (*Data)
107 {
108 CharType C = *Data++;
109 int32 CL = (C&255);
110 CRC = (CRC << 8) ^ CRCTable_DEPRECATED[(CRC >> 24) ^ CL];
111 int32 CH = (C>>8)&255;
112 CRC = (CRC << 8) ^ CRCTable_DEPRECATED[(CRC >> 24) ^ CH];
113 }
114 return ~CRC;
115 }
116
118 template <typename CharType>
119 static inline uint32 StrCrc_DEPRECATED(const int32 DataLen, const CharType* Data)
120 {
121 // make sure table is initialized
122 check(CRCTable_DEPRECATED[1] != 0);
123
124 uint32 CRC = 0xFFFFFFFF;
125 for (int32 i = 0; i < DataLen; i++)
126 {
127 CharType C = *Data++;
128 int32 CL = (C&255);
129 CRC = (CRC << 8) ^ CRCTable_DEPRECATED[(CRC >> 24) ^ CL];
130 int32 CH = (C>>8)&255;
131 CRC = (CRC << 8) ^ CRCTable_DEPRECATED[(CRC >> 24) ^ CH];
132 }
133 return ~CRC;
134 }
135
137 template <typename CharType> static inline uint32 Strihash_DEPRECATED( const CharType* Data );
138 template <typename CharType> static inline uint32 Strihash_DEPRECATED( const int32 DataLen, const CharType* Data );
139
141 static CORE_API uint32 MemCrc_DEPRECATED( const void* Data, int32 Length, uint32 CRC=0 );
142};
143
144template <>
146{
147 // make sure table is initialized
148 check(CRCTable_DEPRECATED[1] != 0);
149
150 uint32 Hash=0;
151 while( *Data )
152 {
154 uint8 B = Ch;
155 Hash = ((Hash >> 8) & 0x00FFFFFF) ^ CRCTable_DEPRECATED[(Hash ^ B) & 0x000000FF];
156 }
157 return Hash;
158}
159
160template <>
162{
163 // make sure table is initialized
164 check(CRCTable_DEPRECATED[1] != 0);
165
166 uint32 Hash=0;
167 for (int32 Idx = 0; Idx < DataLen; ++Idx)
168 {
170 uint8 B = Ch;
171 Hash = ((Hash >> 8) & 0x00FFFFFF) ^ CRCTable_DEPRECATED[(Hash ^ B) & 0x000000FF];
172 }
173 return Hash;
174}
175
176template <>
178{
179 // make sure table is initialized
180 check(CRCTable_DEPRECATED[1] != 0);
181
182 uint32 Hash=0;
183 while( *Data )
184 {
186 uint16 B = (uint16)Ch;
187 Hash = ((Hash >> 8) & 0x00FFFFFF) ^ CRCTable_DEPRECATED[(Hash ^ B) & 0x000000FF];
188 B = (uint16)Ch>>8;
189 Hash = ((Hash >> 8) & 0x00FFFFFF) ^ CRCTable_DEPRECATED[(Hash ^ B) & 0x000000FF];
190 }
191 return Hash;
192}
193
194template <>
196{
197 // make sure table is initialized
198 check(CRCTable_DEPRECATED[1] != 0);
199
200 uint32 Hash=0;
201 for (int32 Idx = 0; Idx < DataLen; ++Idx)
202 {
204 uint16 B = (uint16)Ch;
205 Hash = ((Hash >> 8) & 0x00FFFFFF) ^ CRCTable_DEPRECATED[(Hash ^ B) & 0x000000FF];
206 B = (uint16)Ch>>8;
207 Hash = ((Hash >> 8) & 0x00FFFFFF) ^ CRCTable_DEPRECATED[(Hash ^ B) & 0x000000FF];
209 return Hash;
210}
211
212template <>
214{
215 // We can't utilize StringConv.h here due to circular includes, so do the conversion manually
216
217 int32 Len = FPlatformString::Strlen(Data);
218 int32 ConvertedLen = FPlatformString::ConvertedLength<WIDECHAR>(Data, Len);
219
220 WIDECHAR* Temp = new WIDECHAR[ConvertedLen];
221
222 WIDECHAR* TempEnd = FPlatformString::Convert(Temp, ConvertedLen, Data, Len);
223 checkf(TempEnd!=nullptr, TEXT("String conversion unsuccessful"));
224
225 // This doesn't work for strings containing characters outside the BMP, but
226 // then neither does the WIDECHAR overload.
228
229 delete [] Temp;
230
231 return Result;
232}
233
234template <>
236{
237 // We can't utilize StringConv.h here due to circular includes, so do the conversion manually
238
239 int32 ConvertedLen = FPlatformString::ConvertedLength<WIDECHAR>(Data, DataLen);
240
241 WIDECHAR* Temp = new WIDECHAR[ConvertedLen];
242
243 WIDECHAR* TempEnd = FPlatformString::Convert(Temp, ConvertedLen, Data, DataLen);
244 checkf(TempEnd!=nullptr, TEXT("String conversion unsuccessful"));
245
246 // This doesn't work for strings containing characters outside the BMP, but
247 // then neither does the WIDECHAR overload.
249
250 delete [] Temp;
251
252 return Result;
253}
254
267{
268 return Ptr;
269}
270
274template <typename InKeyType, bool bInAllowDuplicateKeys = false>
276{
277 static_assert(TIsCharType_V<std::remove_pointer_t<decltype(ToCStr(std::declval<InKeyType>()))>>, "TStringPointerSetKeyFuncs_DEPRECATED should only be used with keys which character types");
278
282
284
286 {
287 return Element;
288 }
289
290 template <typename ComparableKey>
292 {
293 return A == B;
294 }
295
296 template <typename ComparableKey = KeyInitType>
301};
302
306template <typename InKeyType, typename InValueType, bool bInAllowDuplicateKeys = false>
308{
309 static_assert(TIsCharType_V<std::remove_pointer_t<decltype(ToCStr(std::declval<InKeyType>()))>>, "TStringPointerMapKeyFuncs_DEPRECATED should only be used with keys which character types");
310
314
316
318 {
319 return Element.Key;
320 }
321
322 template <typename ComparableKey>
324 {
325 return A == B;
326 }
327
328 template <typename ComparableKey = KeyInitType>
333};
334
335#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_5
336#include "Templates/EnableIf.h"
337#endif
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::WIDECHAR WIDECHAR
A wide character. Normally a signed type.
Definition Platform.h:1133
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::UTF8CHAR UTF8CHAR
An 8-bit character containing a UTF8 (Unicode, 8-bit, variable-width) code unit.
Definition Platform.h:1137
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_FORCEINLINE_HINT const TCHAR * ToCStr(const TCHAR *Ptr)
Definition Crc.h:266
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Crc.h:20
static CORE_API void Init()
Definition Crc.cpp:428
static uint32 Strihash_DEPRECATED(const CharType *Data)
static uint32 StrCrc32(const CharType *Data, uint32 CRC=0)
Definition Crc.h:45
static uint32 StrCrc_DEPRECATED(const int32 DataLen, const CharType *Data)
Definition Crc.h:119
static uint32 StrCrc_DEPRECATED(const CharType *Data)
Definition Crc.h:100
static uint32 StrCrc32Len(const CharType *Data, int32 Length, uint32 CRC=0)
Definition Crc.h:67
static CORE_API uint32 MemCrc_DEPRECATED(const void *Data, int32 Length, uint32 CRC=0)
Definition Crc.cpp:592
static CORE_API MemCrc32Functor MemCrc32Func
Definition Crc.h:30
uint32(* MemCrc32Functor)(const void *Data, int32 Length, uint32 CRC)
Definition Crc.h:29
static CORE_API uint32 CRCTable_DEPRECATED[256]
Definition Crc.h:40
static UE_FORCEINLINE_HINT uint32 MemCrc32(const void *Data, int32 Length, uint32 CRC=0)
Definition Crc.h:31
static uint32 Strihash_DEPRECATED(const int32 DataLen, const CharType *Data)
static CORE_API uint32 CRCTablesSB8_DEPRECATED[8][256]
Definition Crc.h:60
static uint32 TypeCrc32(const T &Data, uint32 CRC=0)
Definition Crc.h:38
static CORE_API uint32 CRCTablesSB8[8][256]
Definition Crc.h:208
TCallTraitsParamTypeHelper< T, PassByValue >::ParamType ParamType
Definition UnrealTypeTraits.h:275
static CharType ToUpper(CharType Char)
Definition Char.h:80
static UE_FORCEINLINE_HINT KeyInitType GetSetKey(ElementInitType Element)
Definition Crc.h:317
static constexpr bool bAllowDuplicateKeys
Definition Crc.h:315
static UE_FORCEINLINE_HINT uint32 GetKeyHash(ComparableKey Key)
Definition Crc.h:329
typename TTypeTraits< InKeyType >::ConstPointerType KeyInitType
Definition Crc.h:312
static UE_FORCEINLINE_HINT bool Matches(KeyInitType A, const ComparableKey &B)
Definition Crc.h:323
InKeyType KeyType
Definition Crc.h:311
typename TCallTraits< InKeyType >::ParamType ElementInitType
Definition Crc.h:281
InKeyType KeyType
Definition Crc.h:279
static constexpr bool bAllowDuplicateKeys
Definition Crc.h:283
static UE_FORCEINLINE_HINT uint32 GetKeyHash(ComparableKey Key)
Definition Crc.h:297
typename TTypeTraits< InKeyType >::ConstPointerType KeyInitType
Definition Crc.h:280
static UE_FORCEINLINE_HINT KeyInitType GetSetKey(ElementInitType Element)
Definition Crc.h:285
static UE_FORCEINLINE_HINT bool Matches(KeyInitType A, const ComparableKey &B)
Definition Crc.h:291
Definition Tuple.h:652
TCallTraits< T >::ParamType ConstInitType
Definition UnrealTypeTraits.h:336
TCallTraits< T >::ConstPointerType ConstPointerType
Definition UnrealTypeTraits.h:337