blind

suckless command-line video editing utility
git clone git://git.suckless.org/blind
Log | Files | Refs | README | LICENSE

endian.h (2965B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stdint.h>
      3 
      4 #if defined(HAVE_ENDIAN_H)
      5 # include <endian.h>
      6 #elif defined(HAVE_SYS_ENDIAN_H)
      7 # include <sys/endian.h>
      8 #endif
      9 
     10 #if !defined(HAVE_ENDIAN_H) && !defined(HAVE_SYS_ENDIAN_H)
     11 
     12 # if defined(HOST_ENDIAN_IS_LITTLE_ENDIAN_16)
     13 #  if !defined(htole16)
     14 #   define htole16(x) (x)
     15 #  endif
     16 #  if !defined(htole16)
     17 #   define letoh16(x) (x)
     18 #  endif
     19 # endif
     20 # if defined(HOST_ENDIAN_IS_LITTLE_ENDIAN_32)
     21 #  if !defined(htole32)
     22 #   define htole32(x) (x)
     23 #  endif
     24 #  if !defined(htole32)
     25 #   define letoh32(x) (x)
     26 #  endif
     27 # endif
     28 # if defined(HOST_ENDIAN_IS_LITTLE_ENDIAN_64)
     29 #  if !defined(htole64)
     30 #   define htole16(x) (x)
     31 #  endif
     32 #  if !defined(htole64)
     33 #   define letoh16(x) (x)
     34 #  endif
     35 # endif
     36 
     37 # if !defined(htole16)
     38 #  define htole16 blind_htole16
     39 static inline uint16_t
     40 blind_htole16(uint16_t h)
     41 {
     42 	union {
     43 		unsigned char bytes[2];
     44 		uint16_t value;
     45 	} d;
     46 	d.bytes[0] = h;
     47 	d.bytes[1] = h >> 8;
     48 	return d.value;
     49 }
     50 # endif
     51 
     52 # if !defined(htole32)
     53 #  define htole32 blind_htole32
     54 static inline uint32_t
     55 blind_htole32(uint32_t h)
     56 {
     57 	union {
     58 		unsigned char bytes[4];
     59 		uint32_t value;
     60 	} d;
     61 	d.bytes[0] = h;
     62 	d.bytes[1] = h >> 8;
     63 	d.bytes[2] = h >> 16;
     64 	d.bytes[3] = h >> 24;
     65 	return d.value;
     66 }
     67 # endif
     68 
     69 # if !defined(htole64)
     70 #  define htole64 blind_htole64
     71 static inline uint64_t
     72 blind_htole64(uint64_t h)
     73 {
     74 	union {
     75 		unsigned char bytes[8];
     76 		uint64_t value;
     77 	} d;
     78 	d.bytes[0] = h;
     79 	d.bytes[1] = h >> 8;
     80 	d.bytes[2] = h >> 16;
     81 	d.bytes[3] = h >> 24;
     82 	d.bytes[4] = h >> 32;
     83 	d.bytes[5] = h >> 40;
     84 	d.bytes[6] = h >> 48;
     85 	d.bytes[7] = h >> 56;
     86 	return d.value;
     87 }
     88 # endif
     89 
     90 # if !defined(le16toh)
     91 #  if defined(letoh16)
     92 #   define le16toh letoh16
     93 #  else
     94 #   define le16toh blind_le16toh
     95 static inline uint16_t
     96 blind_le16toh(uint16_t le)
     97 {
     98 	unsigned char *bytes = (unsigned char *)&le;
     99 	return ((uint16_t)(bytes[1]) << 8) | (uint16_t)(bytes[0]);
    100 }
    101 #  endif
    102 # endif
    103 
    104 # if !defined(le32toh)
    105 #  if defined(letoh32)
    106 #   define le32toh letoh32
    107 #  else
    108 #   define le32toh blind_le32toh
    109 static inline uint32_t
    110 blind_le32toh(uint32_t le)
    111 {
    112 	unsigned char *bytes = (unsigned char *)&le;
    113 	return ((uint32_t)(bytes[3]) << 24) |
    114 	       ((uint32_t)(bytes[2]) << 16) |
    115 	       ((uint32_t)(bytes[1]) << 8) |
    116 		(uint32_t)(bytes[0]);
    117 }
    118 #  endif
    119 # endif
    120 
    121 # if !defined(le64toh)
    122 #  if defined(letoh64)
    123 #   define le64toh letoh64
    124 #  else
    125 #   define le64toh blind_le64toh
    126 static inline uint64_t
    127 blind_le64toh(uint64_t le)
    128 {
    129 	unsigned char *bytes = (unsigned char *)&le;
    130 	return ((uint64_t)(bytes[7]) << 56) |
    131 	       ((uint64_t)(bytes[6]) << 48) |
    132 	       ((uint64_t)(bytes[5]) << 40) |
    133 	       ((uint64_t)(bytes[4]) << 32) |
    134 	       ((uint64_t)(bytes[3]) << 24) |
    135 	       ((uint64_t)(bytes[2]) << 16) |
    136 	       ((uint64_t)(bytes[1]) << 8) |
    137 	       (uint64_t)(bytes[0]);
    138 }
    139 #  endif
    140 # endif
    141 
    142 #elif defined(HAVE_OPENBSD_ENDIAN)
    143 # define le16toh letoh16
    144 # define le32toh letoh32
    145 # define le64toh letoh64
    146 #endif