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