1 /*
2  * WinPR: Windows Portable Runtime
3  * Endianness Macros
4  *
5  * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef WINPR_ENDIAN_H
21 #define WINPR_ENDIAN_H
22 
23 #include <winpr/winpr.h>
24 #include <winpr/wtypes.h>
25 #include <winpr/platform.h>
26 
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31 
32 #define Data_Read_UINT8_NE(_d, _v) \
33 	do                             \
34 	{                              \
35 		_v = *((const BYTE*)_d);   \
36 	} while (0)
37 
38 #define Data_Read_UINT8(_d, _v)  \
39 	do                           \
40 	{                            \
41 		_v = *((const BYTE*)_d); \
42 	} while (0)
43 
44 #define Data_Read_UINT16_NE(_d, _v) \
45 	do                              \
46 	{                               \
47 		_v = *((const UINT16*)_d);  \
48 	} while (0)
49 
50 #define Data_Read_UINT16(_d, _v)                                                               \
51 	do                                                                                         \
52 	{                                                                                          \
53 		_v = (UINT16)(*((const BYTE*)_d)) + (UINT16)(((UINT16)(*((const BYTE*)_d + 1))) << 8); \
54 	} while (0)
55 
56 #define Data_Read_UINT16_BE(_d, _v)                                                  \
57 	do                                                                               \
58 	{                                                                                \
59 		_v = (((UINT16)(*(const BYTE*)_d)) << 8) + (UINT16)(*((const BYTE*)_d + 1)); \
60 	} while (0)
61 
62 #define Data_Read_UINT32_NE(_d, _v) \
63 	do                              \
64 	{                               \
65 		_v = *((UINT32*)_d);        \
66 	} while (0)
67 
68 #define Data_Read_UINT32(_d, _v)                                                        \
69 	do                                                                                  \
70 	{                                                                                   \
71 		_v = (UINT32)(*((const BYTE*)_d)) + (((UINT32)(*((const BYTE*)_d + 1))) << 8) + \
72 		     (((UINT32)(*((const BYTE*)_d + 2))) << 16) +                               \
73 		     (((UINT32)(*((const BYTE*)_d + 3))) << 24);                                \
74 	} while (0)
75 
76 #define Data_Read_UINT32_BE(_d, _v)                                                                \
77 	do                                                                                             \
78 	{                                                                                              \
79 		_v = (((UINT32)(*((const BYTE*)_d))) << 24) + (((UINT32)(*((const BYTE*)_d + 1))) << 16) + \
80 		     (((UINT32)(*((const BYTE*)_d + 2))) << 8) + (((UINT32)(*((const BYTE*)_d + 3))));     \
81 	} while (0)
82 
83 #define Data_Read_UINT64_NE(_d, _v) \
84 	do                              \
85 	{                               \
86 		_v = *((UINT64*)_d);        \
87 	} while (0)
88 
89 #define Data_Read_UINT64(_d, _v)                                                        \
90 	do                                                                                  \
91 	{                                                                                   \
92 		_v = (UINT64)(*((const BYTE*)_d)) + (((UINT64)(*((const BYTE*)_d + 1))) << 8) + \
93 		     (((UINT64)(*((const BYTE*)_d + 2))) << 16) +                               \
94 		     (((UINT64)(*((const BYTE*)_d + 3))) << 24) +                               \
95 		     (((UINT64)(*((const BYTE*)_d + 4))) << 32) +                               \
96 		     (((UINT64)(*((const BYTE*)_d + 5))) << 40) +                               \
97 		     (((UINT64)(*((const BYTE*)_d + 6))) << 48) +                               \
98 		     (((UINT64)(*((const BYTE*)_d + 7))) << 56);                                \
99 	} while (0)
100 
101 #define Data_Write_UINT8_NE(_d, _v) \
102 	do                              \
103 	{                               \
104 		*((UINT8*)_d) = v;          \
105 	} while (0)
106 
107 #define Data_Write_UINT8(_d, _v) \
108 	do                           \
109 	{                            \
110 		*_d = (UINT8)(_v);       \
111 	} while (0)
112 
113 #define Data_Write_UINT16_NE(_d, _v) \
114 	do                               \
115 	{                                \
116 		*((UINT16*)_d) = _v;         \
117 	} while (0)
118 
119 #define Data_Write_UINT16(_d, _v)              \
120 	do                                         \
121 	{                                          \
122 		*((BYTE*)_d) = (_v)&0xFF;              \
123 		*((BYTE*)_d + 1) = ((_v) >> 8) & 0xFF; \
124 	} while (0)
125 
126 #define Data_Write_UINT16_BE(_d, _v)       \
127 	do                                     \
128 	{                                      \
129 		*((BYTE*)_d) = ((_v) >> 8) & 0xFF; \
130 		*((BYTE*)_d + 1) = (_v)&0xFF;      \
131 	} while (0)
132 
133 #define Data_Write_UINT32_NE(_d, _v) \
134 	do                               \
135 	{                                \
136 		*((UINT32*)_d) = _v;         \
137 	} while (0)
138 
139 #define Data_Write_UINT32(_d, _v)               \
140 	do                                          \
141 	{                                           \
142 		*((BYTE*)_d) = (_v)&0xFF;               \
143 		*((BYTE*)_d + 1) = ((_v) >> 8) & 0xFF;  \
144 		*((BYTE*)_d + 2) = ((_v) >> 16) & 0xFF; \
145 		*((BYTE*)_d + 3) = ((_v) >> 24) & 0xFF; \
146 	} while (0)
147 
148 #define Data_Write_UINT32_BE(_d, _v)                            \
149 	do                                                          \
150 	{                                                           \
151 		Data_Write_UINT16_BE((BYTE*)_d, ((_v) >> 16 & 0xFFFF)); \
152 		Data_Write_UINT16_BE((BYTE*)_d + 2, ((_v)&0xFFFF));     \
153 	} while (0)
154 
155 #define Data_Write_UINT64_NE(_d, _v) \
156 	do                               \
157 	{                                \
158 		*((UINT64*)_d) = _v;         \
159 	} while (0)
160 
161 #define Data_Write_UINT64(_d, _v)                       \
162 	do                                                  \
163 	{                                                   \
164 		*((BYTE*)_d) = (UINT64)(_v)&0xFF;               \
165 		*((BYTE*)_d + 1) = ((UINT64)(_v) >> 8) & 0xFF;  \
166 		*((BYTE*)_d + 2) = ((UINT64)(_v) >> 16) & 0xFF; \
167 		*((BYTE*)_d + 3) = ((UINT64)(_v) >> 24) & 0xFF; \
168 		*((BYTE*)_d + 4) = ((UINT64)(_v) >> 32) & 0xFF; \
169 		*((BYTE*)_d + 5) = ((UINT64)(_v) >> 40) & 0xFF; \
170 		*((BYTE*)_d + 6) = ((UINT64)(_v) >> 48) & 0xFF; \
171 		*((BYTE*)_d + 7) = ((UINT64)(_v) >> 56) & 0xFF; \
172 	} while (0)
173 
174 #ifdef __cplusplus
175 }
176 #endif
177 
178 #endif /* WINPR_ENDIAN_H */
179