Microsoft Windows icon file: C++11/STL parsing library

Microsoft Windows uses specific file format to store applications icons - ICO. This is a container that contains one or more image files (effectively, DIB parts of BMP files or full PNG files are contained inside).

File extension

ico

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of Microsoft Windows icon file 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.ico", 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:
    ico_t data(&ks);
    

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

data.num_images() // => Number of images contained in this file

C++11/STL source code to parse Microsoft Windows icon file

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

/**
 * Microsoft Windows uses specific file format to store applications
 * icons - ICO. This is a container that contains one or more image
 * files (effectively, DIB parts of BMP files or full PNG files are
 * contained inside).
 * \sa https://learn.microsoft.com/en-us/previous-versions/ms997538(v=msdn.10) Source
 */

class ico_t : public kaitai::kstruct {

public:
    class icon_dir_entry_t;

    ico_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = nullptr, ico_t* p__root = nullptr);

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

public:
    ~ico_t();

    class icon_dir_entry_t : public kaitai::kstruct {

    public:

        icon_dir_entry_t(kaitai::kstream* p__io, ico_t* p__parent = nullptr, ico_t* p__root = nullptr);

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

    public:
        ~icon_dir_entry_t();

    private:
        bool f_img;
        std::string m_img;

    public:

        /**
         * Raw image data. Use `is_png` to determine whether this is an
         * embedded PNG file (true) or a DIB bitmap (false) and call a
         * relevant parser, if needed to parse image data further.
         */
        std::string img();

    private:
        bool f_png_header;
        std::string m_png_header;

    public:

        /**
         * Pre-reads first 8 bytes of the image to determine if it's an
         * embedded PNG file.
         */
        std::string png_header();

    private:
        bool f_is_png;
        bool m_is_png;

    public:

        /**
         * True if this image is in PNG format.
         */
        bool is_png();

    private:
        uint8_t m_width;
        uint8_t m_height;
        uint8_t m_num_colors;
        std::string m_reserved;
        uint16_t m_num_planes;
        uint16_t m_bpp;
        uint32_t m_len_img;
        uint32_t m_ofs_img;
        ico_t* m__root;
        ico_t* m__parent;

    public:

        /**
         * Width of image, px
         */
        uint8_t width() const { return m_width; }

        /**
         * Height of image, px
         */
        uint8_t height() const { return m_height; }

        /**
         * Number of colors in palette of the image or 0 if image has
         * no palette (i.e. RGB, RGBA, etc)
         */
        uint8_t num_colors() const { return m_num_colors; }
        std::string reserved() const { return m_reserved; }

        /**
         * Number of color planes
         */
        uint16_t num_planes() const { return m_num_planes; }

        /**
         * Bits per pixel in the image
         */
        uint16_t bpp() const { return m_bpp; }

        /**
         * Size of the image data
         */
        uint32_t len_img() const { return m_len_img; }

        /**
         * Absolute offset of the image data start in the file
         */
        uint32_t ofs_img() const { return m_ofs_img; }
        ico_t* _root() const { return m__root; }
        ico_t* _parent() const { return m__parent; }
    };

private:
    std::string m_magic;
    uint16_t m_num_images;
    std::unique_ptr<std::vector<std::unique_ptr<icon_dir_entry_t>>> m_images;
    ico_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }

    /**
     * Number of images contained in this file
     */
    uint16_t num_images() const { return m_num_images; }
    std::vector<std::unique_ptr<icon_dir_entry_t>>* images() const { return m_images.get(); }
    ico_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

ico.cpp

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

#include "ico.h"
#include "kaitai/exceptions.h"

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

void ico_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x00\x00\x01\x00", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x00\x01\x00", 4), magic(), _io(), std::string("/seq/0"));
    }
    m_num_images = m__io->read_u2le();
    m_images = std::unique_ptr<std::vector<std::unique_ptr<icon_dir_entry_t>>>(new std::vector<std::unique_ptr<icon_dir_entry_t>>());
    const int l_images = num_images();
    for (int i = 0; i < l_images; i++) {
        m_images->push_back(std::move(std::unique_ptr<icon_dir_entry_t>(new icon_dir_entry_t(m__io, this, m__root))));
    }
}

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

void ico_t::_clean_up() {
}

ico_t::icon_dir_entry_t::icon_dir_entry_t(kaitai::kstream* p__io, ico_t* p__parent, ico_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_img = false;
    f_png_header = false;
    f_is_png = false;
    _read();
}

void ico_t::icon_dir_entry_t::_read() {
    m_width = m__io->read_u1();
    m_height = m__io->read_u1();
    m_num_colors = m__io->read_u1();
    m_reserved = m__io->read_bytes(1);
    if (!(reserved() == std::string("\x00", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00", 1), reserved(), _io(), std::string("/types/icon_dir_entry/seq/3"));
    }
    m_num_planes = m__io->read_u2le();
    m_bpp = m__io->read_u2le();
    m_len_img = m__io->read_u4le();
    m_ofs_img = m__io->read_u4le();
}

ico_t::icon_dir_entry_t::~icon_dir_entry_t() {
    _clean_up();
}

void ico_t::icon_dir_entry_t::_clean_up() {
    if (f_img) {
    }
    if (f_png_header) {
    }
}

std::string ico_t::icon_dir_entry_t::img() {
    if (f_img)
        return m_img;
    std::streampos _pos = m__io->pos();
    m__io->seek(ofs_img());
    m_img = m__io->read_bytes(len_img());
    m__io->seek(_pos);
    f_img = true;
    return m_img;
}

std::string ico_t::icon_dir_entry_t::png_header() {
    if (f_png_header)
        return m_png_header;
    std::streampos _pos = m__io->pos();
    m__io->seek(ofs_img());
    m_png_header = m__io->read_bytes(8);
    m__io->seek(_pos);
    f_png_header = true;
    return m_png_header;
}

bool ico_t::icon_dir_entry_t::is_png() {
    if (f_is_png)
        return m_is_png;
    m_is_png = png_header() == std::string("\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
    f_is_png = true;
    return m_is_png;
}