···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2013 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#include "CoreEndian.h"
2121+#include <endian.h>
2222+#include <stdint.h>
2323+2424+template <typename T> T bswap(T value);
2525+2626+template <> uint16_t bswap(uint16_t value)
2727+{
2828+ return __builtin_bswap16(value);
2929+}
3030+3131+template <> uint32_t bswap(uint32_t value)
3232+{
3333+ return __builtin_bswap32(value);
3434+}
3535+3636+template <> uint64_t bswap(uint64_t value)
3737+{
3838+ return __builtin_bswap64(value);
3939+}
4040+4141+template <typename T> T LtoN(T value)
4242+{
4343+#if __BYTE_ORDER == __LITTLE_ENDIAN
4444+ return value;
4545+#else
4646+ return bswap(value);
4747+#endif
4848+}
4949+5050+template <typename T> T BtoN(T value)
5151+{
5252+#if __BYTE_ORDER == __BIG_ENDIAN
5353+ return value;
5454+#else
5555+ return bswap(value);
5656+#endif
5757+}
5858+5959+template <typename T> T NtoL(T value)
6060+{
6161+ return LtoN(value);
6262+}
6363+6464+template <typename T> T NtoB(T value)
6565+{
6666+ return BtoN(value);
6767+}
6868+6969+UInt16 Endian16_Swap(UInt16 value)
7070+{
7171+ return bswap(value);
7272+}
7373+7474+UInt32 Endian32_Swap(UInt32 value)
7575+{
7676+ return bswap(value);
7777+}
7878+7979+UInt64 Endian64_Swap(UInt64 value)
8080+{
8181+ return bswap(value);
8282+}
8383+8484+SInt16 EndianS16_BtoL(SInt16 value)
8585+{
8686+ return bswap(value);
8787+}
8888+8989+SInt16 EndianS16_BtoN(SInt16 value)
9090+{
9191+ return BtoN(value);
9292+}
9393+9494+SInt16 EndianS16_LtoB(SInt16 value)
9595+{
9696+ return bswap(value);
9797+}
9898+9999+SInt16 EndianS16_LtoN(SInt16 value)
100100+{
101101+ return LtoN(value);
102102+}
103103+104104+SInt16 EndianS16_NtoB(SInt16 value)
105105+{
106106+ return NtoB(value);
107107+}
108108+109109+SInt16 EndianS16_NtoL(SInt16 value)
110110+{
111111+ return NtoL(value);
112112+}
113113+114114+SInt32 EndianS32_BtoL(SInt32 value)
115115+{
116116+ return bswap(value);
117117+}
118118+119119+SInt32 EndianS32_BtoN(SInt32 value)
120120+{
121121+ return BtoN(value);
122122+}
123123+124124+SInt32 EndianS32_LtoB(SInt32 value)
125125+{
126126+ return bswap(value);
127127+}
128128+129129+SInt32 EndianS32_LtoN(SInt32 value)
130130+{
131131+ return LtoN(value);
132132+}
133133+134134+SInt32 EndianS32_NtoB(SInt32 value)
135135+{
136136+ return NtoB(value);
137137+}
138138+139139+SInt32 EndianS32_NtoL(SInt32 value)
140140+{
141141+ return NtoL(value);
142142+}
143143+144144+145145+SInt64 EndianS64_BtoL(SInt64 value)
146146+{
147147+ return bswap(value);
148148+}
149149+150150+SInt64 EndianS64_BtoN(SInt64 value)
151151+{
152152+ return BtoN(value);
153153+}
154154+155155+SInt64 EndianS64_LtoB(SInt64 value)
156156+{
157157+ return bswap(value);
158158+}
159159+160160+SInt64 EndianS64_LtoN(SInt64 value)
161161+{
162162+ return LtoN(value);
163163+}
164164+165165+SInt64 EndianS64_NtoB(SInt64 value)
166166+{
167167+ return NtoB(value);
168168+}
169169+170170+SInt64 EndianS64_NtoL(SInt64 value)
171171+{
172172+ return NtoL(value);
173173+}
174174+175175+UInt16 EndianU16_BtoL(UInt16 value)
176176+{
177177+ return bswap(value);
178178+}
179179+180180+UInt16 EndianU16_BtoN(UInt16 value)
181181+{
182182+ return BtoN(value);
183183+}
184184+185185+UInt16 EndianU16_LtoB(UInt16 value)
186186+{
187187+ return bswap(value);
188188+}
189189+190190+UInt16 EndianU16_LtoN(UInt16 value)
191191+{
192192+ return LtoN(value);
193193+}
194194+195195+UInt16 EndianU16_NtoB(UInt16 value)
196196+{
197197+ return NtoB(value);
198198+}
199199+200200+UInt16 EndianU16_NtoL(UInt16 value)
201201+{
202202+ return NtoL(value);
203203+}
204204+205205+UInt32 EndianU32_BtoL(UInt32 value)
206206+{
207207+ return bswap(value);
208208+}
209209+210210+UInt32 EndianU32_BtoN(UInt32 value)
211211+{
212212+ return BtoN(value);
213213+}
214214+215215+UInt32 EndianU32_LtoB(UInt32 value)
216216+{
217217+ return bswap(value);
218218+}
219219+220220+UInt32 EndianU32_LtoN(UInt32 value)
221221+{
222222+ return LtoN(value);
223223+}
224224+225225+UInt32 EndianU32_NtoB(UInt32 value)
226226+{
227227+ return NtoB(value);
228228+}
229229+230230+UInt32 EndianU32_NtoL(UInt32 value)
231231+{
232232+ return NtoL(value);
233233+}
234234+235235+UInt64 EndianU64_BtoL(UInt64 value)
236236+{
237237+ return bswap(value);
238238+}
239239+240240+UInt64 EndianU64_BtoN(UInt64 value)
241241+{
242242+ return BtoN(value);
243243+}
244244+245245+UInt64 EndianU64_LtoB(UInt64 value)
246246+{
247247+ return bswap(value);
248248+}
249249+250250+UInt64 EndianU64_LtoN(UInt64 value)
251251+{
252252+ return LtoN(value);
253253+}
254254+255255+UInt64 EndianU64_NtoB(UInt64 value)
256256+{
257257+ return NtoB(value);
258258+}
259259+260260+UInt64 EndianU64_NtoL(UInt64 value)
261261+{
262262+ return NtoL(value);
263263+}
264264+
+75
src/CoreServices/CoreEndian.h
···11+/*
22+This file is part of Darling.
33+44+Copyright (C) 2013 Lubos Dolezel
55+66+Darling is free software: you can redistribute it and/or modify
77+it under the terms of the GNU General Public License as published by
88+the Free Software Foundation, either version 3 of the License, or
99+(at your option) any later version.
1010+1111+Darling is distributed in the hope that it will be useful,
1212+but WITHOUT ANY WARRANTY; without even the implied warranty of
1313+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1414+GNU General Public License for more details.
1515+1616+You should have received a copy of the GNU General Public License
1717+along with Darling. If not, see <http://www.gnu.org/licenses/>.
1818+*/
1919+2020+#ifndef COREENDIAN_H
2121+#define COREENDIAN_H
2222+#include "MacTypes.h"
2323+2424+extern "C"
2525+{
2626+2727+UInt16 Endian16_Swap(UInt16 value);
2828+UInt32 Endian32_Swap(UInt32 value);
2929+UInt64 Endian64_Swap(UInt64 value);
3030+3131+SInt16 EndianS16_BtoL(SInt16 value);
3232+SInt16 EndianS16_BtoN(SInt16 value);
3333+SInt16 EndianS16_LtoB(SInt16 value);
3434+SInt16 EndianS16_LtoN(SInt16 value);
3535+SInt16 EndianS16_NtoB(SInt16 value);
3636+SInt16 EndianS16_NtoL(SInt16 value);
3737+3838+SInt32 EndianS32_BtoL(SInt32 value);
3939+SInt32 EndianS32_BtoN(SInt32 value);
4040+SInt32 EndianS32_LtoB(SInt32 value);
4141+SInt32 EndianS32_LtoN(SInt32 value);
4242+SInt32 EndianS32_NtoB(SInt32 value);
4343+SInt32 EndianS32_NtoL(SInt32 value);
4444+4545+SInt64 EndianS64_BtoL(SInt64 value);
4646+SInt64 EndianS64_BtoN(SInt64 value);
4747+SInt64 EndianS64_LtoB(SInt64 value);
4848+SInt64 EndianS64_LtoN(SInt64 value);
4949+SInt64 EndianS64_NtoB(SInt64 value);
5050+SInt64 EndianS64_NtoL(SInt64 value);
5151+5252+UInt16 EndianU16_BtoL(UInt16 value);
5353+UInt16 EndianU16_BtoN(UInt16 value);
5454+UInt16 EndianU16_LtoB(UInt16 value);
5555+UInt16 EndianU16_LtoN(UInt16 value);
5656+UInt16 EndianU16_NtoB(UInt16 value);
5757+UInt16 EndianU16_NtoL(UInt16 value);
5858+5959+UInt32 EndianU32_BtoL(UInt32 value);
6060+UInt32 EndianU32_BtoN(UInt32 value);
6161+UInt32 EndianU32_LtoB(UInt32 value);
6262+UInt32 EndianU32_LtoN(UInt32 value);
6363+UInt32 EndianU32_NtoB(UInt32 value);
6464+UInt32 EndianU32_NtoL(UInt32 value);
6565+6666+UInt64 EndianU64_BtoL(UInt64 value);
6767+UInt64 EndianU64_BtoN(UInt64 value);
6868+UInt64 EndianU64_LtoB(UInt64 value);
6969+UInt64 EndianU64_LtoN(UInt64 value);
7070+UInt64 EndianU64_NtoB(UInt64 value);
7171+UInt64 EndianU64_NtoL(UInt64 value);
7272+7373+}
7474+7575+#endif