xref: /haiku/src/add-ons/kernel/file_systems/ntfs/libntfs/endians.h (revision 6b3592ca4bd03be8c016df05cdd0847dc82b807b)
1 /*
2  * endians.h - Definitions related to handling of byte ordering.
3  *             Originated from the Linux-NTFS project.
4  *
5  * Copyright (c) 2000-2005 Anton Altaparmakov
6  *
7  * This program/include file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program/include file is distributed in the hope that it will be
13  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program (in the main directory of the NTFS-3G
19  * distribution in the file COPYING); if not, write to the Free Software
20  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #ifndef _NTFS_ENDIANS_H
24 #define _NTFS_ENDIANS_H
25 
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 /*
31  * Notes:
32  *	We define the conversion functions including typecasts since the
33  * defaults don't necessarily perform appropriate typecasts.
34  *	Also, using our own functions means that we can change them if it
35  * turns out that we do need to use the unaligned access macros on
36  * architectures requiring aligned memory accesses...
37  */
38 
39 #ifdef HAVE_ENDIAN_H
40 #include <endian.h>
41 #endif
42 #ifdef HAVE_SYS_ENDIAN_H
43 #include <sys/endian.h>
44 #endif
45 #ifdef HAVE_MACHINE_ENDIAN_H
46 #include <machine/endian.h>
47 #endif
48 #ifdef HAVE_SYS_BYTEORDER_H
49 #include <sys/byteorder.h>
50 #endif
51 #ifdef HAVE_BYTEORDER_H
52 #include <byteorder.h>
53 #endif
54 #ifdef HAVE_SYS_PARAM_H
55 #include <sys/param.h>
56 #endif
57 
58 #ifndef __BYTE_ORDER
59 #	if defined(_BYTE_ORDER)
60 #		define __BYTE_ORDER _BYTE_ORDER
61 #		define __LITTLE_ENDIAN _LITTLE_ENDIAN
62 #		define __BIG_ENDIAN _BIG_ENDIAN
63 #	elif defined(BYTE_ORDER)
64 #		define __BYTE_ORDER BYTE_ORDER
65 #		define __LITTLE_ENDIAN LITTLE_ENDIAN
66 #		define __BIG_ENDIAN BIG_ENDIAN
67 #	elif defined(__BYTE_ORDER__)
68 #		define __BYTE_ORDER __BYTE_ORDER__
69 #		define __LITTLE_ENDIAN __LITTLE_ENDIAN__
70 #		define __BIG_ENDIAN __BIG_ENDIAN__
71 #	elif (defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)) || \
72 			defined(WORDS_LITTLEENDIAN)
73 #		define __BYTE_ORDER 1
74 #		define __LITTLE_ENDIAN 1
75 #		define __BIG_ENDIAN 0
76 #	elif (!defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)) || \
77 			defined(WORDS_BIGENDIAN)
78 #		define __BYTE_ORDER 0
79 #		define __LITTLE_ENDIAN 1
80 #		define __BIG_ENDIAN 0
81 #	else
82 #		error "__BYTE_ORDER is not defined."
83 #	endif
84 #endif
85 
86 #define __ntfs_bswap_constant_16(x)		\
87 	  (u16)((((u16)(x) & 0xff00) >> 8) |	\
88 		(((u16)(x) & 0x00ff) << 8))
89 
90 #define __ntfs_bswap_constant_32(x)			\
91 	  (u32)((((u32)(x) & 0xff000000u) >> 24) |	\
92 		(((u32)(x) & 0x00ff0000u) >>  8) |	\
93 		(((u32)(x) & 0x0000ff00u) <<  8) |	\
94 		(((u32)(x) & 0x000000ffu) << 24))
95 
96 #define __ntfs_bswap_constant_64(x)				\
97 	  (u64)((((u64)(x) & 0xff00000000000000ull) >> 56) |	\
98 		(((u64)(x) & 0x00ff000000000000ull) >> 40) |	\
99 		(((u64)(x) & 0x0000ff0000000000ull) >> 24) |	\
100 		(((u64)(x) & 0x000000ff00000000ull) >>  8) |	\
101 		(((u64)(x) & 0x00000000ff000000ull) <<  8) |	\
102 		(((u64)(x) & 0x0000000000ff0000ull) << 24) |	\
103 		(((u64)(x) & 0x000000000000ff00ull) << 40) |	\
104 		(((u64)(x) & 0x00000000000000ffull) << 56))
105 
106 #ifdef HAVE_BYTESWAP_H
107 #	include <byteswap.h>
108 #else
109 #	define bswap_16(x) __ntfs_bswap_constant_16(x)
110 #	define bswap_32(x) __ntfs_bswap_constant_32(x)
111 #	define bswap_64(x) __ntfs_bswap_constant_64(x)
112 #endif
113 
114 #if defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN)
115 
116 #define __le16_to_cpu(x) (x)
117 #define __le32_to_cpu(x) (x)
118 #define __le64_to_cpu(x) (x)
119 
120 #define __cpu_to_le16(x) (x)
121 #define __cpu_to_le32(x) (x)
122 #define __cpu_to_le64(x) (x)
123 
124 #define __constant_le16_to_cpu(x) (x)
125 #define __constant_le32_to_cpu(x) (x)
126 #define __constant_le64_to_cpu(x) (x)
127 
128 #define __constant_cpu_to_le16(x) (x)
129 #define __constant_cpu_to_le32(x) (x)
130 #define __constant_cpu_to_le64(x) (x)
131 
132 #elif defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
133 
134 #define __le16_to_cpu(x) bswap_16(x)
135 #define __le32_to_cpu(x) bswap_32(x)
136 #define __le64_to_cpu(x) bswap_64(x)
137 
138 #define __cpu_to_le16(x) bswap_16(x)
139 #define __cpu_to_le32(x) bswap_32(x)
140 #define __cpu_to_le64(x) bswap_64(x)
141 
142 #define __constant_le16_to_cpu(x) __ntfs_bswap_constant_16((u16)(x))
143 #define __constant_le32_to_cpu(x) __ntfs_bswap_constant_32((u32)(x))
144 #define __constant_le64_to_cpu(x) __ntfs_bswap_constant_64((u64)(x))
145 
146 #define __constant_cpu_to_le16(x) __ntfs_bswap_constant_16((u16)(x))
147 #define __constant_cpu_to_le32(x) __ntfs_bswap_constant_32((u32)(x))
148 #define __constant_cpu_to_le64(x) __ntfs_bswap_constant_64((u64)(x))
149 
150 #else
151 
152 #error "You must define __BYTE_ORDER to be __LITTLE_ENDIAN or __BIG_ENDIAN."
153 
154 #endif
155 
156 /* Unsigned from LE to CPU conversion. */
157 
158 #define le16_to_cpu(x)		(u16)__le16_to_cpu((u16)(x))
159 #define le32_to_cpu(x)		(u32)__le32_to_cpu((u32)(x))
160 #define le64_to_cpu(x)		(u64)__le64_to_cpu((u64)(x))
161 
162 #define le16_to_cpup(x)		(u16)__le16_to_cpu(*(const u16*)(x))
163 #define le32_to_cpup(x)		(u32)__le32_to_cpu(*(const u32*)(x))
164 #define le64_to_cpup(x)		(u64)__le64_to_cpu(*(const u64*)(x))
165 
166 /* Signed from LE to CPU conversion. */
167 
168 #define sle16_to_cpu(x)		(s16)__le16_to_cpu((s16)(x))
169 #define sle32_to_cpu(x)		(s32)__le32_to_cpu((s32)(x))
170 #define sle64_to_cpu(x)		(s64)__le64_to_cpu((s64)(x))
171 
172 #define sle16_to_cpup(x)	(s16)__le16_to_cpu(*(s16*)(x))
173 #define sle32_to_cpup(x)	(s32)__le32_to_cpu(*(s32*)(x))
174 #define sle64_to_cpup(x)	(s64)__le64_to_cpu(*(s64*)(x))
175 
176 /* Unsigned from CPU to LE conversion. */
177 
178 #define cpu_to_le16(x)		(u16)__cpu_to_le16((u16)(x))
179 #define cpu_to_le32(x)		(u32)__cpu_to_le32((u32)(x))
180 #define cpu_to_le64(x)		(u64)__cpu_to_le64((u64)(x))
181 
182 #define cpu_to_le16p(x)		(u16)__cpu_to_le16(*(u16*)(x))
183 #define cpu_to_le32p(x)		(u32)__cpu_to_le32(*(u32*)(x))
184 #define cpu_to_le64p(x)		(u64)__cpu_to_le64(*(u64*)(x))
185 
186 /* Signed from CPU to LE conversion. */
187 
188 #define cpu_to_sle16(x)		(s16)__cpu_to_le16((s16)(x))
189 #define cpu_to_sle32(x)		(s32)__cpu_to_le32((s32)(x))
190 #define cpu_to_sle64(x)		(s64)__cpu_to_le64((s64)(x))
191 
192 #define cpu_to_sle16p(x)	(s16)__cpu_to_le16(*(s16*)(x))
193 #define cpu_to_sle32p(x)	(s32)__cpu_to_le32(*(s32*)(x))
194 #define cpu_to_sle64p(x)	(s64)__cpu_to_le64(*(s64*)(x))
195 
196 /* Constant endianness conversion defines. */
197 
198 #define const_le16_to_cpu(x)	__constant_le16_to_cpu(x)
199 #define const_le32_to_cpu(x)	__constant_le32_to_cpu(x)
200 #define const_le64_to_cpu(x)	__constant_le64_to_cpu(x)
201 
202 #define const_cpu_to_le16(x)	__constant_cpu_to_le16(x)
203 #define const_cpu_to_le32(x)	__constant_cpu_to_le32(x)
204 #define const_cpu_to_le64(x)	__constant_cpu_to_le64(x)
205 
206 #endif /* defined _NTFS_ENDIANS_H */
207