#ifndef NIX_NAR_H_
#define NIX_NAR_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

class nix_nar_t;

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 11000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.11 or later is required"
#endif

/**
 * 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.
 * \sa https://github.com/NixOS/nix/blob/88907193c54fcd5184cf59d14b693188e2875168/doc/manual/source/protocols/nix-archive/index.md Git tag "2.36.0"
 */

class nix_nar_t : public kaitai::kstruct {

public:
    class node_t;
    class padded_bytes_t;
    class padded_str_t;
    class type_directory_t;
    class type_regular_t;
    class type_symlink_t;

    nix_nar_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, nix_nar_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~nix_nar_t();

    /**
     * A single file system node (regular file, directory, or symlink).
     */

    class node_t : public kaitai::kstruct {

    public:

        node_t(int32_t p_depth, kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, nix_nar_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~node_t();

    private:
        std::string m__unnamed0;
        padded_str_t* m_open_paren;
        padded_str_t* m_type_key;
        padded_str_t* m_type_val;
        kaitai::kstruct* m_body;
        bool n_body;

    public:
        bool _is_null_body() { body(); return n_body; };

    private:
        padded_str_t* m_close_paren;
        bool n_close_paren;

    public:
        bool _is_null_close_paren() { close_paren(); return n_close_paren; };

    private:
        int32_t m_depth;
        nix_nar_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        std::string _unnamed0() const { return m__unnamed0; }

        /**
         * Must be `(`, a token starting the node definition.
         */
        padded_str_t* open_paren() const { return m_open_paren; }

        /**
         * Must be `type`.
         */
        padded_str_t* type_key() const { return m_type_key; }

        /**
         * Type of the node (`regular`, `directory`, or `symlink`).
         */
        padded_str_t* type_val() const { return m_type_val; }
        kaitai::kstruct* body() const { return m_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`.
         */
        padded_str_t* close_paren() const { return m_close_paren; }

        /**
         * Nesting depth of the node. The root node has depth 0.
         */
        int32_t depth() const { return m_depth; }
        nix_nar_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { 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.
     */

    class padded_bytes_t : public kaitai::kstruct {

    public:

        padded_bytes_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, nix_nar_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~padded_bytes_t();

    private:
        uint64_t m_len_body;
        std::string m_body;
        std::string m_padding;
        nix_nar_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        uint64_t len_body() const { return m_len_body; }
        std::string body() const { return m_body; }
        std::string padding() const { return m_padding; }
        nix_nar_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { 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.
     */

    class padded_str_t : public kaitai::kstruct {

    public:

        padded_str_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, nix_nar_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~padded_str_t();

    private:
        uint64_t m_len_body;
        std::string m_body;
        std::string m_padding;
        nix_nar_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        uint64_t len_body() const { return m_len_body; }
        std::string body() const { return m_body; }
        std::string padding() const { return m_padding; }
        nix_nar_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { 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.
     */

    class type_directory_t : public kaitai::kstruct {

    public:
        class dir_entry_t;
        class dir_entry_body_t;

        type_directory_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent = 0, nix_nar_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~type_directory_t();

        /**
         * A single entry within a directory, or the terminator, i.e. the `)`
         * token that ends the directory node.
         */

        class dir_entry_t : public kaitai::kstruct {

        public:

            dir_entry_t(kaitai::kstream* p__io, nix_nar_t::type_directory_t* p__parent = 0, nix_nar_t* p__root = 0);

        private:
            void _read();
            void _clean_up();

        public:
            ~dir_entry_t();

        private:
            bool f_is_terminator;
            bool m_is_terminator;

        public:
            bool is_terminator();

        private:
            padded_str_t* m_kind;
            dir_entry_body_t* m_body;
            bool n_body;

        public:
            bool _is_null_body() { body(); return n_body; };

        private:
            nix_nar_t* m__root;
            nix_nar_t::type_directory_t* m__parent;

        public:

            /**
             * Must be `entry` (for a child node) or `)` (for the terminator).
             */
            padded_str_t* kind() const { return m_kind; }

            /**
             * The entry itself. The terminator has no `body`.
             */
            dir_entry_body_t* body() const { return m_body; }
            nix_nar_t* _root() const { return m__root; }
            nix_nar_t::type_directory_t* _parent() const { return m__parent; }
        };

        class dir_entry_body_t : public kaitai::kstruct {

        public:

            dir_entry_body_t(kaitai::kstream* p__io, nix_nar_t::type_directory_t::dir_entry_t* p__parent = 0, nix_nar_t* p__root = 0);

        private:
            void _read();
            void _clean_up();

        public:
            ~dir_entry_body_t();

        private:
            padded_str_t* m_open_paren;
            padded_str_t* m_name_key;
            padded_bytes_t* m_name;
            padded_str_t* m_node_key;
            node_t* m_node;
            padded_str_t* m_close_paren;
            nix_nar_t* m__root;
            nix_nar_t::type_directory_t::dir_entry_t* m__parent;

        public:
            padded_str_t* open_paren() const { return m_open_paren; }
            padded_str_t* name_key() const { return m_name_key; }

            /**
             * 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.
             */
            padded_bytes_t* name() const { return m_name; }
            padded_str_t* node_key() const { return m_node_key; }

            /**
             * The child node.
             */
            node_t* node() const { return m_node; }
            padded_str_t* close_paren() const { return m_close_paren; }
            nix_nar_t* _root() const { return m__root; }
            nix_nar_t::type_directory_t::dir_entry_t* _parent() const { return m__parent; }
        };

    private:
        std::vector<dir_entry_t*>* m_entries;
        nix_nar_t* m__root;
        nix_nar_t::node_t* m__parent;

    public:
        std::vector<dir_entry_t*>* entries() const { return m_entries; }
        nix_nar_t* _root() const { return m__root; }
        nix_nar_t::node_t* _parent() const { return m__parent; }
    };

    /**
     * A regular file node.
     */

    class type_regular_t : public kaitai::kstruct {

    public:
        class file_content_t;

        type_regular_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent = 0, nix_nar_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~type_regular_t();

        /**
         * The contents of the file, prefixed with their length and padded
         * with null bytes to the next 8-byte boundary.
         */

        class file_content_t : public kaitai::kstruct {

        public:

            file_content_t(kaitai::kstream* p__io, nix_nar_t::type_regular_t* p__parent = 0, nix_nar_t* p__root = 0);

        private:
            void _read();
            void _clean_up();

        public:
            ~file_content_t();

        private:
            bool f_nar_offset;
            int32_t m_nar_offset;

        public:

            /**
             * 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.
             */
            int32_t nar_offset();

        private:
            uint64_t m_len_contents;
            std::string m__unnamed1;
            bool n__unnamed1;

        public:
            bool _is_null__unnamed1() { _unnamed1(); return n__unnamed1; };

        private:
            std::string m_contents;
            std::string m_padding;
            nix_nar_t* m__root;
            nix_nar_t::type_regular_t* m__parent;

        public:
            uint64_t len_contents() const { return m_len_contents; }
            std::string _unnamed1() const { return m__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.
             */
            std::string contents() const { return m_contents; }
            std::string padding() const { return m_padding; }
            nix_nar_t* _root() const { return m__root; }
            nix_nar_t::type_regular_t* _parent() const { return m__parent; }
        };

    private:
        bool f_is_executable;
        bool m_is_executable;

    public:

        /**
         * True if the file has the `executable` attribute.
         */
        bool is_executable();

    private:
        padded_str_t* m_first_key;
        padded_str_t* m_executable_val;
        bool n_executable_val;

    public:
        bool _is_null_executable_val() { executable_val(); return n_executable_val; };

    private:
        padded_str_t* m_contents_key;
        bool n_contents_key;

    public:
        bool _is_null_contents_key() { contents_key(); return n_contents_key; };

    private:
        file_content_t* m_file_data;
        nix_nar_t* m__root;
        nix_nar_t::node_t* m__parent;

    public:

        /**
         * Either `executable` or `contents`.
         */
        padded_str_t* first_key() const { return m_first_key; }

        /**
         * Must be empty.
         */
        padded_str_t* executable_val() const { return m_executable_val; }
        padded_str_t* contents_key() const { return m_contents_key; }
        file_content_t* file_data() const { return m_file_data; }
        nix_nar_t* _root() const { return m__root; }
        nix_nar_t::node_t* _parent() const { return m__parent; }
    };

    /**
     * A symbolic link node.
     */

    class type_symlink_t : public kaitai::kstruct {

    public:

        type_symlink_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent = 0, nix_nar_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~type_symlink_t();

    private:
        padded_str_t* m_target_key;
        padded_bytes_t* m_target_val;
        nix_nar_t* m__root;
        nix_nar_t::node_t* m__parent;

    public:

        /**
         * Must be `target`.
         */
        padded_str_t* target_key() const { return m_target_key; }

        /**
         * 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.
         */
        padded_bytes_t* target_val() const { return m_target_val; }
        nix_nar_t* _root() const { return m__root; }
        nix_nar_t::node_t* _parent() const { return m__parent; }
    };

private:
    padded_str_t* m_magic;
    node_t* m_root_node;
    nix_nar_t* m__root;
    kaitai::kstruct* m__parent;

public:

    /**
     * Magic string, must be `nix-archive-1`.
     */
    padded_str_t* magic() const { return m_magic; }

    /**
     * The root of the archive, which is always a single node.
     */
    node_t* root_node() const { return m_root_node; }
    nix_nar_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // NIX_NAR_H_
