implement functions to convert from host byte-order to little-endian or big-endian, convert to network order which defined as CU_NETWORK_ORDER.
This commit is contained in:
parent
5432d8f889
commit
c3b659dce8
4 changed files with 92 additions and 5 deletions
|
|
@ -2,4 +2,6 @@
|
||||||
C library to implement
|
C library to implement
|
||||||
- data structures like linked-lists, vector arrays, maps
|
- data structures like linked-lists, vector arrays, maps
|
||||||
- bitmaps & bitwise operations
|
- bitmaps & bitwise operations
|
||||||
|
- architecture-dependent & hardware features
|
||||||
|
- byte-order endianness detection, byte-swapping
|
||||||
- other stuff
|
- other stuff
|
||||||
65
endian.c
65
endian.c
|
|
@ -18,7 +18,6 @@ static enum cu_endian_e endian_detect()
|
||||||
return LittleEndian;
|
return LittleEndian;
|
||||||
else if (host_order.value == ORDER_BIG_ENDIAN)
|
else if (host_order.value == ORDER_BIG_ENDIAN)
|
||||||
return BigEndian;
|
return BigEndian;
|
||||||
else return MiddleEndian;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t sw_bswap16(uint16_t val)
|
static uint16_t sw_bswap16(uint16_t val)
|
||||||
|
|
@ -78,7 +77,71 @@ bswap32_t cu_bswap32 = sw_bswap32;
|
||||||
bswap64_t cu_bswap64 = sw_bswap64;
|
bswap64_t cu_bswap64 = sw_bswap64;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static uint16_t no_bswap16(uint16_t val)
|
||||||
|
{
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t no_bswap32(uint32_t val)
|
||||||
|
{
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t no_bswap64(uint64_t val)
|
||||||
|
{
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
bswap16_t h16tol16 = no_bswap16;
|
||||||
|
bswap32_t h32tol32 = no_bswap32;
|
||||||
|
bswap64_t h64tol64 = no_bswap64;
|
||||||
|
bswap16_t l16toh16 = no_bswap16;
|
||||||
|
bswap32_t l32toh32 = no_bswap32;
|
||||||
|
bswap64_t l64toh64 = no_bswap64;
|
||||||
|
|
||||||
|
bswap16_t h16tob16 = no_bswap16;
|
||||||
|
bswap32_t h32tob32 = no_bswap32;
|
||||||
|
bswap64_t h64tob64 = no_bswap64;
|
||||||
|
bswap16_t b16toh16 = no_bswap16;
|
||||||
|
bswap32_t b32toh32 = no_bswap32;
|
||||||
|
bswap64_t b64toh64 = no_bswap64;
|
||||||
|
|
||||||
|
bswap16_t h16ton16 = no_bswap16;
|
||||||
|
bswap32_t h32ton32 = no_bswap32;
|
||||||
|
bswap64_t h64ton64 = no_bswap64;
|
||||||
|
bswap16_t n16toh16 = no_bswap16;
|
||||||
|
bswap32_t n32toh32 = no_bswap32;
|
||||||
|
bswap64_t n64toh64 = no_bswap64;
|
||||||
|
|
||||||
void cu_endian_init()
|
void cu_endian_init()
|
||||||
{
|
{
|
||||||
cu_endian = endian_detect();
|
cu_endian = endian_detect();
|
||||||
|
if (cu_endian == LittleEndian)
|
||||||
|
{
|
||||||
|
h16tob16 = cu_bswap16;
|
||||||
|
h32tob32 = cu_bswap32;
|
||||||
|
h64tob64 = cu_bswap64;
|
||||||
|
b16toh16 = cu_bswap16;
|
||||||
|
b32toh32 = cu_bswap32;
|
||||||
|
b64toh64 = cu_bswap64;
|
||||||
|
}
|
||||||
|
else if (cu_endian == BigEndian)
|
||||||
|
{
|
||||||
|
h16tol16 = cu_bswap16;
|
||||||
|
h32tol32 = cu_bswap32;
|
||||||
|
h64tol64 = cu_bswap64;
|
||||||
|
l16toh16 = cu_bswap16;
|
||||||
|
l32toh32 = cu_bswap32;
|
||||||
|
l64toh64 = cu_bswap64;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cu_endian != CU_NETWORK_ORDER)
|
||||||
|
{
|
||||||
|
h16ton16 = cu_bswap16;
|
||||||
|
h32ton32 = cu_bswap32;
|
||||||
|
h64ton64 = cu_bswap64;
|
||||||
|
n16toh16 = cu_bswap16;
|
||||||
|
n32toh32 = cu_bswap32;
|
||||||
|
n64toh64 = cu_bswap64;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
26
endian.h
26
endian.h
|
|
@ -5,8 +5,7 @@
|
||||||
|
|
||||||
enum cu_endian_e {
|
enum cu_endian_e {
|
||||||
LittleEndian,
|
LittleEndian,
|
||||||
BigEndian,
|
BigEndian
|
||||||
MiddleEndian
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern enum cu_endian_e cu_endian;
|
extern enum cu_endian_e cu_endian;
|
||||||
|
|
@ -19,6 +18,29 @@ extern bswap16_t cu_bswap16;
|
||||||
extern bswap32_t cu_bswap32;
|
extern bswap32_t cu_bswap32;
|
||||||
extern bswap64_t cu_bswap64;
|
extern bswap64_t cu_bswap64;
|
||||||
|
|
||||||
|
extern bswap16_t h16tol16;
|
||||||
|
extern bswap32_t h32tol32;
|
||||||
|
extern bswap64_t h64tol64;
|
||||||
|
extern bswap16_t l16toh16;
|
||||||
|
extern bswap32_t l32toh32;
|
||||||
|
extern bswap64_t l64toh64;
|
||||||
|
|
||||||
|
extern bswap16_t h16tob16;
|
||||||
|
extern bswap32_t h32tob32;
|
||||||
|
extern bswap64_t h64tob64;
|
||||||
|
extern bswap16_t b16toh16;
|
||||||
|
extern bswap32_t b32toh32;
|
||||||
|
extern bswap64_t b64toh64;
|
||||||
|
|
||||||
|
#define CU_NETWORK_ORDER BigEndian
|
||||||
|
|
||||||
|
extern bswap16_t h16ton16;
|
||||||
|
extern bswap32_t h32ton32;
|
||||||
|
extern bswap64_t h64ton64;
|
||||||
|
extern bswap16_t n16toh16;
|
||||||
|
extern bswap32_t n32toh32;
|
||||||
|
extern bswap64_t n64toh64;
|
||||||
|
|
||||||
void cu_endian_init();
|
void cu_endian_init();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
4
test.c
4
test.c
|
|
@ -80,8 +80,8 @@ int main()
|
||||||
|
|
||||||
printf("[endian]\n");
|
printf("[endian]\n");
|
||||||
printf("cutil_endian\t%u\n", cu_endian);
|
printf("cutil_endian\t%u\n", cu_endian);
|
||||||
printf("%x\t%x\n", 0x12345678, cu_bswap32(0x12345678));
|
printf("%x\t%x\n", 0x12345678, l32toh32(0x12345678));
|
||||||
printf("%lx\t%lx\n", 0x12345678abcd8765, cu_bswap64(0x12345678abcd8765));
|
printf("%lx\t%lx\n", 0x12345678abcd8765, h64ton64(0x12345678abcd8765));
|
||||||
|
|
||||||
cutil_exit();
|
cutil_exit();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue