Coverage Report

Created: 2026-08-31 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/leveldb/util/coding.cc
Line
Count
Source
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
1.30k
void PutFixed32(std::string* dst, uint32_t value) {
10
1.30k
  char buf[sizeof(value)];
11
1.30k
  EncodeFixed32(buf, value);
12
1.30k
  dst->append(buf, sizeof(buf));
13
1.30k
}
14
15
487
void PutFixed64(std::string* dst, uint64_t value) {
16
487
  char buf[sizeof(value)];
17
487
  EncodeFixed64(buf, value);
18
487
  dst->append(buf, sizeof(buf));
19
487
}
20
21
6.38k
char* EncodeVarint32(char* dst, uint32_t v) {
22
  // Operate on characters as unsigneds
23
6.38k
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
24
6.38k
  static const int B = 128;
25
6.38k
  if (v < (1 << 7)) {
26
3.97k
    *(ptr++) = v;
27
3.97k
  } else if (v < (1 << 14)) {
28
2.17k
    *(ptr++) = v | B;
29
2.17k
    *(ptr++) = v >> 7;
30
2.17k
  } else if (v < (1 << 21)) {
31
237
    *(ptr++) = v | B;
32
237
    *(ptr++) = (v >> 7) | B;
33
237
    *(ptr++) = v >> 14;
34
237
  } 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
0
  } else {
40
0
    *(ptr++) = v | B;
41
0
    *(ptr++) = (v >> 7) | B;
42
0
    *(ptr++) = (v >> 14) | B;
43
0
    *(ptr++) = (v >> 21) | B;
44
0
    *(ptr++) = v >> 28;
45
0
  }
46
6.38k
  return reinterpret_cast<char*>(ptr);
47
6.38k
}
48
49
5.26k
void PutVarint32(std::string* dst, uint32_t v) {
50
5.26k
  char buf[5];
51
5.26k
  char* ptr = EncodeVarint32(buf, v);
52
5.26k
  dst->append(buf, ptr - buf);
53
5.26k
}
54
55
1.85k
char* EncodeVarint64(char* dst, uint64_t v) {
56
1.85k
  static const int B = 128;
57
1.85k
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
58
3.10k
  while (v >= B) {
59
1.25k
    *(ptr++) = v | B;
60
1.25k
    v >>= 7;
61
1.25k
  }
62
1.85k
  *(ptr++) = static_cast<uint8_t>(v);
63
1.85k
  return reinterpret_cast<char*>(ptr);
64
1.85k
}
65
66
1.85k
void PutVarint64(std::string* dst, uint64_t v) {
67
1.85k
  char buf[10];
68
1.85k
  char* ptr = EncodeVarint64(buf, v);
69
1.85k
  dst->append(buf, ptr - buf);
70
1.85k
}
71
72
1.29k
void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
73
1.29k
  PutVarint32(dst, value.size());
74
1.29k
  dst->append(value.data(), value.size());
75
1.29k
}
76
77
1.09k
int VarintLength(uint64_t v) {
78
1.09k
  int len = 1;
79
1.89k
  while (v >= 128) {
80
794
    v >>= 7;
81
794
    len++;
82
794
  }
83
1.09k
  return len;
84
1.09k
}
85
86
const char* GetVarint32PtrFallback(const char* p, const char* limit,
87
5.46k
                                   uint32_t* value) {
88
5.46k
  uint32_t result = 0;
89
11.2k
  for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
90
11.0k
    uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
91
11.0k
    p++;
92
11.0k
    if (byte & 128) {
93
      // More bytes are present
94
5.74k
      result |= ((byte & 127) << shift);
95
5.74k
    } else {
96
5.27k
      result |= (byte << shift);
97
5.27k
      *value = result;
98
5.27k
      return reinterpret_cast<const char*>(p);
99
5.27k
    }
100
11.0k
  }
101
188
  return nullptr;
102
5.46k
}
103
104
2.45k
bool GetVarint32(Slice* input, uint32_t* value) {
105
2.45k
  const char* p = input->data();
106
2.45k
  const char* limit = p + input->size();
107
2.45k
  const char* q = GetVarint32Ptr(p, limit, value);
108
2.45k
  if (q == nullptr) {
109
188
    return false;
110
2.26k
  } else {
111
2.26k
    *input = Slice(q, limit - q);
112
2.26k
    return true;
113
2.26k
  }
114
2.45k
}
115
116
1.60k
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
117
1.60k
  uint64_t result = 0;
118
2.77k
  for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
119
2.77k
    uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
120
2.77k
    p++;
121
2.77k
    if (byte & 128) {
122
      // More bytes are present
123
1.16k
      result |= ((byte & 127) << shift);
124
1.60k
    } else {
125
1.60k
      result |= (byte << shift);
126
1.60k
      *value = result;
127
1.60k
      return reinterpret_cast<const char*>(p);
128
1.60k
    }
129
2.77k
  }
130
0
  return nullptr;
131
1.60k
}
132
133
1.60k
bool GetVarint64(Slice* input, uint64_t* value) {
134
1.60k
  const char* p = input->data();
135
1.60k
  const char* limit = p + input->size();
136
1.60k
  const char* q = GetVarint64Ptr(p, limit, value);
137
1.60k
  if (q == nullptr) {
138
0
    return false;
139
1.60k
  } else {
140
1.60k
    *input = Slice(q, limit - q);
141
1.60k
    return true;
142
1.60k
  }
143
1.60k
}
144
145
1.41k
bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
146
1.41k
  uint32_t len;
147
1.41k
  if (GetVarint32(input, &len) && input->size() >= len) {
148
1.41k
    *result = Slice(input->data(), len);
149
1.41k
    input->remove_prefix(len);
150
1.41k
    return true;
151
1.41k
  } else {
152
0
    return false;
153
0
  }
154
1.41k
}
155
156
}  // namespace leveldb