Coverage Report

Created: 2025-07-11 07:01

/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
721k
void PutFixed32(std::string* dst, uint32_t value) {
10
721k
  char buf[sizeof(value)];
11
721k
  EncodeFixed32(buf, value);
12
721k
  dst->append(buf, sizeof(buf));
13
721k
}
14
15
348k
void PutFixed64(std::string* dst, uint64_t value) {
16
348k
  char buf[sizeof(value)];
17
348k
  EncodeFixed64(buf, value);
18
348k
  dst->append(buf, sizeof(buf));
19
348k
}
20
21
20.1M
char* EncodeVarint32(char* dst, uint32_t v) {
22
  // Operate on characters as unsigneds
23
20.1M
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
24
20.1M
  static const int B = 128;
25
20.1M
  if (v < (1 << 7)) {
26
19.6M
    *(ptr++) = v;
27
19.6M
  } else if (v < (1 << 14)) {
28
431k
    *(ptr++) = v | B;
29
431k
    *(ptr++) = v >> 7;
30
431k
  } else if (v < (1 << 21)) {
31
13.5k
    *(ptr++) = v | B;
32
13.5k
    *(ptr++) = (v >> 7) | B;
33
13.5k
    *(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
20.1M
  return reinterpret_cast<char*>(ptr);
47
20.1M
}
48
49
12.8M
void PutVarint32(std::string* dst, uint32_t v) {
50
12.8M
  char buf[5];
51
12.8M
  char* ptr = EncodeVarint32(buf, v);
52
12.8M
  dst->append(buf, ptr - buf);
53
12.8M
}
54
55
2.93M
char* EncodeVarint64(char* dst, uint64_t v) {
56
2.93M
  static const int B = 128;
57
2.93M
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
58
3.99M
  while (v >= B) {
59
1.05M
    *(ptr++) = v | B;
60
1.05M
    v >>= 7;
61
1.05M
  }
62
2.93M
  *(ptr++) = static_cast<uint8_t>(v);
63
2.93M
  return reinterpret_cast<char*>(ptr);
64
2.93M
}
65
66
2.93M
void PutVarint64(std::string* dst, uint64_t v) {
67
2.93M
  char buf[10];
68
2.93M
  char* ptr = EncodeVarint64(buf, v);
69
2.93M
  dst->append(buf, ptr - buf);
70
2.93M
}
71
72
4.08M
void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
73
4.08M
  PutVarint32(dst, value.size());
74
4.08M
  dst->append(value.data(), value.size());
75
4.08M
}
76
77
7.25M
int VarintLength(uint64_t v) {
78
7.25M
  int len = 1;
79
7.28M
  while (v >= 128) {
80
32.6k
    v >>= 7;
81
32.6k
    len++;
82
32.6k
  }
83
7.25M
  return len;
84
7.25M
}
85
86
const char* GetVarint32PtrFallback(const char* p, const char* limit,
87
1.15M
                                   uint32_t* value) {
88
1.15M
  uint32_t result = 0;
89
2.06M
  for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
90
1.78M
    uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
91
1.78M
    p++;
92
1.78M
    if (byte & 128) {
93
      // More bytes are present
94
906k
      result |= ((byte & 127) << shift);
95
906k
    } else {
96
879k
      result |= (byte << shift);
97
879k
      *value = result;
98
879k
      return reinterpret_cast<const char*>(p);
99
879k
    }
100
1.78M
  }
101
276k
  return nullptr;
102
1.15M
}
103
104
8.66M
bool GetVarint32(Slice* input, uint32_t* value) {
105
8.66M
  const char* p = input->data();
106
8.66M
  const char* limit = p + input->size();
107
8.66M
  const char* q = GetVarint32Ptr(p, limit, value);
108
8.66M
  if (q == nullptr) {
109
275k
    return false;
110
8.39M
  } else {
111
8.39M
    *input = Slice(q, limit - q);
112
8.39M
    return true;
113
8.39M
  }
114
8.66M
}
115
116
5.85M
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
117
5.85M
  uint64_t result = 0;
118
7.23M
  for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
119
7.23M
    uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
120
7.23M
    p++;
121
7.23M
    if (byte & 128) {
122
      // More bytes are present
123
1.38M
      result |= ((byte & 127) << shift);
124
5.85M
    } else {
125
5.85M
      result |= (byte << shift);
126
5.85M
      *value = result;
127
5.85M
      return reinterpret_cast<const char*>(p);
128
5.85M
    }
129
7.23M
  }
130
6
  return nullptr;
131
5.85M
}
132
133
5.85M
bool GetVarint64(Slice* input, uint64_t* value) {
134
5.85M
  const char* p = input->data();
135
5.85M
  const char* limit = p + input->size();
136
5.85M
  const char* q = GetVarint64Ptr(p, limit, value);
137
5.85M
  if (q == nullptr) {
138
0
    return false;
139
5.85M
  } else {
140
5.85M
    *input = Slice(q, limit - q);
141
5.85M
    return true;
142
5.85M
  }
143
5.85M
}
144
145
5.59M
bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
146
5.59M
  uint32_t len;
147
5.59M
  if (GetVarint32(input, &len) && input->size() >= len) {
148
5.59M
    *result = Slice(input->data(), len);
149
5.59M
    input->remove_prefix(len);
150
5.59M
    return true;
151
5.59M
  } else {
152
0
    return false;
153
0
  }
154
5.59M
}
155
156
}  // namespace leveldb