redefined endian conversion routines

This commit is contained in:
2019-05-02 07:58:36 +00:00
parent bfc4364ec2
commit c3359bac1d
2 changed files with 279 additions and 220 deletions

179
lib/utl.c
View File

@ -933,182 +933,3 @@ hcl_bch_t* hcl_dupbchars (hcl_t* hcl, const hcl_bch_t* bcs, hcl_oow_t bcslen)
return ptr;
}
/* ----------------------------------------------------------------------- */
#if defined(HCL_HAVE_UINT16_T)
hcl_uint16_t hcl_ntoh16 (hcl_uint16_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint16_t)(
((hcl_uint16_t)c[0] << 8) |
((hcl_uint16_t)c[1] << 0));
#else
# error Unknown endian
#endif
}
hcl_uint16_t hcl_hton16 (hcl_uint16_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint16_t)(
((hcl_uint16_t)c[0] << 8) |
((hcl_uint16_t)c[1] << 0));
#else
# error Unknown endian
#endif
}
#endif
/* --------------------------------------------------------------- */
#if defined(HCL_HAVE_UINT32_T)
hcl_uint32_t hcl_ntoh32 (hcl_uint32_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint32_t)(
((hcl_uint32_t)c[0] << 24) |
((hcl_uint32_t)c[1] << 16) |
((hcl_uint32_t)c[2] << 8) |
((hcl_uint32_t)c[3] << 0));
#else
# error Unknown endian
#endif
}
hcl_uint32_t hcl_hton32 (hcl_uint32_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint32_t)(
((hcl_uint32_t)c[0] << 24) |
((hcl_uint32_t)c[1] << 16) |
((hcl_uint32_t)c[2] << 8) |
((hcl_uint32_t)c[3] << 0));
#else
# error Unknown endian
#endif
}
#endif
#if defined(HCL_HAVE_UINT64_T)
hcl_uint64_t hcl_ntoh64 (hcl_uint64_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint64_t)(
((hcl_uint64_t)c[0] << 56) |
((hcl_uint64_t)c[1] << 48) |
((hcl_uint64_t)c[2] << 40) |
((hcl_uint64_t)c[3] << 32) |
((hcl_uint64_t)c[4] << 24) |
((hcl_uint64_t)c[5] << 16) |
((hcl_uint64_t)c[6] << 8) |
((hcl_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
}
hcl_uint64_t hcl_hton64 (hcl_uint64_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint64_t)(
((hcl_uint64_t)c[0] << 56) |
((hcl_uint64_t)c[1] << 48) |
((hcl_uint64_t)c[2] << 40) |
((hcl_uint64_t)c[3] << 32) |
((hcl_uint64_t)c[4] << 24) |
((hcl_uint64_t)c[5] << 16) |
((hcl_uint64_t)c[6] << 8) |
((hcl_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
}
#endif
/* --------------------------------------------------------------- */
#if defined(HCL_HAVE_UINT128_T)
hcl_uint128_t hcl_ntoh128 (hcl_uint128_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint128_t)(
((hcl_uint128_t)c[0] << 120) |
((hcl_uint128_t)c[1] << 112) |
((hcl_uint128_t)c[2] << 104) |
((hcl_uint128_t)c[3] << 96) |
((hcl_uint128_t)c[4] << 88) |
((hcl_uint128_t)c[5] << 80) |
((hcl_uint128_t)c[6] << 72) |
((hcl_uint128_t)c[7] << 64) |
((hcl_uint128_t)c[8] << 56) |
((hcl_uint128_t)c[9] << 48) |
((hcl_uint128_t)c[10] << 40) |
((hcl_uint128_t)c[11] << 32) |
((hcl_uint128_t)c[12] << 24) |
((hcl_uint128_t)c[13] << 16) |
((hcl_uint128_t)c[14] << 8) |
((hcl_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
hcl_uint128_t hcl_hton128 (hcl_uint128_t x)
{
#if defined(HCL_ENDIAN_BIG)
return x;
#elif defined(HCL_ENDIAN_LITTLE)
hcl_uint8_t* c = (hcl_uint8_t*)&x;
return (hcl_uint128_t)(
((hcl_uint128_t)c[0] << 120) |
((hcl_uint128_t)c[1] << 112) |
((hcl_uint128_t)c[2] << 104) |
((hcl_uint128_t)c[3] << 96) |
((hcl_uint128_t)c[4] << 88) |
((hcl_uint128_t)c[5] << 80) |
((hcl_uint128_t)c[6] << 72) |
((hcl_uint128_t)c[7] << 64) |
((hcl_uint128_t)c[8] << 56) |
((hcl_uint128_t)c[9] << 48) |
((hcl_uint128_t)c[10] << 40) |
((hcl_uint128_t)c[11] << 32) |
((hcl_uint128_t)c[12] << 24) |
((hcl_uint128_t)c[13] << 16) |
((hcl_uint128_t)c[14] << 8) |
((hcl_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
#endif