fallout2_dat: C++11/STL parsing library

Application

Fallout 2

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of fallout2_dat using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++11/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.fallout2_dat", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    fallout2_dat_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.footer() // => get footer

C++11/STL source code to parse fallout2_dat

fallout2_dat.h

#pragma once

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

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

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

class fallout2_dat_t : public kaitai::kstruct {

public:
    class pstr_t;
    class footer_t;
    class index_t;
    class file_t;

    enum compression_t {
        COMPRESSION_NONE = 0,
        COMPRESSION_ZLIB = 1
    };

    fallout2_dat_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, fallout2_dat_t* p__root = nullptr);

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

public:
    ~fallout2_dat_t();

    class pstr_t : public kaitai::kstruct {

    public:

        pstr_t(kaitai::kstream* p__io, fallout2_dat_t::file_t* p__parent = nullptr, fallout2_dat_t* p__root = nullptr);

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

    public:
        ~pstr_t();

    private:
        uint32_t m_size;
        std::string m_str;
        fallout2_dat_t* m__root;
        fallout2_dat_t::file_t* m__parent;

    public:
        uint32_t size() const { return m_size; }
        std::string str() const { return m_str; }
        fallout2_dat_t* _root() const { return m__root; }
        fallout2_dat_t::file_t* _parent() const { return m__parent; }
    };

    class footer_t : public kaitai::kstruct {

    public:

        footer_t(kaitai::kstream* p__io, fallout2_dat_t* p__parent = nullptr, fallout2_dat_t* p__root = nullptr);

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

    public:
        ~footer_t();

    private:
        uint32_t m_index_size;
        uint32_t m_file_size;
        fallout2_dat_t* m__root;
        fallout2_dat_t* m__parent;

    public:
        uint32_t index_size() const { return m_index_size; }
        uint32_t file_size() const { return m_file_size; }
        fallout2_dat_t* _root() const { return m__root; }
        fallout2_dat_t* _parent() const { return m__parent; }
    };

    class index_t : public kaitai::kstruct {

    public:

        index_t(kaitai::kstream* p__io, fallout2_dat_t* p__parent = nullptr, fallout2_dat_t* p__root = nullptr);

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

    public:
        ~index_t();

    private:
        uint32_t m_file_count;
        std::unique_ptr<std::vector<std::unique_ptr<file_t>>> m_files;
        fallout2_dat_t* m__root;
        fallout2_dat_t* m__parent;

    public:
        uint32_t file_count() const { return m_file_count; }
        std::vector<std::unique_ptr<file_t>>* files() const { return m_files.get(); }
        fallout2_dat_t* _root() const { return m__root; }
        fallout2_dat_t* _parent() const { return m__parent; }
    };

    class file_t : public kaitai::kstruct {

    public:

        file_t(kaitai::kstream* p__io, fallout2_dat_t::index_t* p__parent = nullptr, fallout2_dat_t* p__root = nullptr);

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

    public:
        ~file_t();

    private:
        bool f_contents_raw;
        std::string m_contents_raw;
        bool n_contents_raw;

    public:
        bool _is_null_contents_raw() { contents_raw(); return n_contents_raw; };

    private:

    public:
        std::string contents_raw();

    private:
        bool f_contents_zlib;
        std::string m_contents_zlib;
        bool n_contents_zlib;

    public:
        bool _is_null_contents_zlib() { contents_zlib(); return n_contents_zlib; };

    private:

    public:
        std::string contents_zlib();

    private:
        bool f_contents;
        std::string m_contents;
        bool n_contents;

    public:
        bool _is_null_contents() { contents(); return n_contents; };

    private:

    public:
        std::string contents();

    private:
        std::unique_ptr<pstr_t> m_name;
        compression_t m_flags;
        uint32_t m_size_unpacked;
        uint32_t m_size_packed;
        uint32_t m_offset;
        fallout2_dat_t* m__root;
        fallout2_dat_t::index_t* m__parent;
        std::string m__raw_contents_zlib;
        bool n__raw_contents_zlib;

    public:
        bool _is_null__raw_contents_zlib() { _raw_contents_zlib(); return n__raw_contents_zlib; };

    private:
        std::unique_ptr<kaitai::kstream> m__io_contents_zlib;

    public:
        pstr_t* name() const { return m_name.get(); }
        compression_t flags() const { return m_flags; }
        uint32_t size_unpacked() const { return m_size_unpacked; }
        uint32_t size_packed() const { return m_size_packed; }
        uint32_t offset() const { return m_offset; }
        fallout2_dat_t* _root() const { return m__root; }
        fallout2_dat_t::index_t* _parent() const { return m__parent; }
        std::string _raw_contents_zlib() const { return m__raw_contents_zlib; }
        kaitai::kstream* _io_contents_zlib() const { return m__io_contents_zlib.get(); }
    };

private:
    bool f_footer;
    std::unique_ptr<footer_t> m_footer;

public:
    footer_t* footer();

private:
    bool f_index;
    std::unique_ptr<index_t> m_index;

public:
    index_t* index();

private:
    fallout2_dat_t* m__root;
    kaitai::kstruct* m__parent;

public:
    fallout2_dat_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

fallout2_dat.cpp

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

#include "fallout2_dat.h"

fallout2_dat_t::fallout2_dat_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, fallout2_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_footer = nullptr;
    m_index = nullptr;
    f_footer = false;
    f_index = false;
    _read();
}

