comparison OrthancFramework/Sources/Endianness.h @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/Endianness.h@94f4a18a79cc
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #pragma once
35
36
37 /********************************************************************
38 ** LINUX-LIKE ARCHITECTURES
39 ********************************************************************/
40
41 #if defined(__LSB_VERSION__)
42 // Linux Standard Base (LSB) does not come with be16toh, be32toh, and
43 // be64toh
44 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 0
45 # include <endian.h>
46 #elif defined(__linux__) || defined(__EMSCRIPTEN__)
47 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
48 # include <endian.h>
49 #endif
50
51
52 /********************************************************************
53 ** WINDOWS ARCHITECTURES
54 **
55 ** On Windows x86, "host" will always be little-endian ("le").
56 ********************************************************************/
57
58 #if defined(_WIN32)
59 # if defined(_MSC_VER)
60 // Visual Studio - http://msdn.microsoft.com/en-us/library/a3140177.aspx
61 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
62 # define be16toh(x) _byteswap_ushort(x)
63 # define be32toh(x) _byteswap_ulong(x)
64 # define be64toh(x) _byteswap_uint64(x)
65 # elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
66 // MinGW >= 4.3 - Use builtin intrinsic for byte swapping
67 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
68 # define be16toh(x) __builtin_bswap16(x)
69 # define be32toh(x) __builtin_bswap32(x)
70 # define be64toh(x) __builtin_bswap64(x)
71 # else
72 // MinGW <= 4.2, we must manually implement the byte swapping (*)
73 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 0
74 # define be16toh(x) __orthanc_bswap16(x)
75 # define be32toh(x) __orthanc_bswap32(x)
76 # define be64toh(x) __orthanc_bswap64(x)
77 # endif
78
79 # define htobe16(x) be16toh(x)
80 # define htobe32(x) be32toh(x)
81 # define htobe64(x) be64toh(x)
82
83 # define htole16(x) x
84 # define htole32(x) x
85 # define htole64(x) x
86
87 # define le16toh(x) x
88 # define le32toh(x) x
89 # define le64toh(x) x
90 #endif
91
92
93 /********************************************************************
94 ** FREEBSD ARCHITECTURES
95 ********************************************************************/
96
97 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
98 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
99 # include <arpa/inet.h>
100 #endif
101
102
103 /********************************************************************
104 ** OPENBSD ARCHITECTURES
105 ********************************************************************/
106
107 #if defined(__OpenBSD__)
108 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
109 # include <endian.h>
110 #endif
111
112
113 /********************************************************************
114 ** APPLE ARCHITECTURES (including OS X)
115 ********************************************************************/
116
117 #if defined(__APPLE__)
118 # define ORTHANC_HAS_BUILTIN_BYTE_SWAP 1
119 # include <libkern/OSByteOrder.h>
120 # define be16toh(x) OSSwapBigToHostInt16(x)
121 # define be32toh(x) OSSwapBigToHostInt32(x)
122 # define be64toh(x) OSSwapBigToHostInt64(x)
123
124 # define htobe16(x) OSSwapHostToBigInt16(x)
125 # define htobe32(x) OSSwapHostToBigInt32(x)
126 # define htobe64(x) OSSwapHostToBigInt64(x)
127
128 # define htole16(x) OSSwapHostToLittleInt16(x)
129 # define htole32(x) OSSwapHostToLittleInt32(x)
130 # define htole64(x) OSSwapHostToLittleInt64(x)
131
132 # define le16toh(x) OSSwapLittleToHostInt16(x)
133 # define le32toh(x) OSSwapLittleToHostInt32(x)
134 # define le64toh(x) OSSwapLittleToHostInt64(x)
135 #endif
136
137
138 /********************************************************************
139 ** PORTABLE (BUT SLOW) IMPLEMENTATION OF BYTE-SWAPPING
140 ********************************************************************/
141
142 #if ORTHANC_HAS_BUILTIN_BYTE_SWAP != 1
143
144 #include <stdint.h>
145
146 static inline uint16_t __orthanc_bswap16(uint16_t a)
147 {
148 /**
149 * Note that an alternative implementation was included in Orthanc
150 * 1.4.0 and 1.4.1:
151 *
152 * # hg log -p -r 2706
153 *
154 * This alternative implementation only hid an underlying problem
155 * with pointer alignment on some architectures, and was thus
156 * reverted. Check out issue #99:
157 * https://bitbucket.org/sjodogne/orthanc/issues/99
158 **/
159 return (a << 8) | (a >> 8);
160 }
161
162 static inline uint32_t __orthanc_bswap32(uint32_t a)
163 {
164 const uint8_t* p = reinterpret_cast<const uint8_t*>(&a);
165 return (static_cast<uint32_t>(p[0]) << 24 |
166 static_cast<uint32_t>(p[1]) << 16 |
167 static_cast<uint32_t>(p[2]) << 8 |
168 static_cast<uint32_t>(p[3]));
169 }
170
171 static inline uint64_t __orthanc_bswap64(uint64_t a)
172 {
173 const uint8_t* p = reinterpret_cast<const uint8_t*>(&a);
174 return (static_cast<uint64_t>(p[0]) << 56 |
175 static_cast<uint64_t>(p[1]) << 48 |
176 static_cast<uint64_t>(p[2]) << 40 |
177 static_cast<uint64_t>(p[3]) << 32 |
178 static_cast<uint64_t>(p[4]) << 24 |
179 static_cast<uint64_t>(p[5]) << 16 |
180 static_cast<uint64_t>(p[6]) << 8 |
181 static_cast<uint64_t>(p[7]));
182 }
183
184 #if defined(_WIN32)
185 // Implemented above (*)
186 #elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
187 # if __BYTE_ORDER == __LITTLE_ENDIAN
188 # define be16toh(x) __orthanc_bswap16(x)
189 # define be32toh(x) __orthanc_bswap32(x)
190 # define be64toh(x) __orthanc_bswap64(x)
191 # define htobe16(x) __orthanc_bswap16(x)
192 # define htobe32(x) __orthanc_bswap32(x)
193 # define htobe64(x) __orthanc_bswap64(x)
194 # define htole16(x) x
195 # define htole32(x) x
196 # define htole64(x) x
197 # define le16toh(x) x
198 # define le32toh(x) x
199 # define le64toh(x) x
200 # elif __BYTE_ORDER == __BIG_ENDIAN
201 # define be16toh(x) x
202 # define be32toh(x) x
203 # define be64toh(x) x
204 # define htobe16(x) x
205 # define htobe32(x) x
206 # define htobe64(x) x
207 # define htole16(x) __orthanc_bswap16(x)
208 # define htole32(x) __orthanc_bswap32(x)
209 # define htole64(x) __orthanc_bswap64(x)
210 # define le16toh(x) __orthanc_bswap16(x)
211 # define le32toh(x) __orthanc_bswap32(x)
212 # define le64toh(x) __orthanc_bswap64(x)
213 # else
214 # error Please support your platform here
215 # endif
216 #else
217 # error Please support your platform here
218 #endif
219
220 #endif