mirror of
https://git.robbyzambito.me/zaprus
synced 2025-12-21 00:34:50 +00:00
Correctly handle the endiness and packedness of the the header reading and writing
This commit is contained in:
@@ -64,7 +64,7 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn toBytesAux(
|
fn toBytesAux(
|
||||||
comptime Header: type,
|
Header: type,
|
||||||
header: Header,
|
header: Header,
|
||||||
payload: []const u8,
|
payload: []const u8,
|
||||||
buf: *std.ArrayList(u8),
|
buf: *std.ArrayList(u8),
|
||||||
@@ -81,19 +81,18 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
|
|
||||||
// At this point, payload_list contains the base64 encoded payload.
|
// At this point, payload_list contains the base64 encoded payload.
|
||||||
|
|
||||||
// Write the packet body to the output buf.
|
// Add the payload length to the output buf.
|
||||||
try buf.*.appendSlice(asBytes(&nativeToBig(u16, @intCast(payload_list.items.len))));
|
try buf.*.appendSlice(
|
||||||
|
asBytes(&nativeToBig(u16, @intCast(payload_list.items.len + @bitSizeOf(Header) / 8))),
|
||||||
// Write the header bytes to the output buf.
|
|
||||||
const HeaderInt = @typeInfo(Header).@"struct".backing_integer.?;
|
|
||||||
std.mem.writePackedInt(
|
|
||||||
HeaderInt,
|
|
||||||
try buf.*.addManyAsSlice(@bitSizeOf(Header) / 8),
|
|
||||||
0,
|
|
||||||
@bitCast(header),
|
|
||||||
.little,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Add the header bytes to the output buf.
|
||||||
|
var header_buf: [@sizeOf(Header)]u8 = undefined;
|
||||||
|
var header_buf_stream = std.io.fixedBufferStream(&header_buf);
|
||||||
|
try header_buf_stream.writer().writeStructEndian(header, .big);
|
||||||
|
// Add the exact number of bits in the header without padding.
|
||||||
|
try buf.*.appendSlice(header_buf[0 .. @bitSizeOf(Header) / 8]);
|
||||||
|
|
||||||
try buf.*.appendSlice(payload_list.items);
|
try buf.*.appendSlice(payload_list.items);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,6 +100,7 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
pub fn toBytes(self: SaprusMessage, allocator: Allocator) ![]u8 {
|
pub fn toBytes(self: SaprusMessage, allocator: Allocator) ![]u8 {
|
||||||
// Create a growable list of bytes to store the output in.
|
// Create a growable list of bytes to store the output in.
|
||||||
var buf = std.ArrayList(u8).init(allocator);
|
var buf = std.ArrayList(u8).init(allocator);
|
||||||
|
errdefer buf.deinit();
|
||||||
|
|
||||||
// Start with writing the message type, which is the first 16 bits of every Saprus message.
|
// Start with writing the message type, which is the first 16 bits of every Saprus message.
|
||||||
try buf.appendSlice(asBytes(&nativeToBig(u16, @intFromEnum(self))));
|
try buf.appendSlice(asBytes(&nativeToBig(u16, @intFromEnum(self))));
|
||||||
@@ -116,17 +116,21 @@ pub const SaprusMessage = union(SaprusPacketType) {
|
|||||||
return buf.toOwnedSlice();
|
return buf.toOwnedSlice();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fn fromBytesAux(
|
fn fromBytesAux(
|
||||||
packet: SaprusPacketType,
|
comptime packet: SaprusPacketType,
|
||||||
Header: type,
|
Header: type,
|
||||||
r: std.io.FixedBufferStream([]const u8).Reader,
|
r: std.io.FixedBufferStream([]const u8).Reader,
|
||||||
allocator: Allocator,
|
allocator: Allocator,
|
||||||
) !SaprusMessage {
|
) !SaprusMessage {
|
||||||
// Read the header for the current message type.
|
|
||||||
const header = try r.readStructEndian(Header, .big);
|
|
||||||
// Read the length of the base64 encoded payload.
|
// Read the length of the base64 encoded payload.
|
||||||
const len = try r.readInt(u16, .big);
|
const len = try r.readInt(u16, .big);
|
||||||
|
|
||||||
|
// Read the header for the current message type.
|
||||||
|
var header_bytes: [@sizeOf(Header)]u8 = undefined;
|
||||||
|
_ = try r.read(header_bytes[0 .. @bitSizeOf(Header) / 8]);
|
||||||
|
var header_stream = std.io.fixedBufferStream(&header_bytes);
|
||||||
|
const header = try header_stream.reader().readStructEndian(Header, .big);
|
||||||
|
|
||||||
// Read the base64 bytes into a list to be able to call the decoder on it.
|
// Read the base64 bytes into a list to be able to call the decoder on it.
|
||||||
var payload_buf = std.ArrayList(u8).init(allocator);
|
var payload_buf = std.ArrayList(u8).init(allocator);
|
||||||
defer payload_buf.deinit();
|
defer payload_buf.deinit();
|
||||||
|
|||||||
Reference in New Issue
Block a user