// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
using System.Linq;
using System.Collections.Generic;
namespace Kaitai
{
///
/// Nix Archive (NAR) is the archive format of the [Nix](https://nixos.org/)
/// package manager. A NAR contains a single file system object: a regular
/// file, a symbolic link, or a directory with everything in it. Besides
/// names, file contents and symlink targets, it only stores which regular
/// files are executable. There are
/// [no timestamps and no other permissions](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/command-ref/nix-store/dump.md?plain=1#L15-L19).
///
/// Unlike TAR or ZIP, the format is
/// [canonical](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/command-ref/nix-store/dump.md?plain=1#L21-L27):
/// a given file system object always has the same NAR. For example,
/// directory entries are always sorted by name. Nix identifies the contents
/// of each store path by the SHA-256 hash of its NAR, and
/// [binary caches](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/binary-cache/index.md?plain=1#L8-L10)
/// serve store paths as NARs, usually compressed.
/// [GNU Guix](https://codeberg.org/guix/guix/src/commit/ab6a9f84e783710335a29f6591f570e5e2df143c/guix/serialization.scm#L441-L446)
/// uses the same format.
///
/// This Kaitai Struct implementation rejects the same NARs as Nix 2.36.0
/// with the default settings on Linux, except that it accepts entry names
/// that contain `/`.
///
/// Older versions of Nix may accept more NARs. For example, Nix checks the
/// order of directory entries only since version 2.25.0, and it limits the
/// length of names and symlink targets and the nesting depth only since
/// 2.35.0. Some of these checks were also backported to patch releases of
/// older versions.
///
///
/// Reference: Git tag "2.36.0"
///
public partial class NixNar : KaitaiStruct
{
public static NixNar FromFile(string fileName)
{
return new NixNar(new KaitaiStream(fileName));
}
public NixNar(KaitaiStream p__io, KaitaiStruct p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root ?? this;
_read();
}
private void _read()
{
_magic = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _magic;
if (!(M_.Body == "nix-archive-1"))
{
throw new ValidationExprError(_magic, m_io, "/seq/0");
}
}
_rootNode = new Node(0, m_io, this, m_root);
}
///
/// A single file system node (regular file, directory, or symlink).
///
public partial class Node : KaitaiStruct
{
public Node(int p_depth, KaitaiStream p__io, KaitaiStruct p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_depth = p_depth;
_read();
}
private void _read()
{
__unnamed0 = m_io.ReadBytes(0);
{
byte[] M_ = __unnamed0;
if (!(Depth < 64))
{
throw new ValidationExprError(__unnamed0, m_io, "/types/node/seq/0");
}
}
_openParen = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _openParen;
if (!(M_.Body == "("))
{
throw new ValidationExprError(_openParen, m_io, "/types/node/seq/1");
}
}
_typeKey = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _typeKey;
if (!(M_.Body == "type"))
{
throw new ValidationExprError(_typeKey, m_io, "/types/node/seq/2");
}
}
_typeVal = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _typeVal;
if (!( ((M_.Body == "regular") || (M_.Body == "directory") || (M_.Body == "symlink")) ))
{
throw new ValidationExprError(_typeVal, m_io, "/types/node/seq/3");
}
}
switch (TypeVal.Body) {
case "directory": {
_body = new TypeDirectory(m_io, this, m_root);
break;
}
case "regular": {
_body = new TypeRegular(m_io, this, m_root);
break;
}
case "symlink": {
_body = new TypeSymlink(m_io, this, m_root);
break;
}
}
if (TypeVal.Body != "directory") {
_closeParen = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _closeParen;
if (!(M_.Body == ")"))
{
throw new ValidationExprError(_closeParen, m_io, "/types/node/seq/5");
}
}
}
}
private byte[] __unnamed0;
private PaddedStr _openParen;
private PaddedStr _typeKey;
private PaddedStr _typeVal;
private KaitaiStruct _body;
private PaddedStr _closeParen;
private int _depth;
private NixNar m_root;
private KaitaiStruct m_parent;
public byte[] Unnamed_0 { get { return __unnamed0; } }
///
/// Must be `(`, a token starting the node definition.
///
public PaddedStr OpenParen { get { return _openParen; } }
///
/// Must be `type`.
///
public PaddedStr TypeKey { get { return _typeKey; } }
///
/// Type of the node (`regular`, `directory`, or `symlink`).
///
public PaddedStr TypeVal { get { return _typeVal; } }
public KaitaiStruct Body { get { return _body; } }
///
/// Must be `)`, a token ending the node definition. A directory node
/// has no `close_paren`: its `)` is read as the terminator at the end
/// of `body.entries`.
///
public PaddedStr CloseParen { get { return _closeParen; } }
///
/// Nesting depth of the node. The root node has depth 0.
///
public int Depth { get { return _depth; } }
public NixNar M_Root { get { return m_root; } }
public KaitaiStruct M_Parent { get { return m_parent; } }
}
///
/// A byte sequence, prefixed with its length (u8le) and
/// padded with null bytes to the next 8-byte boundary.
///
/// Used for entry names and symlink targets, which Nix handles as raw
/// bytes, without decoding them as text.
///
public partial class PaddedBytes : KaitaiStruct
{
public static PaddedBytes FromFile(string fileName)
{
return new PaddedBytes(new KaitaiStream(fileName));
}
public PaddedBytes(KaitaiStream p__io, KaitaiStruct p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_read();
}
private void _read()
{
_lenBody = m_io.ReadU8le();
_body = m_io.ReadBytes(LenBody);
_padding = m_io.ReadBytes(KaitaiStream.Mod(8 - KaitaiStream.Mod(LenBody, 8), 8));
{
byte[] M_ = _padding;
if (!((M_.Length != 0 ? M_.Max() == 0 : true)))
{
throw new ValidationExprError(_padding, m_io, "/types/padded_bytes/seq/2");
}
}
}
private ulong _lenBody;
private byte[] _body;
private byte[] _padding;
private NixNar m_root;
private KaitaiStruct m_parent;
public ulong LenBody { get { return _lenBody; } }
public byte[] Body { get { return _body; } }
public byte[] Padding { get { return _padding; } }
public NixNar M_Root { get { return m_root; } }
public KaitaiStruct M_Parent { get { return m_parent; } }
}
///
/// A string, prefixed with its length (u8le) and
/// padded with null bytes to the next 8-byte boundary.
///
/// Used for the tokens of the format, such as `nix-archive-1`, `(` and
/// `type`. They are all ASCII.
///
public partial class PaddedStr : KaitaiStruct
{
public static PaddedStr FromFile(string fileName)
{
return new PaddedStr(new KaitaiStream(fileName));
}
public PaddedStr(KaitaiStream p__io, KaitaiStruct p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_read();
}
private void _read()
{
_lenBody = m_io.ReadU8le();
_body = System.Text.Encoding.GetEncoding("ASCII").GetString(m_io.ReadBytes(LenBody));
_padding = m_io.ReadBytes(KaitaiStream.Mod(8 - KaitaiStream.Mod(LenBody, 8), 8));
{
byte[] M_ = _padding;
if (!((M_.Length != 0 ? M_.Max() == 0 : true)))
{
throw new ValidationExprError(_padding, m_io, "/types/padded_str/seq/2");
}
}
}
private ulong _lenBody;
private string _body;
private byte[] _padding;
private NixNar m_root;
private KaitaiStruct m_parent;
public ulong LenBody { get { return _lenBody; } }
public string Body { get { return _body; } }
public byte[] Padding { get { return _padding; } }
public NixNar M_Root { get { return m_root; } }
public KaitaiStruct M_Parent { get { return m_parent; } }
}
///
/// A directory node, containing a list of entries. Entries must be
/// ordered by their names. Each name must be greater than the previous
/// one when compared lexicographically as arrays of unsigned bytes.
///
public partial class TypeDirectory : KaitaiStruct
{
public static TypeDirectory FromFile(string fileName)
{
return new TypeDirectory(new KaitaiStream(fileName));
}
public TypeDirectory(KaitaiStream p__io, NixNar.Node p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_read();
}
private void _read()
{
_entries = new List();
{
var i = 0;
DirEntry M_;
do {
M_ = new DirEntry(m_io, this, m_root);
_entries.Add(M_);
{
DirEntry M_ = _entries[i];
if (!(( ((i != 0) && (!(M_.IsTerminator))) ? (KaitaiStream.ByteArrayCompare(M_.Body.Name.Body, Entries[i - 1].Body.Name.Body) > 0) : true)))
{
throw new ValidationExprError(_entries[i], m_io, "/types/type_directory/seq/0");
}
}
i++;
} while (!(M_.IsTerminator));
}
}
///
/// A single entry within a directory, or the terminator, i.e. the `)`
/// token that ends the directory node.
///
public partial class DirEntry : KaitaiStruct
{
public static DirEntry FromFile(string fileName)
{
return new DirEntry(new KaitaiStream(fileName));
}
public DirEntry(KaitaiStream p__io, NixNar.TypeDirectory p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
f_isTerminator = false;
_read();
}
private void _read()
{
_kind = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _kind;
if (!( ((M_.Body == "entry") || (M_.Body == ")")) ))
{
throw new ValidationExprError(_kind, m_io, "/types/type_directory/types/dir_entry/seq/0");
}
}
if (!(IsTerminator)) {
_body = new DirEntryBody(m_io, this, m_root);
}
}
private bool f_isTerminator;
private bool _isTerminator;
public bool IsTerminator
{
get
{
if (f_isTerminator)
return _isTerminator;
f_isTerminator = true;
_isTerminator = (bool) (Kind.Body == ")");
return _isTerminator;
}
}
private PaddedStr _kind;
private DirEntryBody _body;
private NixNar m_root;
private NixNar.TypeDirectory m_parent;
///
/// Must be `entry` (for a child node) or `)` (for the terminator).
///
public PaddedStr Kind { get { return _kind; } }
///
/// The entry itself. The terminator has no `body`.
///
public DirEntryBody Body { get { return _body; } }
public NixNar M_Root { get { return m_root; } }
public NixNar.TypeDirectory M_Parent { get { return m_parent; } }
}
public partial class DirEntryBody : KaitaiStruct
{
public static DirEntryBody FromFile(string fileName)
{
return new DirEntryBody(new KaitaiStream(fileName));
}
public DirEntryBody(KaitaiStream p__io, NixNar.TypeDirectory.DirEntry p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_read();
}
private void _read()
{
_openParen = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _openParen;
if (!(M_.Body == "("))
{
throw new ValidationExprError(_openParen, m_io, "/types/type_directory/types/dir_entry_body/seq/0");
}
}
_nameKey = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _nameKey;
if (!(M_.Body == "name"))
{
throw new ValidationExprError(_nameKey, m_io, "/types/type_directory/types/dir_entry_body/seq/1");
}
}
_name = new PaddedBytes(m_io, this, m_root);
{
PaddedBytes M_ = _name;
if (!( ((M_.Body.Length != 0) && (M_.Body.Length <= 255) && ((KaitaiStream.ByteArrayCompare(M_.Body, new byte[] { 46 }) != 0)) && ((KaitaiStream.ByteArrayCompare(M_.Body, new byte[] { 46, 46 }) != 0)) && (M_.Body.Min() != 0)) ))
{
throw new ValidationExprError(_name, m_io, "/types/type_directory/types/dir_entry_body/seq/2");
}
}
_nodeKey = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _nodeKey;
if (!(M_.Body == "node"))
{
throw new ValidationExprError(_nodeKey, m_io, "/types/type_directory/types/dir_entry_body/seq/3");
}
}
_node = new Node(M_Parent.M_Parent.M_Parent.Depth + 1, m_io, this, m_root);
_closeParen = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _closeParen;
if (!(M_.Body == ")"))
{
throw new ValidationExprError(_closeParen, m_io, "/types/type_directory/types/dir_entry_body/seq/5");
}
}
}
private PaddedStr _openParen;
private PaddedStr _nameKey;
private PaddedBytes _name;
private PaddedStr _nodeKey;
private Node _node;
private PaddedStr _closeParen;
private NixNar m_root;
private NixNar.TypeDirectory.DirEntry m_parent;
public PaddedStr OpenParen { get { return _openParen; } }
public PaddedStr NameKey { get { return _nameKey; } }
///
/// Name of the entry, as raw bytes. Nix 2.36.0
/// [doesn't decode names as text](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L245-L248)
/// either, and real NARs contain names that aren't valid UTF-8.
///
/// The name must be 1 to 255 bytes long, must not be `.` or `..`, and
/// must not contain a null byte. Nix also rejects names that contain
/// `/`, but this Kaitai Struct implementation doesn't check that: as
/// of KS 0.11, expressions can't search a byte array for a given
/// byte. A null byte can be detected anyway: 0 is the smallest
/// possible byte value, so a name contains a null byte exactly if its
/// smallest byte is 0.
///
public PaddedBytes Name { get { return _name; } }
public PaddedStr NodeKey { get { return _nodeKey; } }
///
/// The child node.
///
public Node Node { get { return _node; } }
public PaddedStr CloseParen { get { return _closeParen; } }
public NixNar M_Root { get { return m_root; } }
public NixNar.TypeDirectory.DirEntry M_Parent { get { return m_parent; } }
}
private List _entries;
private NixNar m_root;
private NixNar.Node m_parent;
public List Entries { get { return _entries; } }
public NixNar M_Root { get { return m_root; } }
public NixNar.Node M_Parent { get { return m_parent; } }
}
///
/// A regular file node.
///
public partial class TypeRegular : KaitaiStruct
{
public static TypeRegular FromFile(string fileName)
{
return new TypeRegular(new KaitaiStream(fileName));
}
public TypeRegular(KaitaiStream p__io, NixNar.Node p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
f_isExecutable = false;
_read();
}
private void _read()
{
_firstKey = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _firstKey;
if (!( ((M_.Body == "executable") || (M_.Body == "contents")) ))
{
throw new ValidationExprError(_firstKey, m_io, "/types/type_regular/seq/0");
}
}
if (IsExecutable) {
_executableVal = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _executableVal;
if (!(M_.Body == ""))
{
throw new ValidationExprError(_executableVal, m_io, "/types/type_regular/seq/1");
}
}
}
if (IsExecutable) {
_contentsKey = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _contentsKey;
if (!(M_.Body == "contents"))
{
throw new ValidationExprError(_contentsKey, m_io, "/types/type_regular/seq/2");
}
}
}
_fileData = new FileContent(m_io, this, m_root);
}
///
/// The contents of the file, prefixed with their length and padded
/// with null bytes to the next 8-byte boundary.
///
public partial class FileContent : KaitaiStruct
{
public static FileContent FromFile(string fileName)
{
return new FileContent(new KaitaiStream(fileName));
}
public FileContent(KaitaiStream p__io, NixNar.TypeRegular p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
f_narOffset = false;
_read();
}
private void _read()
{
_lenContents = m_io.ReadU8le();
if (NarOffset < 0) {
__unnamed1 = m_io.ReadBytes(0);
}
_contents = m_io.ReadBytes(LenContents);
_padding = m_io.ReadBytes(KaitaiStream.Mod(8 - KaitaiStream.Mod(LenContents, 8), 8));
{
byte[] M_ = _padding;
if (!((M_.Length != 0 ? M_.Max() == 0 : true)))
{
throw new ValidationExprError(_padding, m_io, "/types/type_regular/types/file_content/seq/3");
}
}
}
private bool f_narOffset;
private int _narOffset;
///
/// Offset of `contents` in the stream. It corresponds to
/// [`narOffset`](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/include/nix/util/source-accessor.hh#L134-L138)
/// in Nix.
///
public int NarOffset
{
get
{
if (f_narOffset)
return _narOffset;
f_narOffset = true;
_narOffset = (int) (M_Io.Pos);
return _narOffset;
}
}
private ulong _lenContents;
private byte[] __unnamed1;
private byte[] _contents;
private byte[] _padding;
private NixNar m_root;
private NixNar.TypeRegular m_parent;
public ulong LenContents { get { return _lenContents; } }
public byte[] Unnamed_1 { get { return __unnamed1; } }
///
/// This Kaitai Struct implementation keeps the contents of every
/// file in memory, so parsing a NAR needs at least as much memory
/// as all its files together.
///
public byte[] Contents { get { return _contents; } }
public byte[] Padding { get { return _padding; } }
public NixNar M_Root { get { return m_root; } }
public NixNar.TypeRegular M_Parent { get { return m_parent; } }
}
private bool f_isExecutable;
private bool _isExecutable;
///
/// True if the file has the `executable` attribute.
///
public bool IsExecutable
{
get
{
if (f_isExecutable)
return _isExecutable;
f_isExecutable = true;
_isExecutable = (bool) (FirstKey.Body == "executable");
return _isExecutable;
}
}
private PaddedStr _firstKey;
private PaddedStr _executableVal;
private PaddedStr _contentsKey;
private FileContent _fileData;
private NixNar m_root;
private NixNar.Node m_parent;
///
/// Either `executable` or `contents`.
///
public PaddedStr FirstKey { get { return _firstKey; } }
///
/// Must be empty.
///
public PaddedStr ExecutableVal { get { return _executableVal; } }
public PaddedStr ContentsKey { get { return _contentsKey; } }
public FileContent FileData { get { return _fileData; } }
public NixNar M_Root { get { return m_root; } }
public NixNar.Node M_Parent { get { return m_parent; } }
}
///
/// A symbolic link node.
///
public partial class TypeSymlink : KaitaiStruct
{
public static TypeSymlink FromFile(string fileName)
{
return new TypeSymlink(new KaitaiStream(fileName));
}
public TypeSymlink(KaitaiStream p__io, NixNar.Node p__parent = null, NixNar p__root = null) : base(p__io)
{
m_parent = p__parent;
m_root = p__root;
_read();
}
private void _read()
{
_targetKey = new PaddedStr(m_io, this, m_root);
{
PaddedStr M_ = _targetKey;
if (!(M_.Body == "target"))
{
throw new ValidationExprError(_targetKey, m_io, "/types/type_symlink/seq/0");
}
}
_targetVal = new PaddedBytes(m_io, this, m_root);
{
PaddedBytes M_ = _targetVal;
if (!( ((M_.Body.Length != 0) && (M_.Body.Length <= 4095) && (M_.Body.Min() != 0)) ))
{
throw new ValidationExprError(_targetVal, m_io, "/types/type_symlink/seq/1");
}
}
}
private PaddedStr _targetKey;
private PaddedBytes _targetVal;
private NixNar m_root;
private NixNar.Node m_parent;
///
/// Must be `target`.
///
public PaddedStr TargetKey { get { return _targetKey; } }
///
/// The destination path of the symlink, as raw bytes. Nix 2.36.0
/// [doesn't decode targets as text](https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/src/libutil/archive.cc#L280-L282)
/// either.
///
/// The target must be 1 to 4095 bytes long and must not contain a null
/// byte.
///
public PaddedBytes TargetVal { get { return _targetVal; } }
public NixNar M_Root { get { return m_root; } }
public NixNar.Node M_Parent { get { return m_parent; } }
}
private PaddedStr _magic;
private Node _rootNode;
private NixNar m_root;
private KaitaiStruct m_parent;
///
/// Magic string, must be `nix-archive-1`.
///
public PaddedStr Magic { get { return _magic; } }
///
/// The root of the archive, which is always a single node.
///
public Node RootNode { get { return _rootNode; } }
public NixNar M_Root { get { return m_root; } }
public KaitaiStruct M_Parent { get { return m_parent; } }
}
}