endian.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef parserutils_endian_h_
00009 #define parserutils_endian_h_
00010
00011 static inline bool endian_host_is_le(void)
00012 {
00013 static uint32_t magic = 0x10000002;
00014
00015 return (((uint8_t *) &magic)[0] == 0x02);
00016 }
00017
00018 static inline uint32_t endian_swap(uint32_t val)
00019 {
00020 return ((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) |
00021 ((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24);
00022 }
00023
00024 static inline uint32_t endian_host_to_big(uint32_t host)
00025 {
00026 if (endian_host_is_le())
00027 return endian_swap(host);
00028
00029 return host;
00030 }
00031
00032 static inline uint32_t endian_big_to_host(uint32_t big)
00033 {
00034 if (endian_host_is_le())
00035 return endian_swap(big);
00036
00037 return big;
00038 }
00039
00040 #endif