com.android.opengl.shaders_cache file: C++11/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++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.bin", 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++11/STL source code to parse com.android.opengl.shaders_cache file

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

/**
 * 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 = nullptr, android_opengl_shaders_cache_t* p__root = nullptr);

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 = nullptr, android_opengl_shaders_cache_t* p__root = nullptr);

    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 = nullptr, android_opengl_shaders_cache_t* p__root = nullptr);

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

    public:
        ~prefixed_string_t();

    private:
        uint32_t m_len_str;
        std::string m_str;
        std::unique_ptr<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.get(); }
        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 = nullptr, android_opengl_shaders_cache_t* p__root = nullptr);

    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 = nullptr, android_opengl_shaders_cache_t* p__root = nullptr);

        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;
            std::unique_ptr<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.get(); }
            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;
        std::unique_ptr<prefixed_string_t> m_build_id;
        bool n_build_id;

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

    private:
        std::unique_ptr<std::vector<std::unique_ptr<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.get(); }
        std::vector<std::unique_ptr<entry_t>>* entries() const { return m_entries.get(); }
        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;
    std::unique_ptr<cache_t> m_contents;
    android_opengl_shaders_cache_t* m__root;
    kaitai::kstruct* m__parent;
    std::string m__raw_contents;
    std::unique_ptr<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.get(); }
    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.get(); }
};

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 = nullptr;
    m__io__raw_contents = nullptr;
    _read();
}

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 = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_contents));
    m_contents = std::unique_ptr<cache_t>(new cache_t(m__io__raw_contents.get(), this, m__root));
}

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

void android_opengl_shaders_cache_t::_clean_up() {
}

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;
    _read();
}

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 = nullptr;
    _read();
}

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 = std::unique_ptr<alignment_t>(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() {
}

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 = nullptr;
    m_entries = nullptr;
    _read();
}

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 = std::unique_ptr<prefixed_string_t>(new prefixed_string_t(m__io, this, m__root));
    }
    m_entries = std::unique_ptr<std::vector<std::unique_ptr<entry_t>>>(new std::vector<std::unique_ptr<entry_t>>());
    const int l_entries = num_entries();
    for (int i = 0; i < l_entries; i++) {
        m_entries->push_back(std::move(std::unique_ptr<entry_t>(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) {
    }
}

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 = nullptr;
    _read();
}

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 = std::unique_ptr<alignment_t>(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() {
}