com.android.opengl.shaders_cache file: C++98/STL parsing library

Android apps using directly or indirectly OpenGL cache compiled shaders into com.android.opengl.shaders_cache file.

Application

Android

KS implementation details

License: Apache-2.0

This page hosts a formal specification of com.android.opengl.shaders_cache 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++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.

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.android_opengl_shaders_cache", 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:
    android_opengl_shaders_cache_t data(&ks);
    

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

data.crc32() // => crc32 of `contents`

C++98/STL source code to parse com.android.opengl.shaders_cache file

android_opengl_shaders_cache.h

#ifndef ANDROID_OPENGL_SHADERS_CACHE_H_
#define ANDROID_OPENGL_SHADERS_CACHE_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

/**
 * Android apps using directly or indirectly OpenGL cache compiled shaders
 * into com.android.opengl.shaders_cache file.
 * \sa https://android.googlesource.com/platform/frameworks/native/+/master/opengl/libs/EGL/FileBlobCache.cpp Source
 */

class android_opengl_shaders_cache_t : public kaitai::kstruct {

public:
    class alignment_t;
    class prefixed_string_t;
    class cache_t;

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

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

public:
    ~android_opengl_shaders_cache_t();

    class alignment_t : public kaitai::kstruct {

    public:

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

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

    public:
        ~alignment_t();

    private:
        std::string m_alignment;
        android_opengl_shaders_cache_t* m__root;
        kaitai::kstruct* m__parent;

    public:

        /**
         * garbage from memory
         */
        std::string alignment() const { return m_alignment; }
        android_opengl_shaders_cache_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

    class prefixed_string_t : public kaitai::kstruct {

    public:

        prefixed_string_t(kaitai::kstream* p__io, android_opengl_shaders_cache_t::cache_t* p__parent = 0, android_opengl_shaders_cache_t* p__root = 0);

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

    public:
        ~prefixed_string_t();

    private:
        uint32_t m_len_str;
        std::string m_str;
        alignment_t* m_alignment;
        android_opengl_shaders_cache_t* m__root;
        android_opengl_shaders_cache_t::cache_t* m__parent;

    public:
        uint32_t len_str() const { return m_len_str; }
        std::string str() const { return m_str; }
        alignment_t* alignment() const { return m_alignment; }
        android_opengl_shaders_cache_t* _root() const { return m__root; }
        android_opengl_shaders_cache_t::cache_t* _parent() const { return m__parent; }
    };

    /**
     * \sa https://android.googlesource.com/platform/frameworks/native/+/master/opengl/libs/EGL/BlobCache.cpp Source
     */

    class cache_t : public kaitai::kstruct {

    public:
        class entry_t;

        cache_t(kaitai::kstream* p__io, android_opengl_shaders_cache_t* p__parent = 0, android_opengl_shaders_cache_t* p__root = 0);

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

    public:
        ~cache_t();

        class entry_t : public kaitai::kstruct {

        public:

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

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

        public:
            ~entry_t();

        private:
            uint32_t m_len_key;
            uint32_t m_len_value;
            std::string m_key;
            std::string m_value;
            alignment_t* m_alignment;
            android_opengl_shaders_cache_t* m__root;
            android_opengl_shaders_cache_t::cache_t* m__parent;

        public:
            uint32_t len_key() const { return m_len_key; }
            uint32_t len_value() const { return m_len_value; }
            std::string key() const { return m_key; }
            std::string value() const { return m_value; }
            alignment_t* alignment() const { return m_alignment; }
            android_opengl_shaders_cache_t* _root() const { return m__root; }
            android_opengl_shaders_cache_t::cache_t* _parent() const { return m__parent; }
        };

    private:
        std::string m_magic;
        uint32_t m_version;
        uint32_t m_device_version;
        uint32_t m_num_entries;
        prefixed_string_t* m_build_id;
        bool n_build_id;

    public:
        bool _is_null_build_id() { build_id(); return n_build_id; };

    private:
        std::vector<entry_t*>* m_entries;
        android_opengl_shaders_cache_t* m__root;
        android_opengl_shaders_cache_t* m__parent;

    public:
        std::string magic() const { return m_magic; }
        uint32_t version() const { return m_version; }
        uint32_t device_version() const { return m_device_version; }
        uint32_t num_entries() const { return m_num_entries; }
        prefixed_string_t* build_id() const { return m_build_id; }
        std::vector<entry_t*>* entries() const { return m_entries; }
        android_opengl_shaders_cache_t* _root() const { return m__root; }
        android_opengl_shaders_cache_t* _parent() const { return m__parent; }
    };

private:
    std::string m_magic;
    uint32_t m_crc32;
    cache_t* m_contents;
    android_opengl_shaders_cache_t* m__root;
    kaitai::kstruct* m__parent;
    std::string m__raw_contents;
    kaitai::kstream* m__io__raw_contents;

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

