Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/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
3.77k
void PutFixed32(std::string* dst, uint32_t value) {
10
3.77k
  char buf[sizeof(value)];
11
3.77k
  EncodeFixed32(buf, value);
12
3.77k
  dst->append(buf, sizeof(buf));
13
3.77k
}
14
15
24.9k
void PutFixed64(std::string* dst, uint64_t value) {
16
24.9k
  char buf[sizeof(value)];
17
24.9k
  EncodeFixed64(buf, value);
18
24.9k
  dst->append(buf, sizeof(buf));
19
24.9k
}
20
21
33.1M
char* EncodeVarint32(char* dst, uint32_t v) {
22
  // Operate on characters as unsigneds
23
33.1M
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
24
33.1M
  static const int B = 128;
25
33.1M
  if (v < (1 << 7)) {
  Branch (25:7): [True: 33.1M, False: 44.6k]
26
33.1M
    *(ptr++) = v;
27
33.1M
  } else if (v < (1 << 14)) {
  Branch (27:14): [True: 44.6k, False: 18.4E]
28
44.6k
    *(ptr++) = v | B;
29
44.6k
    *(ptr++) = v >> 7;
30
18.4E
  } else if (v < (1 << 21)) {
  Branch (30:14): [True: 0, False: 18.4E]
31
0
    *(ptr++) = v | B;
32
0
    *(ptr++) = (v >> 7) | B;
33
0
    *(ptr++) = v >> 14;
34
18.4E
  } else if (v < (1 << 28)) {
  Branch (34:14): [True: 0, False: 18.4E]
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
33.1M
  return reinterpret_cast<char*>(ptr);
47
33.1M
}
48
49
14.2M
void PutVarint32(std::string* dst, uint32_t v) {
50
14.2M
  char buf[5];
51
14.2M
  char* ptr = EncodeVarint32(buf, v);
52
14.2M
  dst->append(buf, ptr - buf);
53
14.2M
}
54
55
234k
char* EncodeVarint64(char* dst, uint64_t v) {
56
234k
  static const int B = 128;
57
234k
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
58
236k
  while (v >= B) {
  Branch (58:10): [True: 2.01k, False: 234k]
59
2.01k
    *(ptr++) = v | B;
60
2.01k
    v >>= 7;
61
2.01k
  }
62
234k
  *(ptr++) = static_cast<uint8_t>(v);
63
234k
  return reinterpret_cast<char*>(ptr);
64
234k
}
65
66
234k
void PutVarint64(std::string* dst, uint64_t v) {
67
234k
  char buf[10];
68
234k
  char* ptr = EncodeVarint64(buf, v);
69
234k
  dst->append(buf, ptr - buf);
70
234k
}
71
72
13.8M
void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
73
13.8M
  PutVarint32(dst, value.size());
74
13.8M
  dst->append(value.data(), value.size());
75
13.8M
}
76
77
13.8M
int VarintLength(uint64_t v) {
78
13.8M
  int len = 1;
79
13.8M
  while (v >= 128) {
  Branch (79:10): [True: 20.6k, False: 13.8M]
80
20.6k
    v >>= 7;
81
20.6k
    len++;
82
20.6k
  }
83
13.8M
  return len;
84
13.8M
}
85
86
const char* GetVarint32PtrFallback(const char* p, const char* limit,
87
57.2k
                                   uint32_t* value) {
88
57.2k
  uint32_t result = 0;
89
81.2k
  for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
  Branch (89:28): [True: 81.2k, False: 0]
  Branch (89:43): [True: 47.9k, False: 33.2k]
90
47.9k
    uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
91
47.9k
    p++;
92
47.9k
    if (byte & 128) {
  Branch (92:9): [True: 23.9k, False: 23.9k]
93
      // More bytes are present
94
23.9k
      result |= ((byte & 127) << shift);
95
23.9k
    } else {
96
23.9k
      result |= (byte << shift);
97
23.9k
      *value = result;
98
23.9k
      return reinterpret_cast<const char*>(p);
99
23.9k
    }
100
47.9k
  }
101
33.2k
  return nullptr;
102
57.2k
}
103
104
14.0M
bool GetVarint32(Slice* input, uint32_t* value) {
105
14.0M
  const char* p = input->data();
106
14.0M
  const char* limit = p + input->size();
107
14.0M
  const char* q = GetVarint32Ptr(p, limit, value);
108
14.0M
  if (q == nullptr) {
  Branch (108:7): [True: 33.2k, False: 13.9M]
109
33.2k
    return false;
110
13.9M
  } else {
111
13.9M
    *input = Slice(q, limit - q);
112
13.9M
    return true;
113
13.9M
  }
114
14.0M
}
115
116
100k
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
117
100k
  uint64_t result = 0;
118
100k
  for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
  Branch (118:28): [True: 100k, False: 0]
  Branch (118:43): [True: 100k, False: 0]
119
100k
    uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
120
100k
    p++;
121
100k
    if (byte & 128) {
  Branch (121:9): [True: 243, False: 100k]
122
      // More bytes are present
123
243
      result |= ((byte & 127) << shift);
124
100k
    } else {
125
100k
      result |= (byte << shift);
126
100k
      *value = result;
127
100k
      return reinterpret_cast<const char*>(p);
128
100k
    }
129
100k
  }
130
0
  return nullptr;
131
100k
}
132
133
100k
bool GetVarint64(Slice* input, uint64_t* value) {
134
100k
  const char* p = input->data();
135
100k
  const char* limit = p + input->size();
136
100k
  const char* q = GetVarint64Ptr(p, limit, value);
137
100k
  if (q == nullptr) {
  Branch (137:7): [True: 0, False: 100k]
138
0
    return false;
139
100k
  } else {
140
100k
    *input = Slice(q, limit - q);
141
100k
    return true;
142
100k
  }
143
100k
}
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;
  Branch (149:7): [True: 0, False: 0]
150
0
  if (p + len > limit) return nullptr;
  Branch (150:7): [True: 0, False: 0]
151
0
  *result = Slice(p, len);
152
0
  return p + len;
153
0
}
154
155
13.8M
bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
156
13.8M
  uint32_t len;
157
13.8M
  if (GetVarint32(input, &len) && input->size() >= len) {
  Branch (157:7): [True: 13.8M, False: 0]
  Branch (157:35): [True: 13.8M, False: 0]
158
13.8M
    *result = Slice(input->data(), len);
159
13.8M
    input->remove_prefix(len);
160
13.8M
    return true;
161
13.8M
  } else {
162
0
    return false;
163
0
  }
164
13.8M
}
165
166
}  // namespace leveldb