diff options
| author | Dan Engelbrecht <[email protected]> | 2022-04-07 18:22:26 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-04-07 18:22:26 +0200 |
| commit | 487352bb3e1de5a96268616bb335f9ef857cd629 (patch) | |
| tree | 4b03eb73f02bf03d1b4671775c1c5277a7d7341a /zencore | |
| parent | Add pre-commit config (#69) (diff) | |
| parent | clean up variable naming (diff) | |
| download | zen-487352bb3e1de5a96268616bb335f9ef857cd629.tar.xz zen-487352bb3e1de5a96268616bb335f9ef857cd629.zip | |
Merge pull request #58 from EpicGames/de/cas-store-with-block-store
de/cas store with block store
Diffstat (limited to 'zencore')
| -rw-r--r-- | zencore/filesystem.cpp | 4 | ||||
| -rw-r--r-- | zencore/include/zencore/string.h | 105 | ||||
| -rw-r--r-- | zencore/iobuffer.cpp | 5 |
3 files changed, 90 insertions, 24 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp index 041abaf1d..e2778089b 100644 --- a/zencore/filesystem.cpp +++ b/zencore/filesystem.cpp @@ -921,6 +921,10 @@ PathFromHandle(void* NativeHandle) } const DWORD RequiredLengthIncludingNul = GetFinalPathNameByHandleW(NativeHandle, nullptr, 0, FILE_NAME_OPENED); + if (RequiredLengthIncludingNul == 0) + { + ThrowLastError(fmt::format("failed to get path from file handle {}", NativeHandle)); + } std::wstring FullPath; FullPath.resize(RequiredLengthIncludingNul - 1); diff --git a/zencore/include/zencore/string.h b/zencore/include/zencore/string.h index 4c378730f..e502120ac 100644 --- a/zencore/include/zencore/string.h +++ b/zencore/include/zencore/string.h @@ -9,6 +9,7 @@ #include <string.h> #include <charconv> #include <codecvt> +#include <concepts> #include <optional> #include <span> #include <string_view> @@ -488,6 +489,26 @@ std::string WideToUtf8(const wchar_t* wstr); void WideToUtf8(const std::wstring_view& wstr, StringBuilderBase& out); std::string WideToUtf8(const std::wstring_view Wstr); +inline uint8_t +Char2Nibble(char c) +{ + if (c >= '0' && c <= '9') + { + return uint8_t(c - '0'); + } + if (c >= 'a' && c <= 'f') + { + return uint8_t(c - 'a' + 10); + } + if (c >= 'A' && c <= 'F') + { + return uint8_t(c - 'A' + 10); + } + return uint8_t(0xff); +}; + +static constexpr const char HexChars[] = "0123456789abcdef"; + /// <summary> /// Parse hex string into a byte buffer /// </summary> @@ -501,38 +522,56 @@ ParseHexBytes(const char* InputString, size_t CharacterCount, uint8_t* OutPtr) { ZEN_ASSERT((CharacterCount & 1) == 0); - auto char2nibble = [](char c) { - uint8_t c8 = uint8_t(c - '0'); + uint8_t allBits = 0; - if (c8 < 10) - return c8; + while (CharacterCount) + { + uint8_t n0 = Char2Nibble(InputString[0]); + uint8_t n1 = Char2Nibble(InputString[1]); - c8 -= 'A' - '0' - 10; + allBits |= n0 | n1; - if (c8 < 16) - return c8; + *OutPtr = (n0 << 4) | n1; - c8 -= 'a' - 'A'; + OutPtr += 1; + InputString += 2; + CharacterCount -= 2; + } - if (c8 < 16) - return c8; + return (allBits & 0x80) == 0; +} - return uint8_t(0xff); - }; +inline void +ToHexBytes(const uint8_t* InputData, size_t ByteCount, char* OutString) +{ + while (ByteCount--) + { + uint8_t byte = *InputData++; + + *OutString++ = HexChars[byte >> 4]; + *OutString++ = HexChars[byte & 15]; + } +} + +inline bool +ParseHexNumber(const char* InputString, size_t CharacterCount, uint8_t* OutPtr) +{ + ZEN_ASSERT((CharacterCount & 1) == 0); uint8_t allBits = 0; + InputString += CharacterCount; while (CharacterCount) { - uint8_t n0 = char2nibble(InputString[0]); - uint8_t n1 = char2nibble(InputString[1]); + InputString -= 2; + uint8_t n0 = Char2Nibble(InputString[0]); + uint8_t n1 = Char2Nibble(InputString[1]); allBits |= n0 | n1; *OutPtr = (n0 << 4) | n1; OutPtr += 1; - InputString += 2; CharacterCount -= 2; } @@ -540,19 +579,43 @@ ParseHexBytes(const char* InputString, size_t CharacterCount, uint8_t* OutPtr) } inline void -ToHexBytes(const uint8_t* InputData, size_t ByteCount, char* OutString) +ToHexNumber(const uint8_t* InputData, size_t ByteCount, char* OutString) { - const char hexchars[] = "0123456789abcdef"; - + InputData += ByteCount; while (ByteCount--) { - uint8_t byte = *InputData++; + uint8_t byte = *(--InputData); - *OutString++ = hexchars[byte >> 4]; - *OutString++ = hexchars[byte & 15]; + *OutString++ = HexChars[byte >> 4]; + *OutString++ = HexChars[byte & 15]; } } +/// <summary> +/// Generates a hex number from a pointer to an integer type, this formats the number in the correct order for a hexadecimal number +/// </summary> +/// <param name="Value">Integer value type</param> +/// <param name="outString">Output buffer where resulting string is written</param> +void +ToHexNumber(std::unsigned_integral auto Value, char* OutString) +{ + ToHexNumber((const uint8_t*)&Value, sizeof(Value), OutString); + OutString[sizeof(Value) * 2] = 0; +} + +/// <summary> +/// Parse hex number string into a value, this formats the number in the correct order for a hexadecimal number +/// </summary> +/// <param name="string">Input string</param> +/// <param name="characterCount">Number of characters in string</param> +/// <param name="OutValue">Pointer to output value</param> +/// <returns>true if the input consisted of all valid hexadecimal characters</returns> +bool +ParseHexNumber(const std::string HexString, std::unsigned_integral auto& OutValue) +{ + return ParseHexNumber(HexString.c_str(), sizeof(OutValue) * 2, (uint8_t*)&OutValue); +} + ////////////////////////////////////////////////////////////////////////// // Format numbers for humans // diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp index e2aaa3169..8a3ab8427 100644 --- a/zencore/iobuffer.cpp +++ b/zencore/iobuffer.cpp @@ -186,7 +186,7 @@ IoBufferExtendedCore::IoBufferExtendedCore(const IoBufferExtendedCore* Outer, ui , m_FileHandle(Outer->m_FileHandle) , m_FileOffset(Outer->m_FileOffset + Offset) { - m_Flags.fetch_or(kIsOwnedByThis | kIsExtended, std::memory_order_relaxed); + m_Flags.fetch_or(kIsExtended, std::memory_order_relaxed); } IoBufferExtendedCore::~IoBufferExtendedCore() @@ -217,10 +217,9 @@ IoBufferExtendedCore::~IoBufferExtendedCore() int Fd = int(uintptr_t(m_FileHandle)); bool Success = (close(Fd) == 0); #endif - if (!Success) { - ZEN_WARN("Error reported on file handle close!"); + ZEN_WARN("Error reported on file handle close, reason '{}'", GetLastErrorAsString()); } } |