header may err

This commit is contained in:
Motiejus Jakštys 2022-02-18 07:16:11 +02:00 committed by Motiejus Jakštys
parent 17344507e8
commit ed1e464305

View File

@ -1,10 +1,17 @@
const std = @import("std");
const HeaderSize = @bitSizeOf(Header) / 8;
const HeaderSize = @sizeOf(Header);
const Magic = [4]u8{ 0xf0, 0x9f, 0xa4, 0xb7 };
const Version = 0;
const Bom = 0x1234;
const HeaderError = error{
InvalidMagic,
InvalidVersion,
InvalidBom,
TooManyShells,
};
const Header = packed struct {
magic: [4]u8,
version: u8,
@ -21,10 +28,7 @@ const Header = packed struct {
offset_groupmembers: u32,
offset_additional_gids: u32,
// TODO(motiejus) should we be returning *Header instead? That way all
// stacks could be smaller by 48 bytes, and we would be referring to
// it's fields in the mmap'ed region.
pub fn init(blob: [HeaderSize]u8) Header {
pub fn init(blob: [HeaderSize]u8) HeaderError!Header {
return @bitCast(Header, blob);
}
@ -58,6 +62,6 @@ test "header pack and unpack" {
};
const blob = header.asArray();
const header2 = Header.init(blob);
const header2 = try Header.init(blob);
try testing.expectEqual(header, header2);
}