2024-06-03 21:55:09 -03:00
|
|
|
#include "xna/csharp/binary.hpp"
|
|
|
|
#include "xna/csharp/buffer.hpp"
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
namespace xna {
|
2024-07-13 22:50:52 -03:00
|
|
|
BinaryReader::BinaryReader(sptr<Stream> const& input) {
|
|
|
|
Exception::ThrowIfNull(input, nameof(input));
|
|
|
|
|
|
|
|
stream = input;
|
|
|
|
buffer = std::vector<Byte>(bufferLength);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Int BinaryReader::PeekChar()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
const auto position = stream->Position();
|
2024-05-28 21:16:26 -03:00
|
|
|
const auto num = Read();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
stream->Seek(position, SeekOrigin::Begin);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Int BinaryReader::Read()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 16:59:03 -03:00
|
|
|
const auto result = InternalReadOneChar();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
return result;
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
bool BinaryReader::ReadBoolean()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 16:59:03 -03:00
|
|
|
FillBuffer(1);
|
2024-05-28 21:16:26 -03:00
|
|
|
return buffer[0] > 0;
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Byte BinaryReader::ReadByte()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
const auto num = stream->ReadByte();
|
|
|
|
|
|
|
|
return static_cast<Byte>(num);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Sbyte BinaryReader::ReadSByte()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 16:59:03 -03:00
|
|
|
FillBuffer(1);
|
2024-05-28 21:16:26 -03:00
|
|
|
return static_cast<Sbyte>(buffer[0]);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Char BinaryReader::ReadChar()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
auto num = Read();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
if (num == -1)
|
2024-04-28 20:19:37 -03:00
|
|
|
return '\0';
|
|
|
|
|
|
|
|
return static_cast<Char>(num);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Short BinaryReader::ReadInt16()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(2);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
return static_cast<Short>(
|
|
|
|
static_cast<Int>(buffer[0])
|
|
|
|
| static_cast<Int>(buffer[1]) << 8);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Ushort BinaryReader::ReadUInt16()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(2);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
return static_cast<Ushort>(
|
|
|
|
static_cast<Uint>(buffer[0])
|
|
|
|
| static_cast<Uint>(buffer[1]) << 8);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Int BinaryReader::ReadInt32()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(4);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
return static_cast<Int>(buffer[0])
|
|
|
|
| static_cast<Int>(buffer[1]) << 8
|
|
|
|
| static_cast<Int>(buffer[2]) << 16
|
|
|
|
| static_cast<Int>(buffer[3]) << 24;
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Uint BinaryReader::ReadUInt32()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(4);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
return static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[0])
|
|
|
|
| static_cast<Int>(buffer[1]) << 8
|
|
|
|
| static_cast<Int>(buffer[2]) << 16
|
|
|
|
| static_cast<Int>(buffer[3]) << 24);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Long BinaryReader::ReadInt64()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(8);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
const auto num1 = static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[4])
|
|
|
|
| static_cast<Int>(buffer[5]) << 8
|
|
|
|
| static_cast<Int>(buffer[6]) << 16
|
|
|
|
| static_cast<Int>(buffer[7]) << 24);
|
|
|
|
|
|
|
|
const auto num2 = static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[0])
|
|
|
|
| static_cast<Int>(buffer[1]) << 8
|
|
|
|
| static_cast<Int>(buffer[2]) << 16
|
|
|
|
| static_cast<Int>(buffer[3]) << 24);
|
|
|
|
|
|
|
|
return static_cast<Long>(num1) << 32 | static_cast<Long>(num2);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Ulong BinaryReader::ReadUInt64()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(8);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
const auto num1 = static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[4])
|
|
|
|
| static_cast<Int>(buffer[5]) << 8
|
|
|
|
| static_cast<Int>(buffer[6]) << 16
|
|
|
|
| static_cast<Int>(buffer[7]) << 24);
|
|
|
|
|
|
|
|
const auto num2 = static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[0])
|
|
|
|
| static_cast<Int>(buffer[1]) << 8
|
|
|
|
| static_cast<Int>(buffer[2]) << 16
|
|
|
|
| static_cast<Int>(buffer[3]) << 24);
|
|
|
|
|
|
|
|
return static_cast<Ulong>(num1) << 32 | static_cast<Ulong>(num2);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
float BinaryReader::ReadSingle()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(4);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
const auto num = static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[0])
|
|
|
|
| static_cast<Int>(buffer[1]) << 8
|
|
|
|
| static_cast<Int>(buffer[2]) << 16
|
|
|
|
| static_cast<Int>(buffer[3]) << 24);
|
|
|
|
|
|
|
|
return *(float*)#
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
double BinaryReader::ReadDouble()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
FillBuffer(8);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
const auto num1 = static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[4])
|
|
|
|
| static_cast<Int>(buffer[5]) << 8
|
|
|
|
| static_cast<Int>(buffer[6]) << 16
|
|
|
|
| static_cast<Int>(buffer[7]) << 24);
|
|
|
|
|
|
|
|
const auto num2 = static_cast<Uint>(
|
|
|
|
static_cast<Int>(buffer[0])
|
|
|
|
| static_cast<Int>(buffer[1]) << 8
|
|
|
|
| static_cast<Int>(buffer[2]) << 6
|
|
|
|
| static_cast<Int>(buffer[3]) << 24);
|
|
|
|
|
|
|
|
const auto num3 = static_cast<Ulong>(num1) << 32 | static_cast<Ulong>(num2);
|
|
|
|
|
|
|
|
return *(double*)&num3;
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
std::string BinaryReader::ReadString()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
static const auto empty = std::string();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
Int num = 0;
|
2024-05-28 16:59:03 -03:00
|
|
|
auto val1 = Read7BitEncodedInt();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
if (val1 < 0) {
|
2024-05-28 21:16:26 -03:00
|
|
|
//xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);
|
2024-04-28 20:19:37 -03:00
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val1 == 0)
|
|
|
|
return empty;
|
|
|
|
|
|
|
|
if (charBytes.empty())
|
|
|
|
charBytes.resize(maxCharBytesSize);
|
|
|
|
|
|
|
|
if (charBuffer.empty())
|
|
|
|
charBuffer.resize(maxCharBytesSize);
|
|
|
|
|
|
|
|
std::string sb;
|
|
|
|
|
|
|
|
do {
|
|
|
|
const auto byteCount = stream->Read(charBytes, 0, val1 - num > 128 ? 128 : val1 - num);
|
|
|
|
|
|
|
|
if (byteCount == 0) {
|
2024-05-28 21:16:26 -03:00
|
|
|
//xna_error_apply(err, XnaErrorCode::END_OF_FILE);
|
2024-04-28 20:19:37 -03:00
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = reinterpret_cast<char*>(charBytes.data());
|
|
|
|
const auto result = std::string(data);
|
|
|
|
|
|
|
|
if (num == 0 && byteCount == val1) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.append(result);
|
|
|
|
num += byteCount;
|
|
|
|
|
|
|
|
} while (num < val1);
|
|
|
|
|
2024-05-06 09:58:40 -03:00
|
|
|
return sb;
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 16:59:03 -03:00
|
|
|
Int BinaryReader::InternalReadOneChar()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
Int num1 = 0;
|
|
|
|
Long num2 = 0;
|
2024-05-01 19:09:43 -03:00
|
|
|
Long num3 = stream->Position();
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
if (charBytes.empty())
|
|
|
|
charBytes.resize(128);
|
|
|
|
|
|
|
|
if (singleChar.empty())
|
|
|
|
singleChar.resize(1);
|
|
|
|
|
|
|
|
while (num1 == 0)
|
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
//auto byteCount = m2BytesPerChar ? 2 : 1;
|
|
|
|
auto byteCount = 1;
|
2024-04-28 20:19:37 -03:00
|
|
|
const auto num4 = stream->ReadByte();
|
|
|
|
|
|
|
|
charBytes[0] = static_cast<Byte>(num4);
|
|
|
|
|
|
|
|
if (num4 == -1)
|
|
|
|
byteCount = 0;
|
|
|
|
|
|
|
|
if (byteCount == 2) {
|
|
|
|
auto num5 = stream->ReadByte();
|
|
|
|
charBytes[1] = static_cast<Byte>(num5);
|
|
|
|
|
|
|
|
if (num5 == -1)
|
|
|
|
byteCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (byteCount == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2024-05-28 16:59:03 -03:00
|
|
|
|
2024-04-28 20:19:37 -03:00
|
|
|
auto data = reinterpret_cast<char*>(charBytes.data());
|
|
|
|
const auto result = std::string(data, data + byteCount);
|
|
|
|
|
|
|
|
if (!result.empty())
|
|
|
|
{
|
|
|
|
num1 = static_cast<Int>(result.size());
|
|
|
|
singleChar[0] = result[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return num1 == 0 ? -1 : static_cast<Int>(singleChar[0]);
|
|
|
|
}
|
|
|
|
|
2024-05-28 16:59:03 -03:00
|
|
|
void BinaryReader::FillBuffer(Int numBytes)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
if (numBytes < 0 || numBytes > buffer.size()) {
|
2024-07-13 22:50:52 -03:00
|
|
|
Exception::Throw(Exception::OUT_OF_BOUNDS);
|
2024-05-28 16:59:03 -03:00
|
|
|
}
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 16:59:03 -03:00
|
|
|
Int bytesRead = 0;
|
|
|
|
Int n = 0;
|
|
|
|
|
|
|
|
if (numBytes == 1) {
|
|
|
|
n = stream->ReadByte();
|
|
|
|
|
|
|
|
if (n == -1){
|
2024-07-13 22:50:52 -03:00
|
|
|
Exception::Throw(Exception::END_OF_FILE);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 16:59:03 -03:00
|
|
|
buffer[0] = static_cast<Byte>(n);
|
|
|
|
return;
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 16:59:03 -03:00
|
|
|
do {
|
|
|
|
n = stream->Read(buffer, bytesRead, numBytes - bytesRead);
|
|
|
|
|
|
|
|
if (n == 0) {
|
2024-07-13 22:50:52 -03:00
|
|
|
Exception::Throw(Exception::END_OF_FILE);
|
2024-05-28 16:59:03 -03:00
|
|
|
}
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 16:59:03 -03:00
|
|
|
bytesRead += n;
|
|
|
|
} while (bytesRead < numBytes);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Int BinaryReader::InternalReadChars(Char* buffer, size_t bufferSize, size_t index, size_t count)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
auto charCount = count;
|
|
|
|
|
|
|
|
if (charBytes.empty())
|
|
|
|
charBytes.resize(128);
|
|
|
|
|
|
|
|
while (charCount > 0) {
|
|
|
|
auto count1 = charCount;
|
|
|
|
|
|
|
|
if (count1 > 1)
|
|
|
|
--count1;
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
/*if (m2BytesPerChar)
|
|
|
|
count1 <<= 1;*/
|
2024-04-28 20:19:37 -03:00
|
|
|
|
|
|
|
if (count1 > 128)
|
|
|
|
count1 = 128;
|
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
Int position = 0;
|
2024-04-28 20:19:37 -03:00
|
|
|
Int byteCount;
|
|
|
|
|
|
|
|
std::vector<Byte> numArray;
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
byteCount = stream->Read(charBytes, 0, static_cast<Int>(count1));
|
2024-04-28 20:19:37 -03:00
|
|
|
numArray = charBytes;
|
|
|
|
|
|
|
|
if (byteCount == 0)
|
2024-05-01 19:09:43 -03:00
|
|
|
return static_cast<Int>(count - charCount);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
if (position < 0 || byteCount < 0 || (position + byteCount) > numArray.size()) {
|
2024-04-28 20:19:37 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
if (index < 0 || charCount < 0 || (index + charCount) > bufferSize) {
|
2024-04-28 20:19:37 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto data = reinterpret_cast<char*>(charBytes.data());
|
2024-05-01 19:09:43 -03:00
|
|
|
auto pChars = reinterpret_cast<char*>(buffer);
|
|
|
|
|
|
|
|
//const auto result = std::string((data + position), (pChars + index) + byteCount);
|
|
|
|
const auto result = std::string((data + position), (data + position) + byteCount);
|
|
|
|
Buffer::BlockCopy(result.c_str(), position, pChars, index, byteCount);
|
|
|
|
|
|
|
|
buffer = reinterpret_cast<Char*>(pChars);
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
const auto chars = static_cast<Int>(result.size());
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
charCount -= chars;
|
|
|
|
index += chars;
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-01 19:09:43 -03:00
|
|
|
return static_cast<Int>(count - charCount);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Int BinaryReader::Read7BitEncodedInt()
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
Uint result = 0;
|
|
|
|
Byte byteReadJustNow;
|
|
|
|
|
|
|
|
constexpr Int MaxBytesWithoutOverflow = 4;
|
|
|
|
|
|
|
|
for (size_t shift = 0; shift < MaxBytesWithoutOverflow * 7; shift += 7)
|
|
|
|
{
|
|
|
|
byteReadJustNow = ReadByte();
|
|
|
|
result |= (byteReadJustNow & 0x7Fu) << shift;
|
|
|
|
|
|
|
|
if (byteReadJustNow <= 0x7Fu)
|
|
|
|
{
|
|
|
|
return static_cast<Int>(result);
|
|
|
|
}
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
byteReadJustNow = ReadByte();
|
|
|
|
|
|
|
|
if (byteReadJustNow > 0b1111u) {
|
|
|
|
throw std::format_error("Too many bytes in what should have been a 7-bit encoded integer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
result |= static_cast<Uint>(byteReadJustNow) << (MaxBytesWithoutOverflow * 7);
|
|
|
|
return static_cast<Int>(result);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
Long BinaryReader::Read7BitEncodedInt64() {
|
|
|
|
Ulong result = 0;
|
|
|
|
Byte byteReadJustNow;
|
|
|
|
|
|
|
|
constexpr Int MaxBytesWithoutOverflow = 9;
|
|
|
|
|
|
|
|
for (size_t shift = 0; shift < MaxBytesWithoutOverflow * 7; shift += 7)
|
|
|
|
{
|
|
|
|
byteReadJustNow = ReadByte();
|
|
|
|
result |= (static_cast<Ulong>(byteReadJustNow & 0x7Ful)) << shift;
|
|
|
|
|
|
|
|
if (byteReadJustNow <= 0x7Fu) {
|
|
|
|
return static_cast<Long>(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
byteReadJustNow = ReadByte();
|
|
|
|
|
|
|
|
if (byteReadJustNow > 0b1u)
|
|
|
|
{
|
|
|
|
throw std::format_error("Too many bytes in what should have been a 7-bit encoded integer.");
|
|
|
|
}
|
|
|
|
|
|
|
|
result |= static_cast<Ulong>(byteReadJustNow) << (MaxBytesWithoutOverflow * 7);
|
|
|
|
return static_cast<Long>(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Int BinaryReader::Read(std::vector<Char>& buffer, size_t index, size_t count)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-05-28 21:16:26 -03:00
|
|
|
return InternalReadChars(buffer.data(), buffer.size(), index, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
Int BinaryReader::Read(std::vector<Byte>& buffer, size_t index, size_t count)
|
|
|
|
{
|
|
|
|
auto data = reinterpret_cast<Char*>(buffer.data());
|
|
|
|
return InternalReadChars(data, buffer.size(), index, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Byte> BinaryReader::ReadBytes(size_t count)
|
|
|
|
{
|
|
|
|
std::vector<Byte> result(count);
|
|
|
|
Int numRead = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
const auto n = stream->Read(result, static_cast<Int>(numRead), static_cast<Int>(count));
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
numRead += n;
|
|
|
|
count -= n;
|
|
|
|
} while (count > 0);
|
|
|
|
|
|
|
|
if (numRead != result.size()) {
|
|
|
|
std::vector<Byte> copy(numRead);
|
|
|
|
Buffer::BlockCopy(result.data(), 0, copy.data(), 0, numRead);
|
|
|
|
result = copy;
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:16:26 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Binary Writer
|
|
|
|
|
2024-07-13 22:50:52 -03:00
|
|
|
BinaryWriter::BinaryWriter(sptr<Stream> const& stream) {
|
|
|
|
Exception::ThrowIfNull(stream, nameof(stream));
|
|
|
|
|
|
|
|
OutStream = stream;
|
|
|
|
_buffer = std::vector<Byte>(16);
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
Long BinaryWriter::Seek(Int offset, SeekOrigin origin)
|
2024-05-28 21:16:26 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
return OutStream->Seek(offset, origin);
|
2024-05-28 21:16:26 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(bool value)
|
2024-05-28 21:16:26 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
_buffer[0] = value ? static_cast<Byte>(1) : static_cast<Byte>(0);
|
|
|
|
OutStream->Write(_buffer, 0, 1);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Byte value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->WriteByte(value);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Sbyte value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->WriteByte(static_cast<Byte>(value));
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Byte const* buffer, Int bufferLength)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(buffer, bufferLength, 0, bufferLength);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(std::vector<Byte> const& buffer)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(buffer, 0, static_cast<Int>(buffer.size()));
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
2024-07-13 22:50:52 -03:00
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Byte const* buffer, Int bufferLength, Int index, Int count)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(buffer, bufferLength, index, count);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(std::vector<Byte> const& buffer, Int index, Int count)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(buffer, index, count);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Char ch)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
_buffer[0] = static_cast<Byte>(ch);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 1);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(double value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
Ulong num = (Ulong) * (Long*)&value;
|
|
|
|
_buffer[0] = static_cast<Byte>(num);
|
|
|
|
_buffer[1] = static_cast<Byte>(num >> 8);
|
|
|
|
_buffer[2] = static_cast<Byte>(num >> 16);
|
|
|
|
_buffer[3] = static_cast<Byte>(num >> 24);
|
|
|
|
_buffer[4] = static_cast<Byte>(num >> 32);
|
|
|
|
_buffer[5] = static_cast<Byte>(num >> 40);
|
|
|
|
_buffer[6] = static_cast<Byte>(num >> 48);
|
|
|
|
_buffer[7] = static_cast<Byte>(num >> 56);
|
|
|
|
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 8);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Short value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
_buffer[0] = static_cast<Byte>(value);
|
|
|
|
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 2);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Ushort value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
_buffer[0] = static_cast<Byte>(value);
|
|
|
|
_buffer[1] = static_cast<Byte>((Uint)value >> 8);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 2);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Int value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
_buffer[0] = static_cast<Byte>(value);
|
|
|
|
_buffer[1] = static_cast<Byte>(value >> 8);
|
|
|
|
_buffer[2] = static_cast<Byte>(value >> 16);
|
|
|
|
_buffer[3] = static_cast<Byte>(value >> 24);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 4);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Uint value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
_buffer[0] = static_cast<Byte>(value);
|
|
|
|
_buffer[1] = static_cast<Byte>(value >> 8);
|
|
|
|
_buffer[2] = static_cast<Byte>(value >> 16);
|
|
|
|
_buffer[3] = static_cast<Byte>(value >> 24);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 4);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Ulong value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
_buffer[0] = static_cast<Byte>(value);
|
|
|
|
_buffer[1] = static_cast<Byte>(value >> 8);
|
|
|
|
_buffer[2] = static_cast<Byte>(value >> 16);
|
|
|
|
_buffer[3] = static_cast<Byte>(value >> 24);
|
|
|
|
_buffer[4] = static_cast<Byte>(value >> 32);
|
|
|
|
_buffer[5] = static_cast<Byte>(value >> 40);
|
|
|
|
_buffer[6] = static_cast<Byte>(value >> 48);
|
|
|
|
_buffer[7] = static_cast<Byte>(value >> 56);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 8);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(float value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
Uint num = *(Uint*)&value;
|
|
|
|
_buffer[0] = static_cast<Byte>(num);
|
|
|
|
_buffer[1] = static_cast<Byte>(num >> 8);
|
|
|
|
_buffer[2] = static_cast<Byte>(num >> 16);
|
|
|
|
_buffer[3] = static_cast<Byte>(num >> 24);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 4);
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(std::string const& value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
Write(value.c_str(), value.size());
|
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(const char* _string, size_t stringLength)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
Write7BitEncodedInt(static_cast<Int>(stringLength));
|
|
|
|
const auto b = reinterpret_cast<const Byte*>(_string);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(b, static_cast<Int>(stringLength), 0, static_cast<Int>(stringLength));
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void BinaryWriter::Write7BitEncodedInt(Int value)
|
|
|
|
{
|
|
|
|
Uint num;
|
2024-05-28 21:25:40 -03:00
|
|
|
for (num = static_cast<Uint>(value); num >= static_cast<Uint>(128U); num >>= 7)
|
|
|
|
Write(static_cast<Byte>(num | static_cast<Uint>(128U)));
|
2024-04-28 20:19:37 -03:00
|
|
|
|
2024-05-28 21:25:40 -03:00
|
|
|
Write(static_cast<Byte>(num));
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|
|
|
|
|
2024-05-28 21:20:08 -03:00
|
|
|
void BinaryWriter::Write(Long value)
|
2024-04-28 20:19:37 -03:00
|
|
|
{
|
|
|
|
_buffer[0] = static_cast<Byte>(value);
|
|
|
|
_buffer[1] = static_cast<Byte>(value >> 8);
|
|
|
|
_buffer[2] = static_cast<Byte>(value >> 16);
|
|
|
|
_buffer[3] = static_cast<Byte>(value >> 24);
|
|
|
|
_buffer[4] = static_cast<Byte>(value >> 32);
|
|
|
|
_buffer[5] = static_cast<Byte>(value >> 40);
|
|
|
|
_buffer[6] = static_cast<Byte>(value >> 48);
|
|
|
|
_buffer[7] = static_cast<Byte>(value >> 56);
|
2024-07-13 22:50:52 -03:00
|
|
|
OutStream->Write(_buffer, 0, 8);
|
2024-05-28 21:16:26 -03:00
|
|
|
}
|
2024-04-28 20:19:37 -03:00
|
|
|
}
|