summaryrefslogtreecommitdiff
path: root/ZAPDUtils/Utils/BinaryWriter.cpp
blob: aa7840f01c46b63c5c61741afef4af171a9e15fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "BinaryWriter.h"

BinaryWriter::BinaryWriter(Stream* nStream)
{
	stream.reset(nStream);
}

BinaryWriter::BinaryWriter(std::shared_ptr<Stream> nStream)
{
	stream = nStream;
}

void BinaryWriter::SetEndianness(Endianness endianness)
{
	this->endianness = endianness;
}

void BinaryWriter::Close()
{
	stream->Close();
}

std::shared_ptr<Stream> BinaryWriter::GetStream()
{
	return stream;
}

uint64_t BinaryWriter::GetBaseAddress()
{
	return stream->GetBaseAddress();
}

uint64_t BinaryWriter::GetLength()
{
	return stream->GetLength();
}

void BinaryWriter::Seek(int32_t offset, SeekOffsetType seekType)
{
	stream->Seek(offset, seekType);
}

void BinaryWriter::Write(int8_t value)
{
	stream->Write((char*)&value, sizeof(int8_t));
}

void BinaryWriter::Write(uint8_t value)
{
	stream->Write((char*)&value, sizeof(uint8_t));
}

void BinaryWriter::Write(int16_t value)
{
	if (endianness != Endianness::Native)
		value = BSWAP16(value);

	stream->Write((char*)&value, sizeof(int16_t));
}

void BinaryWriter::Write(uint16_t value)
{
	if (endianness != Endianness::Native)
		value = BSWAP16(value);

	stream->Write((char*)&value, sizeof(uint16_t));
}

void BinaryWriter::Write(int32_t value)
{
	if (endianness != Endianness::Native)
		value = BSWAP32(value);

	stream->Write((char*)&value, sizeof(int32_t));
}

void BinaryWriter::Write(int32_t valueA, int32_t valueB)
{
	Write(valueA);
	Write(valueB);
}

void BinaryWriter::Write(uint32_t value)
{
	if (endianness != Endianness::Native)
		value = BSWAP32(value);

	stream->Write((char*)&value, sizeof(uint32_t));
}

void BinaryWriter::Write(int64_t value)
{
	if (endianness != Endianness::Native)
		value = BSWAP64(value);

	stream->Write((char*)&value, sizeof(int64_t));
}

void BinaryWriter::Write(uint64_t value)
{
	if (endianness != Endianness::Native)
		value = BSWAP64(value);

	stream->Write((char*)&value, sizeof(uint64_t));
}

void BinaryWriter::Write(float value)
{
	if (endianness != Endianness::Native)
		value = BitConverter::ToFloatBE((uint8_t*)&value, 0);

	stream->Write((char*)&value, sizeof(float));
}

void BinaryWriter::Write(double value)
{
	if (endianness != Endianness::Native)
		value = BitConverter::ToDoubleBE((uint8_t*)&value, 0);

	stream->Write((char*)&value, sizeof(double));
}

void BinaryWriter::Write(const std::string& str)
{
	int strLen = str.size();
	Write(strLen);

	for (char c : str)
		stream->WriteByte(c);
}

void BinaryWriter::Write(char* srcBuffer, size_t length)
{
	stream->Write(srcBuffer, length);
}