variablebyteint.cpp
746 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "variablebyteint.h"
#include <cassert>
#include <cstring>
#include <stdexcept>
void VariableByteInt::readIntoBuf(CirBuf &buf) const
{
assert(len > 0);
buf.write(bytes, len);
}
VariableByteInt &VariableByteInt::operator=(uint32_t x)
{
if (x > 268435455)
throw std::runtime_error("Value of variable byte int to encode too big. Bug or corrupt packet?");
len = 0;
do
{
uint8_t encodedByte = x % 128;
x = x / 128;
if (x > 0)
encodedByte = encodedByte | 128;
bytes[len++] = encodedByte;
}
while(x > 0);
return *this;
}
uint8_t VariableByteInt::getLen() const
{
return len;
}
const char *VariableByteInt::data() const
{
return &bytes[0];
}