From 2fc925923ff4184b417dc7c218b62723671411ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Sat, 19 Feb 2022 18:18:14 +0200 Subject: [PATCH] wip between-user padding --- src/padding.zig | 29 +++++++++++++++++++++++++++++ src/shell.zig | 31 +++---------------------------- src/test_main.zig | 1 + src/user.zig | 4 ++++ 4 files changed, 37 insertions(+), 28 deletions(-) create mode 100644 src/padding.zig diff --git a/src/padding.zig b/src/padding.zig new file mode 100644 index 0000000..cfd9a13 --- /dev/null +++ b/src/padding.zig @@ -0,0 +1,29 @@ +const std = @import("std"); + +// rounds up a u12 to the nearest factor of 4 and returns the difference +// (padding) +pub fn roundUp4Padding(comptime T: type, n: T) T { + return roundUp4(T, n) - n; +} + +// rounds up a u12 to the nearest factor of 4. +pub fn roundUp4(comptime T: type, n: T) T { + return ((n + 3) & ~@as(T, 3)); +} + +const testing = std.testing; + +test "padding" { + try testing.expectEqual(roundUp4Padding(u12, 0), 0); + try testing.expectEqual(roundUp4Padding(u12, 1), 3); + try testing.expectEqual(roundUp4Padding(u12, 2), 2); + try testing.expectEqual(roundUp4Padding(u12, 3), 1); + try testing.expectEqual(roundUp4Padding(u12, 4), 0); + try testing.expectEqual(roundUp4Padding(u12, 40), 0); + try testing.expectEqual(roundUp4Padding(u12, 41), 3); + try testing.expectEqual(roundUp4Padding(u12, 42), 2); + try testing.expectEqual(roundUp4Padding(u12, 43), 1); + try testing.expectEqual(roundUp4Padding(u12, 44), 0); + try testing.expectEqual(roundUp4Padding(u12, 4091), 1); + try testing.expectEqual(roundUp4Padding(u12, 4092), 0); +} diff --git a/src/shell.zig b/src/shell.zig index 40d8a0c..9dcacfb 100644 --- a/src/shell.zig +++ b/src/shell.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const pad = @import("padding.zig"); const Allocator = std.mem.Allocator; const PriorityDequeue = std.PriorityDequeue; const StringArrayHashMap = std.StringArrayHashMap; @@ -70,7 +71,7 @@ pub const ShellWriter = struct { }); fullOffset += len; - const padding = roundUp4Padding(fullOffset); + const padding = pad.roundUp4Padding(u12, fullOffset); fullOffset += padding; //const stderr = std.io.getStdErr().writer(); //stderr.print("\n", .{}) catch unreachable; @@ -167,17 +168,6 @@ pub const ShellWriter = struct { } }; -// rounds up a u12 to the nearest factor of 4 and returns the difference -// (padding) -inline fn roundUp4Padding(n: u12) u12 { - return roundUp4(n) - n; -} - -// rounds up a u12 to the nearest factor of 4. -inline fn roundUp4(n: u12) u12 { - return ((n + 3) & ~@intCast(u12, 3)); -} - // 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 @@ -219,7 +209,7 @@ test "basic shellpopcon" { try testing.expectEqual(sections.getIndex(nobody), null); try testing.expectEqual( sections.sectionBlob().len, - roundUp4(bash.len) + roundUp4(zsh.len) + roundUp4(long.len), + pad.roundUp4(u12, bash.len) + pad.roundUp4(u12, zsh.len) + pad.roundUp4(u12, long.len), ); const shellReader = ShellReader.init( @@ -232,18 +222,3 @@ test "basic shellpopcon" { try testing.expectEqual(shellReader.sectionIndex.len, 3); } - -test "padding" { - try testing.expectEqual(roundUp4Padding(@intCast(u12, 0)), 0); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 1)), 3); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 2)), 2); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 3)), 1); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 4)), 0); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 40)), 0); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 41)), 3); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 42)), 2); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 43)), 1); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 44)), 0); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 4091)), 1); - try testing.expectEqual(roundUp4Padding(@intCast(u12, 4092)), 0); -} diff --git a/src/test_main.zig b/src/test_main.zig index 0eb0a4c..6dc2f8f 100644 --- a/src/test_main.zig +++ b/src/test_main.zig @@ -3,4 +3,5 @@ test "turbonss test suite" { _ = @import("shell.zig"); _ = @import("header.zig"); _ = @import("user.zig"); + _ = @import("padding.zig"); } diff --git a/src/user.zig b/src/user.zig index 44b24fc..fd46454 100644 --- a/src/user.zig +++ b/src/user.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const pad = @import("padding.zig"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; @@ -72,6 +73,9 @@ pub const UserWriter = struct { if (puser.shell_here) { try self.appendTo.appendSlice(user.shell); } + + const padding = pad.roundUp4Padding(u64, self.appendTo.items.len); + try self.appendTo.appendNTimes(0, padding); } };