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