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