-
Notifications
You must be signed in to change notification settings - Fork 0
/
pato_bin.h
217 lines (190 loc) · 7.4 KB
/
pato_bin.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef PATO_BIN_H
#define PATO_BIN_H
/*
The MIT License (MIT)
Copyright (c) 2016 Nicolas Daniel Engels
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <array>
namespace pato_bin {
using byte_t = unsigned char;
// 15/08/2016
// Name and idea of detection get from howardhinnant.github.io/endian.html
enum class endian {
big,
little,
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
native = little
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
native = big
#else
#error "mixed endian"
#endif
};
// General template declaration (no definition)
template <class T>
constexpr std::array<byte_t, sizeof(T)> to_bin(T, endian = endian::native);
template <class T>
constexpr T bin_to(const std::array<byte_t, sizeof(T)>&, endian = endian::native);
// Specialization for all signed/unsigned fixed-width integral types
template <>
constexpr std::array<byte_t, 1> to_bin(std::int8_t i, endian)
{
return { static_cast<byte_t>(i) };
}
template <>
constexpr std::array<byte_t, 1> to_bin(std::uint8_t i, endian)
{
return { static_cast<byte_t>(i) };
}
template <>
constexpr std::array<byte_t, 2> to_bin(std::int16_t i, endian e)
{
if (e == endian::big)
return { static_cast<byte_t>(i >> 8), static_cast<byte_t>(i) };
else
return { static_cast<byte_t>(i), static_cast<byte_t>(i >> 8) };
}
template <>
constexpr std::array<byte_t, 2> to_bin(std::uint16_t i, endian e)
{
if (e == endian::big)
return { static_cast<byte_t>(i >> 8), static_cast<byte_t>(i) };
else
return { static_cast<byte_t>(i), static_cast<byte_t>(i >> 8) };
}
template <>
constexpr std::array<byte_t, 4> to_bin(std::int32_t i, endian e)
{
if (e == endian::big)
return { static_cast<byte_t>(i >> 24), static_cast<byte_t>(i >> 16),
static_cast<byte_t>(i >> 8), static_cast<byte_t>(i) };
else
return { static_cast<byte_t>(i), static_cast<byte_t>(i >> 8),
static_cast<byte_t>(i >> 16), static_cast<byte_t>(i >> 24) };
}
template <>
constexpr std::array<byte_t, 4> to_bin(std::uint32_t i, endian e)
{
if (e == endian::big)
return { static_cast<byte_t>(i >> 24), static_cast<byte_t>(i >> 16),
static_cast<byte_t>(i >> 8), static_cast<byte_t>(i) };
else
return { static_cast<byte_t>(i), static_cast<byte_t>(i >> 8),
static_cast<byte_t>(i >> 16), static_cast<byte_t>(i >> 24) };
}
template <>
constexpr std::array<byte_t, 8> to_bin(std::int64_t i, endian e)
{
if (e == endian::big)
return { static_cast<byte_t>(i >> 56), static_cast<byte_t>(i >> 48),
static_cast<byte_t>(i >> 40), static_cast<byte_t>(i >> 32),
static_cast<byte_t>(i >> 24), static_cast<byte_t>(i >> 16),
static_cast<byte_t>(i >> 8), static_cast<byte_t>(i) };
else
return { static_cast<byte_t>(i), static_cast<byte_t>(i >> 8),
static_cast<byte_t>(i >> 16), static_cast<byte_t>(i >> 24),
static_cast<byte_t>(i >> 32), static_cast<byte_t>(i >> 40),
static_cast<byte_t>(i >> 48), static_cast<byte_t>(i >> 56) };
}
template <>
constexpr std::array<byte_t, 8> to_bin(std::uint64_t i, endian e)
{
if (e == endian::big)
return { static_cast<byte_t>(i >> 56), static_cast<byte_t>(i >> 48),
static_cast<byte_t>(i >> 40), static_cast<byte_t>(i >> 32),
static_cast<byte_t>(i >> 24), static_cast<byte_t>(i >> 16),
static_cast<byte_t>(i >> 8), static_cast<byte_t>(i) };
else
return { static_cast<byte_t>(i), static_cast<byte_t>(i >> 8),
static_cast<byte_t>(i >> 16), static_cast<byte_t>(i >> 24),
static_cast<byte_t>(i >> 32), static_cast<byte_t>(i >> 40),
static_cast<byte_t>(i >> 48), static_cast<byte_t>(i >> 56) };
}
template <>
constexpr std::int8_t bin_to(const std::array<byte_t, 1>& a, endian e)
{
return { static_cast<std::int8_t>(a[0]) };
}
template <>
constexpr std::uint8_t bin_to(const std::array<byte_t, 1>& a, endian e)
{
return { static_cast<std::uint8_t>(a[0]) };
}
template <>
constexpr std::int16_t bin_to(const std::array<byte_t, 2>& a, endian e)
{
if (e == endian::big)
return static_cast<std::int16_t>(a[1] + (a[0] << 8));
else
return static_cast<std::int16_t>(a[0] + (a[1] << 8));
}
template <>
constexpr std::uint16_t bin_to(const std::array<byte_t, 2>& a, endian e)
{
if (e == endian::big)
return static_cast<std::uint16_t>(a[1] + (a[0] << 8));
else
return static_cast<std::uint16_t>(a[0] + (a[1] << 8));
}
template <>
constexpr std::int32_t bin_to(const std::array<byte_t, 4>& a, endian e)
{
if (e == endian::big)
return static_cast<std::int32_t>( a[3] + (a[2] << 8) + (a[1] << 16) +
(a[0] << 24));
else
return static_cast<std::int32_t>( a[0] + (a[1] << 8) + (a[2] << 16) +
(a[3] << 24));
}
template <>
constexpr std::uint32_t bin_to(const std::array<byte_t, 4>& a, endian e)
{
if (e == endian::big)
return static_cast<std::uint32_t>( a[3] + (a[2] << 8) + (a[1] << 16) +
(a[0] << 24));
else
return static_cast<std::uint32_t>( a[0] + (a[1] << 8) + (a[2] << 16) +
(a[3] << 24));
}
template <>
constexpr std::int64_t bin_to(const std::array<byte_t, 8>& a, endian e)
{
if (e == endian::big)
return { a[7] + (a[6] << 8) + (a[5] << 16) + (a[4] << 24) +
(std::int64_t { a[3] } << 32) + (std::int64_t { a[2] } << 40) +
(std::int64_t { a[1] } << 48) + (std::int64_t { a[0] } << 56) };
else
return { a[0] + (a[1] << 8) + (a[2] << 16) + (a[3] << 24) +
(std::int64_t { a[4] } << 32) + (std::int64_t { a[5] } << 40) +
(std::int64_t { a[6] } << 48) + (std::int64_t { a[7] } << 56) };
}
template <>
constexpr std::uint64_t bin_to(const std::array<byte_t, 8>& a, endian e)
{
if (e == endian::big)
return { a[7] + (a[6] << 8) + (a[5] << 16) + (a[4] << 24) +
(std::uint64_t { a[3] } << 32) + (std::uint64_t { a[2] } << 40) +
(std::uint64_t { a[1] } << 48) + (std::uint64_t { a[0] } << 56) };
else
return { a[0] + (a[1] << 8) + (a[2] << 16) + (a[3] << 24) +
(std::uint64_t { a[4] } << 32) + (std::uint64_t { a[5] } << 40) +
(std::uint64_t { a[6] } << 48) + (std::uint64_t { a[7] } << 56) };
}
} // namespace pato_bin
#endif
// PATO_BIN_H