Allegro data file: C++/STL parsing library

Allegro library for C (mostly used for game and multimedia apps programming) used its own container file format.

In general, it allows storage of arbitrary binary data blocks bundled together with some simple key-value style metadata ("properties") for every block. Allegro also pre-defines some simple formats for bitmaps, fonts, MIDI music, sound samples and palettes. Allegro library v4.0+ also support LZSS compression.

This spec applies to Allegro data files for library versions 2.2 up to 4.4.

Application

Allegro library (v2.2-v4.4)

KS implementation details

License: CC0-1.0

This page hosts a formal specification of Allegro data file 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.allegro_dat", 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:
    allegro_dat_t data(&ks);

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

data.pack_magic() // => get pack magic

C++/STL source code to parse Allegro data file

allegro_dat.h

#ifndef ALLEGRO_DAT_H_
#define ALLEGRO_DAT_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

/**
 * Allegro library for C (mostly used for game and multimedia apps
 * programming) used its own container file format.
 * 
 * In general, it allows storage of arbitrary binary data blocks
 * bundled together with some simple key-value style metadata
 * ("properties") for every block. Allegro also pre-defines some simple
 * formats for bitmaps, fonts, MIDI music, sound samples and
 * palettes. Allegro library v4.0+ also support LZSS compression.
 * 
 * This spec applies to Allegro data files for library versions 2.2 up
 * to 4.4.
 * \sa Source
 */

class allegro_dat_t : public kaitai::kstruct {

public:
    class dat_font_16_t;
    class dat_bitmap_t;
    class dat_font_t;
    class dat_font_8_t;
    class dat_object_t;
    class dat_font_3_9_t;
    class property_t;
    class dat_rle_sprite_t;

    enum pack_enum_t {
        PACK_ENUM_UNPACKED = 1936484398
    };

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

private:
    void _read();

public:
    ~allegro_dat_t();

    /**
     * Simple monochrome monospaced font, 95 characters, 8x16 px
     * characters.
     */

    class dat_font_16_t : public kaitai::kstruct {

    public:

        dat_font_16_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~dat_font_16_t();

    private:
        std::vector<std::string>* m_chars;
        allegro_dat_t* m__root;
        allegro_dat_t::dat_font_t* m__parent;

    public:
        std::vector<std::string>* chars() const { return m_chars; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t::dat_font_t* _parent() const { return m__parent; }
    };

    class dat_bitmap_t : public kaitai::kstruct {

    public:

        dat_bitmap_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~dat_bitmap_t();

    private:
        int16_t m_bits_per_pixel;
        uint16_t m_width;
        uint16_t m_height;
        std::string m_image;
        allegro_dat_t* m__root;
        allegro_dat_t::dat_object_t* m__parent;

    public:
        int16_t bits_per_pixel() const { return m_bits_per_pixel; }
        uint16_t width() const { return m_width; }
        uint16_t height() const { return m_height; }
        std::string image() const { return m_image; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t::dat_object_t* _parent() const { return m__parent; }
    };

    class dat_font_t : public kaitai::kstruct {

    public:

        dat_font_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~dat_font_t();

    private:
        int16_t m_font_size;
        kaitai::kstruct* m_body;
        bool n_body;

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

    private:
        allegro_dat_t* m__root;
        allegro_dat_t::dat_object_t* m__parent;

    public:
        int16_t font_size() const { return m_font_size; }
        kaitai::kstruct* body() const { return m_body; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t::dat_object_t* _parent() const { return m__parent; }
    };

    /**
     * Simple monochrome monospaced font, 95 characters, 8x8 px
     * characters.
     */

    class dat_font_8_t : public kaitai::kstruct {

    public:

        dat_font_8_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~dat_font_8_t();

    private:
        std::vector<std::string>* m_chars;
        allegro_dat_t* m__root;
        allegro_dat_t::dat_font_t* m__parent;

    public:
        std::vector<std::string>* chars() const { return m_chars; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t::dat_font_t* _parent() const { return m__parent; }
    };

    class dat_object_t : public kaitai::kstruct {

    public:

