1
Fork 0

generalize padding funcs

main
Motiejus Jakštys 2022-02-19 21:23:33 +02:00 committed by Motiejus Jakštys
parent 2fc925923f
commit ae2d3d5443
3 changed files with 20 additions and 19 deletions

View File

@ -2,28 +2,29 @@ const std = @import("std");
// rounds up a u12 to the nearest factor of 4 and returns the difference // rounds up a u12 to the nearest factor of 4 and returns the difference
// (padding) // (padding)
pub fn roundUp4Padding(comptime T: type, n: T) T { pub fn roundUpPadding(comptime T: type, comptime nbits: u8, n: T) T {
return roundUp4(T, n) - n; return roundUp(T, nbits, n) - n;
} }
// rounds up a u12 to the nearest factor of 4. // rounds up a u12 to the nearest factor of 4.
pub fn roundUp4(comptime T: type, n: T) T { pub fn roundUp(comptime T: type, comptime nbits: u8, n: T) T {
return ((n + 3) & ~@as(T, 3)); const factor = comptime (1 << nbits) - 1;
return ((n + factor) & ~@as(T, factor));
} }
const testing = std.testing; const testing = std.testing;
test "padding" { test "padding" {
try testing.expectEqual(roundUp4Padding(u12, 0), 0); try testing.expectEqual(roundUpPadding(u12, 2, 0), 0);
try testing.expectEqual(roundUp4Padding(u12, 1), 3); try testing.expectEqual(roundUpPadding(u12, 2, 1), 3);
try testing.expectEqual(roundUp4Padding(u12, 2), 2); try testing.expectEqual(roundUpPadding(u12, 2, 2), 2);
try testing.expectEqual(roundUp4Padding(u12, 3), 1); try testing.expectEqual(roundUpPadding(u12, 2, 3), 1);
try testing.expectEqual(roundUp4Padding(u12, 4), 0); try testing.expectEqual(roundUpPadding(u12, 2, 4), 0);
try testing.expectEqual(roundUp4Padding(u12, 40), 0); try testing.expectEqual(roundUpPadding(u12, 2, 40), 0);
try testing.expectEqual(roundUp4Padding(u12, 41), 3); try testing.expectEqual(roundUpPadding(u12, 2, 41), 3);
try testing.expectEqual(roundUp4Padding(u12, 42), 2); try testing.expectEqual(roundUpPadding(u12, 2, 42), 2);
try testing.expectEqual(roundUp4Padding(u12, 43), 1); try testing.expectEqual(roundUpPadding(u12, 2, 43), 1);
try testing.expectEqual(roundUp4Padding(u12, 44), 0); try testing.expectEqual(roundUpPadding(u12, 2, 44), 0);
try testing.expectEqual(roundUp4Padding(u12, 4091), 1); try testing.expectEqual(roundUpPadding(u12, 2, 4091), 1);
try testing.expectEqual(roundUp4Padding(u12, 4092), 0); try testing.expectEqual(roundUpPadding(u12, 2, 4092), 0);
} }

View File

@ -71,7 +71,7 @@ pub const ShellWriter = struct {
}); });
fullOffset += len; fullOffset += len;
const padding = pad.roundUp4Padding(u12, fullOffset); const padding = pad.roundUpPadding(u12, 2, fullOffset);
fullOffset += padding; fullOffset += padding;
//const stderr = std.io.getStdErr().writer(); //const stderr = std.io.getStdErr().writer();
//stderr.print("\n", .{}) catch unreachable; //stderr.print("\n", .{}) catch unreachable;
@ -209,7 +209,7 @@ test "basic shellpopcon" {
try testing.expectEqual(sections.getIndex(nobody), null); try testing.expectEqual(sections.getIndex(nobody), null);
try testing.expectEqual( try testing.expectEqual(
sections.sectionBlob().len, sections.sectionBlob().len,
pad.roundUp4(u12, bash.len) + pad.roundUp4(u12, zsh.len) + pad.roundUp4(u12, long.len), pad.roundUp(u12, 2, bash.len) + pad.roundUp(u12, 2, zsh.len) + pad.roundUp(u12, 2, long.len),
); );
const shellReader = ShellReader.init( const shellReader = ShellReader.init(

View File

@ -74,7 +74,7 @@ pub const UserWriter = struct {
try self.appendTo.appendSlice(user.shell); try self.appendTo.appendSlice(user.shell);
} }
const padding = pad.roundUp4Padding(u64, self.appendTo.items.len); const padding = pad.roundUpPadding(u64, 2, self.appendTo.items.len);
try self.appendTo.appendNTimes(0, padding); try self.appendTo.appendNTimes(0, padding);
} }
}; };