This commit is contained in:
Kittywhiskers Van Gogh 2024-12-19 22:22:52 +00:00 committed by GitHub
commit 2dbdc919a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
51 changed files with 1284 additions and 1457 deletions

View File

@ -1053,7 +1053,7 @@ if test x$TARGET_OS = xdarwin; then
AX_CHECK_LINK_FLAG([[-Wl,-fixup_chains]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-fixup_chains"], [], [[$LDFLAG_WERROR]]) AX_CHECK_LINK_FLAG([[-Wl,-fixup_chains]], [HARDENED_LDFLAGS="$HARDENED_LDFLAGS -Wl,-fixup_chains"], [], [[$LDFLAG_WERROR]])
fi fi
AC_CHECK_HEADERS([endian.h sys/endian.h byteswap.h stdio.h stdlib.h unistd.h strings.h sys/types.h sys/stat.h sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h]) AC_CHECK_HEADERS([sys/select.h sys/prctl.h sys/sysctl.h vm/vm_param.h sys/vmmeter.h sys/resources.h])
AC_CHECK_DECLS([getifaddrs, freeifaddrs],[CHECK_SOCKET],, AC_CHECK_DECLS([getifaddrs, freeifaddrs],[CHECK_SOCKET],,
[#include <sys/types.h> [#include <sys/types.h>
@ -1068,18 +1068,6 @@ AC_CHECK_DECLS([pipe2])
AC_CHECK_FUNCS([timingsafe_bcmp]) AC_CHECK_FUNCS([timingsafe_bcmp])
AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,,
[#if HAVE_ENDIAN_H
#include <endian.h>
#elif HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif])
AC_CHECK_DECLS([bswap_16, bswap_32, bswap_64],,,
[#if HAVE_BYTESWAP_H
#include <byteswap.h>
#endif])
dnl Check for mallopt(M_ARENA_MAX) (to set glibc arenas) dnl Check for mallopt(M_ARENA_MAX) (to set glibc arenas)
AC_MSG_CHECKING(for mallopt M_ARENA_MAX) AC_MSG_CHECKING(for mallopt M_ARENA_MAX)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]], AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <malloc.h>]],

View File

@ -73,6 +73,7 @@ endif
# test_dash binary # # test_dash binary #
BITCOIN_TESTS =\ BITCOIN_TESTS =\
test/argsman_tests.cpp \
test/arith_uint256_tests.cpp \ test/arith_uint256_tests.cpp \
test/scriptnum10.h \ test/scriptnum10.h \
test/addrman_tests.cpp \ test/addrman_tests.cpp \

View File

@ -17,12 +17,12 @@ static void EllSwiftCreate(benchmark::Bench& bench)
uint256 entropy = GetRandHash(); uint256 entropy = GetRandHash();
bench.batch(1).unit("pubkey").run([&] { bench.batch(1).unit("pubkey").run([&] {
auto ret = key.EllSwiftCreate(AsBytes(Span{entropy})); auto ret = key.EllSwiftCreate(MakeByteSpan(entropy));
/* Use the first 32 bytes of the ellswift encoded public key as next private key. */ /* Use the first 32 bytes of the ellswift encoded public key as next private key. */
key.Set(UCharCast(ret.data()), UCharCast(ret.data()) + 32, true); key.Set(UCharCast(ret.data()), UCharCast(ret.data()) + 32, true);
assert(key.IsValid()); assert(key.IsValid());
/* Use the last 32 bytes of the ellswift encoded public key as next entropy. */ /* Use the last 32 bytes of the ellswift encoded public key as next entropy. */
std::copy(ret.begin() + 32, ret.begin() + 64, BytePtr(entropy.data())); std::copy(ret.begin() + 32, ret.begin() + 64, MakeWritableByteSpan(entropy).begin());
}); });
ECC_Stop(); ECC_Stop();

View File

@ -26,10 +26,10 @@
#include <util/system.h> #include <util/system.h>
#include <util/translation.h> #include <util/translation.h>
#include <cstdio>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <stdio.h>
#include <stacktraces.h> #include <stacktraces.h>

View File

@ -27,8 +27,8 @@
#include <stacktraces.h> #include <stacktraces.h>
#include <util/url.h> #include <util/url.h>
#include <cstdio>
#include <functional> #include <functional>
#include <stdio.h>
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr; const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
UrlDecodeFn* const URL_DECODE = urlDecode; UrlDecodeFn* const URL_DECODE = urlDecode;

View File

@ -5,44 +5,66 @@
#ifndef BITCOIN_COMPAT_BYTESWAP_H #ifndef BITCOIN_COMPAT_BYTESWAP_H
#define BITCOIN_COMPAT_BYTESWAP_H #define BITCOIN_COMPAT_BYTESWAP_H
#if defined(HAVE_CONFIG_H) #include <cstdint>
#include <config/bitcoin-config.h> #ifdef _MSC_VER
#include <cstdlib>
#endif #endif
#include <stdint.h>
#if defined(HAVE_BYTESWAP_H) // All internal_bswap_* functions can be replaced with std::byteswap once we
#include <byteswap.h> // require c++23. Both libstdc++ and libc++ implement std::byteswap via these
// builtins.
#ifndef DISABLE_BUILTIN_BSWAPS
# if defined __has_builtin
# if __has_builtin(__builtin_bswap16)
# define bitcoin_builtin_bswap16(x) __builtin_bswap16(x)
# endif
# if __has_builtin(__builtin_bswap32)
# define bitcoin_builtin_bswap32(x) __builtin_bswap32(x)
# endif
# if __has_builtin(__builtin_bswap64)
# define bitcoin_builtin_bswap64(x) __builtin_bswap64(x)
# endif
# elif defined(_MSC_VER)
# define bitcoin_builtin_bswap16(x) _byteswap_ushort(x)
# define bitcoin_builtin_bswap32(x) _byteswap_ulong(x)
# define bitcoin_builtin_bswap64(x) _byteswap_uint64(x)
# endif
#endif #endif
#if defined(MAC_OSX) // MSVC's _byteswap_* functions are not constexpr
#include <libkern/OSByteOrder.h>
#define bswap_16(x) OSSwapInt16(x)
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)
#ifndef _MSC_VER
#define BSWAP_CONSTEXPR constexpr
#else #else
// Non-MacOS / non-Darwin #define BSWAP_CONSTEXPR
#endif
#if HAVE_DECL_BSWAP_16 == 0 inline BSWAP_CONSTEXPR uint16_t internal_bswap_16(uint16_t x)
inline uint16_t bswap_16(uint16_t x)
{ {
#ifdef bitcoin_builtin_bswap16
return bitcoin_builtin_bswap16(x);
#else
return (x >> 8) | (x << 8); return (x >> 8) | (x << 8);
#endif
} }
#endif // HAVE_DECL_BSWAP16 == 0
#if HAVE_DECL_BSWAP_32 == 0 inline BSWAP_CONSTEXPR uint32_t internal_bswap_32(uint32_t x)
inline uint32_t bswap_32(uint32_t x)
{ {
#ifdef bitcoin_builtin_bswap32
return bitcoin_builtin_bswap32(x);
#else
return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) | return (((x & 0xff000000U) >> 24) | ((x & 0x00ff0000U) >> 8) |
((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24)); ((x & 0x0000ff00U) << 8) | ((x & 0x000000ffU) << 24));
#endif
} }
#endif // HAVE_DECL_BSWAP32 == 0
#if HAVE_DECL_BSWAP_64 == 0 inline BSWAP_CONSTEXPR uint64_t internal_bswap_64(uint64_t x)
inline uint64_t bswap_64(uint64_t x)
{ {
#ifdef bitcoin_builtin_bswap64
return bitcoin_builtin_bswap64(x);
#else
return (((x & 0xff00000000000000ull) >> 56) return (((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40) | ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24) | ((x & 0x0000ff0000000000ull) >> 24)
@ -51,9 +73,7 @@ inline uint64_t bswap_64(uint64_t x)
| ((x & 0x0000000000ff0000ull) << 24) | ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40) | ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56)); | ((x & 0x00000000000000ffull) << 56));
#endif
} }
#endif // HAVE_DECL_BSWAP64 == 0
#endif // defined(MAC_OSX)
#endif // BITCOIN_COMPAT_BYTESWAP_H #endif // BITCOIN_COMPAT_BYTESWAP_H

View File

@ -10,6 +10,8 @@
#include <cpuid.h> #include <cpuid.h>
#include <cstdint>
// We can't use cpuid.h's __get_cpuid as it does not support subleafs. // We can't use cpuid.h's __get_cpuid as it does not support subleafs.
void static inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) void static inline GetCPUID(uint32_t leaf, uint32_t subleaf, uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d)
{ {

View File

@ -5,237 +5,70 @@
#ifndef BITCOIN_COMPAT_ENDIAN_H #ifndef BITCOIN_COMPAT_ENDIAN_H
#define BITCOIN_COMPAT_ENDIAN_H #define BITCOIN_COMPAT_ENDIAN_H
#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif
#include <compat/byteswap.h> #include <compat/byteswap.h>
#include <stdint.h> #include <bit>
#include <cstdint>
#if defined(HAVE_ENDIAN_H) inline BSWAP_CONSTEXPR uint16_t htobe16_internal(uint16_t host_16bits)
#include <endian.h>
#elif defined(HAVE_SYS_ENDIAN_H)
#include <sys/endian.h>
#endif
#ifndef HAVE_CONFIG_H
// While not technically a supported configuration, defaulting to defining these
// DECLs when we were compiled without autotools makes it easier for other build
// systems to build things like libdashconsensus for strange targets.
#ifdef htobe16
#define HAVE_DECL_HTOBE16 1
#endif
#ifdef htole16
#define HAVE_DECL_HTOLE16 1
#endif
#ifdef be16toh
#define HAVE_DECL_BE16TOH 1
#endif
#ifdef le16toh
#define HAVE_DECL_LE16TOH 1
#endif
#ifdef htobe32
#define HAVE_DECL_HTOBE32 1
#endif
#ifdef htole32
#define HAVE_DECL_HTOLE32 1
#endif
#ifdef be32toh
#define HAVE_DECL_BE32TOH 1
#endif
#ifdef le32toh
#define HAVE_DECL_LE32TOH 1
#endif
#ifdef htobe64
#define HAVE_DECL_HTOBE64 1
#endif
#ifdef htole64
#define HAVE_DECL_HTOLE64 1
#endif
#ifdef be64toh
#define HAVE_DECL_BE64TOH 1
#endif
#ifdef le64toh
#define HAVE_DECL_LE64TOH 1
#endif
#endif // HAVE_CONFIG_H
#if defined(WORDS_BIGENDIAN)
#if HAVE_DECL_HTOBE16 == 0
inline uint16_t htobe16(uint16_t host_16bits)
{ {
return host_16bits; if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(host_16bits);
else return host_16bits;
} }
#endif // HAVE_DECL_HTOBE16 inline BSWAP_CONSTEXPR uint16_t htole16_internal(uint16_t host_16bits)
#if HAVE_DECL_HTOLE16 == 0
inline uint16_t htole16(uint16_t host_16bits)
{ {
return bswap_16(host_16bits); if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(host_16bits);
else return host_16bits;
} }
#endif // HAVE_DECL_HTOLE16 inline BSWAP_CONSTEXPR uint16_t be16toh_internal(uint16_t big_endian_16bits)
#if HAVE_DECL_BE16TOH == 0
inline uint16_t be16toh(uint16_t big_endian_16bits)
{ {
return big_endian_16bits; if constexpr (std::endian::native == std::endian::little) return internal_bswap_16(big_endian_16bits);
else return big_endian_16bits;
} }
#endif // HAVE_DECL_BE16TOH inline BSWAP_CONSTEXPR uint16_t le16toh_internal(uint16_t little_endian_16bits)
#if HAVE_DECL_LE16TOH == 0
inline uint16_t le16toh(uint16_t little_endian_16bits)
{ {
return bswap_16(little_endian_16bits); if constexpr (std::endian::native == std::endian::big) return internal_bswap_16(little_endian_16bits);
else return little_endian_16bits;
} }
#endif // HAVE_DECL_LE16TOH inline BSWAP_CONSTEXPR uint32_t htobe32_internal(uint32_t host_32bits)
#if HAVE_DECL_HTOBE32 == 0
inline uint32_t htobe32(uint32_t host_32bits)
{ {
return host_32bits; if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(host_32bits);
else return host_32bits;
} }
#endif // HAVE_DECL_HTOBE32 inline BSWAP_CONSTEXPR uint32_t htole32_internal(uint32_t host_32bits)
#if HAVE_DECL_HTOLE32 == 0
inline uint32_t htole32(uint32_t host_32bits)
{ {
return bswap_32(host_32bits); if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(host_32bits);
else return host_32bits;
} }
#endif // HAVE_DECL_HTOLE32 inline BSWAP_CONSTEXPR uint32_t be32toh_internal(uint32_t big_endian_32bits)
#if HAVE_DECL_BE32TOH == 0
inline uint32_t be32toh(uint32_t big_endian_32bits)
{ {
return big_endian_32bits; if constexpr (std::endian::native == std::endian::little) return internal_bswap_32(big_endian_32bits);
else return big_endian_32bits;
} }
#endif // HAVE_DECL_BE32TOH inline BSWAP_CONSTEXPR uint32_t le32toh_internal(uint32_t little_endian_32bits)
#if HAVE_DECL_LE32TOH == 0
inline uint32_t le32toh(uint32_t little_endian_32bits)
{ {
return bswap_32(little_endian_32bits); if constexpr (std::endian::native == std::endian::big) return internal_bswap_32(little_endian_32bits);
else return little_endian_32bits;
} }
#endif // HAVE_DECL_LE32TOH inline BSWAP_CONSTEXPR uint64_t htobe64_internal(uint64_t host_64bits)
#if HAVE_DECL_HTOBE64 == 0
inline uint64_t htobe64(uint64_t host_64bits)
{ {
return host_64bits; if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(host_64bits);
else return host_64bits;
} }
#endif // HAVE_DECL_HTOBE64 inline BSWAP_CONSTEXPR uint64_t htole64_internal(uint64_t host_64bits)
#if HAVE_DECL_HTOLE64 == 0
inline uint64_t htole64(uint64_t host_64bits)
{ {
return bswap_64(host_64bits); if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(host_64bits);
else return host_64bits;
} }
#endif // HAVE_DECL_HTOLE64 inline BSWAP_CONSTEXPR uint64_t be64toh_internal(uint64_t big_endian_64bits)
#if HAVE_DECL_BE64TOH == 0
inline uint64_t be64toh(uint64_t big_endian_64bits)
{ {
return big_endian_64bits; if constexpr (std::endian::native == std::endian::little) return internal_bswap_64(big_endian_64bits);
else return big_endian_64bits;
} }
#endif // HAVE_DECL_BE64TOH inline BSWAP_CONSTEXPR uint64_t le64toh_internal(uint64_t little_endian_64bits)
#if HAVE_DECL_LE64TOH == 0
inline uint64_t le64toh(uint64_t little_endian_64bits)
{ {
return bswap_64(little_endian_64bits); if constexpr (std::endian::native == std::endian::big) return internal_bswap_64(little_endian_64bits);
else return little_endian_64bits;
} }
#endif // HAVE_DECL_LE64TOH
#else // WORDS_BIGENDIAN
#if HAVE_DECL_HTOBE16 == 0
inline uint16_t htobe16(uint16_t host_16bits)
{
return bswap_16(host_16bits);
}
#endif // HAVE_DECL_HTOBE16
#if HAVE_DECL_HTOLE16 == 0
inline uint16_t htole16(uint16_t host_16bits)
{
return host_16bits;
}
#endif // HAVE_DECL_HTOLE16
#if HAVE_DECL_BE16TOH == 0
inline uint16_t be16toh(uint16_t big_endian_16bits)
{
return bswap_16(big_endian_16bits);
}
#endif // HAVE_DECL_BE16TOH
#if HAVE_DECL_LE16TOH == 0
inline uint16_t le16toh(uint16_t little_endian_16bits)
{
return little_endian_16bits;
}
#endif // HAVE_DECL_LE16TOH
#if HAVE_DECL_HTOBE32 == 0
inline uint32_t htobe32(uint32_t host_32bits)
{
return bswap_32(host_32bits);
}
#endif // HAVE_DECL_HTOBE32
#if HAVE_DECL_HTOLE32 == 0
inline uint32_t htole32(uint32_t host_32bits)
{
return host_32bits;
}
#endif // HAVE_DECL_HTOLE32
#if HAVE_DECL_BE32TOH == 0
inline uint32_t be32toh(uint32_t big_endian_32bits)
{
return bswap_32(big_endian_32bits);
}
#endif // HAVE_DECL_BE32TOH
#if HAVE_DECL_LE32TOH == 0
inline uint32_t le32toh(uint32_t little_endian_32bits)
{
return little_endian_32bits;
}
#endif // HAVE_DECL_LE32TOH
#if HAVE_DECL_HTOBE64 == 0
inline uint64_t htobe64(uint64_t host_64bits)
{
return bswap_64(host_64bits);
}
#endif // HAVE_DECL_HTOBE64
#if HAVE_DECL_HTOLE64 == 0
inline uint64_t htole64(uint64_t host_64bits)
{
return host_64bits;
}
#endif // HAVE_DECL_HTOLE64
#if HAVE_DECL_BE64TOH == 0
inline uint64_t be64toh(uint64_t big_endian_64bits)
{
return bswap_64(big_endian_64bits);
}
#endif // HAVE_DECL_BE64TOH
#if HAVE_DECL_LE64TOH == 0
inline uint64_t le64toh(uint64_t little_endian_64bits)
{
return little_endian_64bits;
}
#endif // HAVE_DECL_LE64TOH
#endif // WORDS_BIGENDIAN
#endif // BITCOIN_COMPAT_ENDIAN_H #endif // BITCOIN_COMPAT_ENDIAN_H

View File

@ -2,23 +2,19 @@
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#if defined(HAVE_CONFIG_H) #include <compat/stdin.h>
#include <config/bitcoin-config.h>
#endif
#include <cstdio> // for fileno(), stdin #include <cstdio>
#ifdef WIN32 #ifdef WIN32
#include <windows.h> // for SetStdinEcho() #include <windows.h>
#include <io.h> // for isatty() #include <io.h>
#else #else
#include <termios.h> // for SetStdinEcho() #include <termios.h>
#include <unistd.h> // for SetStdinEcho(), isatty() #include <unistd.h>
#include <poll.h> // for StdinReady() #include <poll.h>
#endif #endif
#include <compat/stdin.h>
// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin // https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
void SetStdinEcho(bool enable) void SetStdinEcho(bool enable)
{ {

View File

@ -9,8 +9,8 @@
#include <array> #include <array>
#include <cstddef> #include <cstddef>
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <utility> #include <utility>
// classes for ChaCha20 256-bit stream cipher developed by Daniel J. Bernstein // classes for ChaCha20 256-bit stream cipher developed by Daniel J. Bernstein

View File

@ -5,106 +5,81 @@
#ifndef BITCOIN_CRYPTO_COMMON_H #ifndef BITCOIN_CRYPTO_COMMON_H
#define BITCOIN_CRYPTO_COMMON_H #define BITCOIN_CRYPTO_COMMON_H
#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif
#include <stdint.h>
#include <string.h>
#include <compat/endian.h> #include <compat/endian.h>
#include <cstdint>
#include <cstring>
uint16_t static inline ReadLE16(const unsigned char* ptr) uint16_t static inline ReadLE16(const unsigned char* ptr)
{ {
uint16_t x; uint16_t x;
memcpy((char*)&x, ptr, 2); memcpy(&x, ptr, 2);
return le16toh(x); return le16toh_internal(x);
} }
uint32_t static inline ReadLE32(const unsigned char* ptr) uint32_t static inline ReadLE32(const unsigned char* ptr)
{ {
uint32_t x; uint32_t x;
memcpy((char*)&x, ptr, 4); memcpy(&x, ptr, 4);
return le32toh(x); return le32toh_internal(x);
} }
uint64_t static inline ReadLE64(const unsigned char* ptr) uint64_t static inline ReadLE64(const unsigned char* ptr)
{ {
uint64_t x; uint64_t x;
memcpy((char*)&x, ptr, 8); memcpy(&x, ptr, 8);
return le64toh(x); return le64toh_internal(x);
} }
void static inline WriteLE16(unsigned char* ptr, uint16_t x) void static inline WriteLE16(unsigned char* ptr, uint16_t x)
{ {
uint16_t v = htole16(x); uint16_t v = htole16_internal(x);
memcpy(ptr, (char*)&v, 2); memcpy(ptr, &v, 2);
} }
void static inline WriteLE32(unsigned char* ptr, uint32_t x) void static inline WriteLE32(unsigned char* ptr, uint32_t x)
{ {
uint32_t v = htole32(x); uint32_t v = htole32_internal(x);
memcpy(ptr, (char*)&v, 4); memcpy(ptr, &v, 4);
} }
void static inline WriteLE64(unsigned char* ptr, uint64_t x) void static inline WriteLE64(unsigned char* ptr, uint64_t x)
{ {
uint64_t v = htole64(x); uint64_t v = htole64_internal(x);
memcpy(ptr, (char*)&v, 8); memcpy(ptr, &v, 8);
} }
uint16_t static inline ReadBE16(const unsigned char* ptr) uint16_t static inline ReadBE16(const unsigned char* ptr)
{ {
uint16_t x; uint16_t x;
memcpy((char*)&x, ptr, 2); memcpy(&x, ptr, 2);
return be16toh(x); return be16toh_internal(x);
} }
uint32_t static inline ReadBE32(const unsigned char* ptr) uint32_t static inline ReadBE32(const unsigned char* ptr)
{ {
uint32_t x; uint32_t x;
memcpy((char*)&x, ptr, 4); memcpy(&x, ptr, 4);
return be32toh(x); return be32toh_internal(x);
} }
uint64_t static inline ReadBE64(const unsigned char* ptr) uint64_t static inline ReadBE64(const unsigned char* ptr)
{ {
uint64_t x; uint64_t x;
memcpy((char*)&x, ptr, 8); memcpy(&x, ptr, 8);
return be64toh(x); return be64toh_internal(x);
} }
void static inline WriteBE32(unsigned char* ptr, uint32_t x) void static inline WriteBE32(unsigned char* ptr, uint32_t x)
{ {
uint32_t v = htobe32(x); uint32_t v = htobe32_internal(x);
memcpy(ptr, (char*)&v, 4); memcpy(ptr, &v, 4);
} }
void static inline WriteBE64(unsigned char* ptr, uint64_t x) void static inline WriteBE64(unsigned char* ptr, uint64_t x)
{ {
uint64_t v = htobe64(x); uint64_t v = htobe64_internal(x);
memcpy(ptr, (char*)&v, 8); memcpy(ptr, &v, 8);
}
/** Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set. */
uint64_t static inline CountBits(uint64_t x)
{
#if HAVE_BUILTIN_CLZL
if (sizeof(unsigned long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long) - __builtin_clzl(x) : 0;
}
#endif
#if HAVE_BUILTIN_CLZLL
if (sizeof(unsigned long long) >= sizeof(uint64_t)) {
return x ? 8 * sizeof(unsigned long long) - __builtin_clzll(x) : 0;
}
#endif
int ret = 0;
while (x) {
x >>= 1;
++ret;
}
return ret;
} }
#endif // BITCOIN_CRYPTO_COMMON_H #endif // BITCOIN_CRYPTO_COMMON_H

View File

@ -7,8 +7,8 @@
#include <crypto/hmac_sha256.h> #include <crypto/hmac_sha256.h>
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
/** A rfc5869 HKDF implementation with HMAC_SHA256 and fixed key output length of 32 bytes (L=32) */ /** A rfc5869 HKDF implementation with HMAC_SHA256 and fixed key output length of 32 bytes (L=32) */
class CHKDF_HMAC_SHA256_L32 class CHKDF_HMAC_SHA256_L32

View File

@ -7,8 +7,8 @@
#include <crypto/sha256.h> #include <crypto/sha256.h>
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
/** A hasher class for HMAC-SHA-256. */ /** A hasher class for HMAC-SHA-256. */
class CHMAC_SHA256 class CHMAC_SHA256

View File

@ -7,8 +7,8 @@
#include <crypto/sha512.h> #include <crypto/sha512.h>
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
/** A hasher class for HMAC-SHA-512. */ /** A hasher class for HMAC-SHA-512. */
class CHMAC_SHA512 class CHMAC_SHA512

View File

@ -5,8 +5,8 @@
#ifndef BITCOIN_CRYPTO_PKCS5_PBKDF2_HMAC_SHA512_H #ifndef BITCOIN_CRYPTO_PKCS5_PBKDF2_HMAC_SHA512_H
#define BITCOIN_CRYPTO_PKCS5_PBKDF2_HMAC_SHA512_H #define BITCOIN_CRYPTO_PKCS5_PBKDF2_HMAC_SHA512_H
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
/** A rfc2898 implementation of PKCS#5 v2.0 password based encryption key /** A rfc2898 implementation of PKCS#5 v2.0 password based encryption key
* derivation function PBKDF2 with HMAC_SHA512. This implementation is * derivation function PBKDF2 with HMAC_SHA512. This implementation is

View File

@ -8,8 +8,8 @@
#include <span.h> #include <span.h>
#include <cassert> #include <cassert>
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#define POLY1305_BLOCK_SIZE 16 #define POLY1305_BLOCK_SIZE 16

View File

@ -5,8 +5,8 @@
#ifndef BITCOIN_CRYPTO_RIPEMD160_H #ifndef BITCOIN_CRYPTO_RIPEMD160_H
#define BITCOIN_CRYPTO_RIPEMD160_H #define BITCOIN_CRYPTO_RIPEMD160_H
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
/** A hasher class for RIPEMD-160. */ /** A hasher class for RIPEMD-160. */
class CRIPEMD160 class CRIPEMD160

View File

@ -5,8 +5,8 @@
#ifndef BITCOIN_CRYPTO_SHA1_H #ifndef BITCOIN_CRYPTO_SHA1_H
#define BITCOIN_CRYPTO_SHA1_H #define BITCOIN_CRYPTO_SHA1_H
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
/** A hasher class for SHA1. */ /** A hasher class for SHA1. */
class CSHA1 class CSHA1

View File

@ -5,8 +5,8 @@
#ifndef BITCOIN_CRYPTO_SHA256_H #ifndef BITCOIN_CRYPTO_SHA256_H
#define BITCOIN_CRYPTO_SHA256_H #define BITCOIN_CRYPTO_SHA256_H
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <string> #include <string>
/** A hasher class for SHA-256. */ /** A hasher class for SHA-256. */

View File

@ -5,8 +5,8 @@
// This is a translation to GCC extended asm syntax from YASM code by Intel // This is a translation to GCC extended asm syntax from YASM code by Intel
// (available at the bottom of this file). // (available at the bottom of this file).
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#if defined(__x86_64__) || defined(__amd64__) #if defined(__x86_64__) || defined(__amd64__)

View File

@ -7,8 +7,8 @@
#include <span.h> #include <span.h>
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
//! The Keccak-f[1600] transform. //! The Keccak-f[1600] transform.
void KeccakF(uint64_t (&st)[25]); void KeccakF(uint64_t (&st)[25]);

View File

@ -5,8 +5,8 @@
#ifndef BITCOIN_CRYPTO_SHA512_H #ifndef BITCOIN_CRYPTO_SHA512_H
#define BITCOIN_CRYPTO_SHA512_H #define BITCOIN_CRYPTO_SHA512_H
#include <cstdlib>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
/** A hasher class for SHA-512. */ /** A hasher class for SHA-512. */
class CSHA512 class CSHA512

View File

@ -21,6 +21,8 @@
static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64; static const size_t DBWRAPPER_PREALLOC_KEY_SIZE = 64;
static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024; static const size_t DBWRAPPER_PREALLOC_VALUE_SIZE = 1024;
inline auto CharCast(const std::byte* data) { return reinterpret_cast<const char*>(data); }
class dbwrapper_error : public std::runtime_error class dbwrapper_error : public std::runtime_error
{ {
public: public:
@ -83,12 +85,12 @@ public:
template <typename V> template <typename V>
void Write(const CDataStream& _ssKey, const V& value) void Write(const CDataStream& _ssKey, const V& value)
{ {
leveldb::Slice slKey((const char*)_ssKey.data(), _ssKey.size()); leveldb::Slice slKey(CharCast(_ssKey.data()), _ssKey.size());
ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE); ssValue.reserve(DBWRAPPER_PREALLOC_VALUE_SIZE);
ssValue << value; ssValue << value;
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
leveldb::Slice slValue((const char*)ssValue.data(), ssValue.size()); leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
batch.Put(slKey, slValue); batch.Put(slKey, slValue);
// - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...) // - varint: key length (1 byte up to 127B, 2 bytes up to 16383B, ...)
@ -110,7 +112,7 @@ public:
} }
void Erase(const CDataStream& _ssKey) { void Erase(const CDataStream& _ssKey) {
leveldb::Slice slKey((const char*)_ssKey.data(), _ssKey.size()); leveldb::Slice slKey(CharCast(_ssKey.data()), _ssKey.size());
batch.Delete(slKey); batch.Delete(slKey);
// - byte: header // - byte: header
@ -151,7 +153,7 @@ public:
} }
void Seek(const CDataStream& ssKey) { void Seek(const CDataStream& ssKey) {
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size()); leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
piter->Seek(slKey); piter->Seek(slKey);
} }
@ -259,7 +261,7 @@ public:
bool ReadDataStream(const CDataStream& ssKey, CDataStream& ssValue) const bool ReadDataStream(const CDataStream& ssKey, CDataStream& ssValue) const
{ {
leveldb::Slice slKey((const char*)ssKey.data(), ssKey.size()); leveldb::Slice slKey(CharCast(ssKey.data()), ssKey.size());
std::string strValue; std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@ -319,7 +321,7 @@ public:
bool Exists(const CDataStream& key) const bool Exists(const CDataStream& key) const
{ {
leveldb::Slice slKey((const char*)key.data(), key.size()); leveldb::Slice slKey(CharCast(key.data()), key.size());
std::string strValue; std::string strValue;
leveldb::Status status = pdb->Get(readoptions, slKey, &strValue); leveldb::Status status = pdb->Get(readoptions, slKey, &strValue);
@ -363,8 +365,8 @@ public:
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey1 << key_begin; ssKey1 << key_begin;
ssKey2 << key_end; ssKey2 << key_end;
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size()); leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size());
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size()); leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size());
uint64_t size = 0; uint64_t size = 0;
leveldb::Range range(slKey1, slKey2); leveldb::Range range(slKey1, slKey2);
pdb->GetApproximateSizes(&range, 1, &size); pdb->GetApproximateSizes(&range, 1, &size);
@ -382,8 +384,8 @@ public:
ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE); ssKey2.reserve(DBWRAPPER_PREALLOC_KEY_SIZE);
ssKey1 << key_begin; ssKey1 << key_begin;
ssKey2 << key_end; ssKey2 << key_end;
leveldb::Slice slKey1((const char*)ssKey1.data(), ssKey1.size()); leveldb::Slice slKey1(CharCast(ssKey1.data()), ssKey1.size());
leveldb::Slice slKey2((const char*)ssKey2.data(), ssKey2.size()); leveldb::Slice slKey2(CharCast(ssKey2.data()), ssKey2.size());
pdb->CompactRange(&slKey1, &slKey2); pdb->CompactRange(&slKey1, &slKey2);
} }

View File

@ -19,8 +19,8 @@
#include <util/threadnames.h> #include <util/threadnames.h>
#include <util/translation.h> #include <util/translation.h>
#include <cstdio>
#include <deque> #include <deque>
#include <stdio.h>
#include <string> #include <string>
#include <sys/types.h> #include <sys/types.h>

View File

@ -355,7 +355,7 @@ Binary Session::MyDestination() const
uint16_t cert_len; uint16_t cert_len;
memcpy(&cert_len, &m_private_key.at(CERT_LEN_POS), sizeof(cert_len)); memcpy(&cert_len, &m_private_key.at(CERT_LEN_POS), sizeof(cert_len));
cert_len = be16toh(cert_len); cert_len = be16toh_internal(cert_len);
const size_t dest_len = DEST_LEN_BASE + cert_len; const size_t dest_len = DEST_LEN_BASE + cert_len;

View File

@ -12,11 +12,11 @@
#include <tinyformat.h> #include <tinyformat.h>
#include <util/system.h> #include <util/system.h>
#include <cstdio>
#include <cstdlib>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>

View File

@ -10,10 +10,10 @@
#include <util/strencodings.h> #include <util/strencodings.h>
#include <cstdint> #include <cstdint>
#include <cstdlib>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <system_error> #include <system_error>
#include <unistd.h> #include <unistd.h>

View File

@ -9,9 +9,8 @@
#include <prevector.h> #include <prevector.h>
#include <support/allocators/pool.h> #include <support/allocators/pool.h>
#include <stdlib.h>
#include <cassert> #include <cassert>
#include <cstdlib>
#include <map> #include <map>
#include <memory> #include <memory>
#include <set> #include <set>

View File

@ -6,12 +6,12 @@
#define BITCOIN_PREVECTOR_H #define BITCOIN_PREVECTOR_H
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <cstdlib>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -47,26 +47,28 @@ public:
typedef const value_type* const_pointer; typedef const value_type* const_pointer;
class iterator { class iterator {
T* ptr; T* ptr{};
public: public:
typedef Diff difference_type; typedef Diff difference_type;
typedef T value_type; typedef T value_type;
typedef T* pointer; typedef T* pointer;
typedef T& reference; typedef T& reference;
typedef std::random_access_iterator_tag iterator_category; using element_type = T;
using iterator_category = std::contiguous_iterator_tag;
iterator() = default;
iterator(T* ptr_) : ptr(ptr_) {} iterator(T* ptr_) : ptr(ptr_) {}
T& operator*() const { return *ptr; } T& operator*() const { return *ptr; }
T* operator->() const { return ptr; } T* operator->() const { return ptr; }
T& operator[](size_type pos) { return ptr[pos]; } T& operator[](size_type pos) const { return ptr[pos]; }
const T& operator[](size_type pos) const { return ptr[pos]; }
iterator& operator++() { ptr++; return *this; } iterator& operator++() { ptr++; return *this; }
iterator& operator--() { ptr--; return *this; } iterator& operator--() { ptr--; return *this; }
iterator operator++(int) { iterator copy(*this); ++(*this); return copy; } iterator operator++(int) { iterator copy(*this); ++(*this); return copy; }
iterator operator--(int) { iterator copy(*this); --(*this); return copy; } iterator operator--(int) { iterator copy(*this); --(*this); return copy; }
difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); } difference_type friend operator-(iterator a, iterator b) { return (&(*a) - &(*b)); }
iterator operator+(size_type n) { return iterator(ptr + n); } iterator operator+(size_type n) const { return iterator(ptr + n); }
iterator friend operator+(size_type n, iterator x) { return x + n; }
iterator& operator+=(size_type n) { ptr += n; return *this; } iterator& operator+=(size_type n) { ptr += n; return *this; }
iterator operator-(size_type n) { return iterator(ptr - n); } iterator operator-(size_type n) const { return iterator(ptr - n); }
iterator& operator-=(size_type n) { ptr -= n; return *this; } iterator& operator-=(size_type n) { ptr -= n; return *this; }
bool operator==(iterator x) const { return ptr == x.ptr; } bool operator==(iterator x) const { return ptr == x.ptr; }
bool operator!=(iterator x) const { return ptr != x.ptr; } bool operator!=(iterator x) const { return ptr != x.ptr; }
@ -77,18 +79,17 @@ public:
}; };
class reverse_iterator { class reverse_iterator {
T* ptr; T* ptr{};
public: public:
typedef Diff difference_type; typedef Diff difference_type;
typedef T value_type; typedef T value_type;
typedef T* pointer; typedef T* pointer;
typedef T& reference; typedef T& reference;
typedef std::bidirectional_iterator_tag iterator_category; typedef std::bidirectional_iterator_tag iterator_category;
reverse_iterator() = default;
reverse_iterator(T* ptr_) : ptr(ptr_) {} reverse_iterator(T* ptr_) : ptr(ptr_) {}
T& operator*() { return *ptr; } T& operator*() const { return *ptr; }
const T& operator*() const { return *ptr; } T* operator->() const { return ptr; }
T* operator->() { return ptr; }
const T* operator->() const { return ptr; }
reverse_iterator& operator--() { ptr++; return *this; } reverse_iterator& operator--() { ptr++; return *this; }
reverse_iterator& operator++() { ptr--; return *this; } reverse_iterator& operator++() { ptr--; return *this; }
reverse_iterator operator++(int) { reverse_iterator copy(*this); ++(*this); return copy; } reverse_iterator operator++(int) { reverse_iterator copy(*this); ++(*this); return copy; }
@ -98,13 +99,15 @@ public:
}; };
class const_iterator { class const_iterator {
const T* ptr; const T* ptr{};
public: public:
typedef Diff difference_type; typedef Diff difference_type;
typedef const T value_type; typedef const T value_type;
typedef const T* pointer; typedef const T* pointer;
typedef const T& reference; typedef const T& reference;
typedef std::random_access_iterator_tag iterator_category; using element_type = const T;
using iterator_category = std::contiguous_iterator_tag;
const_iterator() = default;
const_iterator(const T* ptr_) : ptr(ptr_) {} const_iterator(const T* ptr_) : ptr(ptr_) {}
const_iterator(iterator x) : ptr(&(*x)) {} const_iterator(iterator x) : ptr(&(*x)) {}
const T& operator*() const { return *ptr; } const T& operator*() const { return *ptr; }
@ -115,9 +118,10 @@ public:
const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; } const_iterator operator++(int) { const_iterator copy(*this); ++(*this); return copy; }
const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; } const_iterator operator--(int) { const_iterator copy(*this); --(*this); return copy; }
difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); } difference_type friend operator-(const_iterator a, const_iterator b) { return (&(*a) - &(*b)); }
const_iterator operator+(size_type n) { return const_iterator(ptr + n); } const_iterator operator+(size_type n) const { return const_iterator(ptr + n); }
const_iterator friend operator+(size_type n, const_iterator x) { return x + n; }
const_iterator& operator+=(size_type n) { ptr += n; return *this; } const_iterator& operator+=(size_type n) { ptr += n; return *this; }
const_iterator operator-(size_type n) { return const_iterator(ptr - n); } const_iterator operator-(size_type n) const { return const_iterator(ptr - n); }
const_iterator& operator-=(size_type n) { ptr -= n; return *this; } const_iterator& operator-=(size_type n) { ptr -= n; return *this; }
bool operator==(const_iterator x) const { return ptr == x.ptr; } bool operator==(const_iterator x) const { return ptr == x.ptr; }
bool operator!=(const_iterator x) const { return ptr != x.ptr; } bool operator!=(const_iterator x) const { return ptr != x.ptr; }
@ -128,13 +132,14 @@ public:
}; };
class const_reverse_iterator { class const_reverse_iterator {
const T* ptr; const T* ptr{};
public: public:
typedef Diff difference_type; typedef Diff difference_type;
typedef const T value_type; typedef const T value_type;
typedef const T* pointer; typedef const T* pointer;
typedef const T& reference; typedef const T& reference;
typedef std::bidirectional_iterator_tag iterator_category; typedef std::bidirectional_iterator_tag iterator_category;
const_reverse_iterator() = default;
const_reverse_iterator(const T* ptr_) : ptr(ptr_) {} const_reverse_iterator(const T* ptr_) : ptr(ptr_) {}
const_reverse_iterator(reverse_iterator x) : ptr(&(*x)) {} const_reverse_iterator(reverse_iterator x) : ptr(&(*x)) {}
const T& operator*() const { return *ptr; } const T& operator*() const { return *ptr; }

View File

@ -18,7 +18,7 @@
#include <util/system.h> #include <util/system.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <stdio.h> #include <cstdio>
#include <QCloseEvent> #include <QCloseEvent>
#include <QLabel> #include <QLabel>

View File

@ -19,7 +19,7 @@
#include <array> #include <array>
#include <cmath> #include <cmath>
#include <stdlib.h> #include <cstdlib>
#include <thread> #include <thread>
#ifdef WIN32 #ifdef WIN32

View File

@ -11,6 +11,7 @@
#include <span.h> #include <span.h>
#include <uint256.h> #include <uint256.h>
#include <bit>
#include <cassert> #include <cassert>
#include <chrono> // For std::chrono::microseconds #include <chrono> // For std::chrono::microseconds
#include <cstdint> #include <cstdint>
@ -196,7 +197,7 @@ public:
{ {
assert(range); assert(range);
--range; --range;
int bits = CountBits(range); int bits = std::bit_width(range);
while (true) { while (true) {
uint64_t ret = randbits(bits); uint64_t ret = randbits(bits);
if (ret <= range) return ret; if (ret <= range) return ret;

View File

@ -60,27 +60,27 @@ template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
} }
template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj) template<typename Stream> inline void ser_writedata16(Stream &s, uint16_t obj)
{ {
obj = htole16(obj); obj = htole16_internal(obj);
s.write(AsBytes(Span{&obj, 1})); s.write(AsBytes(Span{&obj, 1}));
} }
template<typename Stream> inline void ser_writedata16be(Stream &s, uint16_t obj) template<typename Stream> inline void ser_writedata16be(Stream &s, uint16_t obj)
{ {
obj = htobe16(obj); obj = htobe16_internal(obj);
s.write(AsBytes(Span{&obj, 1})); s.write(AsBytes(Span{&obj, 1}));
} }
template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj) template<typename Stream> inline void ser_writedata32(Stream &s, uint32_t obj)
{ {
obj = htole32(obj); obj = htole32_internal(obj);
s.write(AsBytes(Span{&obj, 1})); s.write(AsBytes(Span{&obj, 1}));
} }
template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj) template<typename Stream> inline void ser_writedata32be(Stream &s, uint32_t obj)
{ {
obj = htobe32(obj); obj = htobe32_internal(obj);
s.write(AsBytes(Span{&obj, 1})); s.write(AsBytes(Span{&obj, 1}));
} }
template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj) template<typename Stream> inline void ser_writedata64(Stream &s, uint64_t obj)
{ {
obj = htole64(obj); obj = htole64_internal(obj);
s.write(AsBytes(Span{&obj, 1})); s.write(AsBytes(Span{&obj, 1}));
} }
template<typename Stream> inline uint8_t ser_readdata8(Stream &s) template<typename Stream> inline uint8_t ser_readdata8(Stream &s)
@ -93,31 +93,31 @@ template<typename Stream> inline uint16_t ser_readdata16(Stream &s)
{ {
uint16_t obj; uint16_t obj;
s.read(AsWritableBytes(Span{&obj, 1})); s.read(AsWritableBytes(Span{&obj, 1}));
return le16toh(obj); return le16toh_internal(obj);
} }
template<typename Stream> inline uint16_t ser_readdata16be(Stream &s) template<typename Stream> inline uint16_t ser_readdata16be(Stream &s)
{ {
uint16_t obj; uint16_t obj;
s.read(AsWritableBytes(Span{&obj, 1})); s.read(AsWritableBytes(Span{&obj, 1}));
return be16toh(obj); return be16toh_internal(obj);
} }
template<typename Stream> inline uint32_t ser_readdata32(Stream &s) template<typename Stream> inline uint32_t ser_readdata32(Stream &s)
{ {
uint32_t obj; uint32_t obj;
s.read(AsWritableBytes(Span{&obj, 1})); s.read(AsWritableBytes(Span{&obj, 1}));
return le32toh(obj); return le32toh_internal(obj);
} }
template<typename Stream> inline uint32_t ser_readdata32be(Stream &s) template<typename Stream> inline uint32_t ser_readdata32be(Stream &s)
{ {
uint32_t obj; uint32_t obj;
s.read(AsWritableBytes(Span{&obj, 1})); s.read(AsWritableBytes(Span{&obj, 1}));
return be32toh(obj); return be32toh_internal(obj);
} }
template<typename Stream> inline uint64_t ser_readdata64(Stream &s) template<typename Stream> inline uint64_t ser_readdata64(Stream &s)
{ {
uint64_t obj; uint64_t obj;
s.read(AsWritableBytes(Span{&obj, 1})); s.read(AsWritableBytes(Span{&obj, 1}));
return le64toh(obj); return le64toh_internal(obj);
} }
@ -655,11 +655,11 @@ struct CustomUintFormatter
{ {
if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range"); if (v < 0 || v > MAX) throw std::ios_base::failure("CustomUintFormatter value out of range");
if (BigEndian) { if (BigEndian) {
uint64_t raw = htobe64(v); uint64_t raw = htobe64_internal(v);
s.write({BytePtr(&raw) + 8 - Bytes, Bytes}); s.write(AsBytes(Span{&raw, 1}).last(Bytes));
} else { } else {
uint64_t raw = htole64(v); uint64_t raw = htole64_internal(v);
s.write({BytePtr(&raw), Bytes}); s.write(AsBytes(Span{&raw, 1}).first(Bytes));
} }
} }
@ -669,11 +669,11 @@ struct CustomUintFormatter
static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small"); static_assert(std::numeric_limits<U>::max() >= MAX && std::numeric_limits<U>::min() <= 0, "Assigned type too small");
uint64_t raw = 0; uint64_t raw = 0;
if (BigEndian) { if (BigEndian) {
s.read({BytePtr(&raw) + 8 - Bytes, Bytes}); s.read(AsWritableBytes(Span{&raw, 1}).last(Bytes));
v = static_cast<I>(be64toh(raw)); v = static_cast<I>(be64toh_internal(raw));
} else { } else {
s.read({BytePtr(&raw), Bytes}); s.read(AsWritableBytes(Span{&raw, 1}).first(Bytes));
v = static_cast<I>(le64toh(raw)); v = static_cast<I>(le64toh_internal(raw));
} }
} }
}; };

