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

Application

Heroes of Might and Magic

File extension

bmp

KS implementation details

License: CC0-1.0

This page hosts a formal specification of .bmp 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.bmp", 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_bmp_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 .bmp file format of Heroes of Might and Magic

heroes_of_might_and_magic_bmp.h

#ifndef HEROES_OF_MIGHT_AND_MAGIC_BMP_H_
#define HEROES_OF_MIGHT_AND_MAGIC_BMP_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>

#if KAITAI_STRUCT_VERSION < 7000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.7 or later is required"
#endif

class heroes_of_might_and_magic_bmp_t : public kaitai::kstruct {

public:

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

private:
    void _read();

public:
    ~heroes_of_might_and_magic_bmp_t();

private:
    uint16_t m_magic;
    uint16_t m_width;
    uint16_t m_height;
    std::string m_data;
    heroes_of_might_and_magic_bmp_t* m__root;
    kaitai::kstruct* m__parent;

public:
    uint16_t magic() const { return m_magic; }
    uint16_t width() const { return m_width; }
    uint16_t height() const { return m_height; }
    std::string data() const { return m_data; }
    heroes_of_might_and_magic_bmp_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // HEROES_OF_MIGHT_AND_MAGIC_BMP_H_

heroes_of_might_and_magic_bmp.cpp

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

#include "heroes_of_might_and_magic_bmp.h"



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

void heroes_of_might_and_magic_bmp_t::_read() {
    m_magic = m__io->read_u2le();
    m_width = m__io->read_u2le();
    m_height = m__io->read_u2le();
    m_data = m__io->read_bytes((width() * height()));
}

heroes_of_might_and_magic_bmp_t::~heroes_of_might_and_magic_bmp_t() {
}