Coverage Report

Created: 2025-07-11 06:41

/src/FreeRDP/winpr/include/winpr/crt.h
Line
Count
Source (jump to first uncovered line)
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
37
#ifndef _WIN32
38
39
#include <unistd.h>
40
41
#ifndef _write
42
0
#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
0
{
56
0
  return (value << shift) | (value >> (32 - shift));
57
0
}
Unexecuted instantiation: TestFuzzCryptoCertificateDataSetPEM.c:_rotl
Unexecuted instantiation: certificate_data.c:_rotl
Unexecuted instantiation: certificate.c:_rotl
Unexecuted instantiation: crypto.c:_rotl
Unexecuted instantiation: ber.c:_rotl
Unexecuted instantiation: x509_utils.c:_rotl
Unexecuted instantiation: cert_common.c:_rotl
Unexecuted instantiation: privatekey.c:_rotl
Unexecuted instantiation: file.c:_rotl
Unexecuted instantiation: hash.c:_rotl
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: nt.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: 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: 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: comm_io.c:_rotl
Unexecuted instantiation: image.c:_rotl
58
#endif /* _rotl */
59
60
#ifndef _rotl64
61
static INLINE UINT64 _rotl64(UINT64 value, int shift)
62
0
{
63
0
  return (value << shift) | (value >> (64 - shift));
64
0
}
Unexecuted instantiation: TestFuzzCryptoCertificateDataSetPEM.c:_rotl64
Unexecuted instantiation: certificate_data.c:_rotl64
Unexecuted instantiation: certificate.c:_rotl64
Unexecuted instantiation: crypto.c:_rotl64
Unexecuted instantiation: ber.c:_rotl64
Unexecuted instantiation: x509_utils.c:_rotl64
Unexecuted instantiation: cert_common.c:_rotl64
Unexecuted instantiation: privatekey.c:_rotl64
Unexecuted instantiation: file.c:_rotl64
Unexecuted instantiation: hash.c:_rotl64
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: nt.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: 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: 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: comm_io.c:_rotl64
Unexecuted instantiation: image.c:_rotl64
65
#endif /* _rotl64 */
66
67
#ifndef _rotr
68
static INLINE UINT32 _rotr(UINT32 value, int shift)
69
0
{
70
0
  return (value >> shift) | (value << (32 - shift));
71
0
}
Unexecuted instantiation: TestFuzzCryptoCertificateDataSetPEM.c:_rotr
Unexecuted instantiation: certificate_data.c:_rotr
Unexecuted instantiation: certificate.c:_rotr
Unexecuted instantiation: crypto.c:_rotr
Unexecuted instantiation: ber.c:_rotr
Unexecuted instantiation: x509_utils.c:_rotr
Unexecuted instantiation: cert_common.c:_rotr
Unexecuted instantiation: privatekey.c:_rotr
Unexecuted instantiation: file.c:_rotr
Unexecuted instantiation: hash.c:_rotr
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: nt.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: 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: 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: comm_io.c:_rotr
Unexecuted instantiation: image.c:_rotr
72
#endif /* _rotr */
73
74
#ifndef _rotr64
75
static INLINE UINT64 _rotr64(UINT64 value, int shift)
76
0
{
77
0
  return (value >> shift) | (value << (64 - shift));
78
0
}
Unexecuted instantiation: TestFuzzCryptoCertificateDataSetPEM.c:_rotr64
Unexecuted instantiation: certificate_data.c:_rotr64
Unexecuted instantiation: certificate.c:_rotr64
Unexecuted instantiation: crypto.c:_rotr64
Unexecuted instantiation: ber.c:_rotr64
Unexecuted instantiation: x509_utils.c:_rotr64
Unexecuted instantiation: cert_common.c:_rotr64
Unexecuted instantiation: privatekey.c:_rotr64
Unexecuted instantiation: file.c:_rotr64
Unexecuted instantiation: hash.c:_rotr64
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: nt.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: 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: 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: comm_io.c:_rotr64
Unexecuted instantiation: image.c:_rotr64
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
0
{
112
0
  return WINPR_CXX_COMPAT_CAST(UINT16, ((_val) >> 8U) | ((_val) << 8U));
113
0
}
Unexecuted instantiation: TestFuzzCryptoCertificateDataSetPEM.c:_byteswap_ushort
Unexecuted instantiation: certificate_data.c:_byteswap_ushort
Unexecuted instantiation: certificate.c:_byteswap_ushort
Unexecuted instantiation: crypto.c:_byteswap_ushort
Unexecuted instantiation: ber.c:_byteswap_ushort
Unexecuted instantiation: x509_utils.c:_byteswap_ushort
Unexecuted instantiation: cert_common.c:_byteswap_ushort
Unexecuted instantiation: privatekey.c:_byteswap_ushort
Unexecuted instantiation: file.c:_byteswap_ushort
Unexecuted instantiation: hash.c:_byteswap_ushort
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: nt.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: 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: 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: comm_io.c:_byteswap_ushort
Unexecuted instantiation: image.c:_byteswap_ushort
114
115
#endif /* (__GNUC__ > 4) || ... */
116
117
0
#define CopyMemory(Destination, Source, Length) memcpy((Destination), (Source), (Length))
118
0
#define MoveMemory(Destination, Source, Length) memmove((Destination), (Source), (Length))
119
#define FillMemory(Destination, Length, Fill) memset((Destination), (Fill), (Length))
120
932
#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 */