From 49a7d79b050fd2cb43c083aef0d5dbb8b2228e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Thu, 24 Feb 2022 17:33:27 +0200 Subject: [PATCH] PackedUser is now readable --- src/user.zig | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/user.zig b/src/user.zig index def7929..692b62a 100644 --- a/src/user.zig +++ b/src/user.zig @@ -124,9 +124,8 @@ pub const PackedUser = struct { // packTo packs the User record and copies it to the given byte slice. The // slice must have at least maxRecordSize() bytes available. pub fn packTo(buf1: *[]u8, user: User, shellIndexFn: shellIndexFnType) error{InvalidRecord}!void { - std.debug.print("\nbuf1.len: {d}\n", .{buf1.*.len}); var buf = buf1.*; - const bufStart = @ptrToInt(&buf[0]); + const bufStart = @ptrToInt(buf.ptr); const home_len = try downCast(u6, user.home.len - 1); const name_len = try downCast(u5, user.name.len - 1); @@ -150,27 +149,32 @@ pub const PackedUser = struct { .gecos_len = gecos_len, }; const innerBytes = mem.asBytes(&inner); + buf.len += innerBytes.len + + user.home.len + + user.gecos.len; - mem.copy(u8, buf, innerBytes); + mem.copy(u8, buf[0..innerBytes.len], innerBytes); buf = buf[innerBytes.len..]; - mem.copy(u8, buf, user.home); + mem.copy(u8, buf[0..user.home.len], user.home); buf = buf[user.home.len..]; if (!inner.name_is_a_suffix) { - mem.copy(u8, buf, user.name); + buf.len += user.name.len; + mem.copy(u8, buf[0..user.name.len], user.name); buf = buf[user.name.len..]; } mem.copy(u8, buf, user.gecos); buf = buf[user.gecos.len..]; if (inner.shell_here) { - mem.copy(u8, buf, user.shell); + buf.len += user.shell.len; + mem.copy(u8, buf[0..user.shell.len], user.shell); buf = buf[user.shell.len..]; } - _ = bufStart; - //const bufLen = @ptrToInt(&buf[0]) - bufStart; - //const padding = pad.roundUpPadding(u64, PackedUserAlignmentBits, bufLen); - //mem.set(u8, buf[0..padding], 0); + const bufLen = @ptrToInt(buf.ptr) - bufStart; + const padding = pad.roundUpPadding(u64, PackedUserAlignmentBits, bufLen); + buf.len += padding; + mem.set(u8, buf[0..padding], 0); } // maxSize is the maximum number of records a PackedUser can take @@ -358,10 +362,7 @@ test "construct PackedUser section" { .shell = "s" ** 64, } }; for (users) |user| { - std.debug.print("\nmaxSize: {d}\n", .{PackedUser.maxSize()}); try buf.ensureUnusedCapacity(PackedUser.maxSize()); - buf.items[0] = 1; - std.debug.print("\nbuf.items.len: {d}\n", .{buf.items.len}); try PackedUser.packTo(&buf.items, user, testShellIndex); }