ResPack: C++11/STL parsing library

Resource file found in CPB firmware archives, mostly used on older CoolPad phones and/or tablets. The only observed files are called "ResPack.cfg".

File extension

cfg

KS implementation details

License: CC0-1.0

This page hosts a formal specification of ResPack 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.cfg", 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:
    respack_t data(&ks);
    

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

data.header() // => get header

C++11/STL source code to parse ResPack

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

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

/**
 * Resource file found in CPB firmware archives, mostly used on older CoolPad
 * phones and/or tablets. The only observed files are called "ResPack.cfg".
 */

class respack_t : public kaitai::kstruct {

public:
    class header_t;

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

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

public:
    ~respack_t();

    class header_t : public kaitai::kstruct {

    public:

        header_t(kaitai::kstream* p__io, respack_t* p__parent = nullptr, respack_t* p__root = nullptr);

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

    public:
        ~header_t();

    private:
        std::string m_magic;
        std::string m_unknown;
        uint32_t m_len_json;
        std::string m_md5;
        respack_t* m__root;
        respack_t* m__parent;

    public:
        std::string magic() const { return m_magic; }
        std::string unknown() const { return m_unknown; }
        uint32_t len_json() const { return m_len_json; }

        /**
         * MD5 of data that follows the header
         */
        std::string md5() const { return m_md5; }
        respack_t* _root() const { return m__root; }
        respack_t* _parent() const { return m__parent; }
    };

private:
    std::unique_ptr<header_t> m_header;
    std::string m_json;
    respack_t* m__root;
    kaitai::kstruct* m__parent;

public:
    header_t* header() const { return m_header.get(); }
    std::string json() const { return m_json; }
    respack_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

respack.cpp

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

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

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

void respack_t::_read() {
    m_header = std::unique_ptr<header_t>(new header_t(m__io, this, m__root));
    m_json = kaitai::kstream::bytes_to_str(m__io->read_bytes(header()->len_json()), std::string("UTF-8"));
}

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

void respack_t::_clean_up() {
}

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

void respack_t::header_t::_read() {
    m_magic = m__io->read_bytes(2);
    if (!(magic() == std::string("\x52\x53", 2))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x52\x53", 2), magic(), _io(), std::string("/types/header/seq/0"));
    }
    m_unknown = m__io->read_bytes(8);
    m_len_json = m__io->read_u4le();
    m_md5 = kaitai::kstream::bytes_to_str(m__io->read_bytes(32), std::string("UTF-8"));
}

respack_t::header_t::~header_t() {
    _clean_up();
}

void respack_t::header_t::_clean_up() {
}