aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/loadstor.h
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-12-23 00:15:50 +0000
committerlloyd <[email protected]>2009-12-23 00:15:50 +0000
commit773fd2d974ab9377505fa07dffd75aaade56ecac (patch)
tree86522a9c6dee3d824e6f6d63549c651c2942bd36 /src/utils/loadstor.h
parentdb37d31b6878bf0dda2819fe87f373f99263d936 (diff)
Deal with unknown or mixed endian architectures properly. If known little
or big endian, for large loads always memcpy, then go back and swap as needed. Otherwise (unknown or mixed endian) just load one at a time as usual.
Diffstat (limited to 'src/utils/loadstor.h')
-rw-r--r--src/utils/loadstor.h40
1 files changed, 18 insertions, 22 deletions
diff --git a/src/utils/loadstor.h b/src/utils/loadstor.h
index 77a6e846c..256a6f849 100644
--- a/src/utils/loadstor.h
+++ b/src/utils/loadstor.h
@@ -202,24 +202,22 @@ inline void load_le(T out[],
const byte in[],
u32bit count)
{
-#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
+#if defined(BOTAN_TARGET_CPU_HAS_KNOWN_ENDIANNESS)
std::memcpy(out, in, sizeof(T)*count);
-#else
+
+#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
const u32bit blocks = count - (count % 4);
const u32bit left = count - blocks;
for(u32bit i = 0; i != blocks; i += 4)
- {
- out[0] = load_le<T>(in, 0);
- out[1] = load_le<T>(in, 1);
- out[2] = load_le<T>(in, 2);
- out[3] = load_le<T>(in, 3);
-
- out += 4;
- in += 4*sizeof(T);
- }
+ bswap_4(out + i);
for(u32bit i = 0; i != left; ++i)
+ out[blocks+i] = reverse_bytes(out[blocks+i]);
+#endif
+
+#else
+ for(u32bit i = 0; i != count; ++i)
out[i] = load_le<T>(in, i);
#endif
}
@@ -261,24 +259,22 @@ inline void load_be(T out[],
const byte in[],
u32bit count)
{
-#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
+#if defined(BOTAN_TARGET_CPU_HAS_KNOWN_ENDIANNESS)
std::memcpy(out, in, sizeof(T)*count);
-#else
+
+#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
const u32bit blocks = count - (count % 4);
const u32bit left = count - blocks;
for(u32bit i = 0; i != blocks; i += 4)
- {
- out[0] = load_be<T>(in, 0);
- out[1] = load_be<T>(in, 1);
- out[2] = load_be<T>(in, 2);
- out[3] = load_be<T>(in, 3);
-
- out += 4;
- in += 4*sizeof(T);
- }
+ bswap_4(out + i);
for(u32bit i = 0; i != left; ++i)
+ out[blocks+i] = reverse_bytes(out[blocks+i]);
+#endif
+
+#else
+ for(u32bit i = 0; i != count; ++i)
out[i] = load_be<T>(in, i);
#endif
}