blob: 051cf17380c54c9d4a8a6b89e77878366539cda6 (
plain) (
blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#include "varint.h"
#include <util/generic/yexception.h>
#include <util/stream/input.h>
#include <util/stream/output.h>
namespace NMonitoring {
ui32 WriteVarUInt32(IOutputStream* output, ui32 value) {
bool stop = false;
ui32 written = 0;
while (!stop) {
ui8 byte = static_cast<ui8>(value | 0x80);
value >>= 7;
if (value == 0) {
stop = true;
byte &= 0x7F;
}
output->Write(byte);
written++;
}
return written;
}
ui32 ReadVarUInt32(IInputStream* input) {
ui32 value = 0;
switch (TryReadVarUInt32(input, &value)) {
case EReadResult::OK:
return value;
case EReadResult::ERR_OVERFLOW:
ythrow yexception() << "the data is too long to read ui32";
case EReadResult::ERR_UNEXPECTED_EOF:
ythrow yexception() << "the data unexpectedly ended";
default:
ythrow yexception() << "unknown error while reading varint";
}
}
size_t ReadVarUInt32(const ui8* buf, size_t len, ui32* result) {
size_t count = 0;
ui32 value = 0;
ui8 byte = 0;
do {
if (7 * count > 8 * sizeof(ui32)) {
ythrow yexception() << "the data is too long to read ui32";
}
if (count == len) {
ythrow yexception() << "the data unexpectedly ended";
}
byte = buf[count];
value |= (static_cast<ui32>(byte & 0x7F)) << (7 * count);
++count;
} while (byte & 0x80);
*result = value;
return count;
}
EReadResult TryReadVarUInt32(IInputStream* input, ui32* value) {
size_t count = 0;
ui32 result = 0;
ui8 byte = 0;
do {
if (7 * count > 8 * sizeof(ui32)) {
return EReadResult::ERR_OVERFLOW;
}
if (input->Read(&byte, 1) != 1) {
return EReadResult::ERR_UNEXPECTED_EOF;
}
result |= (static_cast<ui32>(byte & 0x7F)) << (7 * count);
++count;
} while (byte & 0x80);
*value = result;
return EReadResult::OK;
}
}
|