From b47e687a552ee8d9e8d0200f8368023597ecae45 Mon Sep 17 00:00:00 2001 From: Dave Vasilevsky Date: Sat, 13 Oct 2012 06:55:12 -0400 Subject: [PATCH] Platform independent endian support --- endian.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/endian.c b/endian.c index a0f9fc6..ef66652 100644 --- a/endian.c +++ b/endian.c @@ -25,5 +25,23 @@ void xle64enc(uint8_t *d, uint64_t n) { *(uint64_t*)d = __cpu_to_le64(n); } +#else + +// Platform independent +#include + +uint64_t xle64dec(const uint8_t *d) { + uint64_t r = 0; + for (const uint8_t *p = d + sizeof(r) - 1; p >= d; --p) + r = (r << 8) + *p; + return r; +} + +void xle64enc(uint8_t *d, uint64_t n) { + for (uint8_t *p = d; p < d + sizeof(n); ++p) { + *p = n & 0xff; + n >>= 8; + } +} #endif