|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // エンディアン関係
9: //
10:
11: // エンディアン関係が定義してあるヘッダと、定義内容が全員微妙に異なる。
12: // バイトオーダは BYTE_ORDER、LITTLE_ENDIAN、BIG_ENDIAN の3つの定義。
13: // なお bswapNN() はここではなく mybswap.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:
26: #pragma once
27:
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
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.