.agg file format of Heroes of Might and Magic: C++/STL parsing library

Application

Heroes of Might and Magic

File extension

agg

KS implementation details

License: CC0-1.0

This page hosts a formal specification of .agg file format of Heroes of Might and Magic 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.agg", 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:
    heroes_of_might_and_magic_agg_t data(&ks);

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

data.num_files() // => get num files

C++/STL source code to parse .agg file format of Heroes of Might and Magic

heroes_of_might_and_magic_agg.h

#ifndef HEROES_OF_MIGHT_AND_MAGIC_AGG_H_
#define HEROES_OF_MIGHT_AND_MAGIC_AGG_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 heroes_of_might_and_magic_agg_t : public kaitai::kstruct {

public:
    class entry_t;
    class filename_t;

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

private:
    void _read();

public:
    ~heroes_of_might_and_magic_agg_t();

    class entry_t : public kaitai::kstruct {

    public:

        entry_t(kaitai::kstream* p__io, heroes_of_might_and_magic_agg_t* p__parent = 0, heroes_of_might_and_magic_agg_t* p__root = 0);

    private:
        void _read();

    public:
        ~entry_t();

    private:
        bool f_body;
        std::string m_body;

    public:
        std::string body();

    private:
        uint16_t m_hash;
        uint32_t m_offset;
        uint32_t m_size;
        uint32_t m_size2;
        heroes_of_might_and_magic_agg_t* m__root;
        heroes_of_might_and_magic_agg_t* m__parent;

    public:
        uint16_t hash() const { return m_hash; }
        uint32_t offset() const { return m_offset; }
        uint32_t size() const { return m_size; }
        uint32_t size2() const { return m_size2; }
        heroes_of_might_and_magic_agg_t* _root() const { return m__root; }
        heroes_of_might_and_magic_agg_t* _parent() const { return m__parent; }
    };

    class filename_t : public kaitai::kstruct {

    public:

        filename_t(kaitai::kstream* p__io, heroes_of_might_and_magic_agg_t* p__parent = 0, heroes_of_might_and_magic_agg_t* p__root = 0);

    private:
        void _read();

    public:
        ~filename_t();

    private:
        std::string m_str;
        heroes_of_might_and_magic_agg_t* m__root;
        heroes_of_might_and_magic_agg_t* m__parent;

    public:
        std::string str() const { return m_str; }
        heroes_of_might_and_magic_agg_t* _root() const { return m__root; }
        heroes_of_might_and_magic_agg_t* _parent() const { return m__parent; }
    };

private:
    bool f_filenames;
    std::vector<filename_t*>* m_filenames;

public:
    std::vector<filename_t*>* filenames();

private:
    uint16_t m_num_files;
    std::vector<entry_t*>* m_entries;
    heroes_of_might_and_magic_agg_t* m__root;
    kaitai::kstruct* m__parent;
    std::vector<std::string>* m__raw_filenames;
    std::vector<kaitai::kstream*>* m__io__raw_filenames;

public:
    uint16_t num_files() const { return m_num_files; }
    std::vector<entry_t*>* entries() const { return m_entries; }
    heroes_of_might_and_magic_agg_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
    std::vector<std::string>* _raw_filenames() const { return m__raw_filenames; }
    std::vector<kaitai::kstream*>* _io__raw_filenames() const { return m__io__raw_filenames; }
};

#endif  // HEROES_OF_MIGHT_AND_MAGIC_AGG_H_

heroes_of_might_and_magic_agg.cpp

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

#include "heroes_of_might_and_magic_agg.h"



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

void heroes_of_might_and_magic_agg_t::_read() {
    m_num_files = m__io->read_u2le();
    int l_entries = num_files();
    m_entries = new std::vector<entry_t*>();
    m_entries->reserve(l_entries);
    for (int i = 0; i < l_entries; i++) {
        m_entries->push_back(new entry_t(m__io, this, m__root));
    }
}

heroes_of_might_and_magic_agg_t::~heroes_of_might_and_magic_agg_t() {
    for (std::vector<entry_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
        delete *it;
    }
    delete m_entries;
    if (f_filenames) {
        delete m__raw_filenames;
        for (std::vector<kaitai::kstream*>::iterator it = m__io__raw_filenames->begin(); it != m__io__raw_filenames->end(); ++it) {
            delete *it;
        }
        delete m__io__raw_filenames;
        for (std::vector<filename_t*>::iterator it = m_filenames->begin(); it != m_filenames->end(); ++it) {
            delete *it;
        }
        delete m_filenames;
    }
}

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

void heroes_of_might_and_magic_agg_t::entry_t::_read() {
    m_hash = m__io->read_u2le();
    m_offset = m__io->read_u4le();
    m_size = m__io->read_u4le();
    m_size2 = m__io->read_u4le();
}

heroes_of_might_and_magic_agg_t::entry_t::~entry_t() {
    if (f_body) {
    }
}

std::string heroes_of_might_and_magic_agg_t::entry_t::body() {
    if (f_body)
        return m_body;
    std::streampos _pos = m__io->pos();
    m__io->seek(offset());
    m_body = m__io->read_bytes(size());
    m__io->seek(_pos);
    f_body = true;
    return m_body;
}

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

void heroes_of_might_and_magic_agg_t::filename_t::_read() {
    m_str = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(0, false, true, true), std::string("ASCII"));
}

heroes_of_might_and_magic_agg_t::filename_t::~filename_t() {
}

std::vector<heroes_of_might_and_magic_agg_t::filename_t*>* heroes_of_might_and_magic_agg_t::filenames() {
    if (f_filenames)
        return m_filenames;
    std::streampos _pos = m__io->pos();
    m__io->seek((entries()->back()->offset() + entries()->back()->size()));
    int l_filenames = num_files();
    m__raw_filenames = new std::vector<std::string>();
    m__raw_filenames->reserve(l_filenames);
    m__io__raw_filenames = new std::vector<kaitai::kstream*>();
    m__io__raw_filenames->reserve(l_filenames);
    m_filenames = new std::vector<filename_t*>();
    m_filenames->reserve(l_filenames);
    for (int i = 0; i < l_filenames; i++) {
        m__raw_filenames->push_back(m__io->read_bytes(15));
        kaitai::kstream* io__raw_filenames = new kaitai::kstream(m__raw_filenames->at(m__raw_filenames->size() - 1));
        m__io__raw_filenames->push_back(io__raw_filenames);
        m_filenames->push_back(new filename_t(io__raw_filenames, this, m__root));
    }
    m__io->seek(_pos);
    f_filenames = true;
    return m_filenames;
}