2828#include <stdlib.h>
2929#include <string.h>
3030#include <stdio.h>
31+ #include <stdbool.h>
3132
3233typedef unsigned char byte ;
3334
35+ static uint16_t endswap16 (uint16_t value ) {
36+ uint16_t ret = 0 ;
37+ ret |= (value >> 8 ) & 0x00ff ;
38+ ret |= (value << 8 ) & 0xff00 ;
39+ return ret ;
40+ }
41+
42+ static uint32_t endswap32 (uint32_t value ) {
43+ uint32_t ret = 0 ;
44+ ret |= (value >> 24 ) & 0x000000ff ;
45+ ret |= (value >> 8 ) & 0x0000ff00 ;
46+ ret |= (value << 8 ) & 0x00ff0000 ;
47+ ret |= (value << 24 ) & 0xff000000 ;
48+ return ret ;
49+ }
50+
51+ static uint64_t endswap64 (uint64_t value ) {
52+ uint64_t ret = 0 ;
53+ ret |= (value >> 56 ) & 0x00000000000000ff ;
54+ ret |= (value >> 40 ) & 0x000000000000ff00 ;
55+ ret |= (value >> 24 ) & 0x0000000000ff0000 ;
56+ ret |= (value >> 8 ) & 0x00000000ff000000 ;
57+ ret |= (value << 8 ) & 0x000000ff00000000 ;
58+ ret |= (value << 24 ) & 0x0000ff0000000000 ;
59+ ret |= (value << 40 ) & 0x00ff000000000000 ;
60+ ret |= (value << 56 ) & 0xff00000000000000 ;
61+ return ret ;
62+ }
63+
64+ #ifdef __BYTE_ORDER__
65+
66+ #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
67+ #define bigendian true
68+ #else
69+ #define bigendian false
70+ #endif
71+
72+ #else /* !__BYTE_ORDER__ */
73+
74+ static bool bigendian = false;
75+
76+ #endif /* !__BYTE_ORDER__ */
77+
78+ #define ENDSWAP (VALUE ) (bigendian ? ( \
79+ sizeof(VALUE) == 1 ? (VALUE) : \
80+ sizeof(VALUE) == 2 ? endswap16(VALUE) : \
81+ sizeof(VALUE) == 4 ? endswap32(VALUE) : \
82+ sizeof(VALUE) == 8 ? endswap64(VALUE) : (abort(), 1) \
83+ ) : (VALUE))
84+
3485/* Higher -> better compression with exponentally dimnishing gains. */
3586#define LIMLZ_SA_NEIGHBORS 32
3687
@@ -295,6 +346,11 @@ static uint32_t crc32_nibble(const byte *data, size_t len) {
295346}
296347
297348int main (int argc , char * argv []) {
349+ #ifndef __BYTE_ORDER__
350+ uint32_t endcheck = 0x12345678 ;
351+ uint8_t endbyte = * ((uint8_t * )& endcheck );
352+ bigendian = endbyte == 0x12 ;
353+ #endif
298354 if (argc != 3 ) {
299355 fprintf (stderr , "? %s <input> <output>\n" , argv [0 ]); return 1 ;
300356 }
@@ -318,7 +374,7 @@ int main(int argc, char *argv[]) {
318374 if (!outsz ) {
319375 fprintf (stderr , "? limlzpack\n" ); return 1 ;
320376 }
321- uint32_t crc = crc32_nibble (inbuf , insz );
377+ uint32_t crc = ENDSWAP ( crc32_nibble (inbuf , insz ) );
322378 fwrite (& crc , sizeof (crc ), 1 , fout );
323379 fwrite (outbuf , 1 , outsz , fout );
324380 fclose (fout );
0 commit comments