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