Skip to content

Instantly share code, notes, and snippets.

@bit-hack
Created June 22, 2018 22:06
Show Gist options
  • Save bit-hack/f74367bff17c1f06cc5aeae17fd7b61e to your computer and use it in GitHub Desktop.
Save bit-hack/f74367bff17c1f06cc5aeae17fd7b61e to your computer and use it in GitHub Desktop.
variable length quantity
#pragma once
#include <stdint.h>
#include <array.h>
// read variable length quantity
uint32_t _vlq_read(const uint8_t *&out) {
uint32_t val = 0;
for (;;) {
const uint8_t c = *(out++);
val = (val << 7) | (c & 0x7f);
if ((c & 0x80) == 0) {
break;
}
}
return val;
}
// write variable length quantity
void _vlq_write(uint8_t *&out, uint32_t val) {
std::array<uint8_t, 16> data;
int32_t i = 0;
for (; val; ++i) {
data[i] = val & 0x7f;
val >>= 7;
}
do {
--i;
*(out++) = data[i] | (i ? 0x80 : 0x00);
} while (i > 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment