Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -24,13 +24,11 @@ fi AC_C_BIGENDIAN([ AC_DEFINE(OF_BIG_ENDIAN, 1, [Whether we are big endian]) AC_SUBST(ENDIANESS_DEF, "-DOF_BIG_ENDIAN") - ], [ - AC_DEFINE(OF_LITTLE_ENDIAN, 1, [Whether we are little endian]) - AC_SUBST(ENDIANESS_DEF, "-DOF_LITTLE_ENDIAN")]) + ]) AC_MSG_CHECKING(for SIZE_MAX) AC_EGREP_CPP(yes, [ #include #include Index: src/OFHashes.m ================================================================== --- src/OFHashes.m +++ src/OFHashes.m @@ -165,21 +165,21 @@ memcpy(p, buffer, size); return self; } memcpy(p, buffer, t); - OF_BSWAP_V(in, 16); + OF_BSWAP32_V((uint32_t*)in, 16); md5_transform(buf, (uint32_t*)in); buffer += t; size -= t; } /* Process data in 64-byte chunks */ while (size >= 64) { memcpy(in, buffer, 64); - OF_BSWAP_V(in, 16); + OF_BSWAP32_V((uint32_t*)in, 16); md5_transform(buf, (uint32_t*)in); buffer += 64; size -= 64; } @@ -213,27 +213,27 @@ /* Pad out to 56 mod 64 */ if (count < 8) { /* Two lots of padding: Pad the first block to 64 bytes */ memset(p, 0, count); - OF_BSWAP_V(in, 16); + OF_BSWAP32_V((uint32_t*)in, 16); md5_transform(buf, (uint32_t*)in); /* Now fill the next block with 56 bytes */ memset(in, 0, 56); } else { /* Pad block to 56 bytes */ memset(p, 0, count - 8); } - OF_BSWAP_V(in, 14); + OF_BSWAP32_V((uint32_t*)in, 14); /* Append length in bits and transform */ ((uint32_t*)in)[14] = bits[0]; ((uint32_t*)in)[15] = bits[1]; md5_transform(buf, (uint32_t*)in); - OF_BSWAP_V((uint8_t*)buf, 4); + OF_BSWAP32_V(buf, 4); calculated = YES; return (uint8_t*)buf; } Index: src/OFMacros.h ================================================================== --- src/OFMacros.h +++ src/OFMacros.h @@ -12,10 +12,12 @@ #ifndef OF_CONFIGURED #error You are missing the libobjfw definitions! #error Please use objfw-config! #endif +#include + #ifdef __GNUC__ #define OF_INLINE inline __attribute__((always_inline)) #define OF_LIKELY(cond) __builtin_expect(!!(cond), 1) #define OF_UNLIKELY(cond) __builtin_expect(!!(cond), 0) #else @@ -22,29 +24,53 @@ #define OF_INLINE inline #define OF_LIKELY(cond) cond #define OF_UNLIKELY(cond) cond #endif -#if defined(OF_BIG_ENDIAN) -#include - -static OF_INLINE void -OF_BSWAP_V(uint8_t *buf, size_t len) -{ - uint32_t t; - - while (len--) { - t = (uint32_t)((uint32_t)buf[3] << 8 | buf[2]) << 16 | - ((uint32_t)buf[1] << 8 | buf[0]); - *(uint32_t*)buf = t; - buf += sizeof(t); - } -} -#elif defined(OF_LITTLE_ENDIAN) -#define OF_BSWAP_V(buf, len) -#else -#error Please define either OF_BIG_ENDIAN or OF_LITTLE_ENDIAN! +static OF_INLINE uint16_t +OF_BSWAP16(uint16_t i) +{ + i = (i & UINT16_C(0xFF00)) >> 8 | + (i & UINT16_C(0x00FF)) << 8; + return i; +} + +static OF_INLINE uint32_t +OF_BSWAP32(uint32_t i) +{ + i = (i & UINT32_C(0xFF000000)) >> 24 | + (i & UINT32_C(0x00FF0000)) >> 8 | + (i & UINT32_C(0x0000FF00)) << 8 | + (i & UINT32_C(0x000000FF)) << 24; + return i; +} + +static OF_INLINE uint64_t +OF_BSWAP64(uint64_t i) +{ + i = (i & UINT64_C(0xFF00000000000000)) >> 56 | + (i & UINT64_C(0x00FF000000000000)) >> 40 | + (i & UINT64_C(0x0000FF0000000000)) >> 24 | + (i & UINT64_C(0x000000FF00000000)) >> 8 | + (i & UINT64_C(0x00000000FF000000)) << 8 | + (i & UINT64_C(0x0000000000FF0000)) << 24 | + (i & UINT64_C(0x000000000000FF00)) << 40 | + (i & UINT64_C(0x00000000000000FF)) << 56; + return i; +} + +#ifdef OF_BIG_ENDIAN +static OF_INLINE void +OF_BSWAP32_V(uint32_t *buf, size_t len) +{ + while (len--) { + *buf = OF_BSWAP32(*buf); + buf++; + } +} +#else +#define OF_BSWAP32_V(buf, len) #endif #define OF_ROL(val, bits) \ (((val) << (bits)) | ((val) >> (32 - (bits))))