        dat_object_t(kaitai::kstream* p__io, allegro_dat_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~dat_object_t();

    private:
        bool f_type;
        std::string m_type;

    public:
        std::string type();

    private:
        std::vector<property_t*>* m_properties;
        int32_t m_len_compressed;
        int32_t m_len_uncompressed;
        kaitai::kstruct* m_body;
        bool n_body;

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

    private:
        allegro_dat_t* m__root;
        allegro_dat_t* m__parent;
        std::string m__raw_body;
        kaitai::kstream* m__io__raw_body;

    public:
        std::vector<property_t*>* properties() const { return m_properties; }
        int32_t len_compressed() const { return m_len_compressed; }
        int32_t len_uncompressed() const { return m_len_uncompressed; }
        kaitai::kstruct* body() const { return m_body; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t* _parent() const { return m__parent; }
        std::string _raw_body() const { return m__raw_body; }
        kaitai::kstream* _io__raw_body() const { return m__io__raw_body; }
    };

    /**
     * New bitmap font format introduced since Allegro 3.9: allows
     * flexible designation of character ranges, 8-bit colored
     * characters, etc.
     */

    class dat_font_3_9_t : public kaitai::kstruct {

    public:
        class range_t;
        class font_char_t;

        dat_font_3_9_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~dat_font_3_9_t();

        class range_t : public kaitai::kstruct {

        public:

            range_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_3_9_t* p__parent = 0, allegro_dat_t* p__root = 0);

        private:
            void _read();

        public:
            ~range_t();

        private:
            uint8_t m_mono;
            uint32_t m_start_char;
            uint32_t m_end_char;
            std::vector<font_char_t*>* m_chars;
            allegro_dat_t* m__root;
            allegro_dat_t::dat_font_3_9_t* m__parent;

        public:
            uint8_t mono() const { return m_mono; }

            /**
             * First character in range
             */
            uint32_t start_char() const { return m_start_char; }

            /**
             * Last character in range (inclusive)
             */
            uint32_t end_char() const { return m_end_char; }
            std::vector<font_char_t*>* chars() const { return m_chars; }
            allegro_dat_t* _root() const { return m__root; }
            allegro_dat_t::dat_font_3_9_t* _parent() const { return m__parent; }
        };

        class font_char_t : public kaitai::kstruct {

        public:

            font_char_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_3_9_t::range_t* p__parent = 0, allegro_dat_t* p__root = 0);

        private:
            void _read();

        public:
            ~font_char_t();

        private:
            uint16_t m_width;
            uint16_t m_height;
            std::string m_body;
            allegro_dat_t* m__root;
            allegro_dat_t::dat_font_3_9_t::range_t* m__parent;

        public:
            uint16_t width() const { return m_width; }
            uint16_t height() const { return m_height; }
            std::string body() const { return m_body; }
            allegro_dat_t* _root() const { return m__root; }
            allegro_dat_t::dat_font_3_9_t::range_t* _parent() const { return m__parent; }
        };

    private:
        int16_t m_num_ranges;
        std::vector<range_t*>* m_ranges;
        allegro_dat_t* m__root;
        allegro_dat_t::dat_font_t* m__parent;

    public:
        int16_t num_ranges() const { return m_num_ranges; }
        std::vector<range_t*>* ranges() const { return m_ranges; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t::dat_font_t* _parent() const { return m__parent; }
    };

    class property_t : public kaitai::kstruct {

    public:

        property_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~property_t();

    private:
        bool f_is_valid;
        bool m_is_valid;

    public:
        bool is_valid();

    private:
        std::string m_magic;
        std::string m_type;
        bool n_type;

    public:
        bool _is_null_type() { type(); return n_type; };

    private:
        uint32_t m_len_body;
        bool n_len_body;

    public:
        bool _is_null_len_body() { len_body(); return n_len_body; };

    private:
        std::string m_body;
        bool n_body;

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

    private:
        allegro_dat_t* m__root;
        allegro_dat_t::dat_object_t* m__parent;

    public:
        std::string magic() const { return m_magic; }
        std::string type() const { return m_type; }
        uint32_t len_body() const { return m_len_body; }
        std::string body() const { return m_body; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t::dat_object_t* _parent() const { return m__parent; }
    };

    class dat_rle_sprite_t : public kaitai::kstruct {

    public:

        dat_rle_sprite_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent = 0, allegro_dat_t* p__root = 0);

    private:
        void _read();

    public:
        ~dat_rle_sprite_t();

    private:
        int16_t m_bits_per_pixel;
        uint16_t m_width;
        uint16_t m_height;
        uint32_t m_len_image;
        std::string m_image;
        allegro_dat_t* m__root;
        allegro_dat_t::dat_object_t* m__parent;

