.dat file format of Faster Than Light (FTL): C++11/STL parsing library

Application

Faster Than Light (FTL)

File extension

dat

KS implementation details

License: CC0-1.0

This page hosts a formal specification of .dat file format of Faster Than Light (FTL) 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.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:
    ftl_dat_t data(&ks);
    

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

data.num_files() // => Number of files in the archive

C++11/STL source code to parse .dat file format of Faster Than Light (FTL)

ftl_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 ftl_dat_t : public kaitai::kstruct {

public:
    class file_t;
    class meta_t;

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

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

public:
    ~ftl_dat_t();

    class file_t : public kaitai::kstruct {

    public:

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

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

    public:
        ~file_t();

    private:
        bool f_meta;
        std::unique_ptr<meta_t> m_meta;
        bool n_meta;

    public:
        bool _is_null_meta() { meta(); return n_meta; };

    private:

    public:
        meta_t* meta();

    private:
        uint32_t m_ofs_meta;
        ftl_dat_t* m__root;
        ftl_dat_t* m__parent;

    public:
        uint32_t ofs_meta() const { return m_ofs_meta; }
        ftl_dat_t* _root() const { return m__root; }
        ftl_dat_t* _parent() const { return m__parent; }
    };

    class meta_t : public kaitai::kstruct {

    public:

        meta_t(kaitai::kstream* p__io, ftl_dat_t::file_t* p__parent = nullptr, ftl_dat_t* p__root = nullptr);

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

    public:
        ~meta_t();

    private:
        uint32_t m_len_file;
        uint32_t m_len_filename;
        std::string m_filename;
        std::string m_body;
        ftl_dat_t* m__root;
        ftl_dat_t::file_t* m__parent;

    public:
        uint32_t len_file() const { return m_len_file; }
        uint32_t len_filename() const { return m_len_filename; }
        std::string filename() const { return m_filename; }
        std::string body() const { return m_body; }
        ftl_dat_t* _root() const { return m__root; }
        ftl_dat_t::file_t* _parent() const { return m__parent; }
    };

private:
    uint32_t m_num_files;
    std::unique_ptr<std::vector<std::unique_ptr<file_t>>> m_files;
    ftl_dat_t* m__root;
    kaitai::kstruct* m__parent;

public:

    /**
     * Number of files in the archive
     */
    uint32_t num_files() const { return m_num_files; }
    std::vector<std::unique_ptr<file_t>>* files() const { return m_files.get(); }
    ftl_dat_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

ftl_dat.cpp

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

#include "ftl_dat.h"

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

void ftl_dat_t::_read() {
    m_num_files = m__io->read_u4le();
    m_files = std::unique_ptr<std::vector<std::unique_ptr<file_t>>>(new std::vector<std::unique_ptr<file_t>>());
    const int l_files = num_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))));
    }
}

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

void ftl_dat_t::_clean_up() {
}

ftl_dat_t::file_t::file_t(kaitai::kstream* p__io, ftl_dat_t* p__parent, ftl_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_meta = nullptr;
    f_meta = false;
    _read();
}

void ftl_dat_t::file_t::_read() {
    m_ofs_meta = m__io->read_u4le();
}

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

void ftl_dat_t::file_t::_clean_up() {
    if (f_meta && !n_meta) {
    }
}

ftl_dat_t::meta_t* ftl_dat_t::file_t::meta() {
    if (f_meta)
        return m_meta.get();
    n_meta = true;
    if (ofs_meta() != 0) {
        n_meta = false;
        std::streampos _pos = m__io->pos();
        m__io->seek(ofs_meta());
        m_meta = std::unique_ptr<meta_t>(new meta_t(m__io, this, m__root));
        m__io->seek(_pos);
        f_meta = true;
    }
    return m_meta.get();
}

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

void ftl_dat_t::meta_t::_read() {
    m_len_file = m__io->read_u4le();
    m_len_filename = m__io->read_u4le();
    m_filename = kaitai::kstream::bytes_to_str(m__io->read_bytes(len_filename()), std::string("UTF-8"));
    m_body = m__io->read_bytes(len_file());
}

ftl_dat_t::meta_t::~meta_t() {
    _clean_up();
}

void ftl_dat_t::meta_t::_clean_up() {
}