2020-12-31 09:48:25 +01:00
|
|
|
// Copyright (c) 2014-2020 The Bitcoin Core developers
|
2014-09-27 13:49:21 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-05-03 01:04:18 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_CRYPTO_COMMON_H
|
|
|
|
#define BITCOIN_CRYPTO_COMMON_H
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <compat/endian.h>
|
2014-12-19 09:53:43 +01:00
|
|
|
|
2023-12-08 19:10:18 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
|
2014-12-19 09:53:43 +01:00
|
|
|
uint16_t static inline ReadLE16(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint16_t x;
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(&x, ptr, 2);
|
2017-01-10 14:14:01 -08:00
|
|
|
return le16toh(x);
|
2014-12-19 09:53:43 +01:00
|
|
|
}
|
2014-06-09 15:05:28 -04:00
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
uint32_t static inline ReadLE32(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t x;
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(&x, ptr, 4);
|
2017-01-10 14:14:01 -08:00
|
|
|
return le32toh(x);
|
2014-06-09 15:05:28 -04:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
uint64_t static inline ReadLE64(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t x;
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(&x, ptr, 8);
|
2017-01-10 14:14:01 -08:00
|
|
|
return le64toh(x);
|
2014-12-19 09:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void static inline WriteLE16(unsigned char* ptr, uint16_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint16_t v = htole16(x);
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(ptr, &v, 2);
|
2014-06-09 15:05:28 -04:00
|
|
|
}
|
2014-05-03 01:04:18 +02:00
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
void static inline WriteLE32(unsigned char* ptr, uint32_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t v = htole32(x);
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(ptr, &v, 4);
|
2014-06-09 15:05:28 -04:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
void static inline WriteLE64(unsigned char* ptr, uint64_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t v = htole64(x);
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(ptr, &v, 8);
|
2014-06-09 15:05:28 -04:00
|
|
|
}
|
2014-05-03 01:04:18 +02:00
|
|
|
|
2020-08-27 11:03:21 +02:00
|
|
|
uint16_t static inline ReadBE16(const unsigned char* ptr)
|
|
|
|
{
|
|
|
|
uint16_t x;
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(&x, ptr, 2);
|
2020-08-27 11:03:21 +02:00
|
|
|
return be16toh(x);
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
uint32_t static inline ReadBE32(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t x;
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(&x, ptr, 4);
|
2017-01-10 14:14:01 -08:00
|
|
|
return be32toh(x);
|
2014-05-03 01:04:18 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
uint64_t static inline ReadBE64(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t x;
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(&x, ptr, 8);
|
2017-01-10 14:14:01 -08:00
|
|
|
return be64toh(x);
|
2014-05-03 01:04:18 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
void static inline WriteBE32(unsigned char* ptr, uint32_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t v = htobe32(x);
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(ptr, &v, 4);
|
2014-05-03 01:04:18 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:23:32 +02:00
|
|
|
void static inline WriteBE64(unsigned char* ptr, uint64_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t v = htobe64(x);
|
2023-07-22 16:01:44 +02:00
|
|
|
memcpy(ptr, &v, 8);
|
2014-05-03 01:04:18 +02:00
|
|
|
}
|
|
|
|
|
2014-09-27 13:49:21 +02:00
|
|
|
#endif // BITCOIN_CRYPTO_COMMON_H
|