Add 16-bit endian conversion functions.

This commit is contained in:
Dag-Erling Smørgrav 2015-08-17 15:37:07 +00:00 committed by des
parent e4c5e8a9dd
commit 20cd7d6012
3 changed files with 56 additions and 2 deletions

View file

@ -34,8 +34,11 @@ AC_PROG_INSTALL
#
AC_CHECK_HEADERS([endian.h sys/endian.h])
AC_CHECK_DECLS([be32enc, be32dec, le32enc, le32dec,
be64enc, be64dec, le64enc, le64dec], [], [], [[
AC_CHECK_DECLS([
be16enc, be16dec, le16enc, le16dec,
be32enc, be32dec, le32enc, le32dec,
be64enc, be64dec, le64enc, le64dec
], [], [], [[
#if HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif

View file

@ -38,6 +38,12 @@
#include <endian.h>
#endif
#if !HAVE_DECL_BE16ENC
#define be16enc cryb_be16enc
#endif
#if !HAVE_DECL_BE16DEC
#define be16dec cryb_be16dec
#endif
#if !HAVE_DECL_BE32ENC
#define be32enc cryb_be32enc
#endif
@ -50,6 +56,12 @@
#if !HAVE_DECL_BE64DEC
#define be64dec cryb_be64dec
#endif
#if !HAVE_DECL_LE16ENC
#define le16enc cryb_le16enc
#endif
#if !HAVE_DECL_LE16DEC
#define le16dec cryb_le16dec
#endif
#if !HAVE_DECL_LE32ENC
#define le32enc cryb_le32enc
#endif
@ -63,6 +75,20 @@
#define le64dec cryb_le64dec
#endif
static inline void
cryb_be16enc(void *p, uint16_t u16)
{
((uint8_t *)p)[1] = u16 & 0xff;
((uint8_t *)p)[0] = (u16 >> 8) & 0xff;
}
static inline uint16_t
cryb_be16dec(const void *p)
{
return ((uint16_t)((const uint8_t *)p)[1] |
(uint16_t)((const uint8_t *)p)[0] << 8);
}
static inline void
cryb_be32enc(void *p, uint32_t u32)
{
@ -107,6 +133,20 @@ cryb_be64dec(const void *p)
(uint64_t)((const uint8_t *)p)[0] << 56);
}
static inline void
cryb_le16enc(void *p, uint16_t u16)
{
((uint8_t *)p)[0] = u16 & 0xff;
((uint8_t *)p)[1] = (u16 >> 8) & 0xff;
}
static inline uint16_t
cryb_le16dec(const void *p)
{
return ((uint16_t)((const uint8_t *)p)[0] |
(uint16_t)((const uint8_t *)p)[1] << 8);
}
static inline void
cryb_le32enc(void *p, uint32_t u32)
{

View file

@ -37,10 +37,13 @@
#include "t.h"
static uint16_t be16 = 0xf001U;
static uint32_t be32 = 0xdeadbeefUL;
static uint64_t be64 = 0xdeadbeefcafef001ULL;
static uint16_t le16 = 0x01f0U;
static uint32_t le32 = 0xefbeaddeUL;
static uint64_t le64 = 0x01f0fecaefbeaddeULL;
static const void *s16 = "\xf0\x01";
static const void *s32 = "\xde\xad\xbe\xef";
static const void *s64 = "\xde\xad\xbe\xef\xca\xfe\xf0\x01";
@ -54,8 +57,10 @@ static const void *s64 = "\xde\xad\xbe\xef\xca\xfe\xf0\x01";
return (t_compare_x##w(e##w, dec)); \
}
T_DEC(be, 16)
T_DEC(be, 32)
T_DEC(be, 64)
T_DEC(le, 16)
T_DEC(le, 32)
T_DEC(le, 64)
@ -69,8 +74,10 @@ T_DEC(le, 64)
return (t_compare_x##w(*(const uint##w##_t *)s##w, enc)); \
}
T_ENC(be, 16)
T_ENC(be, 32)
T_ENC(be, 64)
T_ENC(le, 16)
T_ENC(le, 32)
T_ENC(le, 64)
@ -90,12 +97,16 @@ t_prepare(int argc, char *argv[])
(void)argc;
(void)argv;
T_DEC_ADD(be, 16);
T_DEC_ADD(be, 32);
T_DEC_ADD(be, 64);
T_DEC_ADD(le, 16);
T_DEC_ADD(le, 32);
T_DEC_ADD(le, 64);
T_ENC_ADD(be, 16);
T_ENC_ADD(be, 32);
T_ENC_ADD(be, 64);
T_ENC_ADD(le, 16);
T_ENC_ADD(le, 32);
T_ENC_ADD(le, 64);
return (0);