/bitcoin/src/leveldb/util/coding.cc
Line | Count | Source |
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 | 672 | void PutFixed32(std::string* dst, uint32_t value) { |
10 | 672 | char buf[sizeof(value)]; |
11 | 672 | EncodeFixed32(buf, value); |
12 | 672 | dst->append(buf, sizeof(buf)); |
13 | 672 | } |
14 | | |
15 | 24.9k | void PutFixed64(std::string* dst, uint64_t value) { |
16 | 24.9k | char buf[sizeof(value)]; |
17 | 24.9k | EncodeFixed64(buf, value); |
18 | 24.9k | dst->append(buf, sizeof(buf)); |
19 | 24.9k | } |
20 | | |
21 | 251M | char* EncodeVarint32(char* dst, uint32_t v) { |
22 | | // Operate on characters as unsigneds |
23 | 251M | uint8_t* ptr = reinterpret_cast<uint8_t*>(dst); |
24 | 251M | static const int B = 128; |
25 | 251M | if (v < (1 << 7)) { Branch (25:7): [True: 251M, False: 310k]
|
26 | 251M | *(ptr++) = v; |
27 | 251M | } else if (v < (1 << 14)) { Branch (27:14): [True: 310k, False: 0]
|
28 | 310k | *(ptr++) = v | B; |
29 | 310k | *(ptr++) = v >> 7; |
30 | 310k | } else if (v < (1 << 21)) { Branch (30:14): [True: 0, False: 0]
|
31 | 0 | *(ptr++) = v | B; |
32 | 0 | *(ptr++) = (v >> 7) | B; |
33 | 0 | *(ptr++) = v >> 14; |
34 | 0 | } else if (v < (1 << 28)) { Branch (34:14): [True: 0, False: 0]
|
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 | 251M | return reinterpret_cast<char*>(ptr); |
47 | 251M | } |
48 | | |
49 | 124M | void PutVarint32(std::string* dst, uint32_t v) { |
50 | 124M | char buf[5]; |
51 | 124M | char* ptr = EncodeVarint32(buf, v); |
52 | 124M | dst->append(buf, ptr - buf); |
53 | 124M | } |
54 | | |
55 | 919 | char* EncodeVarint64(char* dst, uint64_t v) { |
56 | 919 | static const int B = 128; |
57 | 919 | uint8_t* ptr = reinterpret_cast<uint8_t*>(dst); |
58 | 1.25k | while (v >= B) { Branch (58:10): [True: 336, False: 919]
|
59 | 336 | *(ptr++) = v | B; |
60 | 336 | v >>= 7; |
61 | 336 | } |
62 | 919 | *(ptr++) = static_cast<uint8_t>(v); |
63 | 919 | return reinterpret_cast<char*>(ptr); |
64 | 919 | } |
65 | | |
66 | 919 | void PutVarint64(std::string* dst, uint64_t v) { |
67 | 919 | char buf[10]; |
68 | 919 | char* ptr = EncodeVarint64(buf, v); |
69 | 919 | dst->append(buf, ptr - buf); |
70 | 919 | } |
71 | | |
72 | 124M | void PutLengthPrefixedSlice(std::string* dst, const Slice& value) { |
73 | 124M | PutVarint32(dst, value.size()); |
74 | 124M | dst->append(value.data(), value.size()); |
75 | 124M | } |
76 | | |
77 | 124M | int VarintLength(uint64_t v) { |
78 | 124M | int len = 1; |
79 | 124M | while (v >= 128) { Branch (79:10): [True: 155k, False: 124M]
|
80 | 155k | v >>= 7; |
81 | 155k | len++; |
82 | 155k | } |
83 | 124M | return len; |
84 | 124M | } |
85 | | |
86 | | const char* GetVarint32PtrFallback(const char* p, const char* limit, |
87 | 155k | uint32_t* value) { |
88 | 155k | uint32_t result = 0; |
89 | 310k | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { Branch (89:28): [True: 310k, False: 0]
Branch (89:43): [True: 310k, False: 81]
|
90 | 310k | uint32_t byte = *(reinterpret_cast<const uint8_t*>(p)); |
91 | 310k | p++; |
92 | 310k | if (byte & 128) { Branch (92:9): [True: 155k, False: 155k]
|
93 | | // More bytes are present |
94 | 155k | result |= ((byte & 127) << shift); |
95 | 155k | } else { |
96 | 155k | result |= (byte << shift); |
97 | 155k | *value = result; |
98 | 155k | return reinterpret_cast<const char*>(p); |
99 | 155k | } |
100 | 310k | } |
101 | 81 | return nullptr; |
102 | 155k | } |
103 | | |
104 | 124M | bool GetVarint32(Slice* input, uint32_t* value) { |
105 | 124M | const char* p = input->data(); |
106 | 124M | const char* limit = p + input->size(); |
107 | 124M | const char* q = GetVarint32Ptr(p, limit, value); |
108 | 124M | if (q == nullptr) { Branch (108:7): [True: 81, False: 124M]
|
109 | 81 | return false; |
110 | 124M | } else { |
111 | 124M | *input = Slice(q, limit - q); |
112 | 124M | return true; |
113 | 124M | } |
114 | 124M | } |
115 | | |
116 | 1.19k | const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) { |
117 | 1.19k | uint64_t result = 0; |
118 | 2.03k | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { Branch (118:28): [True: 2.03k, False: 0]
Branch (118:43): [True: 2.03k, False: 0]
|
119 | 2.03k | uint64_t byte = *(reinterpret_cast<const uint8_t*>(p)); |
120 | 2.03k | p++; |
121 | 2.03k | if (byte & 128) { Branch (121:9): [True: 843, False: 1.19k]
|
122 | | // More bytes are present |
123 | 843 | result |= ((byte & 127) << shift); |
124 | 1.19k | } else { |
125 | 1.19k | result |= (byte << shift); |
126 | 1.19k | *value = result; |
127 | 1.19k | return reinterpret_cast<const char*>(p); |
128 | 1.19k | } |
129 | 2.03k | } |
130 | 0 | return nullptr; |
131 | 1.19k | } |
132 | | |
133 | 1.19k | bool GetVarint64(Slice* input, uint64_t* value) { |
134 | 1.19k | const char* p = input->data(); |
135 | 1.19k | const char* limit = p + input->size(); |
136 | 1.19k | const char* q = GetVarint64Ptr(p, limit, value); |
137 | 1.19k | if (q == nullptr) { Branch (137:7): [True: 0, False: 1.19k]
|
138 | 0 | return false; |
139 | 1.19k | } else { |
140 | 1.19k | *input = Slice(q, limit - q); |
141 | 1.19k | return true; |
142 | 1.19k | } |
143 | 1.19k | } |
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; Branch (149:7): [True: 0, False: 0]
|
150 | 0 | if (p + len > limit) return nullptr; Branch (150:7): [True: 0, False: 0]
|
151 | 0 | *result = Slice(p, len); |
152 | 0 | return p + len; |
153 | 0 | } |
154 | | |
155 | 124M | bool GetLengthPrefixedSlice(Slice* input, Slice* result) { |
156 | 124M | uint32_t len; |
157 | 124M | if (GetVarint32(input, &len) && input->size() >= len) { Branch (157:7): [True: 124M, False: 0]
Branch (157:35): [True: 124M, False: 0]
|
158 | 124M | *result = Slice(input->data(), len); |
159 | 124M | input->remove_prefix(len); |
160 | 124M | return true; |
161 | 124M | } else { |
162 | 0 | return false; |
163 | 0 | } |
164 | 124M | } |
165 | | |
166 | | } // namespace leveldb |