tangled
alpha
login
or
join now
altagos.dev
/
aether
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
switch to io.Reader
altagos.dev
9 months ago
aae38607
326d319f
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+29
-27
1 changed file
expand all
collapse all
unified
split
src
root.zig
+29
-27
src/root.zig
···
1
1
const std = @import("std");
2
2
+
const assert = std.debug.assert;
3
3
+
const io = std.io;
2
4
3
5
pub const BytePacketBuffer = struct {
4
6
buf: [512]u8 = undefined,
5
7
pos: usize = 0,
8
8
+
9
9
+
pub const ReadError = error{EndOfBuffer};
10
10
+
11
11
+
pub const Reader = io.Reader(*BytePacketBuffer, ReadError, read);
6
12
7
13
/// Change the buffer position forward a specific number of steps
8
14
pub fn step(self: *BytePacketBuffer, pos: usize) void {
···
14
20
self.pos = pos;
15
21
}
16
22
23
23
+
pub fn reader(self: *BytePacketBuffer) Reader {
24
24
+
return .{ .context = self };
25
25
+
}
26
26
+
17
27
/// Read a single byte and move the position one step forward
18
18
-
pub fn read(self: *BytePacketBuffer) error{EndOfBuffer}!u8 {
19
19
-
if (self.pos >= comptime self.buf.len) return error.EndOfBuffer;
20
20
-
const res = self.buf[self.pos];
21
21
-
self.pos += 1;
22
22
-
return res;
28
28
+
pub fn read(self: *BytePacketBuffer, dest: []u8) ReadError!usize {
29
29
+
const size = @min(dest.len, self.buf.len - self.pos);
30
30
+
const end = self.pos + size;
31
31
+
32
32
+
@memcpy(dest[0..size], self.buf[self.pos..end]);
33
33
+
self.pos = end;
34
34
+
35
35
+
return size;
23
36
}
24
37
25
38
/// Get a single byte without changing the buffer position
26
26
-
pub fn get(self: *const BytePacketBuffer, pos: usize) error{EndOfBuffer}!u8 {
27
27
-
if (pos >= comptime self.buf.len) return error.EndOfBuffer;
39
39
+
pub fn get(self: *const BytePacketBuffer, pos: usize) ReadError!u8 {
40
40
+
if (pos >= comptime self.buf.len) return ReadError.EndOfBuffer;
28
41
return self.buf[pos];
29
42
}
30
43
31
44
/// Get a range of bytes
32
32
-
pub fn get_range(self: *const BytePacketBuffer, start: usize, len: usize) error{EndOfBuffer}![]const u8 {
33
33
-
if (start + len >= comptime self.buf.len) return error.EndOfBuffer;
45
45
+
pub fn get_range(self: *const BytePacketBuffer, start: usize, len: usize) ReadError![]const u8 {
46
46
+
if (start + len >= comptime self.buf.len) return ReadError.EndOfBuffer;
34
47
return self.buf[start .. start + len];
35
48
}
36
49
37
37
-
/// Read two bytes, stepping two steps forward
38
38
-
pub fn read_u16(self: *BytePacketBuffer) error{EndOfBuffer}!u16 {
39
39
-
return (@as(u16, try self.read()) << 8) |
40
40
-
@as(u16, try self.read());
41
41
-
}
42
42
-
43
43
-
/// Read two bytes, stepping two steps forward
44
44
-
pub fn read_u32(self: *BytePacketBuffer) error{EndOfBuffer}!u32 {
45
45
-
return @as(u32, try self.read()) << 24 |
46
46
-
(@as(u32, try self.read()) << 16) |
47
47
-
(@as(u32, try self.read()) << 8) |
48
48
-
(@as(u32, try self.read()));
49
49
-
}
50
50
-
51
50
/// Read a qname
52
51
///
53
52
/// The tricky part: Reading domain names, taking labels into consideration.
···
89
88
continue;
90
89
} else {
91
90
// Move a single byte forward to move path the length
91
91
+
self.pos += 1;
92
92
pos += 1;
93
93
94
94
// Domain names are terminated by an empty label of length 0, so if the length
···
100
100
out_pos += del.len;
101
101
}
102
102
103
103
-
@memcpy(outstr[out_pos .. out_pos + len], try self.get_range(pos, len));
103
103
+
const read_len = try self.read(outstr[out_pos .. out_pos + len]);
104
104
+
assert(read_len == len);
105
105
+
104
106
delim = ".";
105
107
106
108
pos += len;
···
116
118
const testing = std.testing;
117
119
var buf = BytePacketBuffer{};
118
120
buf.buf[0] = 0x1;
119
119
-
try testing.expectEqual(0x1, try buf.read());
121
121
+
try testing.expectEqual(0x1, try buf.reader().readInt(u8, .big));
120
122
}
121
123
122
124
test "BytePacketBuffer.read_u16" {
···
124
126
var buf = BytePacketBuffer{};
125
127
buf.buf[0] = 0x1;
126
128
buf.buf[1] = 0x1;
127
127
-
try testing.expectEqual(0x101, try buf.read_u16());
129
129
+
try testing.expectEqual(0x101, try buf.reader().readInt(u16, .big));
128
130
}
129
131
130
132
test "BytePacketBuffer.read_u32" {
···
134
136
buf.buf[1] = 0x1;
135
137
buf.buf[2] = 0x1;
136
138
buf.buf[3] = 0x1;
137
137
-
try testing.expectEqual(0x1010101, try buf.read_u32());
139
139
+
try testing.expectEqual(0x1010101, try buf.reader().readInt(u32, .big));
138
140
}
139
141
140
142
test "BytePacketBuffer.read_qname" {