Coverage Report

Created: 2023-01-25 06:32

/src/leveldb/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
#include "util/coding.h"
6
7
namespace leveldb {
8
9
673k
void PutFixed32(std::string* dst, uint32_t value) {
10
673k
  char buf[sizeof(value)];
11
673k
  EncodeFixed32(buf, value);
12
673k
  dst->append(buf, sizeof(buf));
13
673k
}
14
15
297k
void PutFixed64(std::string* dst, uint64_t value) {
16
297k
  char buf[sizeof(value)];
17
297k
  EncodeFixed64(buf, value);
18
297k
  dst->append(buf, sizeof(buf));
19
297k
}
20
21
19.8M
char* EncodeVarint32(char* dst, uint32_t v) {
22
  // Operate on characters as unsigneds
23
19.8M
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
24
19.8M
  static const int B = 128;
25
19.8M
  if (v < (1 << 7)) {
26
19.6M
    *(ptr++) = v;
27
19.6M
  } else if (v < (1 << 14)) {
28
195k
    *(ptr++) = v | B;
29
195k
    *(ptr++) = v >> 7;
30
195k
  } else if (v < (1 << 21)) {
31
10.1k
    *(ptr++) = v | B;
32
10.1k
    *(ptr++) = (v >> 7) | B;
33
10.1k
    *(ptr++) = v >> 14;
34
18.4E
  } 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
18.4E
  } else {
40
18.4E
    *(ptr++) = v | B;
41
18.4E
    *(ptr++) = (v >> 7) | B;
42
18.4E
    *(ptr++) = (v >> 14) | B;
43
18.4E
    *(ptr++) = (v >> 21) | B;
44
18.4E
    *(ptr++) = v >> 28;
45
18.4E
  }
46
19.8M
  return reinterpret_cast<char*>(ptr);
47
19.8M
}
48
49
12.1M
void PutVarint32(std::string* dst, uint32_t v) {
50
12.1M
  char buf[5];
51
12.1M
  char* ptr = EncodeVarint32(buf, v);
52
12.1M
  dst->append(buf, ptr - buf);
53
12.1M
}
54
55
2.59M
char* EncodeVarint64(char* dst, uint64_t v) {
56
2.59M
  static const int B = 128;
57
2.59M
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
58
3.33M
  while (v >= B) {
59
743k
    *(ptr++) = v | B;
60
743k
    v >>= 7;
61
743k
  }
62
2.59M
  *(ptr++) = static_cast<uint8_t>(v);
63
2.59M
  return reinterpret_cast<char*>(ptr);
64
2.59M
}
65
66
2.59M
void PutVarint64(std::string* dst, uint64_t v) {
67
2.59M
  char buf[10];
68
2.59M
  char* ptr = EncodeVarint64(buf, v);
69
2.59M
  dst->append(buf, ptr - buf);
70
2.59M
}
71
72
3.96M
void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
73
3.96M
  PutVarint32(dst, value.size());
74
3.96M
  dst->append(value.data(), value.size());
75
3.96M
}
76
77
7.64M
int VarintLength(uint64_t v) {
78
7.64M
  int len = 1;
79
7.66M
  while (v >= 128) {
80
22.6k
    v >>= 7;
81
22.6k
    len++;
82
22.6k
  }
83
7.64M
  return len;
84
7.64M
}
85
86
const char* GetVarint32PtrFallback(const char* p, const char* limit,
87
765k
                                   uint32_t* value) {
88
765k
  uint32_t result = 0;
89
1.28M
  for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
90
1.02M
    uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
91
1.02M
    p++;
92
1.02M
    if (byte & 128) {
93
      // More bytes are present
94
521k
      result |= ((byte & 127) << shift);
95
521k
    } else {
96
500k
      result |= (byte << shift);
97
500k
      *value = result;
98
500k
      return reinterpret_cast<const char*>(p);
99
500k
    }
100
1.02M
  }
101
264k
  return nullptr;
102
765k
}
103
104
8.23M
bool GetVarint32(Slice* input, uint32_t* value) {
105
8.23M
  const char* p = input->data();
106
8.23M
  const char* limit = p + input->size();
107
8.23M
  const char* q = GetVarint32Ptr(p, limit, value);
108
8.23M
  if (q == nullptr) {
109
264k
    return false;
110
7.96M
  } else {
111
7.96M
    *input = Slice(q, limit - q);
112
7.96M
    return true;
113
7.96M
  }
114
8.23M
}
115
116
4.97M
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
117
4.97M
  uint64_t result = 0;
118
5.90M
  for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
119
5.90M
    uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
120
5.90M
    p++;
121
5.90M
    if (byte & 128) {
122
      // More bytes are present
123
925k
      result |= ((byte & 127) << shift);
124
4.97M
    } else {
125
4.97M
      result |= (byte << shift);
126
4.97M
      *value = result;
127
4.97M
      return reinterpret_cast<const char*>(p);
128
4.97M
    }
129
5.90M
  }
130
0
  return nullptr;
131
4.97M
}
132
133
4.97M
bool GetVarint64(Slice* input, uint64_t* value) {
134
4.97M
  const char* p = input->data();
135
4.97M
  const char* limit = p + input->size();
136
4.97M
  const char* q = GetVarint64Ptr(p, limit, value);
137
4.97M
  if (q == nullptr) {
138
0
    return false;
139
4.97M
  } else {
140
4.97M
    *input = Slice(q, limit - q);
141
4.97M
    return true;
142
4.97M
  }
143
4.97M
}
144
145
5.50M
bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
146
5.50M
  uint32_t len;
147
5.50M
  if (GetVarint32(input, &len) && input->size() >= len) {
148
5.50M
    *result = Slice(input->data(), len);
149
5.50M
    input->remove_prefix(len);
150
5.50M
    return true;
151
5.50M
  } else {
152
0
    return false;
153
0
  }
154
5.50M
}
155
156
}  // namespace leveldb