LCOV - code coverage report
Current view: top level - src/leveldb/util - coding.cc (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 80.6 % 108 87
Test Date: 2026-02-04 04:43:42 Functions: 92.9 % 14 13
Branches: 59.4 % 32 19

             Branch data     Line data    Source code
       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                 :        4016 : void PutFixed32(std::string* dst, uint32_t value) {
      10                 :        4016 :   char buf[sizeof(value)];
      11                 :        4016 :   EncodeFixed32(buf, value);
      12                 :        4016 :   dst->append(buf, sizeof(buf));
      13                 :        4016 : }
      14                 :             : 
      15                 :        1129 : void PutFixed64(std::string* dst, uint64_t value) {
      16                 :        1129 :   char buf[sizeof(value)];
      17                 :        1129 :   EncodeFixed64(buf, value);
      18                 :        1129 :   dst->append(buf, sizeof(buf));
      19                 :        1129 : }
      20                 :             : 
      21                 :     5204039 : char* EncodeVarint32(char* dst, uint32_t v) {
      22                 :             :   // Operate on characters as unsigneds
      23                 :     5204039 :   uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
      24                 :     5204039 :   static const int B = 128;
      25         [ +  + ]:     5204039 :   if (v < (1 << 7)) {
      26                 :     5203343 :     *(ptr++) = v;
      27         [ +  - ]:         696 :   } else if (v < (1 << 14)) {
      28                 :         696 :     *(ptr++) = v | B;
      29                 :         696 :     *(ptr++) = v >> 7;
      30         [ #  # ]:           0 :   } else if (v < (1 << 21)) {
      31                 :           0 :     *(ptr++) = v | B;
      32                 :           0 :     *(ptr++) = (v >> 7) | B;
      33                 :           0 :     *(ptr++) = v >> 14;
      34         [ #  # ]:           0 :   } 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                 :             :   } 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                 :             :   }
      46                 :     5204039 :   return reinterpret_cast<char*>(ptr);
      47                 :             : }
      48                 :             : 
      49                 :      277047 : void PutVarint32(std::string* dst, uint32_t v) {
      50                 :      277047 :   char buf[5];
      51                 :      277047 :   char* ptr = EncodeVarint32(buf, v);
      52                 :      277047 :   dst->append(buf, ptr - buf);
      53                 :      277047 : }
      54                 :             : 
      55                 :        4921 : char* EncodeVarint64(char* dst, uint64_t v) {
      56                 :        4921 :   static const int B = 128;
      57                 :        4921 :   uint8_t* ptr = reinterpret_cast<uint8_t*>(dst);
      58         [ +  + ]:        6457 :   while (v >= B) {
      59                 :        1536 :     *(ptr++) = v | B;
      60                 :        1536 :     v >>= 7;
      61                 :             :   }
      62                 :        4921 :   *(ptr++) = static_cast<uint8_t>(v);
      63                 :        4921 :   return reinterpret_cast<char*>(ptr);
      64                 :             : }
      65                 :             : 
      66                 :        4921 : void PutVarint64(std::string* dst, uint64_t v) {
      67                 :        4921 :   char buf[10];
      68                 :        4921 :   char* ptr = EncodeVarint64(buf, v);
      69                 :        4921 :   dst->append(buf, ptr - buf);
      70                 :        4921 : }
      71                 :             : 
      72                 :      184837 : void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
      73                 :      184837 :   PutVarint32(dst, value.size());
      74                 :      184837 :   dst->append(value.data(), value.size());
      75                 :      184837 : }
      76                 :             : 
      77                 :      204274 : int VarintLength(uint64_t v) {
      78                 :      204274 :   int len = 1;
      79         [ +  + ]:      204622 :   while (v >= 128) {
      80                 :         348 :     v >>= 7;
      81                 :         348 :     len++;
      82                 :             :   }
      83                 :      204274 :   return len;
      84                 :             : }
      85                 :             : 
      86                 :        1617 : const char* GetVarint32PtrFallback(const char* p, const char* limit,
      87                 :             :                                    uint32_t* value) {
      88                 :        1617 :   uint32_t result = 0;
      89         [ +  + ]:        2670 :   for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
      90                 :        2106 :     uint32_t byte = *(reinterpret_cast<const uint8_t*>(p));
      91                 :        2106 :     p++;
      92         [ +  + ]:        2106 :     if (byte & 128) {
      93                 :             :       // More bytes are present
      94                 :        1053 :       result |= ((byte & 127) << shift);
      95                 :             :     } else {
      96                 :        1053 :       result |= (byte << shift);
      97                 :        1053 :       *value = result;
      98                 :        1053 :       return reinterpret_cast<const char*>(p);
      99                 :             :     }
     100                 :             :   }
     101                 :             :   return nullptr;
     102                 :             : }
     103                 :             : 
     104                 :      193244 : bool GetVarint32(Slice* input, uint32_t* value) {
     105                 :      193244 :   const char* p = input->data();
     106                 :      193244 :   const char* limit = p + input->size();
     107                 :      193244 :   const char* q = GetVarint32Ptr(p, limit, value);
     108         [ +  + ]:      193244 :   if (q == nullptr) {
     109                 :             :     return false;
     110                 :             :   } else {
     111                 :      192680 :     *input = Slice(q, limit - q);
     112                 :      192680 :     return true;
     113                 :             :   }
     114                 :             : }
     115                 :             : 
     116                 :     2717804 : const char* GetVarint64Ptr(const char* p, const char* limit, uint64_t* value) {
     117                 :     2717804 :   uint64_t result = 0;
     118         [ +  - ]:     6777316 :   for (uint32_t shift = 0; shift <= 63 && p < limit; shift += 7) {
     119                 :     6777316 :     uint64_t byte = *(reinterpret_cast<const uint8_t*>(p));
     120                 :     6777316 :     p++;
     121         [ +  + ]:     6777316 :     if (byte & 128) {
     122                 :             :       // More bytes are present
     123                 :     4059512 :       result |= ((byte & 127) << shift);
     124                 :             :     } else {
     125                 :     2717804 :       result |= (byte << shift);
     126                 :     2717804 :       *value = result;
     127                 :     2717804 :       return reinterpret_cast<const char*>(p);
     128                 :             :     }
     129                 :             :   }
     130                 :             :   return nullptr;
     131                 :             : }
     132                 :             : 
     133                 :     2717804 : bool GetVarint64(Slice* input, uint64_t* value) {
     134                 :     2717804 :   const char* p = input->data();
     135                 :     2717804 :   const char* limit = p + input->size();
     136                 :     2717804 :   const char* q = GetVarint64Ptr(p, limit, value);
     137         [ +  - ]:     2717804 :   if (q == nullptr) {
     138                 :             :     return false;
     139                 :             :   } else {
     140                 :     2717804 :     *input = Slice(q, limit - q);
     141                 :     2717804 :     return true;
     142                 :             :   }
     143                 :             : }
     144                 :             : 
     145                 :           0 : const char* GetLengthPrefixedSlice(const char* p, const char* limit,
     146                 :             :                                    Slice* result) {
     147                 :           0 :   uint32_t len;
     148                 :           0 :   p = GetVarint32Ptr(p, limit, &len);
     149         [ #  # ]:           0 :   if (p == nullptr) return nullptr;
     150         [ #  # ]:           0 :   if (p + len > limit) return nullptr;
     151                 :           0 :   *result = Slice(p, len);
     152                 :           0 :   return p + len;
     153                 :             : }
     154                 :             : 
     155                 :      190266 : bool GetLengthPrefixedSlice(Slice* input, Slice* result) {
     156                 :      190266 :   uint32_t len;
     157   [ +  -  +  - ]:      190266 :   if (GetVarint32(input, &len) && input->size() >= len) {
     158                 :      190266 :     *result = Slice(input->data(), len);
     159                 :      190266 :     input->remove_prefix(len);
     160                 :      190266 :     return true;
     161                 :             :   } else {
     162                 :             :     return false;
     163                 :             :   }
     164                 :             : }
     165                 :             : 
     166                 :             : }  // namespace leveldb
        

Generated by: LCOV version 2.0-1