Coverage Report

Created: 2023-06-07 06:28

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