amiga_imgv
view src/endian.c @ 12:20c7238c60be
img2ham unfinished
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 20 Nov 2017 08:36:16 +0200 |
parents | |
children |
line source
1 #include "endian.h"
3 #ifdef NO_INET
4 static uint32_t init_once32(uint32_t x);
5 static uint16_t init_once16(uint16_t x);
7 static uint16_t swap16(uint16_t x);
8 static uint32_t swap32(uint32_t x);
9 static uint16_t nop16(uint16_t x);
10 static uint32_t nop32(uint32_t x);
12 uint16_t (*ntohs)(uint16_t) = init_once16;
13 uint16_t (*htons)(uint16_t) = init_once16;
14 uint32_t (*ntohl)(uint32_t) = init_once32;
15 uint32_t (*htonl)(uint32_t) = init_once32;
17 static uint16_t init_once16(uint16_t x)
18 {
19 int be = bigendian();
20 ntohs = be ? nop16 : swap16;
21 htons = be ? nop16 : swap16;
22 return ntohs(x);
23 }
25 static uint32_t init_once32(uint32_t x)
26 {
27 int be = bigendian();
28 ntohl = be ? nop32 : swap32;
29 htonl = be ? nop32 : swap32;
30 return ntohl(x);
31 }
33 static uint16_t swap16(uint16_t x)
34 {
35 return (x << 8) | (x >> 8);
36 }
38 static uint32_t swap32(uint32_t x)
39 {
40 return (x << 24) | (x >> 24) | ((x << 16) & 0xff0000) | ((x >> 16) & 0xff00);
41 }
43 static uint16_t nop16(uint16_t x)
44 {
45 return x;
46 }
48 static uint32_t nop32(uint32_t x)
49 {
50 return x;
51 }
52 #endif
54 int bigendian(void)
55 {
56 static const uint16_t x = 0xaabb;
57 return *(unsigned char*)&x == 0xaa;
58 }