diff options
| author | Mattias Andrée <maandree@kth.se> | 2017-05-13 13:52:29 +0200 |
|---|---|---|
| committer | Mattias Andrée <maandree@kth.se> | 2017-05-13 13:53:11 +0200 |
| commit | e7d073bf6fa91c43753ab67644d067e2ce5881a7 (patch) | |
| tree | 340c4fcbdb7b5ccaac4eba36fb99699c6cc7df99 /src/util | |
| parent | Fix errors from the latest commits (diff) | |
| download | blind-e7d073bf6fa91c43753ab67644d067e2ce5881a7.tar.gz blind-e7d073bf6fa91c43753ab67644d067e2ce5881a7.tar.bz2 blind-e7d073bf6fa91c43753ab67644d067e2ce5881a7.tar.xz | |
Add blind-to-portable and blind-from-portable
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/endian.h | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/util/endian.h b/src/util/endian.h index e6805b1..dfe9e77 100644 --- a/src/util/endian.h +++ b/src/util/endian.h @@ -24,6 +24,44 @@ blind_htole16(uint16_t h) } # endif +# if !defined(htole32) +# define htole32 blind_htole32 +static inline uint32_t +blind_htole32(uint32_t h) +{ + union { + unsigned char bytes[4]; + uint32_t value; + } d; + d.bytes[0] = h; + d.bytes[1] = h >> 8; + d.bytes[2] = h >> 16; + d.bytes[3] = h >> 24; + return d.value; +} +# endif + +# if !defined(htole64) +# define htole64 blind_htole64 +static inline uint64_t +blind_htole64(uint64_t h) +{ + union { + unsigned char bytes[8]; + uint64_t value; + } d; + d.bytes[0] = h; + d.bytes[1] = h >> 8; + d.bytes[2] = h >> 16; + d.bytes[3] = h >> 24; + d.bytes[4] = h >> 32; + d.bytes[5] = h >> 40; + d.bytes[6] = h >> 48; + d.bytes[7] = h >> 56; + return d.value; +} +# endif + # if !defined(le16toh) # if defined(letoh16) # define le16toh letoh16 @@ -38,6 +76,46 @@ blind_le16toh(uint16_t le) # endif # endif +# if !defined(le32toh) +# if defined(letoh32) +# define le32toh letoh32 +# else +# define le32toh blind_le32toh +static inline uint32_t +blind_le32toh(uint32_t le) +{ + unsigned char *bytes = (unsigned char *)≤ + return ((uint32_t)(bytes[3]) << 24) | + ((uint32_t)(bytes[2]) << 16) | + ((uint32_t)(bytes[1]) << 8) | + (uint32_t)(bytes[0]); +} +# endif +# endif + +# if !defined(le64toh) +# if defined(letoh64) +# define le64toh letoh64 +# else +# define le64toh blind_le64toh +static inline uint64_t +blind_le64toh(uint64_t le) +{ + unsigned char *bytes = (unsigned char *)≤ + return ((uint64_t)(bytes[7]) << 56) | + ((uint64_t)(bytes[6]) << 48) | + ((uint64_t)(bytes[5]) << 40) | + ((uint64_t)(bytes[4]) << 32) | + ((uint64_t)(bytes[3]) << 24) | + ((uint64_t)(bytes[2]) << 16) | + ((uint64_t)(bytes[1]) << 8) | + (uint64_t)(bytes[0]); +} +# endif +# endif + #elif defined(HAVE_OPENBSD_ENDIAN) # define le16toh letoh16 +# define le32toh letoh32 +# define le64toh letoh64 #endif |
