2014-12-16 17:47:57 -08:00
|
|
|
// Copyright (c) 2014 The Bitcoin Core developers
|
2014-09-27 04:49:21 -07:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2014-05-02 16:04:18 -07:00
|
|
|
|
|
|
|
#ifndef BITCOIN_CRYPTO_COMMON_H
|
|
|
|
#define BITCOIN_CRYPTO_COMMON_H
|
|
|
|
|
2014-06-09 12:05:28 -07:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2018-01-21 22:41:21 -08:00
|
|
|
#include "config/bitcoin-config.h"
|
2014-06-09 12:05:28 -07:00
|
|
|
#endif
|
2014-09-27 04:49:21 -07:00
|
|
|
|
2014-05-02 16:04:18 -07:00
|
|
|
#include <stdint.h>
|
2016-08-18 15:38:20 -07:00
|
|
|
#include <assert.h>
|
2017-01-10 14:14:01 -08:00
|
|
|
#include <string.h>
|
2014-09-27 04:49:21 -07:00
|
|
|
|
2014-12-19 00:53:43 -08:00
|
|
|
#include "compat/endian.h"
|
|
|
|
|
2016-08-18 15:38:20 -07:00
|
|
|
#if defined(NDEBUG)
|
2017-07-21 01:18:09 -07:00
|
|
|
# error "Zcash cannot be compiled without assertions."
|
2016-08-18 15:38:20 -07:00
|
|
|
#endif
|
|
|
|
|
2014-12-19 00:53:43 -08:00
|
|
|
uint16_t static inline ReadLE16(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint16_t x;
|
|
|
|
memcpy((char*)&x, ptr, 2);
|
|
|
|
return le16toh(x);
|
2014-12-19 00:53:43 -08:00
|
|
|
}
|
2014-06-09 12:05:28 -07:00
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
uint32_t static inline ReadLE32(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t x;
|
|
|
|
memcpy((char*)&x, ptr, 4);
|
|
|
|
return le32toh(x);
|
2014-06-09 12:05:28 -07:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
uint64_t static inline ReadLE64(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t x;
|
|
|
|
memcpy((char*)&x, ptr, 8);
|
|
|
|
return le64toh(x);
|
2014-12-19 00:53:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void static inline WriteLE16(unsigned char* ptr, uint16_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint16_t v = htole16(x);
|
|
|
|
memcpy(ptr, (char*)&v, 2);
|
2014-06-09 12:05:28 -07:00
|
|
|
}
|
2014-05-02 16:04:18 -07:00
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
void static inline WriteLE32(unsigned char* ptr, uint32_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t v = htole32(x);
|
|
|
|
memcpy(ptr, (char*)&v, 4);
|
2014-06-09 12:05:28 -07:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
void static inline WriteLE64(unsigned char* ptr, uint64_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t v = htole64(x);
|
|
|
|
memcpy(ptr, (char*)&v, 8);
|
2014-06-09 12:05:28 -07:00
|
|
|
}
|
2014-05-02 16:04:18 -07:00
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
uint32_t static inline ReadBE32(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t x;
|
|
|
|
memcpy((char*)&x, ptr, 4);
|
|
|
|
return be32toh(x);
|
2014-05-02 16:04:18 -07:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
uint64_t static inline ReadBE64(const unsigned char* ptr)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t x;
|
|
|
|
memcpy((char*)&x, ptr, 8);
|
|
|
|
return be64toh(x);
|
2014-05-02 16:04:18 -07:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
void static inline WriteBE32(unsigned char* ptr, uint32_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint32_t v = htobe32(x);
|
|
|
|
memcpy(ptr, (char*)&v, 4);
|
2014-05-02 16:04:18 -07:00
|
|
|
}
|
|
|
|
|
2014-09-24 23:23:32 -07:00
|
|
|
void static inline WriteBE64(unsigned char* ptr, uint64_t x)
|
|
|
|
{
|
2017-01-10 14:14:01 -08:00
|
|
|
uint64_t v = htobe64(x);
|
|
|
|
memcpy(ptr, (char*)&v, 8);
|
2014-05-02 16:04:18 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 04:49:21 -07:00
|
|
|
#endif // BITCOIN_CRYPTO_COMMON_H
|