| 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 |
55 |
| void EncodeFixed32(char* buf, uint32_t value) { |
| 10 |
|
| #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 11 |
55 |
| memcpy(buf, &value, sizeof(value)); |
| 12 |
|
| #else |
| 13 |
|
| buf[0] = value & 0xff; |
| 14 |
|
| buf[1] = (value >> 8) & 0xff; |
| 15 |
|
| buf[2] = (value >> 16) & 0xff; |
| 16 |
|
| buf[3] = (value >> 24) & 0xff; |
| 17 |
|
| #endif |
| 18 |
55 |
| } |
| 19 |
|
| |
| 20 |
68 |
| void EncodeFixed64(char* buf, uint64_t value) { |
| 21 |
|
| #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 22 |
68 |
| memcpy(buf, &value, sizeof(value)); |
| 23 |
|
| #else |
| 24 |
|
| buf[0] = value & 0xff; |
| 25 |
|
| buf[1] = (value >> 8) & 0xff; |
| 26 |
|
| buf[2] = (value >> 16) & 0xff; |
| 27 |
|
| buf[3] = (value >> 24) & 0xff; |
| 28 |
|
| buf[4] = (value >> 32) & 0xff; |
| 29 |
|
| buf[5] = (value >> 40) & 0xff; |
| 30 |
|
| buf[6] = (value >> 48) & 0xff; |
| 31 |
|
| buf[7] = (value >> 56) & 0xff; |
| 32 |
|
| #endif |
| 33 |
68 |
| } |
| 34 |
|
| |
| 35 |
8 |
| void PutFixed32(std::string* dst, uint32_t value) { |
| 36 |
|
| char buf[sizeof(value)]; |
| 37 |
8 |
▶ | EncodeFixed32(buf, value); |
Branch: 1 branch
| Branch | True | False | Total |
| EncodeFixed32(buf, value); |
8 |
0 |
8 |
|
| 38 |
8 |
▶ | dst->append(buf, sizeof(buf)); |
Branch: 1 branch
| Branch | True | False | Total |
| dst->append(buf, sizeof(buf)); |
8 |
0 |
8 |
|
| 39 |
8 |
| } |
| 40 |
|
| |
| 41 |
7 |
| void PutFixed64(std::string* dst, uint64_t value) { |
| 42 |
|
| char buf[sizeof(value)]; |
| 43 |
7 |
▶ | EncodeFixed64(buf, value); |
Branch: 1 branch
| Branch | True | False | Total |
| EncodeFixed64(buf, value); |
7 |
0 |
7 |
|
| 44 |
7 |
▶ | dst->append(buf, sizeof(buf)); |
Branch: 1 branch
| Branch | True | False | Total |
| dst->append(buf, sizeof(buf)); |
7 |
0 |
7 |
|
| 45 |
7 |
| } |
| 46 |
|
| |
| 47 |
177 |
| char* EncodeVarint32(char* dst, uint32_t v) { |
| 48 |
|
| // Operate on characters as unsigneds |
| 49 |
177 |
| unsigned char* ptr = reinterpret_cast<unsigned char*>(dst); |
| 50 |
|
| static const int B = 128; |
| 51 |
177 |
▶ | if (v < (1<<7)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v < (1<<7)) { |
177 |
0 |
177 |
|
| 52 |
177 |
| *(ptr++) = v; |
| 53 |
0 |
▶ | } else if (v < (1<<14)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (v < (1<<14)) { |
0 |
0 |
0 |
|
| 54 |
0 |
| *(ptr++) = v | B; |
| 55 |
0 |
| *(ptr++) = v>>7; |
| 56 |
0 |
▶ | } else if (v < (1<<21)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (v < (1<<21)) { |
0 |
0 |
0 |
|
| 57 |
0 |
| *(ptr++) = v | B; |
| 58 |
0 |
| *(ptr++) = (v>>7) | B; |
| 59 |
0 |
| *(ptr++) = v>>14; |
| 60 |
0 |
▶ | } else if (v < (1<<28)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (v < (1<<28)) { |
0 |
0 |
0 |
|
| 61 |
0 |
| *(ptr++) = v | B; |
| 62 |
0 |
| *(ptr++) = (v>>7) | B; |
| 63 |
0 |
| *(ptr++) = (v>>14) | B; |
| 64 |
0 |
| *(ptr++) = v>>21; |
| 65 |
|
| } else { |
| 66 |
0 |
| *(ptr++) = v | B; |
| 67 |
0 |
| *(ptr++) = (v>>7) | B; |
| 68 |
0 |
| *(ptr++) = (v>>14) | B; |
| 69 |
0 |
| *(ptr++) = (v>>21) | B; |
| 70 |
0 |
| *(ptr++) = v>>28; |
| 71 |
|
| } |
| 72 |
177 |
| return reinterpret_cast<char*>(ptr); |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
107 |
| void PutVarint32(std::string* dst, uint32_t v) { |
| 76 |
|
| char buf[5]; |
| 77 |
107 |
▶ | char* ptr = EncodeVarint32(buf, v); |
Branch: 1 branch
| Branch | True | False | Total |
| char* ptr = EncodeVarint32(buf, v); |
107 |
0 |
107 |
|
| 78 |
107 |
▶ | dst->append(buf, ptr - buf); |
Branch: 1 branch
| Branch | True | False | Total |
| dst->append(buf, ptr - buf); |
107 |
0 |
107 |
|
| 79 |
107 |
| } |
| 80 |
|
| |
| 81 |
26 |
| char* EncodeVarint64(char* dst, uint64_t v) { |
| 82 |
|
| static const int B = 128; |
| 83 |
26 |
| unsigned char* ptr = reinterpret_cast<unsigned char*>(dst); |
| 84 |
30 |
▶ | while (v >= B) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (v >= B) { |
4 |
26 |
30 |
|
| 85 |
4 |
| *(ptr++) = (v & (B-1)) | B; |
| 86 |
4 |
| v >>= 7; |
| 87 |
|
| } |
| 88 |
26 |
| *(ptr++) = static_cast<unsigned char>(v); |
| 89 |
26 |
| return reinterpret_cast<char*>(ptr); |
| 90 |
|
| } |
| 91 |
|
| |
| 92 |
26 |
| void PutVarint64(std::string* dst, uint64_t v) { |
| 93 |
|
| char buf[10]; |
| 94 |
26 |
▶ | char* ptr = EncodeVarint64(buf, v); |
Branch: 1 branch
| Branch | True | False | Total |
| char* ptr = EncodeVarint64(buf, v); |
26 |
0 |
26 |
|
| 95 |
26 |
▶ | dst->append(buf, ptr - buf); |
Branch: 1 branch
| Branch | True | False | Total |
| dst->append(buf, ptr - buf); |
26 |
0 |
26 |
|
| 96 |
26 |
| } |
| 97 |
|
| |
| 98 |
34 |
| void PutLengthPrefixedSlice(std::string* dst, const Slice& value) { |
| 99 |
34 |
| PutVarint32(dst, value.size()); |
| 100 |
34 |
| dst->append(value.data(), value.size()); |
| 101 |
34 |
| } |
| 102 |
|
| |
| 103 |
62 |
| int VarintLength(uint64_t v) { |
| 104 |
62 |
| int len = 1; |
| 105 |
62 |
▶ | while (v >= 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (v >= 128) { |
0 |
62 |
62 |
|
| 106 |
0 |
| v >>= 7; |
| 107 |
0 |
| len++; |
| 108 |
|
| } |
| 109 |
62 |
| return len; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
4 |
| const char* GetVarint32PtrFallback(const char* p, |
| 113 |
|
| const char* limit, |
| 114 |
|
| uint32_t* value) { |
| 115 |
4 |
| uint32_t result = 0; |
| 116 |
4 |
▶ | for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
4 |
0 |
4 |
| for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) { |
0 |
4 |
4 |
|
| 117 |
0 |
| uint32_t byte = *(reinterpret_cast<const unsigned char*>(p)); |
| 118 |
0 |
| p++; |
| 119 |
0 |
▶ | if (byte & 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (byte & 128) { |
0 |
0 |
0 |
|
| 120 |
|
| // More bytes are present |
| 121 |
0 |
| result |= ((byte & 127) << shift); |
| 122 |
|
| } else { |
| 123 |
0 |
| result |= (byte << shift); |
| 124 |
0 |
| *value = result; |
| 125 |
0 |
| return reinterpret_cast<const char*>(p); |
| 126 |
|
| } |
| 127 |
|
| } |
| 128 |
4 |
| return NULL; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
67 |
| bool GetVarint32(Slice* input, uint32_t* value) { |
| 132 |
67 |
| const char* p = input->data(); |
| 133 |
67 |
| const char* limit = p + input->size(); |
| 134 |
67 |
| const char* q = GetVarint32Ptr(p, limit, value); |
| 135 |
67 |
▶ | if (q == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (q == NULL) { |
4 |
63 |
67 |
|
| 136 |
4 |
| return false; |
| 137 |
|
| } else { |
| 138 |
63 |
| *input = Slice(q, limit - q); |
| 139 |
63 |
| return true; |
| 140 |
|
| } |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
16 |
| const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) { |
| 144 |
16 |
| uint64_t result = 0; |
| 145 |
19 |
▶ | for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
19 |
0 |
19 |
| for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) { |
19 |
0 |
19 |
|
| 146 |
19 |
| uint64_t byte = *(reinterpret_cast<const unsigned char*>(p)); |
| 147 |
19 |
| p++; |
| 148 |
19 |
▶ | if (byte & 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (byte & 128) { |
3 |
16 |
19 |
|
| 149 |
|
| // More bytes are present |
| 150 |
3 |
| result |= ((byte & 127) << shift); |
| 151 |
|
| } else { |
| 152 |
16 |
| result |= (byte << shift); |
| 153 |
16 |
| *value = result; |
| 154 |
16 |
| return reinterpret_cast<const char*>(p); |
| 155 |
|
| } |
| 156 |
|
| } |
| 157 |
0 |
| return NULL; |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
16 |
| bool GetVarint64(Slice* input, uint64_t* value) { |
| 161 |
16 |
| const char* p = input->data(); |
| 162 |
16 |
| const char* limit = p + input->size(); |
| 163 |
16 |
| const char* q = GetVarint64Ptr(p, limit, value); |
| 164 |
16 |
▶ | if (q == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (q == NULL) { |
0 |
16 |
16 |
|
| 165 |
0 |
| return false; |
| 166 |
|
| } else { |
| 167 |
16 |
| *input = Slice(q, limit - q); |
| 168 |
16 |
| return true; |
| 169 |
|
| } |
| 170 |
|
| } |
| 171 |
|
| |
| 172 |
0 |
| const char* GetLengthPrefixedSlice(const char* p, const char* limit, |
| 173 |
|
| Slice* result) { |
| 174 |
|
| uint32_t len; |
| 175 |
0 |
▶ | p = GetVarint32Ptr(p, limit, &len); |
Branch: 1 branch
| Branch | True | False | Total |
| p = GetVarint32Ptr(p, limit, &len); |
0 |
0 |
0 |
|
| 176 |
0 |
▶ | if (p == NULL) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p == NULL) return NULL; |
0 |
0 |
0 |
|
| 177 |
0 |
▶ | if (p + len > limit) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p + len > limit) return NULL; |
0 |
0 |
0 |
|
| 178 |
0 |
| *result = Slice(p, len); |
| 179 |
0 |
| return p + len; |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
50 |
| bool GetLengthPrefixedSlice(Slice* input, Slice* result) { |
| 183 |
|
| uint32_t len; |
| 184 |
100 |
▶ | if (GetVarint32(input, &len) && |
Branch: 3 branches
| Branch | True | False | Total |
| if (GetVarint32(input, &len) && |
50 |
0 |
50 |
| if (GetVarint32(input, &len) && |
50 |
0 |
50 |
| if (GetVarint32(input, &len) && |
50 |
0 |
50 |
|
| 185 |
50 |
▶ | input->size() >= len) { |
Branch: 1 branch
| Branch | True | False | Total |
| input->size() >= len) { |
50 |
0 |
50 |
|
| 186 |
50 |
| *result = Slice(input->data(), len); |
| 187 |
50 |
| input->remove_prefix(len); |
| 188 |
50 |
| return true; |
| 189 |
|
| } else { |
| 190 |
0 |
| return false; |
| 191 |
|
| } |
| 192 |
|
| } |
| 193 |
|
| |
| 194 |
|
| } |