FreeRDP
crt.h
1 
20 #ifndef WINPR_CRT_H
21 #define WINPR_CRT_H
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include <winpr/platform.h>
28 #include <winpr/winpr.h>
29 
30 #include <winpr/spec.h>
31 #include <winpr/string.h>
32 
33 WINPR_PRAGMA_DIAG_PUSH
34 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_IDENTIFIER
35 
36 #ifndef _WIN32
37 
38 #include <unistd.h>
39 
40 #ifndef _write
41 #define _write write
42 #endif
43 
44 #ifndef _strtoui64
45 #define _strtoui64 strtoull
46 #endif /* _strtoui64 */
47 
48 #ifndef _strtoi64
49 #define _strtoi64 strtoll
50 #endif /* _strtoi64 */
51 
52 #ifndef _rotl
53 static INLINE UINT32 _rotl(UINT32 value, int shift)
54 {
55  return (value << shift) | (value >> (32 - shift));
56 }
57 #endif /* _rotl */
58 
59 #ifndef _rotl64
60 static INLINE UINT64 _rotl64(UINT64 value, int shift)
61 {
62  return (value << shift) | (value >> (64 - shift));
63 }
64 #endif /* _rotl64 */
65 
66 #ifndef _rotr
67 static INLINE UINT32 _rotr(UINT32 value, int shift)
68 {
69  return (value >> shift) | (value << (32 - shift));
70 }
71 #endif /* _rotr */
72 
73 #ifndef _rotr64
74 static INLINE UINT64 _rotr64(UINT64 value, int shift)
75 {
76  return (value >> shift) | (value << (64 - shift));
77 }
78 #endif /* _rotr64 */
79 
80 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2))
81 
82 #define _byteswap_ulong(_val) __builtin_bswap32(_val)
83 #define _byteswap_uint64(_val) __builtin_bswap64(_val)
84 
85 #else
86 
87 static INLINE UINT32 _byteswap_ulong(UINT32 _val)
88 {
89  return (((_val) >> 24) | (((_val)&0x00FF0000) >> 8) | (((_val)&0x0000FF00) << 8) |
90  ((_val) << 24));
91 }
92 
93 static INLINE UINT64 _byteswap_uint64(UINT64 _val)
94 {
95  return (((_val) << 56) | (((_val) << 40) & 0xFF000000000000) |
96  (((_val) << 24) & 0xFF0000000000) | (((_val) << 8) & 0xFF00000000) |
97  (((_val) >> 8) & 0xFF000000) | (((_val) >> 24) & 0xFF0000) | (((_val) >> 40) & 0xFF00) |
98  ((_val) >> 56));
99 }
100 
101 #endif /* (__GNUC__ > 4) || ... */
102 
103 #if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8))
104 
105 #define _byteswap_ushort(_val) __builtin_bswap16(_val)
106 
107 #else
108 
109 static INLINE UINT16 _byteswap_ushort(UINT16 _val)
110 {
111 #ifdef __cplusplus
112 #define winpr_byteswap_cast(t, val) static_cast<t>(val)
113 #else
114 #define winpr_byteswap_cast(t, val) (t)(val)
115 #endif
116  return winpr_byteswap_cast(UINT16, ((_val) >> 8U) | ((_val) << 8U));
117 #undef winpr_byteswap_cast
118 }
119 
120 #endif /* (__GNUC__ > 4) || ... */
121 
122 #define CopyMemory(Destination, Source, Length) memcpy((Destination), (Source), (Length))
123 #define MoveMemory(Destination, Source, Length) memmove((Destination), (Source), (Length))
124 #define FillMemory(Destination, Length, Fill) memset((Destination), (Fill), (Length))
125 #define ZeroMemory(Destination, Length) memset((Destination), 0, (Length))
126 
127 #ifdef __cplusplus
128 extern "C"
129 {
130 #endif
131 
132  WINPR_API PVOID SecureZeroMemory(PVOID ptr, size_t cnt);
133 
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 #endif /* _WIN32 */
139 
140 /* Data Alignment */
141 
142 WINPR_PRAGMA_DIAG_PUSH
143 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
144 
145 #ifndef _ERRNO_T_DEFINED
146 #define _ERRNO_T_DEFINED
147 typedef int errno_t;
148 #endif /* _ERRNO_T_DEFINED */
149 
150 WINPR_PRAGMA_DIAG_POP
151 
152 #ifndef _WIN32
153 
154 #ifdef __cplusplus
155 extern "C"
156 {
157 #endif
158 
159  /* Data Conversion */
160 
161  WINPR_API errno_t _itoa_s(int value, char* buffer, size_t sizeInCharacters, int radix);
162 
163  /* Buffer Manipulation */
164 
165  WINPR_API errno_t memmove_s(void* dest, size_t numberOfElements, const void* src, size_t count);
166  WINPR_API errno_t wmemmove_s(WCHAR* dest, size_t numberOfElements, const WCHAR* src,
167  size_t count);
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif /* _WIN32 */
173 
174 #if !defined(_WIN32) || (defined(__MINGW32__) && !defined(_UCRT))
175 /* note: we use our own implementation of _aligned_XXX function when:
176  * - it's not win32
177  * - it's mingw with native libs (not ucrt64) because we didn't managed to have it working
178  * and not have C runtime deadly mixes
179  */
180 #if defined(WINPR_MSVCR_ALIGNMENT_EMULATE)
181 #define _aligned_malloc winpr_aligned_malloc
182 #define _aligned_realloc winpr_aligned_realloc
183 #define _aligned_recalloc winpr_aligned_recalloc
184 #define _aligned_offset_malloc winpr_aligned_offset_malloc
185 #define _aligned_offset_realloc winpr_aligned_offset_realloc
186 #define _aligned_offset_recalloc winpr_aligned_offset_recalloc
187 #define _aligned_msize winpr_aligned_msize
188 #define _aligned_free winpr_aligned_free
189 #endif
190 
191 #ifdef __cplusplus
192 extern "C"
193 {
194 #endif
195 
196  WINPR_API void winpr_aligned_free(void* memblock);
197 
198  WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
199  WINPR_API void* winpr_aligned_malloc(size_t size, size_t alignment);
200 
201  WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
202  WINPR_API void* winpr_aligned_calloc(size_t count, size_t size, size_t alignment);
203 
204  WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
205  WINPR_API void* winpr_aligned_realloc(void* memblock, size_t size, size_t alignment);
206 
207  WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
208  WINPR_API void* winpr_aligned_recalloc(void* memblock, size_t num, size_t size,
209  size_t alignment);
210 
211  WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
212  WINPR_API void* winpr_aligned_offset_malloc(size_t size, size_t alignment, size_t offset);
213 
214  WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
215  WINPR_API void* winpr_aligned_offset_realloc(void* memblock, size_t size, size_t alignment,
216  size_t offset);
217 
218  WINPR_ATTR_MALLOC(winpr_aligned_free, 1)
219  WINPR_API void* winpr_aligned_offset_recalloc(void* memblock, size_t num, size_t size,
220  size_t alignment, size_t offset);
221 
222  WINPR_API size_t winpr_aligned_msize(void* memblock, size_t alignment, size_t offset);
223 
224 #ifdef __cplusplus
225 }
226 #endif
227 
228 #else
229 #define winpr_aligned_malloc _aligned_malloc
230 #define winpr_aligned_realloc _aligned_realloc
231 #define winpr_aligned_recalloc _aligned_recalloc
232 #define winpr_aligned_offset_malloc _aligned_offset_malloc
233 #define winpr_aligned_offset_realloc _aligned_offset_realloc
234 #define winpr_aligned_offset_recalloc _aligned_offset_recalloc
235 #define winpr_aligned_msize _aligned_msize
236 #define winpr_aligned_free _aligned_free
237 #endif /* !defined(_WIN32) || (defined(__MINGW32__) ... */
238 
239 #if defined(_WIN32) && (!defined(__MINGW32__) || defined(_UCRT))
240 #define winpr_aligned_calloc(count, size, alignment) _aligned_recalloc(NULL, count, size, alignment)
241 #endif /* defined(_WIN32) && (!defined(__MINGW32__) || defined(_UCRT)) */
242 
243 WINPR_PRAGMA_DIAG_POP
244 
245 #endif /* WINPR_CRT_H */