1
Fork 0

update padded user alignment

main
Motiejus Jakštys 2022-02-20 09:10:25 +02:00 committed by Motiejus Jakštys
parent 330db487ac
commit 988ab9f6d4
2 changed files with 22 additions and 7 deletions

View File

@ -10,6 +10,7 @@ const StringContext = std.hash_map.StringContext;
// MaxShells is the maximum number of "popular" shells.
pub const MaxShells = 63;
pub const MaxShellLen = 64;
const ShellAlignment = 2; // bits
// ShellReader interprets "Shell Index" and "Shell Blob" sections.
pub const ShellReader = struct {
@ -71,7 +72,7 @@ pub const ShellWriter = struct {
});
fullOffset += len;
const padding = pad.roundUpPadding(u12, 2, fullOffset);
const padding = pad.roundUpPadding(u12, ShellAlignment, fullOffset);
fullOffset += padding;
//const stderr = std.io.getStdErr().writer();
//stderr.print("\n", .{}) catch unreachable;

View File

@ -17,6 +17,7 @@ pub const PackedUser = packed struct {
name_len: u5,
gecos_len: u8,
};
const PackedUserAlignment = 3; // bits
pub const User = struct {
uid: u32,
@ -46,11 +47,21 @@ pub const UserWriter = struct {
const fromUserErr = error{InvalidRecord};
pub fn downCast(comptime T: type, n: u64) fromUserErr!T {
if (std.math.cast(T, n)) |result| {
return result;
} else |err| switch (err) {
error.Overflow => {
return error.InvalidRecord;
},
}
}
pub fn appendUser(self: *UserWriter, user: User) !void {
const home_len = cast(u6, user.home.len - 1) catch return error.InvalidRecord;
const name_len = cast(u5, user.name.len - 1) catch return error.InvalidRecord;
const shell_len = cast(u6, user.shell.len - 1) catch return error.InvalidRecord;
const gecos_len = cast(u8, user.gecos.len) catch return error.InvalidRecord;
const home_len = try downCast(u6, user.home.len - 1);
const name_len = try downCast(u5, user.name.len - 1);
const shell_len = try downCast(u6, user.shell.len - 1);
const gecos_len = try downCast(u8, user.gecos.len);
var puser = PackedUser{
.uid = user.uid,
@ -74,8 +85,11 @@ pub const UserWriter = struct {
try self.appendTo.appendSlice(user.shell);
}
const padding = pad.roundUpPadding(u64, 2, self.appendTo.items.len);
try self.appendTo.appendNTimes(0, padding);
try self.appendTo.appendNTimes(0, pad.roundUpPadding(
u64,
PackedUserAlignment,
self.appendTo.items.len,
));
}
};