Bitdefender Hypervisor Memory Introspection
introcrt.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Bitdefender
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef _INTROCRT_H_
6 #define _INTROCRT_H_
7 
8 #include "rbtree.h"
9 
10 //
11 // CRT stuff
12 //
13 #ifndef INT_COMPILER_MSVC
14 
15 #include <stdarg.h>
16 #include <string.h>
17 #include <strings.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <ctype.h>
21 
22 #else
23 #include <stdarg.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #endif // INT_COMPILER_MSVC
29 
30 #include "introtypes.h"
31 #include "pgtable.h"
32 
33 
34 #define strlen_s(s, n) strnlen(s, n)
35 #define memzero(a, s) memset(a, 0, s)
36 
37 #ifdef INT_COMPILER_MSVC
38 size_t
39 strnlen(
40  _In_reads_or_z_ (maxlen)const char *s,
41  _In_ size_t maxlen
42  );
43 #endif
44 
45 __nonnull() int
47  _Inout_updates_(len) WCHAR *buf,
48  _In_ size_t len
49  );
50 
51 __nonnull() int
53  _Inout_updates_(len) char *buf,
54  _In_ size_t len
55  );
56 
57 __nonnull() int
58 wstrcmp(
59  _In_z_ const WCHAR *buf1,
60  _In_z_ const WCHAR *buf2
61  );
62 
63 __nonnull() int
65  _In_z_ const WCHAR *buf1,
66  _In_z_ const WCHAR *buf2
67  );
68 
69 __nonnull() int
71  _In_reads_z_(len) const WCHAR *buf1,
72  _In_reads_z_(len) const WCHAR *buf2,
73  _In_ size_t len
74  );
75 
76 __nonnull() const WCHAR *
78  _In_z_ const WCHAR *buf1,
79  _In_z_ const WCHAR *buf2
80  );
81 
82 #ifdef INT_COMPILER_MSVC
83 
84 const CHAR *
85 strcasestr(
86  _In_z_ const CHAR *buf1,
87  _In_z_ const CHAR *buf2
88  );
89 
90 #endif
91 
92 #ifdef INT_COMPILER_MSVC
93 
94 int
95 strncasecmp(
96  _In_reads_z_(len) const char *buf1,
97  _In_reads_z_(len) const char *buf2,
98  _In_ size_t len
99  );
100 
101 int
102 strcasecmp(
103  _In_z_ const char *buf1,
104  _In_z_ const char *buf2
105  );
106 
107 #endif
108 
109 
110 __nonnull() int
112  _In_reads_z_(len) const char *buf1,
113  _In_reads_z_(len) const WCHAR *buf2,
114  _In_ size_t len
115  );
116 
117 __nonnull() char *
119  _Out_writes_z_(DestinationMaxLength) char *Destination,
120  _In_z_ const WCHAR *Source,
121  _In_ DWORD DestinationMaxLength
122  );
123 
124 char *
126  _Out_writes_z_(DestinationMaxLength) char *Destination,
127  _In_z_ const WCHAR *Source,
128  _In_ DWORD DestinationMaxLength
129  );
130 
131 __nonnull() WCHAR *
133  _Out_writes_bytes_(DestinationMaxLength) WCHAR *Destination,
134  _In_z_ const char *Source,
135  _In_ DWORD DestinationMaxLength
136  );
137 
138 __nonnull() int
140  _In_reads_z_(MaxBufferSize) const char *Buffer,
141  _In_ size_t MaxBufferSize,
142  _In_ size_t MinSize
143  );
144 
145 __nonnull() void
146 memcpy_end(
147  _Out_writes_bytes_(DestinationSize) void *Destination,
148  _In_reads_bytes_(SourceSize) const void *Source,
149  _In_ size_t DestinationSize,
150  _In_ size_t SourceSize
151  );
152 
153 long long
154 my_llabs(
155  _In_ long long value
156  );
157 
158 __nonnull() BOOLEAN
160  _In_z_ char const *Pattern,
161  _In_z_ char const *String,
162  _In_opt_ BOOLEAN IgnoreCase,
163  _In_opt_ BOOLEAN Truncated
164  );
165 
166 __nonnull() BOOLEAN
168  _In_z_ char const *Pattern,
169  _In_z_ char const *String
170  );
171 
172 __nonnull() BOOLEAN
174  _In_z_ char const *Pattern,
175  _In_z_ WCHAR const *String,
176  _In_opt_ BOOLEAN IgnoreCase,
177  _In_opt_ BOOLEAN Truncated
178  );
179 
180 __nonnull() size_t
181 wstrnlen(
182  _In_reads_or_z_(maxlen) const WCHAR *s,
183  _In_ size_t maxlen
184  );
185 
186 __nonnull() size_t
187 wstrlen(
188  _In_z_ const WCHAR *str
189  );
190 
191 __nonnull() size_t
192 strlcpy(
193  char *dst,
194  const char *src,
195  size_t dest_size
196  );
197 
198 __nonnull() size_t
199 wstrlcpy(
200  WCHAR *dst,
201  const WCHAR *src,
202  size_t dest_size
203  );
204 
205 __nonnull() size_t
206 strlcat(
207  char *dst,
208  const char *src,
209  size_t size
210  );
211 
212 int
213 nd_vsnprintf_s(char *str, size_t sizeOfBuffer, size_t count, const char *format, va_list args);
214 
215 
216 //
217 // wstrncasecmp_len is a wrapper over the original mycmp_utf16
218 // This wrapper should be used if one of the buffers could be ONLY a substring of the other.
219 //
220 __nonnull()
221 static inline int wstrncasecmp_len(const WCHAR *buf1, const WCHAR *buf2, size_t len_buf1, size_t len_buf2)
222 {
223  if (len_buf1 < len_buf2)
224  {
225  return -1;
226  }
227 
228  if (len_buf1 > len_buf2)
229  {
230  return 1;
231  }
232 
233  return wstrncasecmp(buf1, buf2, len_buf1);
234 }
235 
236 //
237 // strncasecmp_len is a wrapper over the original mycmp_utf8
238 // This wrapper should be used if one of the buffers could be ONLY a substring of the other.
239 //
240 __nonnull()
241 static inline int strncasecmp_len(const char *buf1, const char *buf2, size_t len_buf1, size_t len_buf2)
242 {
243  if (len_buf1 < len_buf2)
244  {
245  return -1;
246  }
247 
248  if (len_buf1 > len_buf2)
249  {
250  return 1;
251  }
252 
253  return strncasecmp(buf1, buf2, len_buf1);
254 }
255 
256 //
257 // strcasecmp_utf8_utf16_len is a wrapper over the original strcasecmp_utf8_utf16
258 // This wrapper should be used if one of the buffers could be ONLY a substring of the other.
259 //
260 __nonnull()
261 static inline int strcasecmp_utf8_utf16_len(const char *buf1, const WCHAR *buf2, size_t len_buf1, size_t len_buf2)
262 {
263  if (len_buf1 < len_buf2)
264  {
265  return -1;
266  }
267 
268  if (len_buf1 > len_buf2)
269  {
270  return 1;
271  }
272 
273  return strcasecmp_utf8_utf16(buf1, buf2, len_buf1);
274 }
275 
276 
277 //
278 // memcmp_len is a wrapper over the original memcmp
279 // This wrapper should be used if one of the buffers could be ONLY a substring of the other.
280 //
281 __nonnull()
282 static inline int memcmp_len(const void *buf1, const void *buf2, size_t len_buf1, size_t len_buf2)
283 {
284  if (len_buf1 < len_buf2)
285  {
286  return -1;
287  }
288 
289  if (len_buf1 > len_buf2)
290  {
291  return 1;
292  }
293 
294  return memcmp(buf1, buf2, len_buf1);
295 }
296 
297 
298 #endif // _INTROCRT_H_
int wstrncasecmp(const WCHAR *buf1, const WCHAR *buf2, size_t len)
Definition: introcrt.c:146
#define _In_opt_
Definition: intro_sal.h:16
_Bool BOOLEAN
Definition: intro_types.h:58
char * utf16toutf8(char *Destination, const WCHAR *Source, DWORD DestinationMaxLength)
Definition: introcrt.c:460
#define _In_
Definition: intro_sal.h:21
BOOLEAN glob_match_utf8(char const *Pattern, char const *String, BOOLEAN IgnoreCase, BOOLEAN Truncated)
Definition: introcrt.c:609
BOOLEAN glob_match_utf16(char const *Pattern, WCHAR const *String, BOOLEAN IgnoreCase, BOOLEAN Truncated)
Definition: introcrt.c:909
#define _Out_writes_bytes_(expr)
Definition: intro_sal.h:38
long long my_llabs(long long value)
Definition: introcrt.c:600
size_t strlcat(char *dst, const char *src, size_t size)
Definition: introcrt.c:1169
size_t wstrnlen(const WCHAR *s, size_t maxlen)
Definition: introcrt.c:1064
static int memcmp_len(const void *buf1, const void *buf2, size_t len_buf1, size_t len_buf2)
Definition: introcrt.h:282
char * utf16tolowerutf8(char *Destination, const WCHAR *Source, DWORD DestinationMaxLength)
Definition: introcrt.c:480
void memcpy_end(void *Destination, const void *Source, size_t DestinationSize, size_t SourceSize)
Definition: introcrt.c:576
int is_str_ansi(const char *Buffer, size_t MaxBufferSize, size_t MinSize)
Definition: introcrt.c:527
int nd_vsnprintf_s(char *str, size_t sizeOfBuffer, size_t count, const char *format, va_list args)
Definition: introcrt.c:1226
#define _In_reads_z_(expr)
Definition: intro_sal.h:36
size_t wstrlen(const WCHAR *str)
Definition: introcrt.c:1080
int strcasecmp_utf8_utf16(const char *buf1, const WCHAR *buf2, size_t len)
Definition: introcrt.c:413
int strlower_utf16(WCHAR *buf, size_t len)
Definition: introcrt.c:28
static int strcasecmp_utf8_utf16_len(const char *buf1, const WCHAR *buf2, size_t len_buf1, size_t len_buf2)
Definition: introcrt.h:261
#define _In_reads_or_z_(expr)
Definition: intro_sal.h:43
size_t strlcpy(char *dst, const char *src, size_t dest_size)
Definition: introcrt.c:1093
#define _Inout_updates_(expr)
Definition: intro_sal.h:32
uint16_t WCHAR
Definition: intro_types.h:63
uint32_t DWORD
Definition: intro_types.h:49
BOOLEAN glob_match_numeric_utf8(char const *Pattern, char const *String)
Definition: introcrt.c:766
#define _In_reads_bytes_(expr)
Definition: intro_sal.h:25
int wstrcmp(const WCHAR *buf1, const WCHAR *buf2)
Definition: introcrt.c:86
#define _In_z_
Definition: intro_sal.h:17
size_t wstrlcpy(WCHAR *dst, const WCHAR *src, size_t dest_size)
Definition: introcrt.c:1131
int wstrcasecmp(const WCHAR *buf1, const WCHAR *buf2)
Definition: introcrt.c:98
#define _Out_writes_z_(expr)
Definition: intro_sal.h:37
static int wstrncasecmp_len(const WCHAR *buf1, const WCHAR *buf2, size_t len_buf1, size_t len_buf2)
Definition: introcrt.h:221
static int strncasecmp_len(const char *buf1, const char *buf2, size_t len_buf1, size_t len_buf2)
Definition: introcrt.h:241
char CHAR
Definition: intro_types.h:56
WCHAR * utf8toutf16(WCHAR *Destination, const char *Source, DWORD DestinationMaxLength)
Definition: introcrt.c:507
const WCHAR * strstr_utf16(const WCHAR *buf1, const WCHAR *buf2)
Definition: introcrt.c:195
int strlower_utf8(char *buf, size_t len)
Definition: introcrt.c:57