LCOV - code coverage report
Current view: top level - src/crypto - common.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 37 37 100.0 %
Date: 2020-09-26 01:30:44 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2014-2018 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      264814 : uint16_t static inline ReadLE16(const unsigned char* ptr)
      18             : {
      19             :     uint16_t x;
      20      264814 :     memcpy((char*)&x, ptr, 2);
      21      264814 :     return le16toh(x);
      22             : }
      23             : 
      24   156045401 : uint32_t static inline ReadLE32(const unsigned char* ptr)
      25             : {
      26             :     uint32_t x;
      27   156045401 :     memcpy((char*)&x, ptr, 4);
      28   156045401 :     return le32toh(x);
      29             : }
      30             : 
      31    24093484 : uint64_t static inline ReadLE64(const unsigned char* ptr)
      32             : {
      33             :     uint64_t x;
      34    24093484 :     memcpy((char*)&x, ptr, 8);
      35    24093484 :     return le64toh(x);
      36             : }
      37             : 
      38         347 : void static inline WriteLE16(unsigned char* ptr, uint16_t x)
      39             : {
      40         347 :     uint16_t v = htole16(x);
      41         347 :     memcpy(ptr, (char*)&v, 2);
      42         347 : }
      43             : 
      44    87673461 : void static inline WriteLE32(unsigned char* ptr, uint32_t x)
      45             : {
      46    87673461 :     uint32_t v = htole32(x);
      47    87673461 :     memcpy(ptr, (char*)&v, 4);
      48    87673461 : }
      49             : 
      50     9793605 : void static inline WriteLE64(unsigned char* ptr, uint64_t x)
      51             : {
      52     9793605 :     uint64_t v = htole64(x);
      53     9793605 :     memcpy(ptr, (char*)&v, 8);
      54     9793605 : }
      55             : 
      56    20926000 : uint32_t static inline ReadBE32(const unsigned char* ptr)
      57             : {
      58             :     uint32_t x;
      59    20926000 :     memcpy((char*)&x, ptr, 4);
      60    20926000 :     return be32toh(x);
      61             : }
      62             : 
      63   446698874 : uint64_t static inline ReadBE64(const unsigned char* ptr)
      64             : {
      65             :     uint64_t x;
      66   446698874 :     memcpy((char*)&x, ptr, 8);
      67   446698874 :     return be64toh(x);
      68             : }
      69             : 
      70   181893069 : void static inline WriteBE32(unsigned char* ptr, uint32_t x)
      71             : {
      72   181893069 :     uint32_t v = htobe32(x);
      73   181893069 :     memcpy(ptr, (char*)&v, 4);
      74   181893069 : }
      75             : 
      76   264538367 : void static inline WriteBE64(unsigned char* ptr, uint64_t x)
      77             : {
      78   264538367 :     uint64_t v = htobe64(x);
      79   264538367 :     memcpy(ptr, (char*)&v, 8);
      80   264538367 : }
      81             : 
      82             : /** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
      83     8047154 : uint64_t static inline CountBits(uint64_t x)
      84             : {
      85             : #if HAVE_BUILTIN_CLZL
      86             :     if (sizeof(unsigned long) >= sizeof(uint64_t)) {
      87     8047154 :         return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
      88             :     }
      89             : #endif
      90             : #if HAVE_BUILTIN_CLZLL
      91             :     if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
      92             :         return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
      93             :     }
      94             : #endif
      95             :     int ret = 0;
      96             :     while (x) {
      97             :         x >>= 1;
      98             :         ++ret;
      99             :     }
     100             :     return ret;
     101             : }
     102             : 
     103             : #endif // BITCOIN_CRYPTO_COMMON_H

Generated by: LCOV version 1.15