View File

@ -5,10 +5,10 @@
#ifndef BITCOIN_SPAN_H #ifndef BITCOIN_SPAN_H
#define BITCOIN_SPAN_H #define BITCOIN_SPAN_H
#include <type_traits>
#include <cstddef>
#include <algorithm> #include <algorithm>
#include <assert.h> #include <cassert>
#include <cstddef>
#include <type_traits>
#ifdef DEBUG_CORE #ifdef DEBUG_CORE
#define CONSTEXPR_IF_NOT_DEBUG #define CONSTEXPR_IF_NOT_DEBUG
@ -243,21 +243,16 @@ T& SpanPopBack(Span<T>& span)
return back; return back;
} }
//! Convert a data pointer to a std::byte data pointer.
//! Where possible, please use the safer AsBytes helpers.
inline const std::byte* BytePtr(const void* data) { return reinterpret_cast<const std::byte*>(data); }
inline std::byte* BytePtr(void* data) { return reinterpret_cast<std::byte*>(data); }
// From C++20 as_bytes and as_writeable_bytes // From C++20 as_bytes and as_writeable_bytes
template <typename T> template <typename T>
Span<const std::byte> AsBytes(Span<T> s) noexcept Span<const std::byte> AsBytes(Span<T> s) noexcept
{ {
return {BytePtr(s.data()), s.size_bytes()}; return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
} }
template <typename T> template <typename T>
Span<std::byte> AsWritableBytes(Span<T> s) noexcept Span<std::byte> AsWritableBytes(Span<T> s) noexcept
{ {
return {BytePtr(s.data()), s.size_bytes()}; return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
} }
template <typename V> template <typename V>
@ -272,10 +267,10 @@ Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
} }
// Helper functions to safely cast to unsigned char pointers. // Helper functions to safely cast to unsigned char pointers.
inline unsigned char* UCharCast(char* c) { return (unsigned char*)c; } inline unsigned char* UCharCast(char* c) { return reinterpret_cast<unsigned char*>(c); }
inline unsigned char* UCharCast(unsigned char* c) { return c; } inline unsigned char* UCharCast(unsigned char* c) { return c; }
inline unsigned char* UCharCast(std::byte* c) { return (unsigned char*)c; } inline unsigned char* UCharCast(std::byte* c) { return reinterpret_cast<unsigned char*>(c); }
inline const unsigned char* UCharCast(const char* c) { return (unsigned char*)c; } inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast<const unsigned char*>(c); }
inline const unsigned char* UCharCast(const unsigned char* c) { return c; } inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); } inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); }