    public:
        int16_t bits_per_pixel() const { return m_bits_per_pixel; }
        uint16_t width() const { return m_width; }
        uint16_t height() const { return m_height; }
        uint32_t len_image() const { return m_len_image; }
        std::string image() const { return m_image; }
        allegro_dat_t* _root() const { return m__root; }
        allegro_dat_t::dat_object_t* _parent() const { return m__parent; }
    };

private:
    pack_enum_t m_pack_magic;
    std::string m_dat_magic;
    uint32_t m_num_objects;
    std::vector<dat_object_t*>* m_objects;
    allegro_dat_t* m__root;
    kaitai::kstruct* m__parent;

public:
    pack_enum_t pack_magic() const { return m_pack_magic; }
    std::string dat_magic() const { return m_dat_magic; }
    uint32_t num_objects() const { return m_num_objects; }
    std::vector<dat_object_t*>* objects() const { return m_objects; }
    allegro_dat_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // ALLEGRO_DAT_H_

allegro_dat.cpp

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

#include "allegro_dat.h"



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

void allegro_dat_t::_read() {
    m_pack_magic = static_cast<allegro_dat_t::pack_enum_t>(m__io->read_u4be());
    m_dat_magic = m__io->ensure_fixed_contents(std::string("\x41\x4C\x4C\x2E", 4));
    m_num_objects = m__io->read_u4be();
    int l_objects = num_objects();
    m_objects = new std::vector<dat_object_t*>();
    m_objects->reserve(l_objects);
    for (int i = 0; i < l_objects; i++) {
        m_objects->push_back(new dat_object_t(m__io, this, m__root));
    }
}

allegro_dat_t::~allegro_dat_t() {
    for (std::vector<dat_object_t*>::iterator it = m_objects->begin(); it != m_objects->end(); ++it) {
        delete *it;
    }
    delete m_objects;
}

allegro_dat_t::dat_font_16_t::dat_font_16_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_font_16_t::_read() {
    int l_chars = 95;
    m_chars = new std::vector<std::string>();
    m_chars->reserve(l_chars);
    for (int i = 0; i < l_chars; i++) {
        m_chars->push_back(m__io->read_bytes(16));
    }
}

allegro_dat_t::dat_font_16_t::~dat_font_16_t() {
    delete m_chars;
}

allegro_dat_t::dat_bitmap_t::dat_bitmap_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_bitmap_t::_read() {
    m_bits_per_pixel = m__io->read_s2be();
    m_width = m__io->read_u2be();
    m_height = m__io->read_u2be();
    m_image = m__io->read_bytes_full();
}

allegro_dat_t::dat_bitmap_t::~dat_bitmap_t() {
}

allegro_dat_t::dat_font_t::dat_font_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_font_t::_read() {
    m_font_size = m__io->read_s2be();
    n_body = true;
    switch (font_size()) {
    case 8: {
        n_body = false;
        m_body = new dat_font_8_t(m__io, this, m__root);
        break;
    }
    case 16: {
        n_body = false;
        m_body = new dat_font_16_t(m__io, this, m__root);
        break;
    }
    case 0: {
        n_body = false;
        m_body = new dat_font_3_9_t(m__io, this, m__root);
        break;
    }
    }
}

allegro_dat_t::dat_font_t::~dat_font_t() {
    if (!n_body) {
        delete m_body;
    }
}

allegro_dat_t::dat_font_8_t::dat_font_8_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_font_8_t::_read() {
    int l_chars = 95;
    m_chars = new std::vector<std::string>();
    m_chars->reserve(l_chars);
    for (int i = 0; i < l_chars; i++) {
        m_chars->push_back(m__io->read_bytes(8));
    }
}

allegro_dat_t::dat_font_8_t::~dat_font_8_t() {
    delete m_chars;
}

allegro_dat_t::dat_object_t::dat_object_t(kaitai::kstream* p__io, allegro_dat_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_type = false;
    _read();
}

void allegro_dat_t::dat_object_t::_read() {
    m_properties = new std::vector<property_t*>();
    {
        int i = 0;
        property_t* _;
        do {
            _ = new property_t(m__io, this, m__root);
            m_properties->push_back(_);
            i++;
        } while (!(!(_->is_valid())));
    }
    m_len_compressed = m__io->read_s4be();
    m_len_uncompressed = m__io->read_s4be();
    n_body = true;
    {
        std::string on = type();
        if (on == std::string("BMP ")) {
            n_body = false;
            m__raw_body = m__io->read_bytes(len_compressed());
            m__io__raw_body = new kaitai::kstream(m__raw_body);
            m_body = new dat_bitmap_t(m__io__raw_body, this, m__root);
        }
        else if (on == std::string("RLE ")) {
            n_body = false;
            m__raw_body = m__io->read_bytes(len_compressed());
            m__io__raw_body = new kaitai::kstream(m__raw_body);
            m_body = new dat_rle_sprite_t(m__io__raw_body, this, m__root);
        }
        else if (on == std::string("FONT")) {
            n_body = false;
            m__raw_body = m__io->read_bytes(len_compressed());
            m__io__raw_body = new kaitai::kstream(m__raw_body);
            m_body = new dat_font_t(m__io__raw_body, this, m__root);
        }
        else {
            m__raw_body = m__io->read_bytes(len_compressed());
        }
    }
}

allegro_dat_t::dat_object_t::~dat_object_t() {
    for (std::vector<property_t*>::iterator it = m_properties->begin(); it != m_properties->end(); ++it) {
        delete *it;
    }
    delete m_properties;
    if (!n_body) {
        delete m__io__raw_body;
        delete m_body;
    }
}

std::string allegro_dat_t::dat_object_t::type() {
    if (f_type)
        return m_type;
    m_type = properties()->back()->magic();
    f_type = true;
    return m_type;
}

allegro_dat_t::dat_font_3_9_t::dat_font_3_9_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_font_3_9_t::_read() {
    m_num_ranges = m__io->read_s2be();
    int l_ranges = num_ranges();
    m_ranges = new std::vector<range_t*>();
    m_ranges->reserve(l_ranges);
    for (int i = 0; i < l_ranges; i++) {
        m_ranges->push_back(new range_t(m__io, this, m__root));
    }
}

allegro_dat_t::dat_font_3_9_t::~dat_font_3_9_t() {
    for (std::vector<range_t*>::iterator it = m_ranges->begin(); it != m_ranges->end(); ++it) {
        delete *it;
    }
    delete m_ranges;
}

allegro_dat_t::dat_font_3_9_t::range_t::range_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_3_9_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_font_3_9_t::range_t::_read() {
    m_mono = m__io->read_u1();
    m_start_char = m__io->read_u4be();
    m_end_char = m__io->read_u4be();
    int l_chars = ((end_char() - start_char()) + 1);
    m_chars = new std::vector<font_char_t*>();
    m_chars->reserve(l_chars);
    for (int i = 0; i < l_chars; i++) {
        m_chars->push_back(new font_char_t(m__io, this, m__root));
    }
}

allegro_dat_t::dat_font_3_9_t::range_t::~range_t() {
    for (std::vector<font_char_t*>::iterator it = m_chars->begin(); it != m_chars->end(); ++it) {
        delete *it;
    }
    delete m_chars;
}

allegro_dat_t::dat_font_3_9_t::font_char_t::font_char_t(kaitai::kstream* p__io, allegro_dat_t::dat_font_3_9_t::range_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_font_3_9_t::font_char_t::_read() {
    m_width = m__io->read_u2be();
    m_height = m__io->read_u2be();
    m_body = m__io->read_bytes((width() * height()));
}

allegro_dat_t::dat_font_3_9_t::font_char_t::~font_char_t() {
}

allegro_dat_t::property_t::property_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_is_valid = false;
    _read();
}

void allegro_dat_t::property_t::_read() {
    m_magic = kaitai::kstream::bytes_to_str(m__io->read_bytes(4), std::string("UTF-8"));
    n_type = true;
    if (is_valid()) {
        n_type = false;
        m_type = kaitai::kstream::bytes_to_str(m__io->read_bytes(4), std::string("UTF-8"));
    }
    n_len_body = true;
    if (is_valid()) {
        n_len_body = false;
        m_len_body = m__io->read_u4be();
    }
    n_body = true;
    if (is_valid()) {
        n_body = false;
        m_body = kaitai::kstream::bytes_to_str(m__io->read_bytes(len_body()), std::string("UTF-8"));
    }
}

allegro_dat_t::property_t::~property_t() {
    if (!n_type) {
    }
    if (!n_len_body) {
    }
    if (!n_body) {
    }
}

bool allegro_dat_t::property_t::is_valid() {
    if (f_is_valid)
        return m_is_valid;
    m_is_valid = magic() == (std::string("prop"));
    f_is_valid = true;
    return m_is_valid;
}

allegro_dat_t::dat_rle_sprite_t::dat_rle_sprite_t(kaitai::kstream* p__io, allegro_dat_t::dat_object_t* p__parent, allegro_dat_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void allegro_dat_t::dat_rle_sprite_t::_read() {
    m_bits_per_pixel = m__io->read_s2be();
    m_width = m__io->read_u2be();
    m_height = m__io->read_u2be();
    m_len_image = m__io->read_u4be();
    m_image = m__io->read_bytes_full();
}

allegro_dat_t::dat_rle_sprite_t::~dat_rle_sprite_t() {
}