.pak file format of Quake game engine: C++/STL parsing library

Application

Quake game engine

File extension

pak

KS implementation details

License: CC0-1.0

This page hosts a formal specification of .pak file format of Quake game engine 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.pak", 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:
    quake_pak_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 .pak file format of Quake game engine

quake_pak.h

#ifndef QUAKE_PAK_H_
#define QUAKE_PAK_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

/**
 * \sa Source
 */

class quake_pak_t : public kaitai::kstruct {

public:
    class index_struct_t;
    class index_entry_t;

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

private:
    void _read();

public:
    ~quake_pak_t();

    class index_struct_t : public kaitai::kstruct {

    public:

        index_struct_t(kaitai::kstream* p__io, quake_pak_t* p__parent = 0, quake_pak_t* p__root = 0);

    private:
        void _read();

    public:
        ~index_struct_t();

    private:
        std::vector<index_entry_t*>* m_entries;
        quake_pak_t* m__root;
        quake_pak_t* m__parent;

    public:
        std::vector<index_entry_t*>* entries() const { return m_entries; }
        quake_pak_t* _root() const { return m__root; }
        quake_pak_t* _parent() const { return m__parent; }
    };

    class index_entry_t : public kaitai::kstruct {

    public:

        index_entry_t(kaitai::kstream* p__io, quake_pak_t::index_struct_t* p__parent = 0, quake_pak_t* p__root = 0);

    private:
        void _read();

    public:
        ~index_entry_t();

    private:
        bool f_body;
        std::string m_body;

    public:
        std::string body();

    private:
        std::string m_name;
        uint32_t m_ofs;
        uint32_t m_size;
        quake_pak_t* m__root;
        quake_pak_t::index_struct_t* m__parent;

    public:
        std::string name() const { return m_name; }
        uint32_t ofs() const { return m_ofs; }
        uint32_t size() const { return m_size; }
        quake_pak_t* _root() const { return m__root; }
        quake_pak_t::index_struct_t* _parent() const { return m__parent; }
    };

private:
    bool f_index;
    index_struct_t* m_index;

public:
    index_struct_t* index();

private:
    std::string m_magic;
    uint32_t m_ofs_index;
    uint32_t m_len_index;
    quake_pak_t* m__root;
    kaitai::kstruct* m__parent;
    std::string m__raw_index;
    kaitai::kstream* m__io__raw_index;

public:
    std::string magic() const { return m_magic; }
    uint32_t ofs_index() const { return m_ofs_index; }
    uint32_t len_index() const { return m_len_index; }
    quake_pak_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
    std::string _raw_index() const { return m__raw_index; }
    kaitai::kstream* _io__raw_index() const { return m__io__raw_index; }
};

#endif  // QUAKE_PAK_H_

quake_pak.cpp

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

#include "quake_pak.h"



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

void quake_pak_t::_read() {
    m_magic = m__io->ensure_fixed_contents(std::string("\x50\x41\x43\x4B", 4));
    m_ofs_index = m__io->read_u4le();
    m_len_index = m__io->read_u4le();
}

quake_pak_t::~quake_pak_t() {
    if (f_index) {
        delete m__io__raw_index;
        delete m_index;
    }
}

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

void quake_pak_t::index_struct_t::_read() {
    m_entries = new std::vector<index_entry_t*>();
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_entries->push_back(new index_entry_t(m__io, this, m__root));
            i++;
        }
    }
}

quake_pak_t::index_struct_t::~index_struct_t() {
    for (std::vector<index_entry_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
        delete *it;
    }
    delete m_entries;
}

quake_pak_t::index_entry_t::index_entry_t(kaitai::kstream* p__io, quake_pak_t::index_struct_t* p__parent, quake_pak_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_body = false;
    _read();
}

void quake_pak_t::index_entry_t::_read() {
    m_name = kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(kaitai::kstream::bytes_strip_right(m__io->read_bytes(56), 0), 0, false), std::string("UTF-8"));
    m_ofs = m__io->read_u4le();
    m_size = m__io->read_u4le();
}

quake_pak_t::index_entry_t::~index_entry_t() {
    if (f_body) {
    }
}

std::string quake_pak_t::index_entry_t::body() {
    if (f_body)
        return m_body;
    kaitai::kstream *io = _root()->_io();
    std::streampos _pos = io->pos();
    io->seek(ofs());
    m_body = io->read_bytes(size());
    io->seek(_pos);
    f_body = true;
    return m_body;
}

quake_pak_t::index_struct_t* quake_pak_t::index() {
    if (f_index)
        return m_index;
    std::streampos _pos = m__io->pos();
    m__io->seek(ofs_index());
    m__raw_index = m__io->read_bytes(len_index());
    m__io__raw_index = new kaitai::kstream(m__raw_index);
    m_index = new index_struct_t(m__io__raw_index, this, m__root);
    m__io->seek(_pos);
    f_index = true;
    return m_index;
}