]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/coding.cc
add subtree-ish sources for 12.0.3
[ceph.git] / 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.
5 //
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.
9
10 #include "util/coding.h"
11
12 #include <algorithm>
13 #include "rocksdb/slice.h"
14 #include "rocksdb/slice_transform.h"
15
16 namespace rocksdb {
17
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;
22 if (v < (1 << 7)) {
23 *(ptr++) = v;
24 } else if (v < (1 << 14)) {
25 *(ptr++) = v | B;
26 *(ptr++) = v >> 7;
27 } else if (v < (1 << 21)) {
28 *(ptr++) = v | B;
29 *(ptr++) = (v >> 7) | B;
30 *(ptr++) = v >> 14;
31 } else if (v < (1 << 28)) {
32 *(ptr++) = v | B;
33 *(ptr++) = (v >> 7) | B;
34 *(ptr++) = (v >> 14) | B;
35 *(ptr++) = v >> 21;
36 } else {
37 *(ptr++) = v | B;
38 *(ptr++) = (v >> 7) | B;
39 *(ptr++) = (v >> 14) | B;
40 *(ptr++) = (v >> 21) | B;
41 *(ptr++) = v >> 28;
42 }
43 return reinterpret_cast<char*>(ptr);
44 }
45
46 const char* GetVarint32PtrFallback(const char* p, const char* limit,
47 uint32_t* value) {
48 uint32_t result = 0;
49 for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
50 uint32_t byte = *(reinterpret_cast<const unsigned char*>(p));
51 p++;
52 if (byte & 128) {
53 // More bytes are present
54 result |= ((byte & 127) << shift);
55 } else {
56 result |= (byte << shift);
57 *value = result;
58 return reinterpret_cast<const char*>(p);
59 }
60 }
61 return nullptr;
62 }
63
64 const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
65 uint64_t result = 0;
66 for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
67 uint64_t byte = *(reinterpret_cast<const unsigned char*>(p));
68 p++;
69 if (byte & 128) {
70 // More bytes are present
71 result |= ((byte & 127) << shift);
72 } else {
73 result |= (byte << shift);
74 *value = result;
75 return reinterpret_cast<const char*>(p);
76 }
77 }
78 return nullptr;
79 }
80
81 } // namespace rocksdb