]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/coding.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
10 #include "util/coding.h"
13 #include "rocksdb/slice.h"
14 #include "rocksdb/slice_transform.h"
18 char* EncodeVarint32(char* dst
, uint32_t v
) {
19 // Operate on characters as unsigneds
20 unsigned char* ptr
= reinterpret_cast<unsigned char*>(dst
);
21 static const int B
= 128;
24 } else if (v
< (1 << 14)) {
27 } else if (v
< (1 << 21)) {
29 *(ptr
++) = (v
>> 7) | B
;
31 } else if (v
< (1 << 28)) {
33 *(ptr
++) = (v
>> 7) | B
;
34 *(ptr
++) = (v
>> 14) | B
;
38 *(ptr
++) = (v
>> 7) | B
;
39 *(ptr
++) = (v
>> 14) | B
;
40 *(ptr
++) = (v
>> 21) | B
;
43 return reinterpret_cast<char*>(ptr
);
46 const char* GetVarint32PtrFallback(const char* p
, const char* limit
,
49 for (uint32_t shift
= 0; shift
<= 28 && p
< limit
; shift
+= 7) {
50 uint32_t byte
= *(reinterpret_cast<const unsigned char*>(p
));
53 // More bytes are present
54 result
|= ((byte
& 127) << shift
);
56 result
|= (byte
<< shift
);
58 return reinterpret_cast<const char*>(p
);
64 const char* GetVarint64Ptr(const char* p
, const char* limit
, uint64_t* value
) {
66 for (uint32_t shift
= 0; shift
<= 63 && p
< limit
; shift
+= 7) {
67 uint64_t byte
= *(reinterpret_cast<const unsigned char*>(p
));
70 // More bytes are present
71 result
|= ((byte
& 127) << shift
);
73 result
|= (byte
<< shift
);
75 return reinterpret_cast<const char*>(p
);
81 } // namespace rocksdb