aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/utils/loadstor.h58
-rw-r--r--src/lib/utils/mem_ops.h66
2 files changed, 44 insertions, 80 deletions
diff --git a/src/lib/utils/loadstor.h b/src/lib/utils/loadstor.h
index 8d33c2eef..a6c2b7969 100644
--- a/src/lib/utils/loadstor.h
+++ b/src/lib/utils/loadstor.h
@@ -1,6 +1,6 @@
/*
* Load/Store Operators
-* (C) 1999-2007 Jack Lloyd
+* (C) 1999-2007,2015 Jack Lloyd
* 2007 Yves Jerschow
*
* Botan is released under the Simplified BSD License (see license.txt)
@@ -144,10 +144,13 @@ inline T load_le(const byte in[], size_t off)
template<>
inline u16bit load_be<u16bit>(const byte in[], size_t off)
{
+ in += off * sizeof(u16bit);
+
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u16bit*>(in) + off));
+ u16bit x;
+ std::memcpy(&x, in, sizeof(x));
+ return BOTAN_ENDIAN_N2B(x);
#else
- in += off * sizeof(u16bit);
return make_u16bit(in[0], in[1]);
#endif
}
@@ -161,10 +164,13 @@ inline u16bit load_be<u16bit>(const byte in[], size_t off)
template<>
inline u16bit load_le<u16bit>(const byte in[], size_t off)
{
+ in += off * sizeof(u16bit);
+
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u16bit*>(in) + off));
+ u16bit x;
+ std::memcpy(&x, in, sizeof(x));
+ return BOTAN_ENDIAN_N2L(x);
#else
- in += off * sizeof(u16bit);
return make_u16bit(in[1], in[0]);
#endif
}
@@ -178,10 +184,12 @@ inline u16bit load_le<u16bit>(const byte in[], size_t off)
template<>
inline u32bit load_be<u32bit>(const byte in[], size_t off)
{
+ in += off * sizeof(u32bit);
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u32bit*>(in) + off));
+ u32bit x;
+ std::memcpy(&x, in, sizeof(x));
+ return BOTAN_ENDIAN_N2B(x);
#else
- in += off * sizeof(u32bit);
return make_u32bit(in[0], in[1], in[2], in[3]);
#endif
}
@@ -195,10 +203,12 @@ inline u32bit load_be<u32bit>(const byte in[], size_t off)
template<>
inline u32bit load_le<u32bit>(const byte in[], size_t off)
{
+ in += off * sizeof(u32bit);
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u32bit*>(in) + off));
+ u32bit x;
+ std::memcpy(&x, in, sizeof(x));
+ return BOTAN_ENDIAN_N2L(x);
#else
- in += off * sizeof(u32bit);
return make_u32bit(in[3], in[2], in[1], in[0]);
#endif
}
@@ -212,10 +222,12 @@ inline u32bit load_le<u32bit>(const byte in[], size_t off)
template<>
inline u64bit load_be<u64bit>(const byte in[], size_t off)
{
+ in += off * sizeof(u64bit);
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u64bit*>(in) + off));
+ u64bit x;
+ std::memcpy(&x, in, sizeof(x));
+ return BOTAN_ENDIAN_N2B(x);
#else
- in += off * sizeof(u64bit);
return make_u64bit(in[0], in[1], in[2], in[3],
in[4], in[5], in[6], in[7]);
#endif
@@ -230,10 +242,12 @@ inline u64bit load_be<u64bit>(const byte in[], size_t off)
template<>
inline u64bit load_le<u64bit>(const byte in[], size_t off)
{
+ in += off * sizeof(u64bit);
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u64bit*>(in) + off));
+ u64bit x;
+ std::memcpy(&x, in, sizeof(x));
+ return BOTAN_ENDIAN_N2L(x);
#else
- in += off * sizeof(u64bit);
return make_u64bit(in[7], in[6], in[5], in[4],
in[3], in[2], in[1], in[0]);
#endif
@@ -431,7 +445,8 @@ inline void load_be(T out[],
inline void store_be(u16bit in, byte out[2])
{
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- *reinterpret_cast<u16bit*>(out) = BOTAN_ENDIAN_B2N(in);
+ u16bit o = BOTAN_ENDIAN_N2B(in);
+ std::memcpy(out, &o, sizeof(o));
#else
out[0] = get_byte(0, in);
out[1] = get_byte(1, in);
@@ -446,7 +461,8 @@ inline void store_be(u16bit in, byte out[2])
inline void store_le(u16bit in, byte out[2])
{
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- *reinterpret_cast<u16bit*>(out) = BOTAN_ENDIAN_L2N(in);
+ u16bit o = BOTAN_ENDIAN_N2L(in);
+ std::memcpy(out, &o, sizeof(o));
#else
out[0] = get_byte(1, in);
out[1] = get_byte(0, in);
@@ -461,7 +477,8 @@ inline void store_le(u16bit in, byte out[2])
inline void store_be(u32bit in, byte out[4])
{
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- *reinterpret_cast<u32bit*>(out) = BOTAN_ENDIAN_B2N(in);
+ u32bit o = BOTAN_ENDIAN_B2N(in);
+ std::memcpy(out, &o, sizeof(o));
#else
out[0] = get_byte(0, in);
out[1] = get_byte(1, in);
@@ -478,7 +495,8 @@ inline void store_be(u32bit in, byte out[4])
inline void store_le(u32bit in, byte out[4])
{
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- *reinterpret_cast<u32bit*>(out) = BOTAN_ENDIAN_L2N(in);
+ u32bit o = BOTAN_ENDIAN_L2N(in);
+ std::memcpy(out, &o, sizeof(o));
#else
out[0] = get_byte(3, in);
out[1] = get_byte(2, in);
@@ -495,7 +513,8 @@ inline void store_le(u32bit in, byte out[4])
inline void store_be(u64bit in, byte out[8])
{
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- *reinterpret_cast<u64bit*>(out) = BOTAN_ENDIAN_B2N(in);
+ u64bit o = BOTAN_ENDIAN_B2N(in);
+ std::memcpy(out, &o, sizeof(o));
#else
out[0] = get_byte(0, in);
out[1] = get_byte(1, in);
@@ -516,7 +535,8 @@ inline void store_be(u64bit in, byte out[8])
inline void store_le(u64bit in, byte out[8])
{
#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
- *reinterpret_cast<u64bit*>(out) = BOTAN_ENDIAN_L2N(in);
+ u64bit o = BOTAN_ENDIAN_L2N(in);
+ std::memcpy(out, &o, sizeof(o));
#else
out[0] = get_byte(7, in);
out[1] = get_byte(6, in);
diff --git a/src/lib/utils/mem_ops.h b/src/lib/utils/mem_ops.h
index d11e3368c..0d2d0dab0 100644
--- a/src/lib/utils/mem_ops.h
+++ b/src/lib/utils/mem_ops.h
@@ -1,6 +1,6 @@
/*
* Memory Operations
-* (C) 1999-2009,2012 Jack Lloyd
+* (C) 1999-2009,2012,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -81,7 +81,7 @@ template<typename T> inline bool same_mem(const T* p1, const T* p2, size_t n)
}
/**
-* XOR arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length
+* XOR_ arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length
* @param out the input/output buffer
* @param in the read-only input buffer
* @param length the length of the buffers
@@ -89,18 +89,10 @@ template<typename T> inline bool same_mem(const T* p1, const T* p2, size_t n)
template<typename T>
void xor_buf(T out[], const T in[], size_t length)
{
- while(length >= 8)
- {
- out[0] ^= in[0]; out[1] ^= in[1];
- out[2] ^= in[2]; out[3] ^= in[3];
- out[4] ^= in[4]; out[5] ^= in[5];
- out[6] ^= in[6]; out[7] ^= in[7];
-
- out += 8; in += 8; length -= 8;
- }
-
for(size_t i = 0; i != length; ++i)
+ {
out[i] ^= in[i];
+ }
}
/**
@@ -115,60 +107,12 @@ template<typename T> void xor_buf(T out[],
const T in2[],
size_t length)
{
- while(length >= 8)
- {
- out[0] = in[0] ^ in2[0];
- out[1] = in[1] ^ in2[1];
- out[2] = in[2] ^ in2[2];
- out[3] = in[3] ^ in2[3];
- out[4] = in[4] ^ in2[4];
- out[5] = in[5] ^ in2[5];
- out[6] = in[6] ^ in2[6];
- out[7] = in[7] ^ in2[7];
-
- in += 8; in2 += 8; out += 8; length -= 8;
- }
-
for(size_t i = 0; i != length; ++i)
- out[i] = in[i] ^ in2[i];
- }
-
-#if BOTAN_TARGET_UNALIGNED_MEMORY_ACCESS_OK
-
-template<>
-inline void xor_buf<byte>(byte out[], const byte in[], size_t length)
- {
- while(length >= 8)
{
- *reinterpret_cast<u64bit*>(out) ^= *reinterpret_cast<const u64bit*>(in);
- out += 8; in += 8; length -= 8;
- }
-
- for(size_t i = 0; i != length; ++i)
- out[i] ^= in[i];
- }
-
-template<>
-inline void xor_buf<byte>(byte out[],
- const byte in[],
- const byte in2[],
- size_t length)
- {
- while(length >= 8)
- {
- *reinterpret_cast<u64bit*>(out) =
- *reinterpret_cast<const u64bit*>(in) ^
- *reinterpret_cast<const u64bit*>(in2);
-
- in += 8; in2 += 8; out += 8; length -= 8;
- }
-
- for(size_t i = 0; i != length; ++i)
out[i] = in[i] ^ in2[i];
+ }
}
-#endif
-
template<typename Alloc, typename Alloc2>
void xor_buf(std::vector<byte, Alloc>& out,
const std::vector<byte, Alloc2>& in,