-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvariablebyteint.cpp
51 lines (40 loc) · 1009 Bytes
/
variablebyteint.cpp
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
42
43
44
45
46
47
48
49
50
51
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#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];
}