1
Fork 0

fix casting functions, add some tests

This commit is contained in:
Motiejus Jakštys 2022-02-18 15:25:10 +02:00 committed by Motiejus Jakštys
parent 44107bac51
commit 80ec63b6a4
2 changed files with 18 additions and 15 deletions

View File

@ -6,6 +6,8 @@ const Magic = [4]u8{ 0xf0, 0x9f, 0xa4, 0xb7 };
const Version = 0; const Version = 0;
const Bom = 0x1234; const Bom = 0x1234;
pub const SectionLength = 64;
const HeaderError = error{ const HeaderError = error{
InvalidMagic, InvalidMagic,
InvalidVersion, InvalidVersion,
@ -56,7 +58,7 @@ const Header = packed struct {
self.offset_additional_gids, self.offset_additional_gids,
}; };
for (offsets) |offset| { for (offsets) |offset| {
if (offset & @intCast(u32, 63) != 0) { if (offset & (SectionLength - 1) != 0) {
return error.InvalidOffset; return error.InvalidOffset;
} }
} }
@ -71,8 +73,9 @@ const Header = packed struct {
const testing = std.testing; 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.expectEqual(HeaderSize * 8, @bitSizeOf(Header));
try testing.expect(std.math.isPowerOfTwo(SectionLength));
} }
test "header pack and unpack" { test "header pack and unpack" {
@ -117,7 +120,7 @@ test "header pack and unpack" {
{ {
var header = goodHeader; var header = goodHeader;
header.offset_cmph_uid2user = 128; header.offset_cmph_uid2user = 65;
try testing.expectError(error.InvalidOffset, Header.init(header.asArray())); try testing.expectError(error.InvalidOffset, Header.init(header.asArray()));
} }
} }

View File

@ -10,17 +10,6 @@ const StringContext = std.hash_map.StringContext;
pub const MaxShells = 63; pub const MaxShells = 63;
pub const MaxShellLen = 64; 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. // ShellReader interprets "Shell Index" and "Shell Blob" sections.
pub const ShellReader = struct { pub const ShellReader = struct {
sectionIndex: []const ShellIndex, sectionIndex: []const ShellIndex,
@ -81,7 +70,7 @@ pub const ShellWriter = struct {
const padding = roundUp4Padding(fullOffset); const padding = roundUp4Padding(fullOffset);
fullOffset += padding; fullOffset += padding;
//const stderr = std.io.getStdErr().writer(); //const stderr = std.io.getStdErr().writer();
//try stderr.print("\n", .{}); //stderr.print("\n", .{}) catch unreachable;
try self.blob.appendNTimes(0, padding); try self.blob.appendNTimes(0, padding);
idx += 1; idx += 1;
} }
@ -181,6 +170,17 @@ inline fn roundUp4Padding(n: u12) u12 {
return ((n + 3) & ~@intCast(u12, 3)) - n; 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; const testing = std.testing;
test "basic shellpopcon" { test "basic shellpopcon" {