From c6bd3f5cb84f7131f20a1a8a49192517c9eefddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 18 Apr 2022 13:11:20 +0300 Subject: [PATCH] storing getgr_max and getpw_max --- lib/Corpus.zig | 19 +++++++++++++++---- lib/DB.zig | 4 ++-- lib/Group.zig | 7 +++++++ lib/User.zig | 8 ++++++++ lib/header.zig | 4 ++-- 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/lib/Corpus.zig b/lib/Corpus.zig index 0714e24..1ce08c4 100644 --- a/lib/Corpus.zig +++ b/lib/Corpus.zig @@ -26,6 +26,9 @@ name2group: StringHashMap(u32), group2users: []const []const u32, user2groups: []const []const u32, +getgr_max: usize, +getpw_max: usize, + pub fn init( baseAllocator: Allocator, usersConst: []const User, @@ -38,12 +41,18 @@ pub fn init( var allocator = arena.allocator(); errdefer arena.deinit(); - var users_arr = try allocator.alloc(User, usersConst.len); var groups_arr = try allocator.alloc(Group, groupsConst.len); - for (usersConst) |*user, i| - users_arr[i] = try user.clone(allocator); - for (groupsConst) |*group, i| + var users_arr = try allocator.alloc(User, usersConst.len); + var getgr_max: usize = 0; + for (groupsConst) |*group, i| { groups_arr[i] = try group.clone(allocator); + getgr_max = math.max(getgr_max, groups_arr[i].strlenZ()); + } + var getpw_max: usize = 0; + for (usersConst) |*user, i| { + users_arr[i] = try user.clone(allocator); + getpw_max = math.max(getpw_max, users_arr[i].strlenZ()); + } sort.sort(User, users_arr, {}, cmpUser); sort.sort(Group, groups_arr, {}, cmpGroup); @@ -114,6 +123,8 @@ pub fn init( .name2group = name2group, .group2users = group2users, .user2groups = user2groups_final, + .getgr_max = getgr_max, + .getpw_max = getpw_max, }; } diff --git a/lib/DB.zig b/lib/DB.zig index 1b9ebff..36e3c33 100644 --- a/lib/DB.zig +++ b/lib/DB.zig @@ -119,8 +119,8 @@ pub fn fromCorpus( .nblocks_users = nblocks(u64, users.blob), .nblocks_groupmembers = nblocks(u64, groupmembers.blob), .nblocks_additional_gids = nblocks(u64, additional_gids.blob), - .getgr_max = 0, - .getpw_max = 0, + .getgr_max = corpus.getgr_max, + .getpw_max = corpus.getpw_max, }; return DB{ diff --git a/lib/Group.zig b/lib/Group.zig index f6a4e1c..f8b1b6d 100644 --- a/lib/Group.zig +++ b/lib/Group.zig @@ -54,6 +54,13 @@ pub fn deinit(self: *Group, allocator: Allocator) void { self.* = undefined; } +// buffer size in bytes if all strings were zero-terminated. +pub fn strlenZ(self: *const Group) usize { + return self._buf.len + + self.members.len + // each membername sentinel + 1; // name sentinel +} + const testing = std.testing; test "Group.clone" { diff --git a/lib/User.zig b/lib/User.zig index c290fb2..adab1be 100644 --- a/lib/User.zig +++ b/lib/User.zig @@ -103,6 +103,14 @@ fn strlen(self: *const User) usize { self.shell.len; } +// length of all string-data fields, assuming they are zero-terminated. +// Includes one character for "password". +pub fn strlenZ(self: *const User) usize { + return self.strlen() + + 4 + // '\0' of name, gecos, home and shell + 2; // password: 'x\0' +} + pub fn deinit(self: *User, allocator: Allocator) void { const slice = self.home.ptr[0..self.strlen()]; allocator.free(slice); diff --git a/lib/header.zig b/lib/header.zig index 516cf6d..a499ce5 100644 --- a/lib/header.zig +++ b/lib/header.zig @@ -92,8 +92,8 @@ test "header pack and unpack" { .nblocks_users = 0, .nblocks_groupmembers = 0, .nblocks_additional_gids = 1, - .getgr_max = 0, - .getpw_max = 0, + .getgr_max = 16, + .getpw_max = 32, }; const bytes = mem.asBytes(&header1);