amiga_imgv
diff src/endian.c @ 7:4c36d0f44aa6
lbm loading
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 29 Oct 2017 13:21:11 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/endian.c Sun Oct 29 13:21:11 2017 +0200 1.3 @@ -0,0 +1,58 @@ 1.4 +#include "endian.h" 1.5 + 1.6 +#ifdef NO_INET 1.7 +static uint32_t init_once32(uint32_t x); 1.8 +static uint16_t init_once16(uint16_t x); 1.9 + 1.10 +static uint16_t swap16(uint16_t x); 1.11 +static uint32_t swap32(uint32_t x); 1.12 +static uint16_t nop16(uint16_t x); 1.13 +static uint32_t nop32(uint32_t x); 1.14 + 1.15 +uint16_t (*ntohs)(uint16_t) = init_once16; 1.16 +uint16_t (*htons)(uint16_t) = init_once16; 1.17 +uint32_t (*ntohl)(uint32_t) = init_once32; 1.18 +uint32_t (*htonl)(uint32_t) = init_once32; 1.19 + 1.20 +static uint16_t init_once16(uint16_t x) 1.21 +{ 1.22 + int be = bigendian(); 1.23 + ntohs = be ? nop16 : swap16; 1.24 + htons = be ? nop16 : swap16; 1.25 + return ntohs(x); 1.26 +} 1.27 + 1.28 +static uint32_t init_once32(uint32_t x) 1.29 +{ 1.30 + int be = bigendian(); 1.31 + ntohl = be ? nop32 : swap32; 1.32 + htonl = be ? nop32 : swap32; 1.33 + return ntohl(x); 1.34 +} 1.35 + 1.36 +static uint16_t swap16(uint16_t x) 1.37 +{ 1.38 + return (x << 8) | (x >> 8); 1.39 +} 1.40 + 1.41 +static uint32_t swap32(uint32_t x) 1.42 +{ 1.43 + return (x << 24) | (x >> 24) | ((x << 16) & 0xff0000) | ((x >> 16) & 0xff00); 1.44 +} 1.45 + 1.46 +static uint16_t nop16(uint16_t x) 1.47 +{ 1.48 + return x; 1.49 +} 1.50 + 1.51 +static uint32_t nop32(uint32_t x) 1.52 +{ 1.53 + return x; 1.54 +} 1.55 +#endif 1.56 + 1.57 +int bigendian(void) 1.58 +{ 1.59 + static const uint16_t x = 0xaabb; 1.60 + return *(unsigned char*)&x == 0xaa; 1.61 +}