Branch data Line data Source code
# 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 : 30002 : void PutFixed32(std::string* dst, uint32_t value) {
# 10 : 30002 : char buf[sizeof(value)];
# 11 : 30002 : EncodeFixed32(buf, value);
# 12 : 30002 : dst->append(buf, sizeof(buf));
# 13 : 30002 : }
# 14 : :
# 15 : 4202 : void PutFixed64(std::string* dst, uint64_t value) {
# 16 : 4202 : char buf[sizeof(value)];
# 17 : 4202 : EncodeFixed64(buf, value);
# 18 : 4202 : dst->append(buf, sizeof(buf));
# 19 : 4202 : }
# 20 : :
# 21 : 10020255 : char* EncodeVarint32(char* dst, uint32_t v) {
# 22 : : // Operate on characters as unsigneds
# 23 : 10020255 : uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
# 24 : 10020255 : static const int B = 128;
# 25 [ + + ]: 10020255 : if (v < (1 << 7)) {
# 26 : 10016501 : *(ptr++) = v;
# 27 [ + + ]: 10016501 : } else if (v < (1 << 14)) {
# 28 : 3752 : *(ptr++) = v | B;
# 29 : 3752 : *(ptr++) = v >> 7;
# 30 [ - + ]: 3752 : } else if (v < (1 << 21)) {
# 31 : 0 : *(ptr++) = v | B;
# 32 : 0 : *(ptr++) = (v >> 7) | B;
# 33 : 0 : *(ptr++) = v >> 14;
# 34 [ - + ]: 2 : } else if (v < (1 << 28)) {
# 35 : 0 : *(ptr++) = v | B;
# 36 : 0 : *(ptr++) = (v >> 7) | B;
# 37 : 0 : *(ptr++) = (v >> 14) | B;
# 38 : 0 : *(ptr++) = v >> 21;
# 39 : 2 : } else {
# 40 : 2 : *(ptr++) = v | B;
# 41 : 2 : *(ptr++) = (v >> 7) | B;
# 42 : 2 : *(ptr++) = (v >> 14) | B;
# 43 : 2 : *(ptr++) = (v >> 21) | B;
# 44 : 2 : *(ptr++) = v >> 28;
# 45 : 2 : }
# 46 : 10020255 : return reinterpret_cast<char*>(ptr);
# 47 : 10020255 : }
# 48 : :
# 49 : 1213436 : void PutVarint32(std::string* dst, uint32_t v) {
# 50 : 1213436 : char buf[5];
# 51 : 1213436 : char* ptr = EncodeVarint32(buf, v);
# 52 : 1213436 : dst->append(buf, ptr - buf);
# 53 : 1213436 : }
# 54 : :
# 55 : 25064 : char* EncodeVarint64(char* dst, uint64_t v) {
# 56 : 25064 : static const int B = 128;
# 57 : 25064 : uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
# 58 [ + + ]: 39021 : while (v >= B) {
# 59 : 13957 : *(ptr++) = v | B;
# 60 : 13957 : v >>= 7;
# 61 : 13957 : }
# 62 : 25064 : *(ptr++) = static_cast<uint8_t>(v);
# 63 : 25064 : return reinterpret_cast<char*>(ptr);
# 64 : 25064 : }
# 65 : :
# 66 : 25064 : void PutVarint64(std::string* dst, uint64_t v) {
# 67 : 25064 : char buf[10];
# 68 : 25064 : char* ptr = EncodeVarint64(buf, v);
# 69 : 25064 : dst->append(buf, ptr - buf);
# 70 : 25064 : }
# 71 : :
# 72 : 710693 : void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
# 73 : 710693 : PutVarint32(dst, value.size());
# 74 : 710693 : dst->append(value.data(), value.size());
# 75 : 710693 : }
# 76 : :
# 77 : 927510 : int VarintLength(uint64_t v) {
# 78 : 927510 : int len = 1;
# 79 [ + + ]: 929027 : while (v >= 128) {
# 80 : 1517 : v >>= 7;
# 81 : 1517 : len++;
# 82 : 1517 : }
# 83 : 927510 : return len;
# 84 : 927510 : }
# 85 : :
# 86 : : const char* GetVarint32PtrFallback(const char* p, const char* limit,
# 87 : 6938 : uint32_t* value) {
# 88 : 6938 : uint32_t result = 0;
# 89 [ + - ][ + + ]: 11433 : for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
# 90 : 8990 : uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
# 91 : 8990 : p++;
# 92 [ + + ]: 8990 : if (byte & 128) {
# 93 : : // More bytes are present
# 94 : 4495 : result |= ((byte & 127) << shift);
# 95 : 4495 : } else {
# 96 : 4495 : result |= (byte << shift);
# 97 : 4495 : *value = result;
# 98 : 4495 : return reinterpret_cast<const char*>(p);
# 99 : 4495 : }
# 100 : 8990 : }
# 101 : 6938 : return nullptr;
# 102 : 6938 : }
# 103 : :
# 104 : 898578 : bool GetVarint32(Slice* input, uint32_t* value) {
# 105 : 898578 : const char* p = input->data();
# 106 : 898578 : const char* limit = p + input->size();
# 107 : 898578 : const char* q = GetVarint32Ptr(p, limit, value);
# 108 [ + + ]: 898578 : if (q == nullptr) {
# 109 : 2443 : return false;
# 110 : 896135 : } else {
# 111 : 896135 : *input = Slice(q, limit - q);
# 112 : 896135 : return true;
# 113 : 896135 : }
# 114 : 898578 : }
# 115 : :
# 116 : 5102027 : const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
# 117 : 5102027 : uint64_t result = 0;
# 118 [ + + ][ + - ]: 12517602 : for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
# 119 : 12517601 : uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
# 120 : 12517601 : p++;
# 121 [ + + ]: 12517601 : if (byte & 128) {
# 122 : : // More bytes are present
# 123 : 7415575 : result |= ((byte & 127) << shift);
# 124 : 7415575 : } else {
# 125 : 5102026 : result |= (byte << shift);
# 126 : 5102026 : *value = result;
# 127 : 5102026 : return reinterpret_cast<const char*>(p);
# 128 : 5102026 : }
# 129 : 12517601 : }
# 130 : 5102027 : return nullptr;
# 131 : 5102027 : }
# 132 : :
# 133 : 5102027 : bool GetVarint64(Slice* input, uint64_t* value) {
# 134 : 5102027 : const char* p = input->data();
# 135 : 5102027 : const char* limit = p + input->size();
# 136 : 5102027 : const char* q = GetVarint64Ptr(p, limit, value);
# 137 [ - + ]: 5102027 : if (q == nullptr) {
# 138 : 0 : return false;
# 139 : 5102027 : } else {
# 140 : 5102027 : *input = Slice(q, limit - q);
# 141 : 5102027 : return true;
# 142 : 5102027 : }
# 143 : 5102027 : }
# 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;
# 150 [ # # ]: 0 : if (p + len > limit) return nullptr;
# 151 : 0 : *result = Slice(p, len);
# 152 : 0 : return p + len;
# 153 : 0 : }
# 154 : :
# 155 : 886593 : bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
# 156 : 886593 : uint32_t len;
# 157 [ + - ][ + - ]: 886593 : if (GetVarint32(input, &len) && input->size() >= len) {
# 158 : 886593 : *result = Slice(input->data(), len);
# 159 : 886593 : input->remove_prefix(len);
# 160 : 886593 : return true;
# 161 : 886593 : } else {
# 162 : 0 : return false;
# 163 : 0 : }
# 164 : 886593 : }
# 165 : :
# 166 : : } // namespace leveldb
|