ona/src/oar.zig

49 lines
1.3 KiB
Zig
Raw Normal View History

const std = @import("std");
const sys = @import("./sys.zig");
///
/// An entry block of an Oar archive file.
///
/// Typically, following this block in memory is the file data it holds the meta-information for.
///
pub const Entry = extern struct {
2022-10-11 01:03:02 +01:00
signature: [3]u8,
revision: u8,
name_length: u8,
name_buffer: [255]u8 = std.mem.zeroes([255]u8),
file_size: u64,
2022-10-11 01:03:02 +01:00
padding: [244]u8,
///
/// Returns `true` if `entry` correctly identifies itself as a valid Oar entry, otherwise
/// `false`.
///
pub fn isValid(entry: Entry) bool {
return std.mem.eql(u8, &entry.signature, "oar");
}
///
/// Attempts to read an [Entry] from `file_access` at its current cursor position.
///
/// Returns the read [Entry] value or `null` if the end of file is reached before completing the
/// read.
///
pub fn read(file_access: *sys.FileAccess) sys.FileAccess.Error!?Entry {
var entry = std.mem.zeroes(Entry);
const origin = try file_access.queryCursor();
if ((try file_access.read(std.mem.asBytes(&entry))) != @sizeOf(Entry)) {
try file_access.seek(origin);
return null;
}
return entry;
}
///
/// Magic identifier used to validate [Entry] data.
///
const signature_magic = "oar";
};