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.
All parsing code for C++98/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.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
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);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
quake_pak_t data(&ks);
After that, one can get various attributes from the structure by invoking getter methods like:
data.magic() // => get magic
#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 < 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 = 0, quake_pak_t* p__root = 0);
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 = 0, quake_pak_t* p__root = 0);
private:
void _read();
void _clean_up();
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();
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;
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_
// 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 = 0;
m__io__raw_index = 0;
f_index = false;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
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) {
if (m__io__raw_index) {
delete m__io__raw_index; m__io__raw_index = 0;
}
if (m_index) {
delete m_index; m_index = 0;
}
}
}
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 = 0;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
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() {
_clean_up();
}
void quake_pak_t::index_struct_t::_clean_up() {
if (m_entries) {
for (std::vector<index_entry_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
delete *it;
}
delete m_entries; m_entries = 0;
}
}
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;
try {
_read();
} catch(...) {
_clean_up();
throw;
}
}
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;
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;
}