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

#include "nix_nar.h"
#include "kaitai/exceptions.h"

nix_nar_t::nix_nar_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root ? p__root : this;
    m_magic = 0;
    m_root_node = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::_read() {
    m_magic = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_magic;
        if (!(_->body() == std::string("nix-archive-1"))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_magic, m__io, std::string("/seq/0"));
        }
    }
    m_root_node = new node_t(0, m__io, this, m__root);
}

nix_nar_t::~nix_nar_t() {
    _clean_up();
}

void nix_nar_t::_clean_up() {
    if (m_magic) {
        delete m_magic; m_magic = 0;
    }
    if (m_root_node) {
        delete m_root_node; m_root_node = 0;
    }
}

nix_nar_t::node_t::node_t(int32_t p_depth, kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_depth = p_depth;
    m_open_paren = 0;
    m_type_key = 0;
    m_type_val = 0;
    m_close_paren = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::node_t::_read() {
    m__unnamed0 = m__io->read_bytes(0);
    {
        std::string _ = m__unnamed0;
        if (!(depth() < 64)) {
            throw kaitai::validation_expr_error<std::string>(m__unnamed0, m__io, std::string("/types/node/seq/0"));
        }
    }
    m_open_paren = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_open_paren;
        if (!(_->body() == std::string("("))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_open_paren, m__io, std::string("/types/node/seq/1"));
        }
    }
    m_type_key = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_type_key;
        if (!(_->body() == std::string("type"))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_type_key, m__io, std::string("/types/node/seq/2"));
        }
    }
    m_type_val = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_type_val;
        if (!( ((_->body() == std::string("regular")) || (_->body() == std::string("directory")) || (_->body() == std::string("symlink"))) )) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_type_val, m__io, std::string("/types/node/seq/3"));
        }
    }
    n_body = true;
    {
        std::string on = type_val()->body();
        if (on == std::string("directory")) {
            n_body = false;
            m_body = new type_directory_t(m__io, this, m__root);
        }
        else if (on == std::string("regular")) {
            n_body = false;
            m_body = new type_regular_t(m__io, this, m__root);
        }
        else if (on == std::string("symlink")) {
            n_body = false;
            m_body = new type_symlink_t(m__io, this, m__root);
        }
    }
    n_close_paren = true;
    if (type_val()->body() != std::string("directory")) {
        n_close_paren = false;
        m_close_paren = new padded_str_t(m__io, this, m__root);
        {
            padded_str_t* _ = m_close_paren;
            if (!(_->body() == std::string(")"))) {
                throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_close_paren, m__io, std::string("/types/node/seq/5"));
            }
        }
    }
}

nix_nar_t::node_t::~node_t() {
    _clean_up();
}

void nix_nar_t::node_t::_clean_up() {
    if (m_open_paren) {
        delete m_open_paren; m_open_paren = 0;
    }
    if (m_type_key) {
        delete m_type_key; m_type_key = 0;
    }
    if (m_type_val) {
        delete m_type_val; m_type_val = 0;
    }
    if (!n_body) {
        if (m_body) {
            delete m_body; m_body = 0;
        }
    }
    if (!n_close_paren) {
        if (m_close_paren) {
            delete m_close_paren; m_close_paren = 0;
        }
    }
}

nix_nar_t::padded_bytes_t::padded_bytes_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::padded_bytes_t::_read() {
    m_len_body = m__io->read_u8le();
    m_body = m__io->read_bytes(len_body());
    m_padding = m__io->read_bytes(kaitai::kstream::mod(8 - kaitai::kstream::mod(len_body(), 8), 8));
    {
        std::string _ = m_padding;
        if (!(((_.length() != 0) ? (kaitai::kstream::byte_array_max(_) == 0) : (true)))) {
            throw kaitai::validation_expr_error<std::string>(m_padding, m__io, std::string("/types/padded_bytes/seq/2"));
        }
    }
}

nix_nar_t::padded_bytes_t::~padded_bytes_t() {
    _clean_up();
}

void nix_nar_t::padded_bytes_t::_clean_up() {
}

nix_nar_t::padded_str_t::padded_str_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::padded_str_t::_read() {
    m_len_body = m__io->read_u8le();
    m_body = kaitai::kstream::bytes_to_str(m__io->read_bytes(len_body()), "ASCII");
    m_padding = m__io->read_bytes(kaitai::kstream::mod(8 - kaitai::kstream::mod(len_body(), 8), 8));
    {
        std::string _ = m_padding;
        if (!(((_.length() != 0) ? (kaitai::kstream::byte_array_max(_) == 0) : (true)))) {
            throw kaitai::validation_expr_error<std::string>(m_padding, m__io, std::string("/types/padded_str/seq/2"));
        }
    }
}

