/root/bitcoin/src/leveldb/util/coding.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
4 | | |
5 | | #include "util/coding.h" |
6 | | |
7 | | namespace leveldb { |
8 | | |
9 | 0 | void PutFixed32(std::string* dst, uint32_t value) { |
10 | 0 | char buf[sizeof(value)]; |
11 | 0 | EncodeFixed32(buf, value); |
12 | 0 | dst->append(buf, sizeof(buf)); |
13 | 0 | } |
14 | | |
15 | 0 | void PutFixed64(std::string* dst, uint64_t value) { |
16 | 0 | char buf[sizeof(value)]; |
17 | 0 | EncodeFixed64(buf, value); |
18 | 0 | dst->append(buf, sizeof(buf)); |
19 | 0 | } |
20 | | |
21 | 0 | char* EncodeVarint32(char* dst, uint32_t v) { |
22 | | // Operate on characters as unsigneds |
23 | 0 | uint8_t* ptr = reinterpret_cast<uint8_t*>(dst); |
24 | 0 | static const int B = 128; |
25 | 0 | if (v < (1 << 7)) { |
26 | 0 | *(ptr++) = v; |
27 | 0 | } else if (v < (1 << 14)) { |
28 | 0 | *(ptr++) = v | B; |
29 | 0 | *(ptr++) = v >> 7; |
30 | 0 | } else if (v < (1 << 21)) { |
31 | 0 | *(ptr++) = v | B; |
32 | 0 | *(ptr++) = (v >> 7) | B; |
33 | 0 | *(ptr++) = v >> 14; |
34 | 0 | } else if (v < (1 << 28)) { |
35 | 0 | *(ptr++) = v | B; |
36 | 0 | *(ptr++) = (v >> 7) | B; |
37 | 0 | *(ptr++) = (v >> 14) | B; |
38 | 0 | *(ptr++) = v >> 21; |
39 | 0 | } else { |
40 | 0 | *(ptr++) = v | B; |
41 | 0 | *(ptr++) = (v >> 7) | B; |
42 | 0 | *(ptr++) = (v >> 14) | B; |
43 | 0 | *(ptr++) = (v >> 21) | B; |
44 | 0 | *(ptr++) = v >> 28; |
45 | 0 | } |
46 | 0 | return reinterpret_cast<char*>(ptr); |
47 | 0 | } |
48 | | |
49 | 0 | void PutVarint32(std::string* dst, uint32_t v) { |
50 | 0 | char buf[5]; |
51 | 0 | char* ptr = EncodeVarint32(buf, v); |
52 | 0 | dst->append(buf, ptr - buf); |
53 | 0 | } |
54 | | |
55 | 0 | char* EncodeVarint64(char* dst, uint64_t v) { |
56 | 0 | static const int B = 128; |
57 | 0 | uint8_t* ptr = reinterpret_cast<uint8_t*>(dst); |
58 | 0 | while (v >= B) { |
59 | 0 | *(ptr++) = v | B; |
60 | 0 | v >>= 7; |
61 | 0 | } |
62 | 0 | *(ptr++) = static_cast<uint8_t>(v); |
63 | 0 | return reinterpret_cast<char*>(ptr); |
64 | 0 | } |
65 | | |
66 | 0 | void PutVarint64(std::string* dst, uint64_t v) { |
67 | 0 | char buf[10]; |
68 | 0 | char* ptr = EncodeVarint64(buf, v); |
69 | 0 | dst->append(buf, ptr - buf); |
70 | 0 | } |
71 | | |
72 | 0 | void PutLengthPrefixedSlice(std::string* dst, const Slice& value) { |
73 | 0 | PutVarint32(dst, value.size()); |
74 | 0 | dst->append(value.data(), value.size()); |
75 | 0 | } |
76 | | |
77 | 0 | int VarintLength(uint64_t v) { |
78 | 0 | int len = 1; |
79 | 0 | while (v >= 128) { |
80 | 0 | v >>= 7; |
81 | 0 | len++; |
82 | 0 | } |
83 | 0 | return len; |
84 | 0 | } |
85 | | |
86 | | const char* GetVarint32PtrFallback(const char* p, const char* limit, |
87 | 0 | uint32_t* value) { |
88 | 0 | uint32_t result = 0; |
89 | 0 | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
90 | 0 | uint32_t byte = *(reinterpret_cast<const uint8_t*>(p)); |
91 | 0 | p++; |
92 | 0 | if (byte & 128) { |
93 | | // More bytes are present |
94 | 0 | result |= ((byte & 127) << shift); |
95 | 0 | } else { |
96 | 0 | result |= (byte << shift); |
97 | 0 | *value = result; |
98 | 0 | return reinterpret_cast<const char*>(p); |
99 | 0 | } |
100 | 0 | } |
101 | 0 | return nullptr; |
102 | 0 | } |
103 | | |
104 | 0 | bool GetVarint32(Slice* input, uint32_t* value) { |
105 | 0 | const char* p = input->data(); |
106 | 0 | const char* limit = p + input->size(); |
107 | 0 | const char* q = GetVarint32Ptr(p, limit, value); |
108 | 0 | if (q == nullptr) { |
109 | 0 | return false; |
110 | 0 | } else { |
111 | 0 | *input = Slice(q, limit - q); |
112 | 0 | return true; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | 0 | const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) { |
117 | 0 | uint64_t result = 0; |
118 | 0 | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
119 | 0 | uint64_t byte = *(reinterpret_cast<const uint8_t*>(p)); |
120 | 0 | p++; |
121 | 0 | if (byte & 128) { |
122 | | // More bytes are present |
123 | 0 | result |= ((byte & 127) << shift); |
124 | 0 | } else { |
125 | 0 | result |= (byte << shift); |
126 | 0 | *value = result; |
127 | 0 | return reinterpret_cast<const char*>(p); |
128 | 0 | } |
129 | 0 | } |
130 | 0 | return nullptr; |
131 | 0 | } |
132 | | |
133 | 0 | bool GetVarint64(Slice* input, uint64_t* value) { |
134 | 0 | const char* p = input->data(); |
135 | 0 | const char* limit = p + input->size(); |
136 | 0 | const char* q = GetVarint64Ptr(p, limit, value); |
137 | 0 | if (q == nullptr) { |
138 | 0 | return false; |
139 | 0 | } else { |
140 | 0 | *input = Slice(q, limit - q); |
141 | 0 | return true; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | | const char* GetLengthPrefixedSlice(const char* p, const char* limit, |
146 | 0 | Slice* result) { |
147 | 0 | uint32_t len; |
148 | 0 | p = GetVarint32Ptr(p, limit, &len); |
149 | 0 | if (p == nullptr) return nullptr; |
150 | 0 | if (p + len > limit) return nullptr; |
151 | 0 | *result = Slice(p, len); |
152 | 0 | return p + len; |
153 | 0 | } |
154 | | |
155 | 0 | bool GetLengthPrefixedSlice(Slice* input, Slice* result) { |
156 | 0 | uint32_t len; |
157 | 0 | if (GetVarint32(input, &len) && input->size() >= len) { |
158 | 0 | *result = Slice(input->data(), len); |
159 | 0 | input->remove_prefix(len); |
160 | 0 | return true; |
161 | 0 | } else { |
162 | 0 | return false; |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | } // namespace leveldb |