View File

@ -13,11 +13,11 @@
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
#include <cstdio>
#include <ios> #include <ios>
#include <limits> #include <limits>
#include <optional> #include <optional>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <utility> #include <utility>

View File

@ -6,7 +6,7 @@
#ifndef BITCOIN_SUPPORT_CLEANSE_H #ifndef BITCOIN_SUPPORT_CLEANSE_H
#define BITCOIN_SUPPORT_CLEANSE_H #define BITCOIN_SUPPORT_CLEANSE_H
#include <stdlib.h> #include <cstdlib>
/** Secure overwrite a buffer (possibly containing secret data) with zero-bytes. The write /** Secure overwrite a buffer (possibly containing secret data) with zero-bytes. The write
* operation will not be optimized out by the compiler. */ * operation will not be optimized out by the compiler. */

1043
src/test/argsman_tests.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,13 +4,13 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <stdlib.h>
#include <chain.h> #include <chain.h>
#include <rpc/blockchain.h> #include <rpc/blockchain.h>
#include <test/util/setup_common.h> #include <test/util/setup_common.h>
#include <util/string.h> #include <util/string.h>
#include <cstdlib>
/* Equality between doubles is imprecise. Comparison should be done /* Equality between doubles is imprecise. Comparison should be done
* with a small threshold of tolerance, rather than exact equality. * with a small threshold of tolerance, rather than exact equality.
*/ */

