LCOV - code coverage report
Current view: top level - src/crypto - common.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 33 40 82.5 %
Date: 2023-11-10 23:46:46 Functions: 10 12 83.3 %
Branches: 2 2 100.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2014-2020 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                 :            : #if defined(HAVE_CONFIG_H)
       9                 :            : #include <config/bitcoin-config.h>
      10                 :            : #endif
      11                 :            : 
      12                 :            : #include <stdint.h>
      13                 :            : #include <string.h>
      14                 :            : 
      15                 :            : #include <compat/endian.h>
      16                 :            : 
      17                 :        347 : uint16_t static inline ReadLE16(const unsigned char* ptr)
      18                 :            : {
      19                 :            :     uint16_t x;
      20                 :        347 :     memcpy(&x, ptr, 2);
      21                 :        347 :     return le16toh(x);
      22                 :            : }
      23                 :            : 
      24                 :   10798323 : uint32_t static inline ReadLE32(const unsigned char* ptr)
      25                 :            : {
      26                 :            :     uint32_t x;
      27                 :   10798323 :     memcpy(&x, ptr, 4);
      28                 :   10798323 :     return le32toh(x);
      29                 :            : }
      30                 :            : 
      31                 :    3152436 : uint64_t static inline ReadLE64(const unsigned char* ptr)
      32                 :            : {
      33                 :            :     uint64_t x;
      34                 :    3152436 :     memcpy(&x, ptr, 8);
      35                 :    3152436 :     return le64toh(x);
      36                 :            : }
      37                 :            : 
      38                 :          0 : void static inline WriteLE16(unsigned char* ptr, uint16_t x)
      39                 :            : {
      40                 :          0 :     uint16_t v = htole16(x);
      41                 :          0 :     memcpy(ptr, &v, 2);
      42                 :          0 : }
      43                 :            : 
      44                 :    5004648 : void static inline WriteLE32(unsigned char* ptr, uint32_t x)
      45                 :            : {
      46                 :    5004648 :     uint32_t v = htole32(x);
      47                 :    5004648 :     memcpy(ptr, &v, 4);
      48                 :    5004648 : }
      49                 :            : 
      50                 :     569765 : void static inline WriteLE64(unsigned char* ptr, uint64_t x)
      51                 :            : {
      52                 :     569765 :     uint64_t v = htole64(x);
      53                 :     569765 :     memcpy(ptr, &v, 8);
      54                 :     569765 : }
      55                 :            : 
      56                 :          0 : uint16_t static inline ReadBE16(const unsigned char* ptr)
      57                 :            : {
      58                 :            :     uint16_t x;
      59                 :          0 :     memcpy(&x, ptr, 2);
      60                 :          0 :     return be16toh(x);
      61                 :            : }
      62                 :            : 
      63                 :       1880 : uint32_t static inline ReadBE32(const unsigned char* ptr)
      64                 :            : {
      65                 :            :     uint32_t x;
      66                 :       1880 :     memcpy(&x, ptr, 4);
      67                 :       1880 :     return be32toh(x);
      68                 :            : }
      69                 :            : 
      70                 :    3932640 : uint64_t static inline ReadBE64(const unsigned char* ptr)
      71                 :            : {
      72                 :            :     uint64_t x;
      73                 :    3932640 :     memcpy(&x, ptr, 8);
      74                 :    3932640 :     return be64toh(x);
      75                 :            : }
      76                 :            : 
      77                 :    6179994 : void static inline WriteBE32(unsigned char* ptr, uint32_t x)
      78                 :            : {
      79                 :    6179994 :     uint32_t v = htobe32(x);
      80                 :    6179994 :     memcpy(ptr, &v, 4);
      81                 :    6179994 : }
      82                 :            : 
      83                 :    2627586 : void static inline WriteBE64(unsigned char* ptr, uint64_t x)
      84                 :            : {
      85                 :    2627586 :     uint64_t v = htobe64(x);
      86                 :    2627586 :     memcpy(ptr, &v, 8);
      87                 :    2627586 : }
      88                 :            : 
      89                 :            : /** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
      90                 :     189772 : uint64_t static inline CountBits(uint64_t x)
      91                 :            : {
      92                 :            : #if HAVE_BUILTIN_CLZL
      93                 :            :     if (sizeof(unsigned long) >= sizeof(uint64_t)) {
      94         [ +  + ]:     189772 :         return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
      95                 :            :     }
      96                 :            : #endif
      97                 :            : #if HAVE_BUILTIN_CLZLL
      98                 :            :     if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
      99                 :            :         return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
     100                 :            :     }
     101                 :            : #endif
     102                 :            :     int ret = 0;
     103                 :            :     while (x) {
     104                 :            :         x >>= 1;
     105                 :            :         ++ret;
     106                 :            :     }
     107                 :            :     return ret;
     108                 :            : }
     109                 :            : 
     110                 :            : #endif // BITCOIN_CRYPTO_COMMON_H

Generated by: LCOV version 1.14