Coverage Report

Created: 2026-05-11 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/include/winpr/crt.h
Line
Count
Source
1
/**
2
 * WinPR: Windows Portable Runtime
3
 * C Run-Time Library Routines
4
 *
5
 * Copyright 2012 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_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
// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
37
38
#ifndef _WIN32
39
40
#include <unistd.h>
41
42
#ifndef _write
43
0
#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
55
WINPR_ATTR_NODISCARD
56
static inline UINT32 _rotl(UINT32 value, int shift)
57
0
{
58
0
  return (value << shift) | (value >> (32 - shift));
59
0
}
Unexecuted instantiation: stream.c:_rotl
Unexecuted instantiation: debug.c:_rotl
Unexecuted instantiation: wlog.c:_rotl
Unexecuted instantiation: FileAppender.c:_rotl
Unexecuted instantiation: BinaryAppender.c:_rotl
Unexecuted instantiation: UdpAppender.c:_rotl
Unexecuted instantiation: process.c:_rotl
Unexecuted instantiation: winsock.c:_rotl
Unexecuted instantiation: unicode.c:_rotl
Unexecuted instantiation: string.c:_rotl
Unexecuted instantiation: critical.c:_rotl
Unexecuted instantiation: event.c:_rotl
Unexecuted instantiation: wait.c:_rotl
Unexecuted instantiation: generic.c:_rotl
Unexecuted instantiation: namedPipeClient.c:_rotl
Unexecuted instantiation: pattern.c:_rotl
Unexecuted instantiation: file.c:_rotl
Unexecuted instantiation: comm.c:_rotl
Unexecuted instantiation: comm_ioctl.c:_rotl
Unexecuted instantiation: comm_serial_sys.c:_rotl
Unexecuted instantiation: comm_sercx_sys.c:_rotl
Unexecuted instantiation: comm_sercx2_sys.c:_rotl
Unexecuted instantiation: pipe.c:_rotl
Unexecuted instantiation: environment.c:_rotl
Unexecuted instantiation: path.c:_rotl
Unexecuted instantiation: shell.c:_rotl
Unexecuted instantiation: winpr.c:_rotl
Unexecuted instantiation: ArrayList.c:_rotl
Unexecuted instantiation: Layout.c:_rotl
Unexecuted instantiation: Message.c:_rotl
Unexecuted instantiation: PacketMessage.c:_rotl
Unexecuted instantiation: sysinfo.c:_rotl
Unexecuted instantiation: handle.c:_rotl
Unexecuted instantiation: argv.c:_rotl
Unexecuted instantiation: thread.c:_rotl
Unexecuted instantiation: buffer.c:_rotl
Unexecuted instantiation: nt.c:_rotl
Unexecuted instantiation: ntstatus.c:_rotl
Unexecuted instantiation: comm_io.c:_rotl
Unexecuted instantiation: image.c:_rotl
60
#endif /* _rotl */
61
62
#ifndef _rotl64
63
WINPR_ATTR_NODISCARD
64
static inline UINT64 _rotl64(UINT64 value, int shift)
65
0
{
66
0
  return (value << shift) | (value >> (64 - shift));
67
0
}
Unexecuted instantiation: stream.c:_rotl64
Unexecuted instantiation: debug.c:_rotl64
Unexecuted instantiation: wlog.c:_rotl64
Unexecuted instantiation: FileAppender.c:_rotl64
Unexecuted instantiation: BinaryAppender.c:_rotl64
Unexecuted instantiation: UdpAppender.c:_rotl64
Unexecuted instantiation: process.c:_rotl64
Unexecuted instantiation: winsock.c:_rotl64
Unexecuted instantiation: unicode.c:_rotl64
Unexecuted instantiation: string.c:_rotl64
Unexecuted instantiation: critical.c:_rotl64
Unexecuted instantiation: event.c:_rotl64
Unexecuted instantiation: wait.c:_rotl64
Unexecuted instantiation: generic.c:_rotl64
Unexecuted instantiation: namedPipeClient.c:_rotl64
Unexecuted instantiation: pattern.c:_rotl64
Unexecuted instantiation: file.c:_rotl64
Unexecuted instantiation: comm.c:_rotl64
Unexecuted instantiation: comm_ioctl.c:_rotl64
Unexecuted instantiation: comm_serial_sys.c:_rotl64
Unexecuted instantiation: comm_sercx_sys.c:_rotl64
Unexecuted instantiation: comm_sercx2_sys.c:_rotl64
Unexecuted instantiation: pipe.c:_rotl64
Unexecuted instantiation: environment.c:_rotl64
Unexecuted instantiation: path.c:_rotl64
Unexecuted instantiation: shell.c:_rotl64
Unexecuted instantiation: winpr.c:_rotl64
Unexecuted instantiation: ArrayList.c:_rotl64
Unexecuted instantiation: Layout.c:_rotl64
Unexecuted instantiation: Message.c:_rotl64
Unexecuted instantiation: PacketMessage.c:_rotl64
Unexecuted instantiation: sysinfo.c:_rotl64
Unexecuted instantiation: handle.c:_rotl64
Unexecuted instantiation: argv.c:_rotl64
Unexecuted instantiation: thread.c:_rotl64
Unexecuted instantiation: buffer.c:_rotl64
Unexecuted instantiation: nt.c:_rotl64
Unexecuted instantiation: ntstatus.c:_rotl64
Unexecuted instantiation: comm_io.c:_rotl64
Unexecuted instantiation: image.c:_rotl64
68
#endif /* _rotl64 */
69
70
#ifndef _rotr
71
WINPR_ATTR_NODISCARD
72
static inline UINT32 _rotr(UINT32 value, int shift)
73
0
{
74
0
  return (value >> shift) | (value << (32 - shift));
75
0
}
Unexecuted instantiation: stream.c:_rotr
Unexecuted instantiation: debug.c:_rotr
Unexecuted instantiation: wlog.c:_rotr
Unexecuted instantiation: FileAppender.c:_rotr
Unexecuted instantiation: BinaryAppender.c:_rotr
Unexecuted instantiation: UdpAppender.c:_rotr
Unexecuted instantiation: process.c:_rotr
Unexecuted instantiation: winsock.c:_rotr
Unexecuted instantiation: unicode.c:_rotr
Unexecuted instantiation: string.c:_rotr
Unexecuted instantiation: critical.c:_rotr
Unexecuted instantiation: event.c:_rotr
Unexecuted instantiation: wait.c:_rotr
Unexecuted instantiation: generic.c:_rotr
Unexecuted instantiation: namedPipeClient.c:_rotr
Unexecuted instantiation: pattern.c:_rotr
Unexecuted instantiation: file.c:_rotr
Unexecuted instantiation: comm.c:_rotr
Unexecuted instantiation: comm_ioctl.c:_rotr
Unexecuted instantiation: comm_serial_sys.c:_rotr
Unexecuted instantiation: comm_sercx_sys.c:_rotr
Unexecuted instantiation: comm_sercx2_sys.c:_rotr
Unexecuted instantiation: pipe.c:_rotr
Unexecuted instantiation: environment.c:_rotr
Unexecuted instantiation: path.c:_rotr
Unexecuted instantiation: shell.c:_rotr
Unexecuted instantiation: winpr.c:_rotr
Unexecuted instantiation: ArrayList.c:_rotr
Unexecuted instantiation: Layout.c:_rotr
Unexecuted instantiation: Message.c:_rotr
Unexecuted instantiation: PacketMessage.c:_rotr
Unexecuted instantiation: sysinfo.c:_rotr
Unexecuted instantiation: handle.c:_rotr
Unexecuted instantiation: argv.c:_rotr
Unexecuted instantiation: thread.c:_rotr
Unexecuted instantiation: buffer.c:_rotr
Unexecuted instantiation: nt.c:_rotr
Unexecuted instantiation: ntstatus.c:_rotr
Unexecuted instantiation: comm_io.c:_rotr
Unexecuted instantiation: image.c:_rotr
76
#endif /* _rotr */
77
78
#ifndef _rotr64
79
WINPR_ATTR_NODISCARD
80
static inline UINT64 _rotr64(UINT64 value, int shift)
81
0
{
82
0
  return (value >> shift) | (value << (64 - shift));
83
0
}
Unexecuted instantiation: stream.c:_rotr64
Unexecuted instantiation: debug.c:_rotr64
Unexecuted instantiation: wlog.c:_rotr64
Unexecuted instantiation: FileAppender.c:_rotr64
Unexecuted instantiation: BinaryAppender.c:_rotr64
Unexecuted instantiation: UdpAppender.c:_rotr64
Unexecuted instantiation: process.c:_rotr64
Unexecuted instantiation: winsock.c:_rotr64
Unexecuted instantiation: unicode.c:_rotr64
Unexecuted instantiation: string.c:_rotr64
Unexecuted instantiation: critical.c:_rotr64
Unexecuted instantiation: event.c:_rotr64
Unexecuted instantiation: wait.c:_rotr64
Unexecuted instantiation: generic.c:_rotr64
Unexecuted instantiation: namedPipeClient.c:_rotr64
Unexecuted instantiation: pattern.c:_rotr64
Unexecuted instantiation: file.c:_rotr64
Unexecuted instantiation: comm.c:_rotr64
Unexecuted instantiation: comm_ioctl.c:_rotr64
Unexecuted instantiation: comm_serial_sys.c:_rotr64
Unexecuted instantiation: comm_sercx_sys.c:_rotr64
Unexecuted instantiation: comm_sercx2_sys.c:_rotr64
Unexecuted instantiation: pipe.c:_rotr64
Unexecuted instantiation: environment.c:_rotr64
Unexecuted instantiation: path.c:_rotr64
Unexecuted instantiation: shell.c:_rotr64
Unexecuted instantiation: winpr.c:_rotr64
Unexecuted instantiation: ArrayList.c:_rotr64
Unexecuted instantiation: Layout.c:_rotr64
Unexecuted instantiation: Message.c:_rotr64
Unexecuted instantiation: PacketMessage.c:_rotr64
Unexecuted instantiation: sysinfo.c:_rotr64
Unexecuted instantiation: handle.c:_rotr64
Unexecuted instantiation: argv.c:_rotr64
Unexecuted instantiation: thread.c:_rotr64
Unexecuted instantiation: buffer.c:_rotr64
Unexecuted instantiation: nt.c:_rotr64
Unexecuted instantiation: ntstatus.c:_rotr64
Unexecuted instantiation: comm_io.c:_rotr64
Unexecuted instantiation: image.c:_rotr64
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
93
WINPR_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
99
WINPR_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
115
WINPR_ATTR_NODISCARD
116
static inline UINT16 _byteswap_ushort(UINT16 _val)
117
0
{
118
0
  return WINPR_CXX_COMPAT_CAST(UINT16, ((_val) >> 8U) | ((_val) << 8U));
119
0
}
Unexecuted instantiation: stream.c:_byteswap_ushort
Unexecuted instantiation: debug.c:_byteswap_ushort
Unexecuted instantiation: wlog.c:_byteswap_ushort
Unexecuted instantiation: FileAppender.c:_byteswap_ushort
Unexecuted instantiation: BinaryAppender.c:_byteswap_ushort
Unexecuted instantiation: UdpAppender.c:_byteswap_ushort
Unexecuted instantiation: process.c:_byteswap_ushort
Unexecuted instantiation: winsock.c:_byteswap_ushort
Unexecuted instantiation: unicode.c:_byteswap_ushort
Unexecuted instantiation: string.c:_byteswap_ushort
Unexecuted instantiation: critical.c:_byteswap_ushort
Unexecuted instantiation: event.c:_byteswap_ushort
Unexecuted instantiation: wait.c:_byteswap_ushort
Unexecuted instantiation: generic.c:_byteswap_ushort
Unexecuted instantiation: namedPipeClient.c:_byteswap_ushort
Unexecuted instantiation: pattern.c:_byteswap_ushort
Unexecuted instantiation: file.c:_byteswap_ushort
Unexecuted instantiation: comm.c:_byteswap_ushort
Unexecuted instantiation: comm_ioctl.c:_byteswap_ushort
Unexecuted instantiation: comm_serial_sys.c:_byteswap_ushort
Unexecuted instantiation: comm_sercx_sys.c:_byteswap_ushort
Unexecuted instantiation: comm_sercx2_sys.c:_byteswap_ushort
Unexecuted instantiation: pipe.c:_byteswap_ushort
Unexecuted instantiation: environment.c:_byteswap_ushort
Unexecuted instantiation: path.c:_byteswap_ushort
Unexecuted instantiation: shell.c:_byteswap_ushort
Unexecuted instantiation: winpr.c:_byteswap_ushort
Unexecuted instantiation: ArrayList.c:_byteswap_ushort
Unexecuted instantiation: Layout.c:_byteswap_ushort
Unexecuted instantiation: Message.c:_byteswap_ushort
Unexecuted instantiation: PacketMessage.c:_byteswap_ushort
Unexecuted instantiation: sysinfo.c:_byteswap_ushort
Unexecuted instantiation: handle.c:_byteswap_ushort
Unexecuted instantiation: argv.c:_byteswap_ushort
Unexecuted instantiation: thread.c:_byteswap_ushort
Unexecuted instantiation: buffer.c:_byteswap_ushort
Unexecuted instantiation: nt.c:_byteswap_ushort
Unexecuted instantiation: ntstatus.c:_byteswap_ushort
Unexecuted instantiation: comm_io.c:_byteswap_ushort
Unexecuted instantiation: image.c:_byteswap_ushort
120
121
#endif /* (__GNUC__ > 4) || ... */
122
123
0
#define CopyMemory(Destination, Source, Length) memcpy((Destination), (Source), (Length))
124
0
#define MoveMemory(Destination, Source, Length) memmove((Destination), (Source), (Length))
125
#define FillMemory(Destination, Length, Fill) memset((Destination), (Fill), (Length))
126
2.34k
#define ZeroMemory(Destination, Length) memset((Destination), 0, (Length))
127
128
#ifdef __cplusplus
129
extern "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
143
WINPR_PRAGMA_DIAG_PUSH
144
WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
145
146
#ifndef _ERRNO_T_DEFINED
147
#define _ERRNO_T_DEFINED
148
typedef int errno_t;
149
#endif /* _ERRNO_T_DEFINED */
150
151
WINPR_PRAGMA_DIAG_POP
152
153
#ifndef _WIN32
154
155
#ifdef __cplusplus
156
extern "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
197
extern "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)
251
WINPR_PRAGMA_DIAG_POP
252
253
#endif /* WINPR_CRT_H */