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 : : uint16_t static inline ReadLE16(const unsigned char* ptr)
# 18 : 286129 : {
# 19 : 286129 : uint16_t x;
# 20 : 286129 : memcpy((char*)&x, ptr, 2);
# 21 : 286129 : return le16toh(x);
# 22 : 286129 : }
# 23 : :
# 24 : : uint32_t static inline ReadLE32(const unsigned char* ptr)
# 25 : 227894024 : {
# 26 : 227894024 : uint32_t x;
# 27 : 227894024 : memcpy((char*)&x, ptr, 4);
# 28 : 227894024 : return le32toh(x);
# 29 : 227894024 : }
# 30 : :
# 31 : : uint64_t static inline ReadLE64(const unsigned char* ptr)
# 32 : 379041618 : {
# 33 : 379041618 : uint64_t x;
# 34 : 379041618 : memcpy((char*)&x, ptr, 8);
# 35 : 379041618 : return le64toh(x);
# 36 : 379041618 : }
# 37 : :
# 38 : : void static inline WriteLE16(unsigned char* ptr, uint16_t x)
# 39 : 704 : {
# 40 : 704 : uint16_t v = htole16(x);
# 41 : 704 : memcpy(ptr, (char*)&v, 2);
# 42 : 704 : }
# 43 : :
# 44 : : void static inline WriteLE32(unsigned char* ptr, uint32_t x)
# 45 : 263396822 : {
# 46 : 263396822 : uint32_t v = htole32(x);
# 47 : 263396822 : memcpy(ptr, (char*)&v, 4);
# 48 : 263396822 : }
# 49 : :
# 50 : : void static inline WriteLE64(unsigned char* ptr, uint64_t x)
# 51 : 19306957 : {
# 52 : 19306957 : uint64_t v = htole64(x);
# 53 : 19306957 : memcpy(ptr, (char*)&v, 8);
# 54 : 19306957 : }
# 55 : :
# 56 : : uint16_t static inline ReadBE16(const unsigned char* ptr)
# 57 : 21104 : {
# 58 : 21104 : uint16_t x;
# 59 : 21104 : memcpy((char*)&x, ptr, 2);
# 60 : 21104 : return be16toh(x);
# 61 : 21104 : }
# 62 : :
# 63 : : uint32_t static inline ReadBE32(const unsigned char* ptr)
# 64 : 46564248 : {
# 65 : 46564248 : uint32_t x;
# 66 : 46564248 : memcpy((char*)&x, ptr, 4);
# 67 : 46564248 : return be32toh(x);
# 68 : 46564248 : }
# 69 : :
# 70 : : uint64_t static inline ReadBE64(const unsigned char* ptr)
# 71 : 644410614 : {
# 72 : 644410614 : uint64_t x;
# 73 : 644410614 : memcpy((char*)&x, ptr, 8);
# 74 : 644410614 : return be64toh(x);
# 75 : 644410614 : }
# 76 : :
# 77 : : void static inline WriteBE32(unsigned char* ptr, uint32_t x)
# 78 : 4804682982 : {
# 79 : 4804682982 : uint32_t v = htobe32(x);
# 80 : 4804682982 : memcpy(ptr, (char*)&v, 4);
# 81 : 4804682982 : }
# 82 : :
# 83 : : void static inline WriteBE64(unsigned char* ptr, uint64_t x)
# 84 : 940128832 : {
# 85 : 940128832 : uint64_t v = htobe64(x);
# 86 : 940128832 : memcpy(ptr, (char*)&v, 8);
# 87 : 940128832 : }
# 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 : 33532302 : {
# 92 : 33532302 : #if HAVE_BUILTIN_CLZL
# 93 : 33532302 : if (sizeof(unsigned long) >= sizeof(uint64_t)) {
# 94 [ # # ][ # # ]: 33532302 : return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
# [ # # ][ # # ]
# [ # # ][ + + ]
# [ # # ][ # # ]
# [ # # ][ # # ]
# [ + + ][ # # ]
# [ # # ][ # # ]
# [ # # ][ # # ]
# [ # # ][ # # ]
# [ + - ][ # # ]
# [ # # ][ # # ]
# [ + - ][ # # ]
# [ # # ][ # # ]
# [ + + ]
# 95 : 33532302 : }
# 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
|