This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endianreader.cpp
159 lines (127 loc) · 4.24 KB
/
endianreader.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "endianreader.h"
#include "endianswap.h"
#include "readerhelpers.h"
#include <concepts>
#include <bit>
#include <array>
#include <algorithm>
#if defined(_WIN32) || defined(__WINDOWS__) || defined(__WIN32__)
#include <WinSock2.h>
#elif __linux__
#include <arpa/inet.h>
#endif
VRCE::EndianType g_computerEndianess;
VRCE::EndianType VRCE::EndianReader::ComputerEndianess() noexcept
{
if (0x00FFu == ntohl(0x00FFu)) {
g_computerEndianess = EndianType::BigEndian;
}
else {
g_computerEndianess = EndianType::LittleEndian;
}
return g_computerEndianess;
}
template <typename T>
inline T read_endian_helper(VRCE::IReadSource* readsource, bool diffEndian)
{
return VRCE::endian_swap_if(VRCE::ReaderHelpers::read_type<T>(readsource), diffEndian);
}
VRCE::EndianReader::EndianReader(const VRCE::BinaryReader& reader, VRCE::EndianType endianess)
: BinaryReader(reader)
, m_differentEndianess(endianess != ComputerEndianess())
{
}
VRCE::EndianType VRCE::EndianReader::endianess() const noexcept
{
switch (g_computerEndianess) {
case VRCE::EndianType::BigEndian:
return m_differentEndianess ? VRCE::EndianType::LittleEndian : VRCE::EndianType::BigEndian;
case VRCE::EndianType::LittleEndian:
return m_differentEndianess ? VRCE::EndianType::BigEndian : VRCE::EndianType::LittleEndian;
}
}
std::int16_t VRCE::EndianReader::read16s()
{
return read_endian_helper<std::int16_t>(m_readsource, m_differentEndianess);
}
std::uint16_t VRCE::EndianReader::read16u()
{
return read_endian_helper<std::uint16_t>(m_readsource, m_differentEndianess);
}
std::int32_t VRCE::EndianReader::read32s()
{
return read_endian_helper<std::int32_t>(m_readsource, m_differentEndianess);
}
std::uint32_t VRCE::EndianReader::read32u()
{
return read_endian_helper<std::uint32_t>(m_readsource, m_differentEndianess);
}
std::int64_t VRCE::EndianReader::read64s()
{
return read_endian_helper<std::int64_t>(m_readsource, m_differentEndianess);
}
std::uint64_t VRCE::EndianReader::read64u()
{
return read_endian_helper<std::uint64_t>(m_readsource, m_differentEndianess);
}
float VRCE::EndianReader::readFloat()
{
return read_endian_helper<float>(m_readsource, m_differentEndianess);
}
double VRCE::EndianReader::readDouble()
{
return read_endian_helper<double>(m_readsource, m_differentEndianess);
}
template <typename T>
inline std::vector<T> vec_read_endian_helper(VRCE::IReadSource* readsource, bool diffEndian)
{
std::int32_t vecSize = read_endian_helper<std::int32_t>(readsource, diffEndian);
std::vector<T> vec;
vec.resize(vecSize);
VRCE::ReaderHelpers::read_array<T>(readsource, vec);
for (auto it = vec.begin(); it != vec.end(); it++)
{
*it = VRCE::endian_swap_if(*it, diffEndian);
}
return vec;
}
std::vector<std::int8_t> VRCE::EndianReader::read8sVector()
{
return vec_read_endian_helper<std::int8_t>(m_readsource, m_differentEndianess);
}
std::vector<std::uint8_t> VRCE::EndianReader::read8uVector()
{
return vec_read_endian_helper<std::uint8_t>(m_readsource, m_differentEndianess);
}
std::vector<std::int16_t> VRCE::EndianReader::read16sVector()
{
return vec_read_endian_helper<std::int16_t>(m_readsource, m_differentEndianess);
}
std::vector<std::uint16_t> VRCE::EndianReader::read16uVector()
{
return vec_read_endian_helper<std::uint16_t>(m_readsource, m_differentEndianess);
}
std::vector<std::int32_t> VRCE::EndianReader::read32sVector()
{
return vec_read_endian_helper<std::int32_t>(m_readsource, m_differentEndianess);
}
std::vector<std::uint32_t> VRCE::EndianReader::read32uVector()
{
return vec_read_endian_helper<std::uint32_t>(m_readsource, m_differentEndianess);
}
std::vector<std::int64_t> VRCE::EndianReader::read64sVector()
{
return vec_read_endian_helper<std::int64_t>(m_readsource, m_differentEndianess);
}
std::vector<std::uint64_t> VRCE::EndianReader::read64uVector()
{
return vec_read_endian_helper<std::uint64_t>(m_readsource, m_differentEndianess);
}
std::vector<float> VRCE::EndianReader::readFloatVector()
{
return vec_read_endian_helper<float>(m_readsource, m_differentEndianess);
}
std::vector<double> VRCE::EndianReader::readDoubleVector()
{
return vec_read_endian_helper<double>(m_readsource, m_differentEndianess);
}