fallout2_dat: C++/STL parsing library

Application

Fallout2

KS implementation details

License: CC0-1.0

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

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 a stream for reading from a local file:
      #include <fstream>
      
      std::ifstream is("path/to/local/file.fallout2_dat", std::ifstream::binary);
    • Or one can prepare a stream for reading from existing std::string str:
      #include <sstream>
      
      std::istringstream is(str);
    • Or one can parse arbitrary char* buffer in memory, given that we know its size:
      #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++/STL source code to parse fallout2_dat

fallout2_dat.h

#ifndef FALLOUT2_DAT_H_
#define FALLOUT2_DAT_H_

// 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 <vector>

#if KAITAI_STRUCT_VERSION < 7000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.7 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 = 0, fallout2_dat_t* p__root = 0);

private:
    void _read();

public:
    ~fallout2_dat_t();

    class pstr_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    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 = 0, fallout2_dat_t* p__root = 0);

    private:
        void _read();

    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 = 0, fallout2_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~index_t();

    private:
        uint32_t m_file_count;
        std::vector<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<file_t*>* files() const { return m_files; }
        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 = 0, fallout2_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~file_t();

    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:
        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;

    public:
        pstr_t* name() const { return m_name; }
        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() const { return m__raw_contents; }
    };

private:
    bool f_footer;
    footer_t* m_footer;

public:
    footer_t* footer();

private:
    bool f_index;
    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; }
};

#endif  // FALLOUT2_DAT_H_

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;
    f_footer = false;
    f_index = false;
    _read();
}

void fallout2_dat_t::_read() {
}

fallout2_dat_t::~fallout2_dat_t() {
    if (f_footer) {
        delete m_footer;
    }
    if (f_index) {
        delete m_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() {
}

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() {
}

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;
    _read();
}

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

fallout2_dat_t::index_t::~index_t() {
    for (std::vector<file_t*>::iterator it = m_files->begin(); it != m_files->end(); ++it) {
        delete *it;
    }
    delete m_files;
}

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;
    f_contents = false;
    _read();
}

void fallout2_dat_t::file_t::_read() {
    m_name = 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() {
    delete m_name;
    if (f_contents && !n_contents) {
    }
}

std::string fallout2_dat_t::file_t::contents() {
    if (f_contents)
        return m_contents;
    n_contents = true;
    if (flags() == COMPRESSION_ZLIB) {
        n_contents = false;
        kaitai::kstream *io = _root()->_io();
        std::streampos _pos = io->pos();
        io->seek(offset());
        m__raw_contents = io->read_bytes(size_packed());
        m_contents = kaitai::kstream::process_zlib(m__raw_contents);
        io->seek(_pos);
    }
    f_contents = true;
    return m_contents;
}

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

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