-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcoding.cpp
78 lines (73 loc) · 1.61 KB
/
coding.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
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
#include "coding.h"
using namespace std;
namespace aliyun_log_sdk_v6
{
char* EncodeVarint32(char* dst, uint32_t v)
{
// Operate on characters as unsigneds
unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
static const int B = 128;
if (v < (1 << 7))
{
*(ptr++) = v;
}
else if (v < (1 << 14))
{
*(ptr++) = v | B;
*(ptr++) = v >> 7;
}
else if (v < (1 << 21))
{
*(ptr++) = v | B;
*(ptr++) = (v >> 7) | B;
*(ptr++) = v >> 14;
}
else if (v < (1 << 28))
{
*(ptr++) = v | B;
*(ptr++) = (v >> 7) | B;
*(ptr++) = (v >> 14) | B;
*(ptr++) = v >> 21;
}
else
{
*(ptr++) = v | B;
*(ptr++) = (v >> 7) | B;
*(ptr++) = (v >> 14) | B;
*(ptr++) = (v >> 21) | B;
*(ptr++) = v >> 28;
}
return reinterpret_cast<char*>(ptr);
}
const char* SkipProtobufField(const char* pos, const char* limit, uint32_t wireType)
{
if (pos > limit)
return nullptr;
switch (wireType)
{
case 0: // Varint
{
uint32_t v;
return GetVarint32Ptr(pos, limit, &v);
}
case 1: // fixed 64bit
{
return pos + 8 > limit ? nullptr : pos + 8;
}
case 2: // dynamic length
{
uint32_t len;
pos = GetVarint32Ptr(pos, limit, &len);
if (pos == nullptr || pos + len > limit)
return nullptr;
return pos + len;
}
case 5: // fixed 32bit
{
return pos + 4 > limit ? nullptr : pos + 4;
}
default:
return nullptr;
}
}
} // namespace aliyun_log_sdk_v6