nix_nar_t::padded_str_t::~padded_str_t() {
    _clean_up();
}

void nix_nar_t::padded_str_t::_clean_up() {
}

nix_nar_t::type_directory_t::type_directory_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_entries = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::type_directory_t::_read() {
    m_entries = new std::vector<dir_entry_t*>();
    {
        int i = 0;
        dir_entry_t* _;
        do {
            _ = new dir_entry_t(m__io, this, m__root);
            m_entries->push_back(_);
            {
                dir_entry_t* _ = m_entries->at(i);
                if (!((( ((i != 0) && (!(_->is_terminator()))) ) ? (_->body()->name()->body() > entries()->at(i - 1)->body()->name()->body()) : (true)))) {
                    throw kaitai::validation_expr_error<nix_nar_t::type_directory_t::dir_entry_t*>(m_entries->at(i), m__io, std::string("/types/type_directory/seq/0"));
                }
            }
            i++;
        } while (!(_->is_terminator()));
    }
}

nix_nar_t::type_directory_t::~type_directory_t() {
    _clean_up();
}

void nix_nar_t::type_directory_t::_clean_up() {
    if (m_entries) {
        for (std::vector<dir_entry_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
            delete *it;
        }
        delete m_entries; m_entries = 0;
    }
}

nix_nar_t::type_directory_t::dir_entry_t::dir_entry_t(kaitai::kstream* p__io, nix_nar_t::type_directory_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_kind = 0;
    m_body = 0;
    f_is_terminator = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::type_directory_t::dir_entry_t::_read() {
    m_kind = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_kind;
        if (!( ((_->body() == std::string("entry")) || (_->body() == std::string(")"))) )) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_kind, m__io, std::string("/types/type_directory/types/dir_entry/seq/0"));
        }
    }
    n_body = true;
    if (!(is_terminator())) {
        n_body = false;
        m_body = new dir_entry_body_t(m__io, this, m__root);
    }
}

nix_nar_t::type_directory_t::dir_entry_t::~dir_entry_t() {
    _clean_up();
}

void nix_nar_t::type_directory_t::dir_entry_t::_clean_up() {
    if (m_kind) {
        delete m_kind; m_kind = 0;
    }
    if (!n_body) {
        if (m_body) {
            delete m_body; m_body = 0;
        }
    }
}

bool nix_nar_t::type_directory_t::dir_entry_t::is_terminator() {
    if (f_is_terminator)
        return m_is_terminator;
    f_is_terminator = true;
    m_is_terminator = kind()->body() == std::string(")");
    return m_is_terminator;
}

nix_nar_t::type_directory_t::dir_entry_body_t::dir_entry_body_t(kaitai::kstream* p__io, nix_nar_t::type_directory_t::dir_entry_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_open_paren = 0;
    m_name_key = 0;
    m_name = 0;
    m_node_key = 0;
    m_node = 0;
    m_close_paren = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::type_directory_t::dir_entry_body_t::_read() {
    m_open_paren = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_open_paren;
        if (!(_->body() == std::string("("))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_open_paren, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/0"));
        }
    }
    m_name_key = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_name_key;
        if (!(_->body() == std::string("name"))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_name_key, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/1"));
        }
    }
    m_name = new padded_bytes_t(m__io, this, m__root);
    {
        padded_bytes_t* _ = m_name;
        if (!( ((_->body().length() != 0) && (_->body().length() <= 255) && (_->body() != std::string("\x2E", 1)) && (_->body() != std::string("\x2E\x2E", 2)) && (kaitai::kstream::byte_array_min(_->body()) != 0)) )) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_bytes_t*>(m_name, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/2"));
        }
    }
    m_node_key = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_node_key;
        if (!(_->body() == std::string("node"))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_node_key, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/3"));
        }
    }
    m_node = new node_t(_parent()->_parent()->_parent()->depth() + 1, m__io, this, m__root);
    m_close_paren = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_close_paren;
        if (!(_->body() == std::string(")"))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_close_paren, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/5"));
        }
    }
}

nix_nar_t::type_directory_t::dir_entry_body_t::~dir_entry_body_t() {
    _clean_up();
}

void nix_nar_t::type_directory_t::dir_entry_body_t::_clean_up() {
    if (m_open_paren) {
        delete m_open_paren; m_open_paren = 0;
    }
    if (m_name_key) {
        delete m_name_key; m_name_key = 0;
    }
    if (m_name) {
        delete m_name; m_name = 0;
    }
    if (m_node_key) {
        delete m_node_key; m_node_key = 0;
    }
    if (m_node) {
        delete m_node; m_node = 0;
    }
    if (m_close_paren) {
        delete m_close_paren; m_close_paren = 0;
    }
}