View File

@ -16,9 +16,9 @@ BOOST_AUTO_TEST_CASE(bswap_tests)
uint16_t e1 = 0x3412; uint16_t e1 = 0x3412;
uint32_t e2 = 0xbc9a7856; uint32_t e2 = 0xbc9a7856;
uint64_t e3 = 0xbc9a78563412f0de; uint64_t e3 = 0xbc9a78563412f0de;
BOOST_CHECK(bswap_16(u1) == e1); BOOST_CHECK(internal_bswap_16(u1) == e1);
BOOST_CHECK(bswap_32(u2) == e2); BOOST_CHECK(internal_bswap_32(u2) == e2);
BOOST_CHECK(bswap_64(u3) == e3); BOOST_CHECK(internal_bswap_64(u3) == e3);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -1083,28 +1083,6 @@ BOOST_AUTO_TEST_CASE(hkdf_hmac_sha256_l32_tests)
"8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d"); "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d");
} }
BOOST_AUTO_TEST_CASE(countbits_tests)
{
FastRandomContext ctx;
for (unsigned int i = 0; i <= 64; ++i) {
if (i == 0) {
// Check handling of zero.
BOOST_CHECK_EQUAL(CountBits(0), 0U);
} else if (i < 10) {
for (uint64_t j = (uint64_t)1 << (i - 1); (j >> i) == 0; ++j) {
// Exhaustively test up to 10 bits
BOOST_CHECK_EQUAL(CountBits(j), i);
}
} else {
for (int k = 0; k < 1000; k++) {
// Randomly test 1000 samples of each length above 10 bits.
uint64_t j = ((uint64_t)1) << (i - 1) | ctx.randbits(i - 1);
BOOST_CHECK_EQUAL(CountBits(j), i);
}
}
}
}
BOOST_AUTO_TEST_CASE(sha256d64) BOOST_AUTO_TEST_CASE(sha256d64)
{ {
for (int i = 0; i <= 32; ++i) { for (int i = 0; i <= 32; ++i) {

View File

@ -75,7 +75,6 @@ FUZZ_TARGET_INIT(integer, initialize_integer)
static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); static const uint256 u256_max(uint256S("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
const std::vector<uint256> v256{u256, u256_min, u256_max}; const std::vector<uint256> v256{u256, u256_min, u256_max};
(void)ComputeMerkleRoot(v256); (void)ComputeMerkleRoot(v256);
(void)CountBits(u64);
(void)DecompressAmount(u64); (void)DecompressAmount(u64);
{ {

View File

@ -4,8 +4,8 @@
#include <event2/event.h> #include <event2/event.h>
#include <cstdlib>
#include <map> #include <map>
#include <stdlib.h>
#include <support/events.h> #include <support/events.h>

File diff suppressed because it is too large Load Diff

View File

@ -5,11 +5,11 @@
#include <util/asmap.h> #include <util/asmap.h>
#include <clientversion.h> #include <clientversion.h>
#include <crypto/common.h>
#include <fs.h> #include <fs.h>
#include <logging.h> #include <logging.h>
#include <streams.h> #include <streams.h>
#include <bit>
#include <cassert> #include <cassert>
#include <map> #include <map>
#include <vector> #include <vector>
@ -108,7 +108,7 @@ uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip)
} else if (opcode == Instruction::MATCH) { } else if (opcode == Instruction::MATCH) {
match = DecodeMatch(pos, endpos); match = DecodeMatch(pos, endpos);
if (match == INVALID) break; // Match bits straddle EOF if (match == INVALID) break; // Match bits straddle EOF
matchlen = CountBits(match) - 1; matchlen = std::bit_width(match) - 1;
if (bits < matchlen) break; // Not enough input bits if (bits < matchlen) break; // Not enough input bits
for (uint32_t bit = 0; bit < matchlen; bit++) { for (uint32_t bit = 0; bit < matchlen; bit++) {
if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) { if ((ip[ip.size() - bits]) != ((match >> (matchlen - 1 - bit)) & 1)) {
@ -172,7 +172,7 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
} else if (opcode == Instruction::MATCH) { } else if (opcode == Instruction::MATCH) {
uint32_t match = DecodeMatch(pos, endpos); uint32_t match = DecodeMatch(pos, endpos);
if (match == INVALID) return false; // Match bits straddle EOF if (match == INVALID) return false; // Match bits straddle EOF
int matchlen = CountBits(match) - 1; int matchlen = std::bit_width(match) - 1;
if (prevopcode != Instruction::MATCH) had_incomplete_match = false; if (prevopcode != Instruction::MATCH) had_incomplete_match = false;
if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete if (matchlen < 8 && had_incomplete_match) return false; // Within a sequence of matches only at most one should be incomplete
had_incomplete_match = (matchlen < 8); had_incomplete_match = (matchlen < 8);

View File

@ -3,11 +3,11 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <sstream> #include <sstream>
#include <stdio.h>
#include <tinyformat.h> #include <tinyformat.h>
#include <util/bip32.h> #include <util/bip32.h>
#include <util/strencodings.h> #include <util/strencodings.h>
#include <cstdio>
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath) bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath)
{ {

View File

@ -7,8 +7,8 @@
#include <fs.h> #include <fs.h>
#include <cstdio>
#include <limits> #include <limits>
#include <stdio.h>
#include <string> #include <string>
#include <utility> #include <utility>

View File

@ -5,7 +5,8 @@
#include <util/url.h> #include <util/url.h>
#include <event2/http.h> #include <event2/http.h>
#include <stdlib.h>
#include <cstdlib>
#include <string> #include <string>
std::string urlDecode(const std::string &urlEncoded) { std::string urlDecode(const std::string &urlEncoded) {

View File

@ -25,6 +25,10 @@
#endif #endif
namespace { namespace {
Span<const std::byte> SpanFromDbt(const BerkeleyBatch::SafeDbt& dbt)
{
return {reinterpret_cast<const std::byte*>(dbt.get_data()), dbt.get_size()};
}
//! Make sure database has a unique fileid within the environment. If it //! Make sure database has a unique fileid within the environment. If it
//! doesn't, throw an error. BDB caches do not work properly when more than one //! doesn't, throw an error. BDB caches do not work properly when more than one
@ -689,10 +693,10 @@ bool BerkeleyBatch::ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool&
// Convert to streams // Convert to streams
ssKey.SetType(SER_DISK); ssKey.SetType(SER_DISK);
ssKey.clear(); ssKey.clear();
ssKey.write({BytePtr(datKey.get_data()), datKey.get_size()}); ssKey.write(SpanFromDbt(datKey));
ssValue.SetType(SER_DISK); ssValue.SetType(SER_DISK);
ssValue.clear(); ssValue.clear();
ssValue.write({BytePtr(datValue.get_data()), datValue.get_size()}); ssValue.write(SpanFromDbt(datValue));
return true; return true;
} }
@ -764,7 +768,8 @@ bool BerkeleyBatch::ReadKey(CDataStream&& key, CDataStream& value)
SafeDbt datValue; SafeDbt datValue;
int ret = pdb->get(activeTxn, datKey, datValue, 0); int ret = pdb->get(activeTxn, datKey, datValue, 0);
if (ret == 0 && datValue.get_data() != nullptr) { if (ret == 0 && datValue.get_data() != nullptr) {
value.write({BytePtr(datValue.get_data()), datValue.get_size()}); value.clear();
value.write(SpanFromDbt(datValue));
return true; return true;
} }
return false; return false;

View File

@ -168,6 +168,7 @@ public:
/** RAII class that provides access to a Berkeley database */ /** RAII class that provides access to a Berkeley database */
class BerkeleyBatch : public DatabaseBatch class BerkeleyBatch : public DatabaseBatch
{ {
public:
/** RAII class that automatically cleanses its data on destruction */ /** RAII class that automatically cleanses its data on destruction */
class SafeDbt final class SafeDbt final
{ {

View File

@ -111,7 +111,7 @@ bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vch
memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE); memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
if(!cKeyCrypter.SetKey(vMasterKey, chIV)) if(!cKeyCrypter.SetKey(vMasterKey, chIV))
return false; return false;
return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext); return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext);
} }
// General secure AES 256 CBC encryption routine // General secure AES 256 CBC encryption routine

View File

@ -25,6 +25,12 @@ static constexpr int32_t WALLET_SCHEMA_VERSION = 0;
static Mutex g_sqlite_mutex; static Mutex g_sqlite_mutex;
static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0; static int g_sqlite_count GUARDED_BY(g_sqlite_mutex) = 0;
static Span<const std::byte> SpanFromBlob(sqlite3_stmt* stmt, int col)
{
return {reinterpret_cast<const std::byte*>(sqlite3_column_blob(stmt, col)),
static_cast<size_t>(sqlite3_column_bytes(stmt, col))};
}
static void ErrorLogCallback(void* arg, int code, const char* msg) static void ErrorLogCallback(void* arg, int code, const char* msg)
{ {
// From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option: // From sqlite3_config() documentation for the SQLITE_CONFIG_LOG option:
@ -404,9 +410,8 @@ bool SQLiteBatch::ReadKey(CDataStream&& key, CDataStream& value)
return false; return false;
} }
// Leftmost column in result is index 0 // Leftmost column in result is index 0
const std::byte* data{BytePtr(sqlite3_column_blob(m_read_stmt, 0))}; value.clear();
size_t data_size(sqlite3_column_bytes(m_read_stmt, 0)); value.write(SpanFromBlob(m_read_stmt, 0));
value.write({data, data_size});
sqlite3_clear_bindings(m_read_stmt); sqlite3_clear_bindings(m_read_stmt);
sqlite3_reset(m_read_stmt); sqlite3_reset(m_read_stmt);
@ -495,13 +500,12 @@ bool SQLiteBatch::ReadAtCursor(CDataStream& key, CDataStream& value, bool& compl
return false; return false;
} }
key.clear();
value.clear();
// Leftmost column in result is index 0 // Leftmost column in result is index 0
const std::byte* key_data{BytePtr(sqlite3_column_blob(m_cursor_stmt, 0))}; key.write(SpanFromBlob(m_cursor_stmt, 0));
size_t key_data_size(sqlite3_column_bytes(m_cursor_stmt, 0)); value.write(SpanFromBlob(m_cursor_stmt, 1));
key.write({key_data, key_data_size});
const std::byte* value_data{BytePtr(sqlite3_column_blob(m_cursor_stmt, 1))};
size_t value_data_size(sqlite3_column_bytes(m_cursor_stmt, 1));
value.write({value_data, value_data_size});
return true; return true;
} }