From 80ec63b6a422392936ef6b9630baed55452b5df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Fri, 18 Feb 2022 15:25:10 +0200 Subject: [PATCH] fix casting functions, add some tests --- src/header.zig | 9 ++++++--- src/shell.zig | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/header.zig b/src/header.zig index bfff6c7..ea55359 100644 --- a/src/header.zig +++ b/src/header.zig @@ -6,6 +6,8 @@ const Magic = [4]u8{ 0xf0, 0x9f, 0xa4, 0xb7 }; const Version = 0; const Bom = 0x1234; +pub const SectionLength = 64; + const HeaderError = error{ InvalidMagic, InvalidVersion, @@ -56,7 +58,7 @@ const Header = packed struct { self.offset_additional_gids, }; for (offsets) |offset| { - if (offset & @intCast(u32, 63) != 0) { + if (offset & (SectionLength - 1) != 0) { return error.InvalidOffset; } } @@ -71,8 +73,9 @@ const Header = packed struct { const testing = std.testing; -test "header size is byte-aligned" { +test "constants and types are reasonable" { try testing.expectEqual(HeaderSize * 8, @bitSizeOf(Header)); + try testing.expect(std.math.isPowerOfTwo(SectionLength)); } test "header pack and unpack" { @@ -117,7 +120,7 @@ test "header pack and unpack" { { var header = goodHeader; - header.offset_cmph_uid2user = 128; + header.offset_cmph_uid2user = 65; try testing.expectError(error.InvalidOffset, Header.init(header.asArray())); } } diff --git a/src/shell.zig b/src/shell.zig index cd0a279..2d1c5b8 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -10,17 +10,6 @@ const StringContext = std.hash_map.StringContext; pub const MaxShells = 63; pub const MaxShellLen = 64; -// ShellIndex is an index to the shell strings. As shell can be up to 64 bytes -// (1<<6), maximum number of shells is 63 (1<<6-1), the maximum location offset -// is 1<<12. To make location resolvable in 10 bits, all shells will be padded -// to 4 bytes. -// The actual shell length is len+1: we don't allow empty shells, and the real -// length of the shell is 1-64 bytes. -const ShellIndex = struct { - offset: u10, - len: u6, -}; - // ShellReader interprets "Shell Index" and "Shell Blob" sections. pub const ShellReader = struct { sectionIndex: []const ShellIndex, @@ -81,7 +70,7 @@ pub const ShellWriter = struct { const padding = roundUp4Padding(fullOffset); fullOffset += padding; //const stderr = std.io.getStdErr().writer(); - //try stderr.print("\n", .{}); + //stderr.print("\n", .{}) catch unreachable; try self.blob.appendNTimes(0, padding); idx += 1; } @@ -181,6 +170,17 @@ inline fn roundUp4Padding(n: u12) u12 { return ((n + 3) & ~@intCast(u12, 3)) - n; } +// ShellIndex is an index to the shell strings. As shell can be up to 64 bytes +// (1<<6), maximum number of shells is 63 (1<<6-1), the maximum location offset +// is 1<<12. To make location resolvable in 10 bits, all shells will be padded +// to 4 bytes. +// The actual shell length is len+1: we don't allow empty shells, and the real +// length of the shell is 1-64 bytes. +const ShellIndex = struct { + offset: u10, + len: u6, +}; + const testing = std.testing; test "basic shellpopcon" {