ResPack: Ruby 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 Ruby generated by Kaitai Struct depends on the Ruby runtime library. You have to install it before you can parse data.

The Ruby runtime library can be installed from RubyGems:

gem install kaitai-struct

Code

Parse a local file and get structure in memory:

data = Respack.from_file("path/to/local/file.cfg")

Or parse structure from a string of bytes:

bytes = "\x00\x01\x02..."
data = Respack.new(Kaitai::Struct::Stream.new(bytes))

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

data.header # => get header

Ruby source code to parse ResPack

respack.rb

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

require 'kaitai/struct/struct'

unless Gem::Version.new(Kaitai::Struct::VERSION) >= Gem::Version.new('0.9')
  raise "Incompatible Kaitai Struct Ruby API: 0.9 or later is required, but you have #{Kaitai::Struct::VERSION}"
end


##
# 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 < Kaitai::Struct::Struct
  def initialize(_io, _parent = nil, _root = self)
    super(_io, _parent, _root)
    _read
  end

  def _read
    @header = Header.new(@_io, self, @_root)
    @json = (@_io.read_bytes(header.len_json)).force_encoding("UTF-8")
    self
  end
  class Header < Kaitai::Struct::Struct
    def initialize(_io, _parent = nil, _root = self)
      super(_io, _parent, _root)
      _read
    end

    def _read
      @magic = @_io.read_bytes(2)
      raise Kaitai::Struct::ValidationNotEqualError.new([82, 83].pack('C*'), magic, _io, "/types/header/seq/0") if not magic == [82, 83].pack('C*')
      @unknown = @_io.read_bytes(8)
      @len_json = @_io.read_u4le
      @md5 = (@_io.read_bytes(32)).force_encoding("UTF-8")
      self
    end
    attr_reader :magic
    attr_reader :unknown
    attr_reader :len_json

    ##
    # MD5 of data that follows the header
    attr_reader :md5
  end
  attr_reader :header
  attr_reader :json
end