From 8e88412debac1863015dae08e8af54a68ee8f034 Mon Sep 17 00:00:00 2001 From: karutoh Date: Sat, 17 Feb 2024 00:42:24 -0800 Subject: [PATCH] Fixed error in Serializer::WriteStr. --- include/ehs/Serializer.h | 69 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/include/ehs/Serializer.h b/include/ehs/Serializer.h index bcc1597..5234a41 100644 --- a/include/ehs/Serializer.h +++ b/include/ehs/Serializer.h @@ -395,6 +395,67 @@ namespace ehs offset += sizeof(T) * value.Size(); } + template + void WriteStr(const T* str, N inSize = 0) + { + if (!inSize) + while (str[inSize]) + inSize++; + + N bSize = sizeof(T) * inSize; + N bSizeN = sizeof(T) * (inSize + 1); + + if (bSizeN > size - offset) + { + N remainder = bSizeN - (size - offset); + + Byte* r = new Byte[size + remainder]; + + Util::Copy(r, data, size); + + delete[] data; + data = r; + size += remainder; + } + + if (CPU::GetEndianness() == Endianness::LE) + { + if (endianness == Endianness::LE) + { + Util::Copy(&data[offset], &str[0], bSize); + + *(T*)&data[offset + inSize] = 0; + } + else + { + for (N i = 0; i < inSize; ++i) + for (N b = 0; b < sizeof(T); ++b) + data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; + + *(T*)&data[offset + bSize] = 0; + } + } + else + { + if (endianness == Endianness::LE) + { + for (N i = 0; i < inSize; ++i) + for (N b = 0; b < sizeof(T); ++b) + data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; + + *(T*)&data[offset + bSize] = 0; + } + else + { + Util::Copy(&data[offset], &str[0], bSize); + + *(T*)&data[offset + bSize] = 0; + } + } + + offset += bSizeN; + } + template void WriteStr(const Str& str) { @@ -419,7 +480,7 @@ namespace ehs { Util::Copy(&data[offset], &str[0], str.Size(true)); - *(T*)&data[offset + str.Size()] = 0; + *(T*)&data[offset + str.Size(true)] = 0; } else { @@ -427,7 +488,7 @@ namespace ehs for (N b = 0; b < sizeof(T); ++b) data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; - *(T*)&data[offset + str.Size()] = 0; + *(T*)&data[offset + str.Size(true)] = 0; } } else @@ -438,13 +499,13 @@ namespace ehs for (N b = 0; b < sizeof(T); ++b) data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; - *(T*)&data[offset + str.Size()] = 0; + *(T*)&data[offset + str.Size(true)] = 0; } else { Util::Copy(&data[offset], &str[0], str.Size(true)); - *(T*)&data[offset + str.Size()] = 0; + *(T*)&data[offset + str.Size(true)] = 0; } }