-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitcore_test.cpp
More file actions
210 lines (185 loc) · 7.42 KB
/
Copy pathbitcore_test.cpp
File metadata and controls
210 lines (185 loc) · 7.42 KB
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
// Standalone test of the MSB-first bit_writer/bit_reader core used by erlbits.
// This part needs no reflection at all, so it's fully testable with a normal compiler.
#include <cstdint>
#include <cstddef>
#include <vector>
#include <span>
#include <stdexcept>
#include <cassert>
#include <cstring>
#include <iostream>
#include <bit>
namespace bitsyntax::detail {
// Erlang's bit syntax is big-endian (MSB-first) by default, and bit fields are
// packed contiguously without padding, e.g. <<1:1,0:1,1:1,1:1,0:4>> is one byte.
class bit_writer {
public:
void write_bits(std::uint64_t value, std::size_t width, bool little_endian_word = false) {
if (width == 0) return;
if (width > 64) throw std::domain_error("field width > 64 bits not supported");
// For little-endian integers, Erlang packs byte-swapped *bytes*, not swapped bits.
// We only allow little-endian on byte-aligned widths, matching Erlang semantics.
if (little_endian_word) {
if (width % 8 != 0) throw std::domain_error("little-endian requires byte-aligned width");
std::size_t nbytes = width / 8;
for (std::size_t i = 0; i < nbytes; ++i) {
std::uint8_t byte = static_cast<std::uint8_t>((value >> (8 * i)) & 0xFF);
write_bits_msb(byte, 8);
}
return;
}
write_bits_msb(value, width);
}
void write_bytes(std::span<const std::byte> bytes) {
if (bit_pos_ % 8 != 0) throw std::domain_error("write_bytes requires byte alignment");
buf_.insert(buf_.end(), bytes.begin(), bytes.end());
bit_pos_ += bytes.size() * 8;
}
std::vector<std::byte> bytes() && {
if (bit_pos_ % 8 != 0) throw std::domain_error("final buffer is not byte-aligned");
return std::move(buf_);
}
std::size_t bit_size() const { return bit_pos_; }
private:
void write_bits_msb(std::uint64_t value, std::size_t width) {
// Ensure capacity.
std::size_t needed_bytes = (bit_pos_ + width + 7) / 8;
if (buf_.size() < needed_bytes) buf_.resize(needed_bytes, std::byte{0});
for (std::size_t i = 0; i < width; ++i) {
// Most significant bit of the field goes out first.
std::uint64_t bit = (value >> (width - 1 - i)) & 1ULL;
std::size_t global_bit = bit_pos_ + i;
std::size_t byte_idx = global_bit / 8;
std::size_t bit_idx_in_byte = 7 - (global_bit % 8); // MSB-first within byte
auto b = static_cast<std::uint8_t>(buf_[byte_idx]);
b = static_cast<std::uint8_t>(b | (bit << bit_idx_in_byte));
buf_[byte_idx] = static_cast<std::byte>(b);
}
bit_pos_ += width;
}
std::vector<std::byte> buf_;
std::size_t bit_pos_ = 0;
};
class bit_reader {
public:
explicit bit_reader(std::span<const std::byte> data) : data_(data) {}
std::uint64_t read_bits(std::size_t width, bool little_endian_word = false) {
if (width == 0) return 0;
if (width > 64) throw std::domain_error("field width > 64 bits not supported");
if (bit_pos_ + width > data_.size() * 8)
throw std::out_of_range("not enough bits remaining (badmatch)");
if (little_endian_word) {
if (width % 8 != 0) throw std::domain_error("little-endian requires byte-aligned width");
std::size_t nbytes = width / 8;
std::uint64_t v = 0;
for (std::size_t i = 0; i < nbytes; ++i) {
std::uint64_t byte = read_bits_msb(8);
v |= (byte << (8 * i));
}
return v;
}
return read_bits_msb(width);
}
std::span<const std::byte> read_rest() {
if (bit_pos_ % 8 != 0) throw std::domain_error("read_rest requires byte alignment");
auto rest = data_.subspan(bit_pos_ / 8);
bit_pos_ = data_.size() * 8;
return rest;
}
std::size_t bits_remaining() const { return data_.size() * 8 - bit_pos_; }
private:
std::uint64_t read_bits_msb(std::size_t width) {
std::uint64_t value = 0;
for (std::size_t i = 0; i < width; ++i) {
std::size_t global_bit = bit_pos_ + i;
std::size_t byte_idx = global_bit / 8;
std::size_t bit_idx_in_byte = 7 - (global_bit % 8);
auto b = static_cast<std::uint8_t>(data_[byte_idx]);
std::uint64_t bit = (b >> bit_idx_in_byte) & 1U;
value = (value << 1) | bit;
}
bit_pos_ += width;
return value;
}
std::span<const std::byte> data_;
std::size_t bit_pos_ = 0;
};
} // namespace bitsyntax::detail
using namespace bitsyntax::detail;
static void dump(const std::vector<std::byte>& b) {
for (auto byte : b) std::cout << std::hex << (int)(unsigned char)byte << " ";
std::cout << std::dec << "\n";
}
int main() {
// Mirrors: <<Version:4, Type:4, Len:16, Body/binary>>
{
bit_writer w;
w.write_bits(0xA, 4); // Version = 10
w.write_bits(0x3, 4); // Type = 3
w.write_bits(0x1234, 16); // Len = 0x1234
std::byte body[] = { std::byte{'h'}, std::byte{'i'} };
w.write_bytes(body);
auto out = std::move(w).bytes();
dump(out);
assert(out.size() == 5); // 4+4+16 bits = 3 header bytes, + 2 body bytes
assert((unsigned char)out[0] == 0xA3); // 1010 0011
assert((unsigned char)out[1] == 0x12);
assert((unsigned char)out[2] == 0x34);
assert((unsigned char)out[3] == 'h');
assert((unsigned char)out[4] == 'i');
bit_reader r(out);
auto version = r.read_bits(4);
auto type = r.read_bits(4);
auto len = r.read_bits(16);
auto rest = r.read_rest();
assert(version == 0xA);
assert(type == 0x3);
assert(len == 0x1234);
assert(rest.size() == 2);
std::cout << "test 1 OK: version=" << version << " type=" << type
<< " len=0x" << std::hex << len << std::dec
<< " rest_len=" << rest.size() << "\n";
}
// Odd bit widths crossing byte boundaries: <<A:3, B:5, C:8>>
{
bit_writer w;
w.write_bits(0b101, 3);
w.write_bits(0b11010, 5);
w.write_bits(0xAB, 8);
auto out = std::move(w).bytes();
assert(out.size() == 2);
bit_reader r(out);
assert(r.read_bits(3) == 0b101);
assert(r.read_bits(5) == 0b11010);
assert(r.read_bits(8) == 0xAB);
std::cout << "test 2 OK (unaligned bit widths)\n";
}
// Little-endian byte-aligned field: <<N:32/little>>
{
bit_writer w;
w.write_bits(0x01020304, 32, /*little_endian_word=*/true);
auto out = std::move(w).bytes();
assert((unsigned char)out[0] == 0x04);
assert((unsigned char)out[1] == 0x03);
assert((unsigned char)out[2] == 0x02);
assert((unsigned char)out[3] == 0x01);
bit_reader r(out);
assert(r.read_bits(32, true) == 0x01020304);
std::cout << "test 3 OK (little-endian)\n";
}
// Under-read should behave like Erlang's badmatch (throw).
{
bool threw = false;
try {
std::byte one[] = { std::byte{0xFF} };
bit_reader r(one);
r.read_bits(16);
} catch (const std::out_of_range&) {
threw = true;
}
assert(threw);
std::cout << "test 4 OK (badmatch on short buffer)\n";
}
std::cout << "ALL TESTS PASSED\n";
return 0;
}