LCOV - code coverage report
Current view: top level - src/crypto - common.h (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 39 39
Test Date: 2025-02-19 05:07:00 Functions: 100.0 % 8 8
Branches: 66.7 % 84 56

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2014-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #ifndef BITCOIN_CRYPTO_COMMON_H
       6                 :             : #define BITCOIN_CRYPTO_COMMON_H
       7                 :             : 
       8                 :             : #include <compat/endian.h>
       9                 :             : 
      10                 :             : #include <concepts>
      11                 :             : #include <cstddef>
      12                 :             : #include <cstdint>
      13                 :             : #include <cstring>
      14                 :             : 
      15                 :             : template <typename B>
      16                 :             : concept ByteType = std::same_as<B, unsigned char> || std::same_as<B, std::byte>;
      17                 :             : 
      18                 :             : template <ByteType B>
      19                 :       10584 : inline uint16_t ReadLE16(const B* ptr)
      20                 :             : {
      21                 :             :     uint16_t x;
      22                 :       10584 :     memcpy(&x, ptr, 2);
      23                 :             :     return le16toh_internal(x);
      24                 :             : }
      25                 :             : 
      26                 :             : template <ByteType B>
      27         [ +  + ]:   163379247 : inline uint32_t ReadLE32(const B* ptr)
      28                 :             : {
      29                 :             :     uint32_t x;
      30         [ +  + ]:   225377208 :     memcpy(&x, ptr, 4);
      31                 :             :     return le32toh_internal(x);
      32                 :             : }
      33                 :             : 
      34                 :             : template <ByteType B>
      35   [ +  +  +  - ]:   344649907 : inline uint64_t ReadLE64(const B* ptr)
           [ +  +  +  + ]
      36                 :             : {
      37                 :             :     uint64_t x;
      38   [ +  +  +  - ]:   344649907 :     memcpy(&x, ptr, 8);
           [ +  +  +  + ]
      39                 :             :     return le64toh_internal(x);
      40                 :             : }
      41                 :             : 
      42                 :             : template <ByteType B>
      43                 :         359 : inline void WriteLE16(B* ptr, uint16_t x)
      44                 :             : {
      45                 :         359 :     uint16_t v = htole16_internal(x);
      46                 :         359 :     memcpy(ptr, &v, 2);
      47                 :         359 : }
      48                 :             : 
      49                 :             : template <ByteType B>
      50                 :   318281880 : inline void WriteLE32(B* ptr, uint32_t x)
      51                 :             : {
      52                 :   318281880 :     uint32_t v = htole32_internal(x);
      53                 :   318281880 :     memcpy(ptr, &v, 4);
      54                 :   318281880 : }
      55                 :             : 
      56                 :             : template <ByteType B>
      57                 :    16482159 : inline void WriteLE64(B* ptr, uint64_t x)
      58                 :             : {
      59                 :    16482159 :     uint64_t v = htole64_internal(x);
      60                 :    16482159 :     memcpy(ptr, &v, 8);
      61                 :    16482159 : }
      62                 :             : 
      63                 :             : template <ByteType B>
      64   [ +  -  +  +  :        2132 : inline uint16_t ReadBE16(const B* ptr)
             +  +  -  + ]
      65                 :             : {
      66                 :             :     uint16_t x;
      67   [ +  -  +  -  :        2137 :     memcpy(&x, ptr, 2);
          +  +  +  +  +  
                -  -  + ]
      68   [ +  -  +  -  :        2137 :     return be16toh_internal(x);
          +  +  +  +  +  
                -  -  + ]
      69                 :             : }
      70                 :             : 
      71                 :             : template <ByteType B>
      72   [ +  -  +  - ]:    29449234 : inline uint32_t ReadBE32(const B* ptr)
           [ +  +  #  # ]
           [ +  -  +  -  
             +  -  +  - ]
      73                 :             : {
      74                 :             :     uint32_t x;
      75                 :   432812344 :     memcpy(&x, ptr, 4);
      76   [ +  -  +  - ]:    29449234 :     return be32toh_internal(x);
           [ +  +  #  # ]
           [ +  -  +  -  
             +  -  +  - ]
      77                 :             : }
      78                 :             : 
      79                 :             : template <ByteType B>
      80                 :    87352785 : inline uint64_t ReadBE64(const B* ptr)
      81                 :             : {
      82                 :             :     uint64_t x;
      83                 :  1397644560 :     memcpy(&x, ptr, 8);
      84                 :    87352785 :     return be64toh_internal(x);
      85                 :             : }
      86                 :             : 
      87                 :             : template <ByteType B>
      88                 :          20 : inline void WriteBE16(B* ptr, uint16_t x)
      89                 :             : {
      90                 :          20 :     uint16_t v = htobe16_internal(x);
      91                 :          20 :     memcpy(ptr, &v, 2);
      92                 :          20 : }
      93                 :             : 
      94                 :             : template <ByteType B>
      95                 :   339997226 : inline void WriteBE32(B* ptr, uint32_t x)
      96                 :             : {
      97                 :   339997226 :     uint32_t v = htobe32_internal(x);
      98                 :   339997226 :     memcpy(ptr, &v, 4);
      99                 :   339997226 : }
     100                 :             : 
     101                 :             : template <ByteType B>
     102                 :   805766419 : inline void WriteBE64(B* ptr, uint64_t x)
     103                 :             : {
     104                 :   805766419 :     uint64_t v = htobe64_internal(x);
     105                 :   805766419 :     memcpy(ptr, &v, 8);
     106                 :   805766419 : }
     107                 :             : 
     108                 :             : #endif // BITCOIN_CRYPTO_COMMON_H
        

Generated by: LCOV version 2.0-1