2022-03-25 14:31:56 +02:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const mem = std.mem;
|
2022-07-21 11:23:13 +03:00
|
|
|
const assert = std.debug.assert;
|
2022-03-25 14:31:56 +02:00
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
const ArrayList = std.ArrayList;
|
|
|
|
const BufSet = std.BufSet;
|
|
|
|
|
2022-07-21 11:23:13 +03:00
|
|
|
const pad = @import("padding.zig");
|
|
|
|
const validate = @import("validate.zig");
|
|
|
|
const compress = @import("compress.zig");
|
|
|
|
const InvalidRecord = validate.InvalidRecord;
|
|
|
|
|
2022-03-25 14:31:56 +02:00
|
|
|
const PackedGroup = @This();
|
|
|
|
|
|
|
|
pub const GroupStored = struct {
|
|
|
|
gid: u32,
|
|
|
|
name: []const u8,
|
|
|
|
members_offset: u64,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const alignment_bits = 3;
|
|
|
|
|
|
|
|
const Inner = packed struct {
|
|
|
|
gid: u32,
|
|
|
|
padding: u3 = 0,
|
|
|
|
groupname_len: u5,
|
|
|
|
|
|
|
|
pub fn groupnameLen(self: *const Inner) usize {
|
|
|
|
return @as(usize, self.groupname_len) + 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inner: *const Inner,
|
|
|
|
groupdata: []const u8,
|
|
|
|
members_offset: u64,
|
|
|
|
|
|
|
|
pub const Entry = struct {
|
|
|
|
group: PackedGroup,
|
2022-07-14 11:09:01 +03:00
|
|
|
end: usize,
|
2022-03-25 14:31:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn fromBytes(bytes: []const u8) Entry {
|
|
|
|
const inner = mem.bytesAsValue(Inner, bytes[0..@sizeOf(Inner)]);
|
|
|
|
const start_blob = @sizeOf(Inner);
|
|
|
|
const end_strings = @sizeOf(Inner) + inner.groupnameLen();
|
|
|
|
const members_offset = compress.uvarint(bytes[end_strings..]) catch |err| switch (err) {
|
|
|
|
error.Overflow => unreachable,
|
|
|
|
};
|
|
|
|
const end_blob = end_strings + members_offset.bytes_read;
|
|
|
|
|
|
|
|
return Entry{
|
|
|
|
.group = PackedGroup{
|
|
|
|
.inner = inner,
|
|
|
|
.groupdata = bytes[start_blob..end_strings],
|
|
|
|
.members_offset = members_offset.value,
|
|
|
|
},
|
2022-07-14 11:09:01 +03:00
|
|
|
.end = pad.roundUp(usize, alignment_bits, end_blob),
|
2022-03-25 14:31:56 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validateUtf8(s: []const u8) InvalidRecord!void {
|
|
|
|
if (!std.unicode.utf8ValidateSlice(s))
|
|
|
|
return error.InvalidRecord;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const Iterator = struct {
|
2022-07-14 11:09:01 +03:00
|
|
|
section: []const u8,
|
2022-07-21 11:23:13 +03:00
|
|
|
next_start: usize = 0,
|
2022-07-14 11:09:01 +03:00
|
|
|
idx: u32 = 0,
|
|
|
|
total: u32,
|
2022-07-21 11:23:13 +03:00
|
|
|
advance_by: usize = 0,
|
2022-03-25 14:31:56 +02:00
|
|
|
|
|
|
|
pub fn next(it: *Iterator) ?PackedGroup {
|
2022-07-21 11:23:13 +03:00
|
|
|
const gr = it.peek() orelse return null;
|
|
|
|
it.advance();
|
|
|
|
return gr;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn peek(it: *Iterator) ?PackedGroup {
|
2022-07-14 11:09:01 +03:00
|
|
|
if (it.idx == it.total) return null;
|
2022-07-21 11:23:13 +03:00
|
|
|
const entry = fromBytes(it.section[it.next_start..]);
|
|
|
|
it.advance_by = entry.end;
|
|
|
|
return entry.group;
|
|
|
|
}
|
2022-07-14 11:09:01 +03:00
|
|
|
|
2022-07-21 11:23:13 +03:00
|
|
|
pub fn advance(it: *Iterator) void {
|
|
|
|
assert(it.advance_by > 0);
|
2022-07-14 11:09:01 +03:00
|
|
|
it.idx += 1;
|
2022-07-21 11:23:13 +03:00
|
|
|
it.next_start += it.advance_by;
|
|
|
|
it.advance_by = 0;
|
2022-03-25 14:31:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-14 11:09:01 +03:00
|
|
|
pub fn iterator(section: []const u8, total: u32) Iterator {
|
|
|
|
return Iterator{
|
|
|
|
.section = section,
|
|
|
|
.total = total,
|
|
|
|
};
|
2022-03-25 14:31:56 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 09:01:51 +03:00
|
|
|
pub inline fn gid(self: *const PackedGroup) u32 {
|
2022-03-25 14:31:56 +02:00
|
|
|
return self.inner.gid;
|
|
|
|
}
|
|
|
|
|
2022-07-15 09:01:51 +03:00
|
|
|
pub inline fn membersOffset(self: *const PackedGroup) u64 {
|
2022-03-25 14:31:56 +02:00
|
|
|
return self.members_offset;
|
|
|
|
}
|
|
|
|
|
2022-07-15 09:01:51 +03:00
|
|
|
pub inline fn name(self: *const PackedGroup) []const u8 {
|
2022-03-25 14:31:56 +02:00
|
|
|
return self.groupdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn packTo(
|
|
|
|
arr: *ArrayList(u8),
|
|
|
|
group: GroupStored,
|
|
|
|
) error{ InvalidRecord, OutOfMemory }!void {
|
|
|
|
std.debug.assert(arr.items.len & 7 == 0);
|
|
|
|
try validate.utf8(group.name);
|
|
|
|
const len = try validate.downCast(u5, group.name.len - 1);
|
|
|
|
const inner = Inner{ .gid = group.gid, .groupname_len = len };
|
|
|
|
try arr.*.appendSlice(mem.asBytes(&inner));
|
|
|
|
try arr.*.appendSlice(group.name);
|
|
|
|
try compress.appendUvarint(arr, group.members_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
const testing = std.testing;
|
|
|
|
|
|
|
|
test "PackedGroup alignment" {
|
|
|
|
try testing.expectEqual(@sizeOf(PackedGroup) * 8, @bitSizeOf(PackedGroup));
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:59:47 +03:00
|
|
|
test "PackedGroup construct" {
|
2022-03-25 14:31:56 +02:00
|
|
|
var buf = ArrayList(u8).init(testing.allocator);
|
|
|
|
defer buf.deinit();
|
|
|
|
|
|
|
|
const groups = [_]GroupStored{
|
|
|
|
GroupStored{
|
|
|
|
.gid = 1000,
|
|
|
|
.name = "sudo",
|
|
|
|
.members_offset = 1,
|
|
|
|
},
|
|
|
|
GroupStored{
|
|
|
|
.gid = std.math.maxInt(u32),
|
|
|
|
.name = "Name" ** 8, // 32
|
|
|
|
.members_offset = std.math.maxInt(u64),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (groups) |group| {
|
|
|
|
try PackedGroup.packTo(&buf, group);
|
|
|
|
try pad.arrayList(&buf, PackedGroup.alignment_bits);
|
|
|
|
}
|
|
|
|
|
|
|
|
var i: u29 = 0;
|
2022-07-14 11:09:01 +03:00
|
|
|
var it = PackedGroup.iterator(buf.items, groups.len);
|
2022-03-25 14:31:56 +02:00
|
|
|
while (it.next()) |group| : (i += 1) {
|
|
|
|
try testing.expectEqual(groups[i].gid, group.gid());
|
|
|
|
try testing.expectEqualStrings(groups[i].name, group.name());
|
|
|
|
try testing.expectEqual(groups[i].members_offset, group.membersOffset());
|
|
|
|
}
|
|
|
|
try testing.expectEqual(groups.len, i);
|
|
|
|
}
|