nix_nar_t::type_regular_t::type_regular_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_first_key = 0;
    m_executable_val = 0;
    m_contents_key = 0;
    m_file_data = 0;
    f_is_executable = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::type_regular_t::_read() {
    m_first_key = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_first_key;
        if (!( ((_->body() == std::string("executable")) || (_->body() == std::string("contents"))) )) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_first_key, m__io, std::string("/types/type_regular/seq/0"));
        }
    }
    n_executable_val = true;
    if (is_executable()) {
        n_executable_val = false;
        m_executable_val = new padded_str_t(m__io, this, m__root);
        {
            padded_str_t* _ = m_executable_val;
            if (!(_->body() == std::string(""))) {
                throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_executable_val, m__io, std::string("/types/type_regular/seq/1"));
            }
        }
    }
    n_contents_key = true;
    if (is_executable()) {
        n_contents_key = false;
        m_contents_key = new padded_str_t(m__io, this, m__root);
        {
            padded_str_t* _ = m_contents_key;
            if (!(_->body() == std::string("contents"))) {
                throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_contents_key, m__io, std::string("/types/type_regular/seq/2"));
            }
        }
    }
    m_file_data = new file_content_t(m__io, this, m__root);
}

nix_nar_t::type_regular_t::~type_regular_t() {
    _clean_up();
}

void nix_nar_t::type_regular_t::_clean_up() {
    if (m_first_key) {
        delete m_first_key; m_first_key = 0;
    }
    if (!n_executable_val) {
        if (m_executable_val) {
            delete m_executable_val; m_executable_val = 0;
        }
    }
    if (!n_contents_key) {
        if (m_contents_key) {
            delete m_contents_key; m_contents_key = 0;
        }
    }
    if (m_file_data) {
        delete m_file_data; m_file_data = 0;
    }
}

nix_nar_t::type_regular_t::file_content_t::file_content_t(kaitai::kstream* p__io, nix_nar_t::type_regular_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_nar_offset = false;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::type_regular_t::file_content_t::_read() {
    m_len_contents = m__io->read_u8le();
    n__unnamed1 = true;
    if (nar_offset() < 0) {
        n__unnamed1 = false;
        m__unnamed1 = m__io->read_bytes(0);
    }
    m_contents = m__io->read_bytes(len_contents());
    m_padding = m__io->read_bytes(kaitai::kstream::mod(8 - kaitai::kstream::mod(len_contents(), 8), 8));
    {
        std::string _ = m_padding;
        if (!(((_.length() != 0) ? (kaitai::kstream::byte_array_max(_) == 0) : (true)))) {
            throw kaitai::validation_expr_error<std::string>(m_padding, m__io, std::string("/types/type_regular/types/file_content/seq/3"));
        }
    }
}

nix_nar_t::type_regular_t::file_content_t::~file_content_t() {
    _clean_up();
}

void nix_nar_t::type_regular_t::file_content_t::_clean_up() {
    if (!n__unnamed1) {
    }
}

int32_t nix_nar_t::type_regular_t::file_content_t::nar_offset() {
    if (f_nar_offset)
        return m_nar_offset;
    f_nar_offset = true;
    m_nar_offset = _io()->pos();
    return m_nar_offset;
}

bool nix_nar_t::type_regular_t::is_executable() {
    if (f_is_executable)
        return m_is_executable;
    f_is_executable = true;
    m_is_executable = first_key()->body() == std::string("executable");
    return m_is_executable;
}

nix_nar_t::type_symlink_t::type_symlink_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_target_key = 0;
    m_target_val = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void nix_nar_t::type_symlink_t::_read() {
    m_target_key = new padded_str_t(m__io, this, m__root);
    {
        padded_str_t* _ = m_target_key;
        if (!(_->body() == std::string("target"))) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_str_t*>(m_target_key, m__io, std::string("/types/type_symlink/seq/0"));
        }
    }
    m_target_val = new padded_bytes_t(m__io, this, m__root);
    {
        padded_bytes_t* _ = m_target_val;
        if (!( ((_->body().length() != 0) && (_->body().length() <= 4095) && (kaitai::kstream::byte_array_min(_->body()) != 0)) )) {
            throw kaitai::validation_expr_error<nix_nar_t::padded_bytes_t*>(m_target_val, m__io, std::string("/types/type_symlink/seq/1"));
        }
    }
}

nix_nar_t::type_symlink_t::~type_symlink_t() {
    _clean_up();
}

void nix_nar_t::type_symlink_t::_clean_up() {
    if (m_target_key) {
        delete m_target_key; m_target_key = 0;
    }
    if (m_target_val) {
        delete m_target_val; m_target_val = 0;
    }
}
