aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-03-18 13:23:29 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-03-18 13:33:47 +0100
commit0b5bcb0393df10ab671cfc76209b9b8b565323f1 (patch)
tree31baed534f3a4b59fe3ff1d580c71d86d184d446 /lib
parent7c29dc8db20a3e6f1fb351c44a068171bb6c1ff1 (diff)
Restore workaround for unaligned reads in xxhash
The code was originally used inside the block processor, where 32 bit aligned data could be guaranteed. If it is available in libutil, I cannot possibly guarantee for alignment in future use elsewhere. Even for the block processor it was rather risky "remember this detail very well" buisness. This commit restores the unaligned read treatment of the original. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/util/xxhash.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/lib/util/xxhash.c b/lib/util/xxhash.c
index 4e1e0e0..0644e75 100644
--- a/lib/util/xxhash.c
+++ b/lib/util/xxhash.c
@@ -37,6 +37,8 @@
#include "config.h"
#include "util.h"
+#include <string.h>
+
#define xxh_rotl32(x, r) ((x << r) | (x >> (32 - r)))
static const sqfs_u32 PRIME32_1 = 2654435761U;
@@ -53,6 +55,13 @@ static sqfs_u32 xxh32_round(sqfs_u32 seed, sqfs_u32 input)
return seed;
}
+static sqfs_u32 XXH_readLE32(const sqfs_u8 *ptr)
+{
+ sqfs_u32 value;
+ memcpy(&value, ptr, sizeof(value));
+ return le32toh(value);
+}
+
sqfs_u32 xxh32(const void *input, const size_t len)
{
const sqfs_u8 *p = (const sqfs_u8 *)input;
@@ -67,10 +76,10 @@ sqfs_u32 xxh32(const void *input, const size_t len)
sqfs_u32 v4 = PRIME32_1;
do {
- v1 = xxh32_round(v1, ((sqfs_u32 *)p)[0]);
- v2 = xxh32_round(v2, ((sqfs_u32 *)p)[1]);
- v3 = xxh32_round(v3, ((sqfs_u32 *)p)[2]);
- v4 = xxh32_round(v4, ((sqfs_u32 *)p)[3]);
+ v1 = xxh32_round(v1, XXH_readLE32(p ));
+ v2 = xxh32_round(v2, XXH_readLE32(p + 4));
+ v3 = xxh32_round(v3, XXH_readLE32(p + 8));
+ v4 = xxh32_round(v4, XXH_readLE32(p + 12));
p += 16;
} while (p <= limit);
@@ -83,7 +92,7 @@ sqfs_u32 xxh32(const void *input, const size_t len)
h32 += (sqfs_u32)len;
while (p + 4 <= b_end) {
- h32 += (*((sqfs_u32 *)p)) * PRIME32_3;
+ h32 += XXH_readLE32(p) * PRIME32_3;
h32 = xxh_rotl32(h32, 17) * PRIME32_4;
p += 4;
}