|
|
1.1 root 1: //
2: // nono
1.1.1.3 root 3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
1.1 root 5: //
6:
1.1.1.5 ! root 7: //
! 8: // エンディアン関係
! 9: //
! 10:
! 11: // エンディアン関係が定義してあるヘッダと、定義内容が全員微妙に異なる。
! 12: // バイトオーダは BYTE_ORDER、LITTLE_ENDIAN、BIG_ENDIAN の3つの定義。
! 13: // なお bswapNN() はここではなく missing_bswap.h 参照。
! 14: //
! 15: // OS | ヘッダ | バイトオーダ | htobeNN() 系マクロ
! 16: // ---------+--------------------+--------------+-------------------
! 17: // NetBSD | <sys/endian.h> | _BYTE_ORDER | あり
! 18: // FreeBSD | <sys/endian.h> | | あり
! 19: // OpenBSD | <sys/endian.h> | | あり(*1)
! 20: // Linux | <endian.h> | BYTE_ORDER | あり
! 21: // OSX | <machine/endian.h> | BYTE_ORDER | なし
! 22: //
! 23: // *1: OpenBSD は本来 betohNN() のように NN は常に後置だったが beNNtoh()
! 24: // 形式も使える。
! 25:
1.1 root 26: #pragma once
27:
1.1.1.5 ! root 28: #if defined(HAVE_ENDIAN_H)
! 29: #include <endian.h> // Linux
! 30: #endif
! 31: #if defined(HAVE_SYS_ENDIAN_H)
! 32: #include <sys/endian.h> // *BSD
! 33: #endif
! 34: #if defined(HAVE_MACHINE_ENDIAN_H)
! 35: #include <machine/endian.h> // OSX
! 36: #endif
! 37:
! 38: #if defined(BYTE_ORDER)
! 39: #elif defined(_BYTE_ORDER)
! 40: #define BYTE_ORDER _BYTE_ORDER
! 41: #elif defined(__BYTE_ORDER)
! 42: #define BYTE_ORDER __BYTE_ORDER
! 43: #else
! 44: #error no BYTE_ORDER defined
! 45: #endif
! 46:
! 47: #if defined(BIG_ENDIAN)
! 48: #elif defined(_BIG_ENDIAN)
! 49: #define BIG_ENDIAN _BIG_ENDIAN
! 50: #elif defined(__BIG_ENDIAN)
! 51: #define BIG_ENDIAN __BIG_ENDIAN
! 52: #else
! 53: #error no BIG_ENDIAN defined
! 54: #endif
! 55:
! 56: #if defined(LITTLE_ENDIAN)
! 57: #elif defined(_LITTLE_ENDIAN)
! 58: #define LITTLE_ENDIAN _LITTLE_ENDIAN
! 59: #elif defined(__LITTLE_ENDIAN)
! 60: #define LITTLE_ENDIAN __LITTLE_ENDIAN
! 61: #else
! 62: #error no LITTLE_ENDIAN defined
! 63: #endif
! 64:
! 65: // htobeNN() 系マクロ。
! 66: // 代表として htobe16() の有無だけチェックしている。
! 67: #if !defined(HAVE_HTOBE16)
! 68: #if defined(HAVE_LIBKERN_OSBYTEORDER_H)
! 69: #include <libkern/OSByteOrder.h>
! 70: #define htobe16(x) OSSwapHostToBigInt16(x)
! 71: #define htobe32(x) OSSwapHostToBigInt32(x)
! 72: #define htobe64(x) OSSwapHostToBigInt64(x)
! 73: #define htole16(x) OSSwapHostToLittleInt16(x)
! 74: #define htole32(x) OSSwapHostToLittleInt32(x)
! 75: #define htole64(x) OSSwapHostToLittleInt64(x)
! 76:
! 77: #define be16toh(x) OSSwapBigToHostInt16(x)
! 78: #define be32toh(x) OSSwapBigToHostInt32(x)
! 79: #define be64toh(x) OSSwapBigToHostInt64(x)
! 80: #define le16toh(x) OSSwapLittleToHostInt16(x)
! 81: #define le32toh(x) OSSwapLittleToHostInt32(x)
! 82: #define le64toh(x) OSSwapLittleToHostInt64(x)
! 83:
! 84: #else
! 85: #error No htobeNN() family defined
! 86: #endif
! 87:
! 88: #endif // !HAVE_HTOBE16
! 89:
! 90: // HTOBEnn() 系マクロ。
! 91: // 代表として HTOBE16() の有無だけチェックしている。
! 92: #if !defined(HAVE_INPLACE_HTOBE16)
! 93: #if BYTE_ORDER == BIG_ENDIAN
! 94: #define HTOBE16(x) (void)(x)
! 95: #define HTOBE32(x) (void)(x)
! 96: #define HTOBE64(x) (void)(x)
! 97: #define HTOLE16(x) (x) = (uint16)htole16(x)
! 98: #define HTOLE32(x) (x) = (uint32)htole32(x)
! 99: #define HTOLE64(x) (x) = (uint64)htole64(x)
1.1 root 100: #else
1.1.1.5 ! root 101: #define HTOBE16(x) (x) = (uint16)htobe16(x)
! 102: #define HTOBE32(x) (x) = (uint32)htobe32(x)
! 103: #define HTOBE64(x) (x) = (uint64)htobe64(x)
! 104: #define HTOLE16(x) (void)(x)
! 105: #define HTOLE32(x) (void)(x)
! 106: #define HTOLE64(x) (void)(x)
1.1 root 107: #endif
108:
1.1.1.5 ! root 109: #define BE16TOH(x) HTOBE16(x)
! 110: #define BE32TOH(x) HTOBE32(x)
! 111: #define BE64TOH(x) HTOBE64(x)
! 112: #define LE16TOH(x) HTOLE16(x)
! 113: #define LE32TOH(x) HTOLE32(x)
! 114: #define LE64TOH(x) HTOLE64(x)
! 115: #endif // !HAVE_INPLACE_HTOBE16
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.