1
Fork 0

header may err

main
Motiejus Jakštys 2022-02-18 07:16:11 +02:00 committed by Motiejus Jakštys
parent 17344507e8
commit ed1e464305
1 changed files with 10 additions and 6 deletions

View File

@ -1,10 +1,17 @@
const std = @import("std"); const std = @import("std");
const HeaderSize = @bitSizeOf(Header) / 8; const HeaderSize = @sizeOf(Header);
const Magic = [4]u8{ 0xf0, 0x9f, 0xa4, 0xb7 }; const Magic = [4]u8{ 0xf0, 0x9f, 0xa4, 0xb7 };
const Version = 0; const Version = 0;
const Bom = 0x1234; const Bom = 0x1234;
const HeaderError = error{
InvalidMagic,
InvalidVersion,
InvalidBom,
TooManyShells,
};
const Header = packed struct { const Header = packed struct {
magic: [4]u8, magic: [4]u8,
version: u8, version: u8,
@ -21,10 +28,7 @@ const Header = packed struct {
offset_groupmembers: u32, offset_groupmembers: u32,
offset_additional_gids: u32, offset_additional_gids: u32,
// TODO(motiejus) should we be returning *Header instead? That way all pub fn init(blob: [HeaderSize]u8) HeaderError!Header {
// 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 {
return @bitCast(Header, blob); return @bitCast(Header, blob);
} }
@ -58,6 +62,6 @@ test "header pack and unpack" {
}; };
const blob = header.asArray(); const blob = header.asArray();
const header2 = Header.init(blob); const header2 = try Header.init(blob);
try testing.expectEqual(header, header2); try testing.expectEqual(header, header2);
} }