/src/rocksdb/util/coding.cc
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under both the GPLv2 (found in the |
3 | | // COPYING file in the root directory) and Apache 2.0 License |
4 | | // (found in the LICENSE.Apache file in the root 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 | | |
14 | | #include "rocksdb/slice.h" |
15 | | #include "rocksdb/slice_transform.h" |
16 | | |
17 | | namespace ROCKSDB_NAMESPACE { |
18 | | |
19 | | // conversion' conversion from 'type1' to 'type2', possible loss of data |
20 | | #if defined(_MSC_VER) |
21 | | #pragma warning(push) |
22 | | #pragma warning(disable : 4244) |
23 | | #endif |
24 | 9.96M | char* EncodeVarint32(char* dst, uint32_t v) { |
25 | | // Operate on characters as unsigneds |
26 | 9.96M | unsigned char* ptr = reinterpret_cast<unsigned char*>(dst); |
27 | 9.96M | static const int B = 128; |
28 | 9.96M | if (v < (1 << 7)) { |
29 | 9.18M | *(ptr++) = v; |
30 | 9.18M | } else if (v < (1 << 14)) { |
31 | 769k | *(ptr++) = v | B; |
32 | 769k | *(ptr++) = v >> 7; |
33 | 769k | } else if (v < (1 << 21)) { |
34 | 7.20k | *(ptr++) = v | B; |
35 | 7.20k | *(ptr++) = (v >> 7) | B; |
36 | 7.20k | *(ptr++) = v >> 14; |
37 | 18.4E | } else if (v < (1 << 28)) { |
38 | 0 | *(ptr++) = v | B; |
39 | 0 | *(ptr++) = (v >> 7) | B; |
40 | 0 | *(ptr++) = (v >> 14) | B; |
41 | 0 | *(ptr++) = v >> 21; |
42 | 18.4E | } else { |
43 | 18.4E | *(ptr++) = v | B; |
44 | 18.4E | *(ptr++) = (v >> 7) | B; |
45 | 18.4E | *(ptr++) = (v >> 14) | B; |
46 | 18.4E | *(ptr++) = (v >> 21) | B; |
47 | 18.4E | *(ptr++) = v >> 28; |
48 | 18.4E | } |
49 | 9.96M | return reinterpret_cast<char*>(ptr); |
50 | 9.96M | } |
51 | | #if defined(_MSC_VER) |
52 | | #pragma warning(pop) |
53 | | #endif |
54 | | |
55 | | const char* GetVarint32PtrFallback(const char* p, const char* limit, |
56 | 2.69M | uint32_t* value) { |
57 | 2.69M | uint32_t result = 0; |
58 | 4.94M | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
59 | 4.48M | uint32_t byte = *(reinterpret_cast<const unsigned char*>(p)); |
60 | 4.48M | p++; |
61 | 4.48M | if (byte & 128) { |
62 | | // More bytes are present |
63 | 2.25M | result |= ((byte & 127) << shift); |
64 | 2.25M | } else { |
65 | 2.23M | result |= (byte << shift); |
66 | 2.23M | *value = result; |
67 | 2.23M | return reinterpret_cast<const char*>(p); |
68 | 2.23M | } |
69 | 4.48M | } |
70 | 457k | return nullptr; |
71 | 2.69M | } |
72 | | |
73 | 5.34M | const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) { |
74 | 5.34M | uint64_t result = 0; |
75 | 7.29M | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
76 | 7.25M | uint64_t byte = *(reinterpret_cast<const unsigned char*>(p)); |
77 | 7.25M | p++; |
78 | 7.25M | if (byte & 128) { |
79 | | // More bytes are present |
80 | 1.95M | result |= ((byte & 127) << shift); |
81 | 5.30M | } else { |
82 | 5.30M | result |= (byte << shift); |
83 | 5.30M | *value = result; |
84 | 5.30M | return reinterpret_cast<const char*>(p); |
85 | 5.30M | } |
86 | 7.25M | } |
87 | 39.4k | return nullptr; |
88 | 5.34M | } |
89 | | |
90 | | } // namespace ROCKSDB_NAMESPACE |