Gran Turismo File System (GTFS): C++/STL parsing library

File extension

vol

KS implementation details

License: CC0-1.0

This page hosts a formal specification of Gran Turismo File System (GTFS) 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.vol", 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:
    gran_turismo_vol_t data(&ks);

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

data.magic() // => get magic

C++/STL source code to parse Gran Turismo File System (GTFS)

gran_turismo_vol.h

#ifndef GRAN_TURISMO_VOL_H_
#define GRAN_TURISMO_VOL_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 gran_turismo_vol_t : public kaitai::kstruct {

public:
    class file_info_t;

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

private:
    void _read();

public:
    ~gran_turismo_vol_t();

    class file_info_t : public kaitai::kstruct {

    public:

        file_info_t(kaitai::kstream* p__io, gran_turismo_vol_t* p__parent = 0, gran_turismo_vol_t* p__root = 0);

    private:
        void _read();

    public:
        ~file_info_t();

    private:
        bool f_size;
        int32_t m_size;

    public:
        int32_t size();

    private:
        bool f_body;
        std::string m_body;
        bool n_body;

    public:
        bool _is_null_body() { body(); return n_body; };

    private:

    public:
        std::string body();

    private:
        bool f_is_dir;
        bool m_is_dir;

    public:
        bool is_dir();

    private:
        bool f_is_last_entry;
        bool m_is_last_entry;

    public:
        bool is_last_entry();

    private:
        uint32_t m_timestamp;
        uint16_t m_offset_idx;
        uint8_t m_flags;
        std::string m_name;
        gran_turismo_vol_t* m__root;
        gran_turismo_vol_t* m__parent;

    public:
        uint32_t timestamp() const { return m_timestamp; }
        uint16_t offset_idx() const { return m_offset_idx; }
        uint8_t flags() const { return m_flags; }
        std::string name() const { return m_name; }
        gran_turismo_vol_t* _root() const { return m__root; }
        gran_turismo_vol_t* _parent() const { return m__parent; }
    };

private:
    bool f_ofs_dir;
    uint32_t m_ofs_dir;

public:
    uint32_t ofs_dir();

private:
    bool f_files;
    std::vector<file_info_t*>* m_files;

public:
    std::vector<file_info_t*>* files();

private:
    std::string m_magic;
    uint16_t m_num_files;
    uint16_t m_num_entries;
    std::string m_reserved;
    std::vector<uint32_t>* m_offsets;
    gran_turismo_vol_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }
    uint16_t num_files() const { return m_num_files; }
    uint16_t num_entries() const { return m_num_entries; }
    std::string reserved() const { return m_reserved; }
    std::vector<uint32_t>* offsets() const { return m_offsets; }
    gran_turismo_vol_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // GRAN_TURISMO_VOL_H_

gran_turismo_vol.cpp

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

#include "gran_turismo_vol.h"



gran_turismo_vol_t::gran_turismo_vol_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, gran_turismo_vol_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    f_ofs_dir = false;
    f_files = false;
    _read();
}

void gran_turismo_vol_t::_read() {
    m_magic = m__io->ensure_fixed_contents(std::string("\x47\x54\x46\x53\x00\x00\x00\x00", 8));
    m_num_files = m__io->read_u2le();
    m_num_entries = m__io->read_u2le();
    m_reserved = m__io->ensure_fixed_contents(std::string("\x00\x00\x00\x00", 4));
    int l_offsets = num_files();
    m_offsets = new std::vector<uint32_t>();
    m_offsets->reserve(l_offsets);
    for (int i = 0; i < l_offsets; i++) {
        m_offsets->push_back(m__io->read_u4le());
    }
}

gran_turismo_vol_t::~gran_turismo_vol_t() {
    delete m_offsets;
    if (f_files) {
        for (std::vector<file_info_t*>::iterator it = m_files->begin(); it != m_files->end(); ++it) {
            delete *it;
        }
        delete m_files;
    }
}

gran_turismo_vol_t::file_info_t::file_info_t(kaitai::kstream* p__io, gran_turismo_vol_t* p__parent, gran_turismo_vol_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_size = false;
    f_body = false;
    f_is_dir = false;
    f_is_last_entry = false;
    _read();
}

void gran_turismo_vol_t::file_info_t::_read() {
    m_timestamp = m__io->read_u4le();
    m_offset_idx = m__io->read_u2le();
    m_flags = m__io->read_u1();
    m_name = kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(kaitai::kstream::bytes_strip_right(m__io->read_bytes(25), 0), 0, false), std::string("ASCII"));
}

gran_turismo_vol_t::file_info_t::~file_info_t() {
    if (f_body && !n_body) {
    }
}

int32_t gran_turismo_vol_t::file_info_t::size() {
    if (f_size)
        return m_size;
    m_size = ((_root()->offsets()->at((offset_idx() + 1)) & 4294965248) - _root()->offsets()->at(offset_idx()));
    f_size = true;
    return m_size;
}

std::string gran_turismo_vol_t::file_info_t::body() {
    if (f_body)
        return m_body;
    n_body = true;
    if (!(is_dir())) {
        n_body = false;
        std::streampos _pos = m__io->pos();
        m__io->seek((_root()->offsets()->at(offset_idx()) & 4294965248));
        m_body = m__io->read_bytes(size());
        m__io->seek(_pos);
    }
    f_body = true;
    return m_body;
}

bool gran_turismo_vol_t::file_info_t::is_dir() {
    if (f_is_dir)
        return m_is_dir;
    m_is_dir = (flags() & 1) != 0;
    f_is_dir = true;
    return m_is_dir;
}

bool gran_turismo_vol_t::file_info_t::is_last_entry() {
    if (f_is_last_entry)
        return m_is_last_entry;
    m_is_last_entry = (flags() & 128) != 0;
    f_is_last_entry = true;
    return m_is_last_entry;
}

uint32_t gran_turismo_vol_t::ofs_dir() {
    if (f_ofs_dir)
        return m_ofs_dir;
    m_ofs_dir = offsets()->at(1);
    f_ofs_dir = true;
    return m_ofs_dir;
}

std::vector<gran_turismo_vol_t::file_info_t*>* gran_turismo_vol_t::files() {
    if (f_files)
        return m_files;
    std::streampos _pos = m__io->pos();
    m__io->seek((ofs_dir() & 4294965248));
    int l_files = _root()->num_entries();
    m_files = new std::vector<file_info_t*>();
    m_files->reserve(l_files);
    for (int i = 0; i < l_files; i++) {
        m_files->push_back(new file_info_t(m__io, this, m__root));
    }
    m__io->seek(_pos);
    f_files = true;
    return m_files;
}