Variable length quantity, unsigned integer, base128, big-endian: Perl parsing library

A variable-length unsigned integer using base128 encoding. 1-byte groups consists of 1-bit flag of continuation and 7-bit value, 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

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

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.

Perl source code to parse Variable length quantity, unsigned integer, base128, big-endian

VlqBase128Be.pm

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

use strict;
use warnings;
use IO::KaitaiStruct 0.007_000;

########################################################################
package VlqBase128Be;

our @ISA = 'IO::KaitaiStruct::Struct';

sub from_file {
    my ($class, $filename) = @_;
    my $fd;

    open($fd, '<', $filename) or return undef;
    binmode($fd);
    return new($class, IO::KaitaiStruct::Stream->new($fd));
}

sub new {
    my ($class, $_io, $_parent, $_root) = @_;
    my $self = IO::KaitaiStruct::Struct->new($_io);

    bless $self, $class;
    $self->{_parent} = $_parent;
    $self->{_root} = $_root || $self;;

    $self->_read();

    return $self;
}

sub _read {
    my ($self) = @_;

    $self->{groups} = ();
    do {
        $_ = VlqBase128Be::Group->new($self->{_io}, $self, $self->{_root});
        push @{$self->{groups}}, $_;
    } until (!($_->has_next()));
}

sub last {
    my ($self) = @_;
    return $self->{last} if ($self->{last});
    $self->{last} = (scalar(@{$self->groups()}) - 1);
    return $self->{last};
}

sub value {
    my ($self) = @_;
    return $self->{value} if ($self->{value});
    $self->{value} = (((((((@{$self->groups()}[$self->last()]->value() + ($self->last() >= 1 ? (@{$self->groups()}[($self->last() - 1)]->value() << 7) : 0)) + ($self->last() >= 2 ? (@{$self->groups()}[($self->last() - 2)]->value() << 14) : 0)) + ($self->last() >= 3 ? (@{$self->groups()}[($self->last() - 3)]->value() << 21) : 0)) + ($self->last() >= 4 ? (@{$self->groups()}[($self->last() - 4)]->value() << 28) : 0)) + ($self->last() >= 5 ? (@{$self->groups()}[($self->last() - 5)]->value() << 35) : 0)) + ($self->last() >= 6 ? (@{$self->groups()}[($self->last() - 6)]->value() << 42) : 0)) + ($self->last() >= 7 ? (@{$self->groups()}[($self->last() - 7)]->value() << 49) : 0));
    return $self->{value};
}

sub groups {
    my ($self) = @_;
    return $self->{groups};
}

########################################################################
package VlqBase128Be::Group;

our @ISA = 'IO::KaitaiStruct::Struct';

sub from_file {
    my ($class, $filename) = @_;
    my $fd;

    open($fd, '<', $filename) or return undef;
    binmode($fd);
    return new($class, IO::KaitaiStruct::Stream->new($fd));
}

sub new {
    my ($class, $_io, $_parent, $_root) = @_;
    my $self = IO::KaitaiStruct::Struct->new($_io);

    bless $self, $class;
    $self->{_parent} = $_parent;
    $self->{_root} = $_root || $self;;

    $self->_read();

    return $self;
}

sub _read {
    my ($self) = @_;

    $self->{b} = $self->{_io}->read_u1();
}

sub has_next {
    my ($self) = @_;
    return $self->{has_next} if ($self->{has_next});
    $self->{has_next} = ($self->b() & 128) != 0;
    return $self->{has_next};
}

sub value {
    my ($self) = @_;
    return $self->{value} if ($self->{value});
    $self->{value} = ($self->b() & 127);
    return $self->{value};
}

sub b {
    my ($self) = @_;
    return $self->{b};
}

1;