Variable length quantity, unsigned integer, base128, big-endian: C++11/STL parsing library

A variable-length unsigned integer using base128 encoding. 1-byte groups consist of 1-bit flag of continuation and 7-bit value chunk, and are ordered "most significant group first", i.e. in "big-endian" manner.

This particular encoding is specified and used in:

  • Standard MIDI file format
  • ASN.1 BER encoding
  • RAR 5.0 file format

More information on this encoding is available at https://en.wikipedia.org/wiki/Variable-length_quantity

This particular implementation supports serialized values to up 8 bytes long.

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.9

This page hosts a formal specification of Variable length quantity, unsigned integer, base128, big-endian 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:
    vlq_base128_be_t data(&ks);
    

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

data.value() // => Resulting value as normal integer

C++11/STL source code to parse Variable length quantity, unsigned integer, base128, big-endian

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

/**
 * A variable-length unsigned integer using base128 encoding. 1-byte groups
 * consist of 1-bit flag of continuation and 7-bit value chunk, and are ordered
 * "most significant group first", i.e. in "big-endian" manner.
 * 
 * This particular encoding is specified and used in:
 * 
 * * Standard MIDI file format
 * * ASN.1 BER encoding
 * * RAR 5.0 file format
 * 
 * More information on this encoding is available at
 * <https://en.wikipedia.org/wiki/Variable-length_quantity>
 * 
 * This particular implementation supports serialized values to up 8 bytes long.
 */

class vlq_base128_be_t : public kaitai::kstruct {

public:
    class group_t;

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

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

public:
    ~vlq_base128_be_t();

    /**
     * One byte group, clearly divided into 7-bit "value" chunk and 1-bit "continuation" flag.
     */

    class group_t : public kaitai::kstruct {

    public:

        group_t(kaitai::kstream* p__io, vlq_base128_be_t* p__parent = nullptr, vlq_base128_be_t* p__root = nullptr);

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

    public:
        ~group_t();

    private:
        bool m_has_next;
        uint64_t m_value;
        vlq_base128_be_t* m__root;
        vlq_base128_be_t* m__parent;

    public:

        /**
         * If true, then we have more bytes to read
         */
        bool has_next() const { return m_has_next; }

        /**
         * The 7-bit (base128) numeric value chunk of this group
         */
        uint64_t value() const { return m_value; }
        vlq_base128_be_t* _root() const { return m__root; }
        vlq_base128_be_t* _parent() const { return m__parent; }
    };

private:
    bool f_last;
    int32_t m_last;

public:
    int32_t last();

private:
    bool f_value;
    uint64_t m_value;

public:

    /**
     * Resulting value as normal integer
     */
    uint64_t value();

private:
    std::unique_ptr<std::vector<std::unique_ptr<group_t>>> m_groups;
    vlq_base128_be_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::vector<std::unique_ptr<group_t>>* groups() const { return m_groups.get(); }
    vlq_base128_be_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

vlq_base128_be.cpp

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

#include "vlq_base128_be.h"

vlq_base128_be_t::vlq_base128_be_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, vlq_base128_be_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_groups = nullptr;
    f_last = false;
    f_value = false;
    _read();
}

void vlq_base128_be_t::_read() {
    m_groups = std::unique_ptr<std::vector<std::unique_ptr<group_t>>>(new std::vector<std::unique_ptr<group_t>>());
    {
        int i = 0;
        group_t* _;
        do {
            _ = new group_t(m__io, this, m__root);
            m_groups->push_back(std::move(std::unique_ptr<group_t>(_)));
            i++;
        } while (!(!(_->has_next())));
    }
}

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

void vlq_base128_be_t::_clean_up() {
}

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

void vlq_base128_be_t::group_t::_read() {
    m_has_next = m__io->read_bits_int_be(1);
    m_value = m__io->read_bits_int_be(7);
}

vlq_base128_be_t::group_t::~group_t() {
    _clean_up();
}

void vlq_base128_be_t::group_t::_clean_up() {
}

int32_t vlq_base128_be_t::last() {
    if (f_last)
        return m_last;
    m_last = (groups()->size() - 1);
    f_last = true;
    return m_last;
}

uint64_t vlq_base128_be_t::value() {
    if (f_value)
        return m_value;
    m_value = static_cast<uint64_t>((((((((groups()->at(last())->value() + ((last() >= 1) ? ((groups()->at((last() - 1))->value() << 7)) : (0))) + ((last() >= 2) ? ((groups()->at((last() - 2))->value() << 14)) : (0))) + ((last() >= 3) ? ((groups()->at((last() - 3))->value() << 21)) : (0))) + ((last() >= 4) ? ((groups()->at((last() - 4))->value() << 28)) : (0))) + ((last() >= 5) ? ((groups()->at((last() - 5))->value() << 35)) : (0))) + ((last() >= 6) ? ((groups()->at((last() - 6))->value() << 42)) : (0))) + ((last() >= 7) ? ((groups()->at((last() - 7))->value() << 49)) : (0))));
    f_value = true;
    return m_value;
}