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

Application

Quake game engine

File extension

pak

KS implementation details

License: CC0-1.0

References

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

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.pak", 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:
    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++11/STL source code to parse .pak file format of Quake game engine

quake_pak.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

/**
 * \sa https://quakewiki.org/wiki/.pak#Format_specification 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 = nullptr, quake_pak_t* p__root = nullptr);

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

public:
    ~quake_pak_t();

    class index_struct_t : public kaitai::kstruct {

    public:

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

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

    public:
        ~index_struct_t();

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

    public:
        std::vector<std::unique_ptr<index_entry_t>>* entries() const { return m_entries.get(); }
        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 = nullptr, quake_pak_t* p__root = nullptr);

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

    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;
    std::unique_ptr<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;
    std::unique_ptr<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.get(); }
};

quake_pak.cpp

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

#include "quake_pak.h"
#include "kaitai/exceptions.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;
    m_index = nullptr;
    m__io__raw_index = nullptr;
    f_index = false;
    _read();
}

void quake_pak_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x50\x41\x43\x4B", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x50\x41\x43\x4B", 4), magic(), _io(), std::string("/seq/0"));
    }
    m_ofs_index = m__io->read_u4le();
    m_len_index = m__io->read_u4le();
}

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

void quake_pak_t::_clean_up() {
    if (f_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;
    m_entries = nullptr;
    _read();
}

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

quake_pak_t::index_struct_t::~index_struct_t() {
    _clean_up();
}

void quake_pak_t::index_struct_t::_clean_up() {
}

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

void quake_pak_t::index_entry_t::_clean_up() {
    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.get();
    std::streampos _pos = m__io->pos();
    m__io->seek(ofs_index());
    m__raw_index = m__io->read_bytes(len_index());
    m__io__raw_index = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_index));
    m_index = std::unique_ptr<index_struct_t>(new index_struct_t(m__io__raw_index.get(), this, m__root));
    m__io->seek(_pos);
    f_index = true;
    return m_index.get();
}