ona/src/oar.zig
kayomn e108486c17
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Add data validation to Oar entries
2022-10-11 01:03:02 +01:00

49 lines
1.3 KiB
Zig

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 {
signature: [3]u8,
revision: u8,
name_length: u8,
name_buffer: [255]u8 = std.mem.zeroes([255]u8),
file_size: u64,
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";
};