1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
9 // Our tests don't care about secure CRT
10 #define _CRT_SECURE_NO_WARNINGS 1
13 // Ensure that both UNICODE and _UNICODE are set.
26 // This macro is used to standardize the wide character string literals between UNIX and Windows.
27 // Unix L"" is UTF32, and on windows it's UTF16. Because of built-in assumptions on the size
28 // of string literals, it's important to match behaviour between Unix and Windows. Unix will be defined
41 #include <combaseapi.h>
44 #define snprintf _snprintf
54 #define DLL_EXPORT __declspec(dllexport)
59 #define DLL_EXPORT __attribute__ ((visibility ("default")))
66 // Calling conventions
69 #define STDMETHODCALLTYPE
72 #define __stdcall __attribute__((stdcall))
73 #define __cdecl __attribute__((cdecl))
80 inline void *CoreClrAlloc(size_t cb)
83 return ::CoTaskMemAlloc(cb);
89 inline void CoreClrFree(void *p)
92 return ::CoTaskMemFree(p);
98 inline void *CoreClrBstrAlloc(size_t cb)
101 // A null is automatically applied in the SysAllocStringByteLen API.
102 // Remove a single OLECHAR for the implied null.
103 // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/api/oleauto/nf-oleauto-sysallocstringbytelen
104 if (cb >= sizeof(OLECHAR))
105 cb -= sizeof(OLECHAR);
107 return ::SysAllocStringByteLen(nullptr, static_cast<UINT>(cb));
113 inline void CoreClrBstrFree(void *p)
116 return ::SysFreeString((BSTR)p);
120 // redirected types not-windows only
126 virtual int QueryInterface(void* riid,void** ppvObject) = 0;
127 virtual unsigned long AddRef() = 0;
128 virtual unsigned long Release() = 0;
131 // function implementation
132 size_t strncpy_s(char* strDest, size_t numberOfElements, const char *strSource, size_t count)
134 // NOTE: Need to pass count + 1 since strncpy_s does not count null,
135 // while snprintf does.
136 return snprintf(strDest, count + 1, "%s", strSource);
139 size_t strcpy_s(char *dest, size_t n, char const *src)
141 return snprintf(dest, n, "%s", src);
145 size_t wcslen(const WCHAR *str)
148 while ('\0' != *(str + len)) len++;
153 int wcsncpy_s(LPWSTR strDestination, size_t size1, LPCWSTR strSource, size_t size2)
155 // copy sizeInBytes bytes of strSource into strDestination
156 if (NULL == strDestination || NULL == strSource) return 1;
159 while (cnt < size1 && '\0' != strSource[cnt])
161 strDestination[cnt] = strSource[cnt];
165 strDestination[cnt] = '\0';
169 int wcsncpy_s(LPWSTR strDestination, size_t size1, LPCWSTR strSource)
171 return wcsncpy_s(strDestination, size1, strSource, 0);
174 int wcsncmp(LPCWSTR str1, LPCWSTR str2,size_t len)
176 // < 0 str1 less than str2
177 // 0 str1 identical to str2
178 // > 0 str1 greater than str2
179 if (NULL == str1 && NULL != str2) return -1;
180 if (NULL != str1 && NULL == str2) return 1;
181 if (NULL == str1 && NULL == str2) return 0;
183 while (*str1 == *str2 && '\0' != *str1 && '\0' != *str2 && len--!= 0)
189 if ('\0' == *str1 && '\0' == *str2) return 0;
190 if ('\0' != *str1) return -1;
191 if ('\0' != *str2) return 1;
193 return (*str1 > *str2) ? 1 : -1;
196 int wmemcmp(LPCWSTR str1, LPCWSTR str2,size_t len)
198 return wcsncmp(str1, str2, len);
203 #endif // __XPLAT_H__