Skip to content

Instantly share code, notes, and snippets.

@adzcai
Created July 12, 2021 15:11
Show Gist options
  • Save adzcai/07f00808b302a5f04570c45edf3d0332 to your computer and use it in GitHub Desktop.
Save adzcai/07f00808b302a5f04570c45edf3d0332 to your computer and use it in GitHub Desktop.
JavaScript variable integer (vint) definition for debugging using the Kaitai Web IDE.
class Vint {
constructor(_io) {
this._io = _io;
}
_read() {
this.val = this._io.readU1();
// negative value
if ((this.val & 0x80) > 0) {
let size = 0;
// count number of leading ones
for (let track = 0x80; (this.val & track) > 0; track >>= 1)
size++;
// get val after the first zero
this.val = this.val & (0xff >> size);
for (let i = 0; i < size; ++i) {
const b = this._io.readU1();
this.val <<= 8;
this.val |= b & 0xff;
}
}
}
}
this.Vint = Vint;
class DeserializationHelper {
constructor(_io) {}
}
this.DeserializationHelper = DeserializationHelper;
module.exports = function read_vint(byte_array) {
let k = 0;
let val = byte_array[k++];
if ((val & 0x80) > 0) {
let size = 0;
// count number of leading ones
for (let track = 0x80; (val & track) > 0; track >>= 1)
size++;
// get val after the first zero
val = val & (0xff >> size);
for (let i = 0; i < size; ++i) {
const b = byte_array[k++];
val <<= 8;
val |= b & 0xff;
}
}
return [k, val];
}
this.Vint = Vint;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment