Skip to content

Commit

Permalink
Merge GH #3649 Remove some macros from loadstor.h
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Jul 28, 2023
2 parents e735243 + b174490 commit 3c7229c
Showing 1 changed file with 52 additions and 56 deletions.
108 changes: 52 additions & 56 deletions src/lib/utils/loadstor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@
#include <botan/internal/bswap.h>
#include <vector>

#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
#define BOTAN_ENDIAN_N2L(x) reverse_bytes(x)
#define BOTAN_ENDIAN_L2N(x) reverse_bytes(x)
#define BOTAN_ENDIAN_N2B(x) (x)
#define BOTAN_ENDIAN_B2N(x) (x)

#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
#define BOTAN_ENDIAN_N2L(x) (x)
#define BOTAN_ENDIAN_L2N(x) (x)
#define BOTAN_ENDIAN_N2B(x) reverse_bytes(x)
#define BOTAN_ENDIAN_B2N(x) reverse_bytes(x)

#endif

namespace Botan {

/**
Expand Down Expand Up @@ -138,10 +124,10 @@ template <>
inline constexpr uint16_t load_be<uint16_t>(const uint8_t in[], size_t off) {
in += off * sizeof(uint16_t);

#if defined(BOTAN_ENDIAN_N2B)
uint16_t x = 0;
typecast_copy(x, in);
return BOTAN_ENDIAN_N2B(x);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
return typecast_copy<uint16_t>(in);
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
return reverse_bytes(typecast_copy<uint16_t>(in));
#else
return make_uint16(in[0], in[1]);
#endif
Expand All @@ -157,10 +143,10 @@ template <>
inline constexpr uint16_t load_le<uint16_t>(const uint8_t in[], size_t off) {
in += off * sizeof(uint16_t);

#if defined(BOTAN_ENDIAN_N2L)
uint16_t x = 0;
typecast_copy(x, in);
return BOTAN_ENDIAN_N2L(x);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
return reverse_bytes(typecast_copy<uint16_t>(in));
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
return typecast_copy<uint16_t>(in);
#else
return make_uint16(in[1], in[0]);
#endif
Expand All @@ -175,10 +161,11 @@ inline constexpr uint16_t load_le<uint16_t>(const uint8_t in[], size_t off) {
template <>
inline constexpr uint32_t load_be<uint32_t>(const uint8_t in[], size_t off) {
in += off * sizeof(uint32_t);
#if defined(BOTAN_ENDIAN_N2B)
uint32_t x = 0;
typecast_copy(x, in);
return BOTAN_ENDIAN_N2B(x);

#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
return typecast_copy<uint32_t>(in);
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
return reverse_bytes(typecast_copy<uint32_t>(in));
#else
return make_uint32(in[0], in[1], in[2], in[3]);
#endif
Expand All @@ -193,10 +180,11 @@ inline constexpr uint32_t load_be<uint32_t>(const uint8_t in[], size_t off) {
template <>
inline constexpr uint32_t load_le<uint32_t>(const uint8_t in[], size_t off) {
in += off * sizeof(uint32_t);
#if defined(BOTAN_ENDIAN_N2L)
uint32_t x = 0;
typecast_copy(x, in);
return BOTAN_ENDIAN_N2L(x);

#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
return reverse_bytes(typecast_copy<uint32_t>(in));
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
return typecast_copy<uint32_t>(in);
#else
return make_uint32(in[3], in[2], in[1], in[0]);
#endif
Expand All @@ -211,10 +199,11 @@ inline constexpr uint32_t load_le<uint32_t>(const uint8_t in[], size_t off) {
template <>
inline constexpr uint64_t load_be<uint64_t>(const uint8_t in[], size_t off) {
in += off * sizeof(uint64_t);
#if defined(BOTAN_ENDIAN_N2B)
uint64_t x = 0;
typecast_copy(x, in);
return BOTAN_ENDIAN_N2B(x);

#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
return typecast_copy<uint64_t>(in);
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
return reverse_bytes(typecast_copy<uint64_t>(in));
#else
return make_uint64(in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
#endif
Expand All @@ -229,10 +218,11 @@ inline constexpr uint64_t load_be<uint64_t>(const uint8_t in[], size_t off) {
template <>
inline constexpr uint64_t load_le<uint64_t>(const uint8_t in[], size_t off) {
in += off * sizeof(uint64_t);
#if defined(BOTAN_ENDIAN_N2L)
uint64_t x = 0;
typecast_copy(x, in);
return BOTAN_ENDIAN_N2L(x);

#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
return reverse_bytes(typecast_copy<uint64_t>(in));
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
return typecast_copy<uint64_t>(in);
#else
return make_uint64(in[7], in[6], in[5], in[4], in[3], in[2], in[1], in[0]);
#endif
Expand Down Expand Up @@ -397,9 +387,10 @@ inline constexpr void load_be(T out[], const uint8_t in[], size_t count) {
* @param out the byte array to write to
*/
inline constexpr void store_be(uint16_t in, uint8_t out[2]) {
#if defined(BOTAN_ENDIAN_N2B)
uint16_t o = BOTAN_ENDIAN_N2B(in);
typecast_copy(out, o);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
typecast_copy(out, in);
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
typecast_copy(out, reverse_bytes(in));
#else
out[0] = get_byte<0>(in);
out[1] = get_byte<1>(in);
Expand All @@ -412,9 +403,10 @@ inline constexpr void store_be(uint16_t in, uint8_t out[2]) {
* @param out the byte array to write to
*/
inline constexpr void store_le(uint16_t in, uint8_t out[2]) {
#if defined(BOTAN_ENDIAN_N2L)
uint16_t o = BOTAN_ENDIAN_N2L(in);
typecast_copy(out, o);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
typecast_copy(out, reverse_bytes(in));
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
typecast_copy(out, in);
#else
out[0] = get_byte<1>(in);
out[1] = get_byte<0>(in);
Expand All @@ -427,9 +419,10 @@ inline constexpr void store_le(uint16_t in, uint8_t out[2]) {
* @param out the byte array to write to
*/
inline constexpr void store_be(uint32_t in, uint8_t out[4]) {
#if defined(BOTAN_ENDIAN_B2N)
uint32_t o = BOTAN_ENDIAN_B2N(in);
typecast_copy(out, o);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
typecast_copy(out, in);
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
typecast_copy(out, reverse_bytes(in));
#else
out[0] = get_byte<0>(in);
out[1] = get_byte<1>(in);
Expand All @@ -444,9 +437,10 @@ inline constexpr void store_be(uint32_t in, uint8_t out[4]) {
* @param out the byte array to write to
*/
inline constexpr void store_le(uint32_t in, uint8_t out[4]) {
#if defined(BOTAN_ENDIAN_L2N)
uint32_t o = BOTAN_ENDIAN_L2N(in);
typecast_copy(out, o);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
typecast_copy(out, reverse_bytes(in));
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
typecast_copy(out, in);
#else
out[0] = get_byte<3>(in);
out[1] = get_byte<2>(in);
Expand All @@ -461,9 +455,10 @@ inline constexpr void store_le(uint32_t in, uint8_t out[4]) {
* @param out the byte array to write to
*/
inline constexpr void store_be(uint64_t in, uint8_t out[8]) {
#if defined(BOTAN_ENDIAN_B2N)
uint64_t o = BOTAN_ENDIAN_B2N(in);
typecast_copy(out, o);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
typecast_copy(out, in);
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
typecast_copy(out, reverse_bytes(in));
#else
out[0] = get_byte<0>(in);
out[1] = get_byte<1>(in);
Expand All @@ -482,9 +477,10 @@ inline constexpr void store_be(uint64_t in, uint8_t out[8]) {
* @param out the byte array to write to
*/
inline constexpr void store_le(uint64_t in, uint8_t out[8]) {
#if defined(BOTAN_ENDIAN_L2N)
uint64_t o = BOTAN_ENDIAN_L2N(in);
typecast_copy(out, o);
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
typecast_copy(out, reverse_bytes(in));
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
typecast_copy(out, in);
#else
out[0] = get_byte<7>(in);
out[1] = get_byte<6>(in);
Expand Down

0 comments on commit 3c7229c

Please sign in to comment.