Coverage Report

Created: 2026-06-12 16:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/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
320k
void PutFixed32(std::string* dst, uint32_t value) {
10
320k
  char buf[sizeof(value)];
11
320k
  EncodeFixed32(buf, value);
12
320k
  dst->append(buf, sizeof(buf));
13
320k
}
14
15
52.7k
void PutFixed64(std::string* dst, uint64_t value) {
16
52.7k
  char buf[sizeof(value)];
17
52.7k
  EncodeFixed64(buf, value);
18
52.7k
  dst->append(buf, sizeof(buf));
19
52.7k
}
20
21
13.8M
char* EncodeVarint32(char* dst, uint32_t v) {
22
  // Operate on characters as unsigneds
23
13.8M
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
24
13.8M
  static const int B = 128;
25
13.8M
  if (v < (1 << 7)) {
  Branch (25:7): [True: 13.6M, False: 195k]
26
13.6M
    *(ptr++) = v;
27
13.6M
  } else if (v < (1 << 14)) {
  Branch (27:14): [True: 106k, False: 88.5k]
28
106k
    *(ptr++) = v | B;
29
106k
    *(ptr++) = v >> 7;
30
106k
  } else if (v < (1 << 21)) {
  Branch (30:14): [True: 82.3k, False: 6.17k]
31
82.3k
    *(ptr++) = v | B;
32
82.3k
    *(ptr++) = (v >> 7) | B;
33
82.3k
    *(ptr++) = v >> 14;
34
82.3k
  } else if (v < (1 << 28)) {
  Branch (34:14): [True: 0, False: 6.17k]
35
0
    *(ptr++) = v | B;
36
0
    *(ptr++) = (v >> 7) | B;
37
0
    *(ptr++) = (v >> 14) | B;
38
0
    *(ptr++) = v >> 21;
39
6.17k
  } else {
40
6.17k
    *(ptr++) = v | B;
41
6.17k
    *(ptr++) = (v >> 7) | B;
42
6.17k
    *(ptr++) = (v >> 14) | B;
43
6.17k
    *(ptr++) = (v >> 21) | B;
44
6.17k
    *(ptr++) = v >> 28;
45
6.17k
  }
46
13.8M
  return reinterpret_cast<char*>(ptr);
47
13.8M
}
48
49
6.18M
void PutVarint32(std::string* dst, uint32_t v) {
50
6.18M
  char buf[5];
51
6.18M
  char* ptr = EncodeVarint32(buf, v);
52
6.18M
  dst->append(buf, ptr - buf);
53
6.18M
}
54
55
60.2k
char* EncodeVarint64(char* dst, uint64_t v) {
56
60.2k
  static const int B = 128;
57
60.2k
  uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
58
138k
  while (v >= B) {
  Branch (58:10): [True: 78.1k, False: 60.2k]
59
78.1k
    *(ptr++) = v | B;
60
78.1k
    v >>= 7;
61
78.1k
  }
62
60.2k
  *(ptr++) = static_cast<uint8_t>(v);
63
60.2k
  return reinterpret_cast<char*>(ptr);
64
60.2k
}
65
66
60.2k
void PutVarint64(std::string* dst, uint64_t v) {
67
60.2k
  char buf[10];
68
60.2k
  char* ptr = EncodeVarint64(buf, v);
69
60.2k
  dst->append(buf, ptr - buf);
70
60.2k
}
71
72
3.27M
void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
73
3.27M
  PutVarint32(dst, value.size());
74
3.27M
  dst->append(value.data(), value.size());
75
3.27M
}
76
77
3.26M
int VarintLength(uint64_t v) {
78
3.26M
  int len = 1;
79
3.38M
  while (v >= 128) {
  Branch (79:10): [True: 120k, False: 3.26M]
80
120k
    v >>= 7;
81
120k
    len++;
82
120k
  }
83
3.26M
  return len;
84
3.26M
}
85
86
const char* GetVarint32PtrFallback(const char* p, const char* limit,
87
1.71M
                                   uint32_t* value) {
88
1.71M
  uint32_t result = 0;
89
3.94M
  for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
  Branch (89:28): [True: 3.94M, False: 352]
  Branch (89:43): [True: 3.94M, False: 1.47k]
90
3.94M
    uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
91
3.94M
    p++;
92
3.94M
    if (byte & 128) {
  Branch (92:9): [True: 2.23M, False: 1.70M]
93
      // More bytes are present
94
2.23M
      result |= ((byte & 127) << shift);
95
2.23M
    } else {
96
1.70M
      result |= (byte << shift);
97
1.70M
      *value = result;
98
1.70M
      return reinterpret_cast<const char*>(p);
99
1.70M
    }
100
3.94M
  }
101
1.83k
  return nullptr;
102
1.71M
}
103
104
3.27M
bool GetVarint32(Slice* input, uint32_t* value) {
105
3.27M
  const char* p = input->data();
106
3.27M
  const char* limit = p + input->size();
107
3.27M
  const char* q = GetVarint32Ptr(p, limit, value);
108
3.27M
  if (q == nullptr) {
  Branch (108:7): [True: 1.55k, False: 3.27M]
109
1.55k
    return false;
110
3.27M
  } else {
111
3.27M
    *input = Slice(q, limit - q);
112
3.27M
    return true;
113
3.27M
  }
114
3.27M
}
115
116
9.39k
const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
117
9.39k
  uint64_t result = 0;
118
15.8k
  for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
  Branch (118:28): [True: 15.8k, False: 0]
  Branch (118:43): [True: 15.8k, False: 0]
119
15.8k
    uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
120
15.8k
    p++;
121
15.8k
    if (byte & 128) {
  Branch (121:9): [True: 6.42k, False: 9.39k]
122
      // More bytes are present
123
6.42k
      result |= ((byte & 127) << shift);
124
9.39k
    } else {
125
9.39k
      result |= (byte << shift);
126
9.39k
      *value = result;
127
9.39k
      return reinterpret_cast<const char*>(p);
128
9.39k
    }
129
15.8k
  }
130
0
  return nullptr;
131
9.39k
}
132
133
9.39k
bool GetVarint64(Slice* input, uint64_t* value) {
134
9.39k
  const char* p = input->data();
135
9.39k
  const char* limit = p + input->size();
136
9.39k
  const char* q = GetVarint64Ptr(p, limit, value);
137
9.39k
  if (q == nullptr) {
  Branch (137:7): [True: 0, False: 9.39k]
138
0
    return false;
139
9.39k
  } else {
140
9.39k
    *input = Slice(q, limit - q);
141
9.39k
    return true;
142
9.39k
  }
143
9.39k
}
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
3.26M
bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
156
3.26M
  uint32_t len;
157
3.26M
  if (GetVarint32(input, &len) && input->size() >= len) {
  Branch (157:7): [True: 3.26M, False: 0]
  Branch (157:35): [True: 3.26M, False: 0]
158
3.26M
    *result = Slice(input->data(), len);
159
3.26M
    input->remove_prefix(len);
160
3.26M
    return true;
161
3.26M
  } else {
162
0
    return false;
163
0
  }
164
3.26M
}
165
166
}  // namespace leveldb