void fallout2_dat_t::_read() {
}

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

void fallout2_dat_t::_clean_up() {
    if (f_footer) {
    }
    if (f_index) {
    }
}

fallout2_dat_t::pstr_t::pstr_t(kaitai::kstream* p__io, fallout2_dat_t::file_t* p__parent, fallout2_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void fallout2_dat_t::pstr_t::_read() {
    m_size = m__io->read_u4le();
    m_str = kaitai::kstream::bytes_to_str(m__io->read_bytes(size()), std::string("ASCII"));
}

fallout2_dat_t::pstr_t::~pstr_t() {
    _clean_up();
}

void fallout2_dat_t::pstr_t::_clean_up() {
}

fallout2_dat_t::footer_t::footer_t(kaitai::kstream* p__io, fallout2_dat_t* p__parent, fallout2_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void fallout2_dat_t::footer_t::_read() {
    m_index_size = m__io->read_u4le();
    m_file_size = m__io->read_u4le();
}

fallout2_dat_t::footer_t::~footer_t() {
    _clean_up();
}

void fallout2_dat_t::footer_t::_clean_up() {
}

fallout2_dat_t::index_t::index_t(kaitai::kstream* p__io, fallout2_dat_t* p__parent, fallout2_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_files = nullptr;
    _read();
}

void fallout2_dat_t::index_t::_read() {
    m_file_count = m__io->read_u4le();
    int l_files = file_count();
    m_files = std::unique_ptr<std::vector<std::unique_ptr<file_t>>>(new std::vector<std::unique_ptr<file_t>>());
    m_files->reserve(l_files);
    for (int i = 0; i < l_files; i++) {
        m_files->push_back(std::move(std::unique_ptr<file_t>(new file_t(m__io, this, m__root))));
    }
}

fallout2_dat_t::index_t::~index_t() {
    _clean_up();
}

void fallout2_dat_t::index_t::_clean_up() {
}

fallout2_dat_t::file_t::file_t(kaitai::kstream* p__io, fallout2_dat_t::index_t* p__parent, fallout2_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_name = nullptr;
    m__io_contents_zlib = nullptr;
    f_contents_raw = false;
    f_contents_zlib = false;
    f_contents = false;
    _read();
}

void fallout2_dat_t::file_t::_read() {
    m_name = std::unique_ptr<pstr_t>(new pstr_t(m__io, this, m__root));
    m_flags = static_cast<fallout2_dat_t::compression_t>(m__io->read_u1());
    m_size_unpacked = m__io->read_u4le();
    m_size_packed = m__io->read_u4le();
    m_offset = m__io->read_u4le();
}

fallout2_dat_t::file_t::~file_t() {
    _clean_up();
}

void fallout2_dat_t::file_t::_clean_up() {
    if (f_contents_raw && !n_contents_raw) {
    }
    if (f_contents_zlib && !n_contents_zlib) {
    }
}

std::string fallout2_dat_t::file_t::contents_raw() {
    if (f_contents_raw)
        return m_contents_raw;
    n_contents_raw = true;
    if (flags() == fallout2_dat_t::COMPRESSION_NONE) {
        n_contents_raw = false;
        kaitai::kstream *io = _root()->_io();
        std::streampos _pos = io->pos();
        io->seek(offset());
        m_contents_raw = io->read_bytes(size_unpacked());
        io->seek(_pos);
        f_contents_raw = true;
    }
    return m_contents_raw;
}

std::string fallout2_dat_t::file_t::contents_zlib() {
    if (f_contents_zlib)
        return m_contents_zlib;
    n_contents_zlib = true;
    if (flags() == fallout2_dat_t::COMPRESSION_ZLIB) {
        n_contents_zlib = false;
        kaitai::kstream *io = _root()->_io();
        std::streampos _pos = io->pos();
        io->seek(offset());
        m__raw_contents_zlib = io->read_bytes(size_packed());
        m_contents_zlib = kaitai::kstream::process_zlib(m__raw_contents_zlib);
        io->seek(_pos);
        f_contents_zlib = true;
    }
    return m_contents_zlib;
}

std::string fallout2_dat_t::file_t::contents() {
    if (f_contents)
        return m_contents;
    n_contents = true;
    if ( ((flags() == fallout2_dat_t::COMPRESSION_ZLIB) || (flags() == fallout2_dat_t::COMPRESSION_NONE)) ) {
        n_contents = false;
        m_contents = ((flags() == fallout2_dat_t::COMPRESSION_ZLIB) ? (contents_zlib()) : (contents_raw()));
    }
    f_contents = true;
    return m_contents;
}

fallout2_dat_t::footer_t* fallout2_dat_t::footer() {
    if (f_footer)
        return m_footer.get();
    std::streampos _pos = m__io->pos();
    m__io->seek((_io()->size() - 8));
    m_footer = std::unique_ptr<footer_t>(new footer_t(m__io, this, m__root));
    m__io->seek(_pos);
    f_footer = true;
    return m_footer.get();
}

fallout2_dat_t::index_t* fallout2_dat_t::index() {
    if (f_index)
        return m_index.get();
    std::streampos _pos = m__io->pos();
    m__io->seek(((_io()->size() - 8) - footer()->index_size()));
    m_index = std::unique_ptr<index_t>(new index_t(m__io, this, m__root));
    m__io->seek(_pos);
    f_index = true;
    return m_index.get();
}