8
8
// found in the LICENSE file. See the AUTHORS file for names of contributors.
9
9
10
10
#include " util/coding.h"
11
-
12
11
#include < algorithm>
13
12
14
13
namespace rocksdb {
15
14
16
- void EncodeFixed32 (char * buf, uint32_t value) {
17
- #if __BYTE_ORDER == __LITTLE_ENDIAN
18
- memcpy (buf, &value, sizeof (value));
19
- #else
20
- buf[0 ] = value & 0xff ;
21
- buf[1 ] = (value >> 8 ) & 0xff ;
22
- buf[2 ] = (value >> 16 ) & 0xff ;
23
- buf[3 ] = (value >> 24 ) & 0xff ;
24
- #endif
25
- }
26
-
27
- void EncodeFixed64 (char * buf, uint64_t value) {
28
- #if __BYTE_ORDER == __LITTLE_ENDIAN
29
- memcpy (buf, &value, sizeof (value));
30
- #else
31
- buf[0 ] = value & 0xff ;
32
- buf[1 ] = (value >> 8 ) & 0xff ;
33
- buf[2 ] = (value >> 16 ) & 0xff ;
34
- buf[3 ] = (value >> 24 ) & 0xff ;
35
- buf[4 ] = (value >> 32 ) & 0xff ;
36
- buf[5 ] = (value >> 40 ) & 0xff ;
37
- buf[6 ] = (value >> 48 ) & 0xff ;
38
- buf[7 ] = (value >> 56 ) & 0xff ;
39
- #endif
40
- }
41
-
42
- void PutFixed32 (std::string* dst, uint32_t value) {
43
- char buf[sizeof (value)];
44
- EncodeFixed32 (buf, value);
45
- dst->append (buf, sizeof (buf));
46
- }
47
-
48
- void PutFixed64 (std::string* dst, uint64_t value) {
49
- char buf[sizeof (value)];
50
- EncodeFixed64 (buf, value);
51
- dst->append (buf, sizeof (buf));
52
- }
53
-
54
15
char * EncodeVarint32 (char * dst, uint32_t v) {
55
16
// Operate on characters as unsigneds
56
17
unsigned char * ptr = reinterpret_cast <unsigned char *>(dst);
57
18
static const int B = 128 ;
58
- if (v < (1 << 7 )) {
19
+ if (v < (1 << 7 )) {
59
20
*(ptr++) = v;
60
- } else if (v < (1 << 14 )) {
21
+ } else if (v < (1 << 14 )) {
61
22
*(ptr++) = v | B;
62
- *(ptr++) = v>> 7 ;
63
- } else if (v < (1 << 21 )) {
23
+ *(ptr++) = v >> 7 ;
24
+ } else if (v < (1 << 21 )) {
64
25
*(ptr++) = v | B;
65
- *(ptr++) = (v>> 7 ) | B;
66
- *(ptr++) = v>> 14 ;
67
- } else if (v < (1 << 28 )) {
26
+ *(ptr++) = (v >> 7 ) | B;
27
+ *(ptr++) = v >> 14 ;
28
+ } else if (v < (1 << 28 )) {
68
29
*(ptr++) = v | B;
69
- *(ptr++) = (v>> 7 ) | B;
70
- *(ptr++) = (v>> 14 ) | B;
71
- *(ptr++) = v>> 21 ;
30
+ *(ptr++) = (v >> 7 ) | B;
31
+ *(ptr++) = (v >> 14 ) | B;
32
+ *(ptr++) = v >> 21 ;
72
33
} else {
73
34
*(ptr++) = v | B;
74
- *(ptr++) = (v>>7 ) | B;
75
- *(ptr++) = (v>>14 ) | B;
76
- *(ptr++) = (v>>21 ) | B;
77
- *(ptr++) = v>>28 ;
78
- }
79
- return reinterpret_cast <char *>(ptr);
80
- }
81
-
82
- void PutVarint32 (std::string* dst, uint32_t v) {
83
- char buf[5 ];
84
- char * ptr = EncodeVarint32 (buf, v);
85
- dst->append (buf, ptr - buf);
86
- }
87
-
88
- char * EncodeVarint64 (char * dst, uint64_t v) {
89
- static const unsigned int B = 128 ;
90
- unsigned char * ptr = reinterpret_cast <unsigned char *>(dst);
91
- while (v >= B) {
92
- *(ptr++) = (v & (B-1 )) | B;
93
- v >>= 7 ;
35
+ *(ptr++) = (v >> 7 ) | B;
36
+ *(ptr++) = (v >> 14 ) | B;
37
+ *(ptr++) = (v >> 21 ) | B;
38
+ *(ptr++) = v >> 28 ;
94
39
}
95
- *(ptr++) = static_cast <unsigned char >(v);
96
40
return reinterpret_cast <char *>(ptr);
97
41
}
98
42
99
- void PutVarint64 (std::string* dst, uint64_t v) {
100
- char buf[10 ];
101
- char * ptr = EncodeVarint64 (buf, v);
102
- dst->append (buf, ptr - buf);
103
- }
104
-
105
- void PutLengthPrefixedSlice (std::string* dst, const Slice& value) {
106
- PutVarint32 (dst, value.size ());
107
- dst->append (value.data (), value.size ());
108
- }
109
-
110
- void PutLengthPrefixedSliceParts (std::string* dst,
111
- const SliceParts& slice_parts) {
112
- uint32_t total_bytes = 0 ;
113
- for (int i = 0 ; i < slice_parts.num_parts ; ++i) {
114
- total_bytes += slice_parts.parts [i].size ();
115
- }
116
- PutVarint32 (dst, total_bytes);
117
- for (int i = 0 ; i < slice_parts.num_parts ; ++i) {
118
- dst->append (slice_parts.parts [i].data (), slice_parts.parts [i].size ());
119
- }
120
- }
121
-
122
- int VarintLength (uint64_t v) {
123
- int len = 1 ;
124
- while (v >= 128 ) {
125
- v >>= 7 ;
126
- len++;
127
- }
128
- return len;
129
- }
130
-
131
- const char * GetVarint32PtrFallback (const char * p,
132
- const char * limit,
43
+ const char * GetVarint32PtrFallback (const char * p, const char * limit,
133
44
uint32_t * value) {
134
45
uint32_t result = 0 ;
135
46
for (uint32_t shift = 0 ; shift <= 28 && p < limit; shift += 7 ) {
@@ -147,18 +58,6 @@ const char* GetVarint32PtrFallback(const char* p,
147
58
return nullptr ;
148
59
}
149
60
150
- bool GetVarint32 (Slice* input, uint32_t * value) {
151
- const char * p = input->data ();
152
- const char * limit = p + input->size ();
153
- const char * q = GetVarint32Ptr (p, limit, value);
154
- if (q == nullptr ) {
155
- return false ;
156
- } else {
157
- *input = Slice (q, limit - q);
158
- return true ;
159
- }
160
- }
161
-
162
61
const char * GetVarint64Ptr (const char * p, const char * limit, uint64_t * value) {
163
62
uint64_t result = 0 ;
164
63
for (uint32_t shift = 0 ; shift <= 63 && p < limit; shift += 7 ) {
@@ -176,58 +75,6 @@ const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
176
75
return nullptr ;
177
76
}
178
77
179
- bool GetVarint64 (Slice* input, uint64_t * value) {
180
- const char * p = input->data ();
181
- const char * limit = p + input->size ();
182
- const char * q = GetVarint64Ptr (p, limit, value);
183
- if (q == nullptr ) {
184
- return false ;
185
- } else {
186
- *input = Slice (q, limit - q);
187
- return true ;
188
- }
189
- }
190
-
191
- const char * GetLengthPrefixedSlice (const char * p, const char * limit,
192
- Slice* result) {
193
- uint32_t len;
194
- p = GetVarint32Ptr (p, limit, &len);
195
- if (p == nullptr ) return nullptr ;
196
- if (p + len > limit) return nullptr ;
197
- *result = Slice (p, len);
198
- return p + len;
199
- }
200
-
201
- bool GetLengthPrefixedSlice (Slice* input, Slice* result) {
202
- uint32_t len;
203
- if (GetVarint32 (input, &len) &&
204
- input->size () >= len) {
205
- *result = Slice (input->data (), len);
206
- input->remove_prefix (len);
207
- return true ;
208
- } else {
209
- return false ;
210
- }
211
- }
212
-
213
- Slice GetLengthPrefixedSlice (const char * data) {
214
- uint32_t len;
215
- const char * p = data;
216
- p = GetVarint32Ptr (p, p + 5 , &len); // +5: we assume "p" is not corrupted
217
- return Slice (p, len);
218
- }
219
-
220
- Slice GetSliceUntil (Slice* slice, char delimiter) {
221
- uint32_t len;
222
- for (len = 0 ; len < slice->size () && slice->data ()[len] != delimiter; ++len) {
223
- // nothing
224
- }
225
-
226
- Slice ret (slice->data (), len);
227
- slice->remove_prefix (len + ((len < slice->size ()) ? 1 : 0 ));
228
- return ret;
229
- }
230
-
231
78
void BitStreamPutInt (char * dst, size_t dstlen, size_t offset,
232
79
uint32_t bits, uint64_t value) {
233
80
assert ((offset + bits + 7 )/8 <= dstlen);
@@ -316,14 +163,4 @@ void BitStreamPutInt(std::string* dst, size_t offset, uint32_t bits,
316
163
BitStreamGetInt (dst, offset, bits));
317
164
}
318
165
319
- uint64_t BitStreamGetInt (const std::string* src, size_t offset,
320
- uint32_t bits) {
321
- return BitStreamGetInt (src->data (), src->size (), offset, bits);
322
- }
323
-
324
- uint64_t BitStreamGetInt (const Slice* src, size_t offset,
325
- uint32_t bits) {
326
- return BitStreamGetInt (src->data (), src->size (), offset, bits);
327
- }
328
-
329
166
} // namespace rocksdb
0 commit comments