diff --git a/include/ehs/Serializer.h b/include/ehs/Serializer.h index 7f58f92..fb1b55d 100644 --- a/include/ehs/Serializer.h +++ b/include/ehs/Serializer.h @@ -401,9 +401,11 @@ namespace ehs template void WriteStr(const Str& str) { - if (sizeof(N) + sizeof(T) * str.Size() > size - offset) + N inSize = sizeof(T) * (str.Size() + 1); + + if (inSize > size - offset) { - N remainder = sizeof(N) + sizeof(T) * str.Size() - (size - offset); + N remainder = inSize - (size - offset); Byte* r = new Byte[size + remainder]; @@ -419,54 +421,38 @@ namespace ehs { if (endianness == Endianness::LE) { - *(N*)&data[offset] = (N)str.Size(); + Util::Copy(&data[offset], &str[0], str.Size(true)); - offset += sizeof(N); - - for (N i = 0; i < str.Size(); ++i) - *(T*)&data[offset + i * sizeof(T)] = str[i]; + *(T*)&data[offset + str.Size()] = 0; } else { - N tmpSize = (N)str.Size(); - - for (N i = 0; i < sizeof(N); ++i) - data[offset + i] = ((Byte*)&tmpSize)[sizeof(N) - i - 1]; - - offset += sizeof(N); - for (N i = 0; i < str.Size(); ++i) for (N b = 0; b < sizeof(T); ++b) - data[offset + i * sizeof(T) + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; + data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; + + *(T*)&data[offset + str.Size()] = 0; } } else { if (endianness == Endianness::LE) { - N tmpSize = (N)str.Size(); - - for (N i = 0; i < sizeof(N); ++i) - data[offset + i] = ((Byte*)&tmpSize)[sizeof(N) - i - 1]; - - offset += sizeof(N); - for (N i = 0; i < str.Size(); ++i) for (N b = 0; b < sizeof(T); ++b) - data[offset + i * sizeof(T) + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; + data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; + + *(T*)&data[offset + str.Size()] = 0; } else { - *(N*)&data[offset] = (N)str.Size(); + Util::Copy(&data[offset], &str[0], str.Size(true)); - offset += sizeof(N); - - for (N i = 0; i < str.Size(); ++i) - *(T*)&data[offset + i * sizeof(T)] = str[i]; + *(T*)&data[offset + str.Size()] = 0; } } - offset += str.Size() * sizeof(T); + offset += inSize; } void WriteVersion(const Version& value) @@ -1014,12 +1000,34 @@ namespace ehs } template - Str ReadStr(const O size = 0) + Str ReadStr(O size = 0) { - Str result(size ? size : (O)Read()); + if (!size) + while (&data[offset + sizeof(T) * size]) + size++; - for (O i = 0; i < result.Size(); ++i) - result[i] = Read(); + Str result(size); + + if (CPU::GetEndianness() == Endianness::LE) + { + if (endianness == Endianness::LE) + Util::Copy(&result[0], &data[offset], result.Size(true)); + else + for (N i = 0; i < size; ++i) + for (N b = 0; b < sizeof(T); ++b) + ((Byte*)&result[i])[sizeof(T) - i - 1] = data[offset + sizeof(T) * i + b]; + } + else + { + if (endianness == Endianness::LE) + for (N i = 0; i < size; ++i) + for (N b = 0; b < sizeof(T); ++b) + ((Byte*)&result[i])[sizeof(T) - i - 1] = data[offset + sizeof(T) * i + b]; + else + Util::Copy(&result[0], &data[offset], result.Size(true)); + } + + offset += result.Size(true) + 1; return result; }