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

Application

Faster Than Light (FTL)

File extension

dat

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

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.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:
    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++/STL source code to parse .dat file format of Faster Than Light (FTL)

ftl_dat.h

#ifndef FTL_DAT_H_
#define FTL_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 ftl_dat_t : public kaitai::kstruct {

public:
    class file_t;
    class meta_t;

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

private:
    void _read();

public:
    ~ftl_dat_t();

    class file_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    public:
        ~file_t();

    private:
        bool f_meta;
        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_meta_ofs;
        ftl_dat_t* m__root;
        ftl_dat_t* m__parent;

    public:
        uint32_t meta_ofs() const { return m_meta_ofs; }
        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 = 0, ftl_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~meta_t();

    private:
        uint32_t m_file_size;
        uint32_t m_filename_size;
        std::string m_filename;
        std::string m_body;
        ftl_dat_t* m__root;
        ftl_dat_t::file_t* m__parent;

    public:
        uint32_t file_size() const { return m_file_size; }
        uint32_t filename_size() const { return m_filename_size; }
        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::vector<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<file_t*>* files() const { return m_files; }
    ftl_dat_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // FTL_DAT_H_

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

void ftl_dat_t::_read() {
    m_num_files = m__io->read_u4le();
    int l_files = num_files();
    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));
    }
}

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

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

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

ftl_dat_t::file_t::~file_t() {
    if (f_meta && !n_meta) {
        delete m_meta;
    }
}

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

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_file_size = m__io->read_u4le();
    m_filename_size = m__io->read_u4le();
    m_filename = kaitai::kstream::bytes_to_str(m__io->read_bytes(filename_size()), std::string("UTF-8"));
    m_body = m__io->read_bytes(file_size());
}

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