    /**
     * crc32 of `contents`
     */
    uint32_t crc32() const { return m_crc32; }
    cache_t* contents() const { return m_contents; }
    android_opengl_shaders_cache_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
    std::string _raw_contents() const { return m__raw_contents; }
    kaitai::kstream* _io__raw_contents() const { return m__io__raw_contents; }
};

#endif  // ANDROID_OPENGL_SHADERS_CACHE_H_

android_opengl_shaders_cache.cpp

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

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

android_opengl_shaders_cache_t::android_opengl_shaders_cache_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, android_opengl_shaders_cache_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_contents = 0;
    m__io__raw_contents = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void android_opengl_shaders_cache_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x45\x47\x4C\x24", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x45\x47\x4C\x24", 4), magic(), _io(), std::string("/seq/0"));
    }
    m_crc32 = m__io->read_u4le();
    m__raw_contents = m__io->read_bytes_full();
    m__io__raw_contents = new kaitai::kstream(m__raw_contents);
    m_contents = new cache_t(m__io__raw_contents, this, m__root);
}

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

void android_opengl_shaders_cache_t::_clean_up() {
    if (m__io__raw_contents) {
        delete m__io__raw_contents; m__io__raw_contents = 0;
    }
    if (m_contents) {
        delete m_contents; m_contents = 0;
    }
}

android_opengl_shaders_cache_t::alignment_t::alignment_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, android_opengl_shaders_cache_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void android_opengl_shaders_cache_t::alignment_t::_read() {
    m_alignment = m__io->read_bytes(((_io()->pos() + 3) & (~3 - _io()->pos())));
}

android_opengl_shaders_cache_t::alignment_t::~alignment_t() {
    _clean_up();
}

void android_opengl_shaders_cache_t::alignment_t::_clean_up() {
}

android_opengl_shaders_cache_t::prefixed_string_t::prefixed_string_t(kaitai::kstream* p__io, android_opengl_shaders_cache_t::cache_t* p__parent, android_opengl_shaders_cache_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_alignment = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void android_opengl_shaders_cache_t::prefixed_string_t::_read() {
    m_len_str = m__io->read_u4le();
    m_str = kaitai::kstream::bytes_to_str(kaitai::kstream::bytes_terminate(m__io->read_bytes(len_str()), 0, false), std::string("ascii"));
    m_alignment = new alignment_t(m__io, this, m__root);
}

android_opengl_shaders_cache_t::prefixed_string_t::~prefixed_string_t() {
    _clean_up();
}

void android_opengl_shaders_cache_t::prefixed_string_t::_clean_up() {
    if (m_alignment) {
        delete m_alignment; m_alignment = 0;
    }
}

android_opengl_shaders_cache_t::cache_t::cache_t(kaitai::kstream* p__io, android_opengl_shaders_cache_t* p__parent, android_opengl_shaders_cache_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_build_id = 0;
    m_entries = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void android_opengl_shaders_cache_t::cache_t::_read() {
    m_magic = m__io->read_bytes(4);
    if (!(magic() == std::string("\x24\x62\x42\x5F", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x24\x62\x42\x5F", 4), magic(), _io(), std::string("/types/cache/seq/0"));
    }
    m_version = m__io->read_u4le();
    m_device_version = m__io->read_u4le();
    m_num_entries = m__io->read_u4le();
    n_build_id = true;
    if (version() >= 3) {
        n_build_id = false;
        m_build_id = new prefixed_string_t(m__io, this, m__root);
    }
    int l_entries = num_entries();
    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));
    }
}

android_opengl_shaders_cache_t::cache_t::~cache_t() {
    _clean_up();
}

void android_opengl_shaders_cache_t::cache_t::_clean_up() {
    if (!n_build_id) {
        if (m_build_id) {
            delete m_build_id; m_build_id = 0;
        }
    }
    if (m_entries) {
        for (std::vector<entry_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
            delete *it;
        }
        delete m_entries; m_entries = 0;
    }
}

android_opengl_shaders_cache_t::cache_t::entry_t::entry_t(kaitai::kstream* p__io, android_opengl_shaders_cache_t::cache_t* p__parent, android_opengl_shaders_cache_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_alignment = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void android_opengl_shaders_cache_t::cache_t::entry_t::_read() {
    m_len_key = m__io->read_u4le();
    m_len_value = m__io->read_u4le();
    m_key = m__io->read_bytes(len_key());
    m_value = m__io->read_bytes(len_value());
    m_alignment = new alignment_t(m__io, this, m__root);
}

android_opengl_shaders_cache_t::cache_t::entry_t::~entry_t() {
    _clean_up();
}

void android_opengl_shaders_cache_t::cache_t::entry_t::_clean_up() {
    if (m_alignment) {
        delete m_alignment; m_alignment = 0;
    }
}