/Users/deen/code/yugabyte-db/src/yb/util/coding.cc
Line | Count | Source (jump to first uncovered line) |
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 | | // The following only applies to changes made to this file as part of YugaByte development. |
6 | | // |
7 | | // Portions Copyright (c) YugaByte, Inc. |
8 | | // |
9 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
10 | | // in compliance with the License. You may obtain a copy of the License at |
11 | | // |
12 | | // http://www.apache.org/licenses/LICENSE-2.0 |
13 | | // |
14 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
15 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
16 | | // or implied. See the License for the specific language governing permissions and limitations |
17 | | // under the License. |
18 | | // |
19 | | |
20 | | #include "yb/util/coding.h" |
21 | | |
22 | | #include "yb/gutil/casts.h" |
23 | | |
24 | | #include "yb/util/coding-inl.h" |
25 | | |
26 | | namespace yb { |
27 | | |
28 | 0 | void PutVarint32(faststring* dst, uint32_t v) { |
29 | 0 | uint8_t buf[16]; |
30 | 0 | uint8_t* ptr = InlineEncodeVarint32(buf, v); |
31 | 0 | dst->append(buf, ptr - buf); |
32 | 0 | } |
33 | | |
34 | 2.53M | uint8_t* EncodeVarint64(uint8_t* dst, uint64_t v) { |
35 | 2.53M | static const int B = 128; |
36 | 2.59M | while (v >= B) { |
37 | 61.4k | *(dst++) = (v & (B-1)) | B; |
38 | 61.4k | v >>= 7; |
39 | 61.4k | } |
40 | 2.53M | *(dst++) = static_cast<uint8_t>(v); |
41 | 2.53M | return dst; |
42 | 2.53M | } |
43 | | |
44 | 153k | void PutFixed32(faststring *dst, uint32_t value) { |
45 | 153k | InlinePutFixed32(dst, value); |
46 | 153k | } |
47 | | |
48 | 0 | void PutFixed64(faststring *dst, uint64_t value) { |
49 | 0 | InlinePutFixed64(dst, value); |
50 | 0 | } |
51 | | |
52 | 0 | void PutVarint64(faststring *dst, uint64_t v) { |
53 | 0 | uint8_t buf[16]; |
54 | 0 | uint8_t* ptr = EncodeVarint64(buf, v); |
55 | 0 | dst->append(buf, ptr - buf); |
56 | 0 | } |
57 | | |
58 | 1.10M | void PutVarint64(boost::container::small_vector_base<uint8_t>* dst, uint64_t value) { |
59 | 1.10M | uint8_t buf[16]; |
60 | 1.10M | dst->insert(dst->end(), buf, EncodeVarint64(buf, value)); |
61 | 1.10M | } |
62 | | |
63 | 0 | void PutLengthPrefixedSlice(faststring* dst, const Slice& value) { |
64 | 0 | PutVarint32(dst, narrow_cast<uint32_t>(value.size())); |
65 | 0 | dst->append(value.data(), value.size()); |
66 | 0 | } |
67 | | |
68 | 0 | void PutFixed32LengthPrefixedSlice(faststring* dst, const Slice& value) { |
69 | 0 | PutFixed32(dst, narrow_cast<uint32_t>(value.size())); |
70 | 0 | dst->append(value.data(), value.size()); |
71 | 0 | } |
72 | | |
73 | 0 | int VarintLength(uint64_t v) { |
74 | 0 | int len = 1; |
75 | 0 | while (v >= 128) { |
76 | 0 | v >>= 7; |
77 | 0 | len++; |
78 | 0 | } |
79 | 0 | return len; |
80 | 0 | } |
81 | | |
82 | | const uint8_t *GetVarint32PtrFallback(const uint8_t *p, |
83 | | const uint8_t *limit, |
84 | 0 | uint32_t* value) { |
85 | 0 | uint32_t result = 0; |
86 | 0 | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
87 | 0 | uint32_t byte = *p; |
88 | 0 | p++; |
89 | 0 | if (byte & 128) { |
90 | | // More bytes are present |
91 | 0 | result |= ((byte & 127) << shift); |
92 | 0 | } else { |
93 | 0 | result |= (byte << shift); |
94 | 0 | *value = result; |
95 | 0 | return p; |
96 | 0 | } |
97 | 0 | } |
98 | 0 | return nullptr; |
99 | 0 | } |
100 | | |
101 | 0 | bool GetVarint32(Slice* input, uint32_t* value) { |
102 | 0 | const uint8_t *p = input->data(); |
103 | 0 | const uint8_t *limit = p + input->size(); |
104 | 0 | const uint8_t *q = GetVarint32Ptr(p, limit, value); |
105 | 0 | if (q == nullptr) { |
106 | 0 | return false; |
107 | 0 | } else { |
108 | 0 | *input = Slice(q, limit - q); |
109 | 0 | return true; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | 10.3k | const uint8_t *GetVarint64Ptr(const uint8_t *p, const uint8_t *limit, uint64_t* value) { |
114 | 10.3k | uint64_t result = 0; |
115 | 13.2k | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
116 | 13.2k | uint64_t byte = *p; |
117 | 13.2k | p++; |
118 | 13.2k | if (byte & 128) { |
119 | | // More bytes are present |
120 | 2.82k | result |= ((byte & 127) << shift); |
121 | 10.3k | } else { |
122 | 10.3k | result |= (byte << shift); |
123 | 10.3k | *value = result; |
124 | 10.3k | return p; |
125 | 10.3k | } |
126 | 13.2k | } |
127 | 0 | return nullptr; |
128 | 10.3k | } |
129 | | |
130 | 10.3k | bool GetVarint64(Slice* input, uint64_t* value) { |
131 | 10.3k | const uint8_t *p = input->data(); |
132 | 10.3k | const uint8_t *limit = p + input->size(); |
133 | 10.3k | const uint8_t *q = GetVarint64Ptr(p, limit, value); |
134 | 10.3k | if (q == nullptr) { |
135 | 0 | return false; |
136 | 10.3k | } else { |
137 | 10.3k | *input = Slice(q, limit - q); |
138 | 10.3k | return true; |
139 | 10.3k | } |
140 | 10.3k | } |
141 | | |
142 | | const uint8_t *GetLengthPrefixedSlice(const uint8_t *p, const uint8_t *limit, |
143 | 0 | Slice* result) { |
144 | 0 | uint32_t len = 0; |
145 | 0 | p = GetVarint32Ptr(p, limit, &len); |
146 | 0 | if (p == nullptr) return nullptr; |
147 | 0 | if (p + len > limit) return nullptr; |
148 | 0 | *result = Slice(p, len); |
149 | 0 | return p + len; |
150 | 0 | } |
151 | | |
152 | 0 | bool GetLengthPrefixedSlice(Slice* input, Slice* result) { |
153 | 0 | uint32_t len = 0; |
154 | 0 | if (GetVarint32(input, &len) && |
155 | 0 | input->size() >= len) { |
156 | 0 | *result = Slice(input->data(), len); |
157 | 0 | input->remove_prefix(len); |
158 | 0 | return true; |
159 | 0 | } else { |
160 | 0 | return false; |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | } // namespace yb |