1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3//
4
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <cstdint>
10
11#ifndef _PLATFORMDEFINES__H
12#define _PLATFORMDEFINES__H
13
14//
15// types and constants
16//
17#ifdef WINDOWS
18#include <windows.h>
19
20#define FS_SEPERATOR L"\\"
21#define PATH_DELIMITER L";"
22#define L(t) L##t
23#define W(str) L##str
24
25typedef unsigned error_t;
26typedef HANDLE THREAD_ID;
27
28#define DLL_EXPORT __declspec(dllexport)
29
30#else // !WINDOWS
31#include <pthread.h>
32
33typedef char16_t WCHAR;
34typedef unsigned int DWORD;
35typedef int BOOL;
36typedef WCHAR *LPWSTR, *PWSTR;
37typedef const WCHAR *LPCWSTR, *PCWSTR;
38
39typedef int HRESULT;
40#define LONGLONG long long
41#define ULONGLONG unsigned LONGLONG
42typedef unsigned int ULONG, *PULONG;
43#define S_OK 0x0
44#define SUCCEEDED(_hr) ((HRESULT)(_hr) >= 0)
45#define FAILED(_hr) ((HRESULT)(_hr) < 0)
46
47#ifdef ULONG_MAX
48#undef ULONG_MAX
49#endif
50#define ULONG_MAX 0xffffffffUL
51#define CCH_BSTRMAX 0x7FFFFFFF // 4 + (0x7ffffffb + 1 ) * 2 ==> 0xFFFFFFFC
52#define CB_BSTRMAX 0xFFFFFFFa // 4 + (0xfffffff6 + 2) ==> 0xFFFFFFFC
53
54#ifdef RC_INVOKED
55#define _HRESULT_TYPEDEF_(_sc) _sc
56#else // RC_INVOKED
57#define _HRESULT_TYPEDEF_(_sc) ((HRESULT)_sc)
58#endif // RC_INVOKED
59#define E_INVALIDARG _HRESULT_TYPEDEF_(0x80070057L)
60#define UInt32x32To64(a, b) ((unsigned __int64)((ULONG)(a)) * (unsigned __int64)((ULONG)(b)))
61
62#define ARRAYSIZE(x) (sizeof(x)/sizeof(*x))
63
64#ifndef TRUE
65#define TRUE 1
66#endif
67
68#ifndef FALSE
69#define FALSE 0
70#endif
71
72#ifndef WINAPI
73#define WINAPI __stdcall
74#endif
75
76#ifndef _MSC_VER
77#if __i386__
78#define __stdcall __attribute__((stdcall))
79#define _cdecl __attribute__((cdecl))
80#define __cdecl __attribute__((cdecl))
81#else
82#define __stdcall
83#define _cdecl
84#define __cdecl
85#endif
86#endif
87
88#if __GNUC__ >= 4
89#define DLL_EXPORT __attribute__ ((visibility ("default")))
90#else
91#define DLL_EXPORT
92#endif
93
94LPWSTR HackyConvertToWSTR(const char* pszInput);
95
96#define FS_SEPERATOR L("/")
97#define PATH_DELIMITER L(":")
98#define L(t) HackyConvertToWSTR(t)
99#define W(str) u##str
100#define MAX_PATH 260
101
102typedef pthread_t THREAD_ID;
103typedef void* (*MacWorker)(void*);
104typedef DWORD __stdcall (*LPTHREAD_START_ROUTINE)(void*);
105#ifdef UNICODE
106typedef WCHAR TCHAR;
107#else // ANSI
108typedef char TCHAR;
109#endif // UNICODE
110typedef char* LPSTR;
111typedef const char* LPCSTR;
112typedef TCHAR* LPTSTR;
113typedef const TCHAR* LPCTSTR;
114typedef void* FARPROC;
115typedef void* HMODULE;
116typedef void* ULONG_PTR;
117typedef int error_t;
118typedef void* LPVOID;
119typedef unsigned char BYTE;
120typedef WCHAR OLECHAR;
121#endif
122
123typedef ULONG_PTR DWORD_PTR;
124
125//
126// Method declarations
127//
128error_t TP_scpy_s(LPWSTR strDestination, size_t sizeInWords, LPCWSTR strSource);
129error_t TP_scat_s(LPWSTR strDestination, size_t sizeInWords, LPCWSTR strSource);
130size_t TP_slen(LPCWSTR str);
131int TP_scmp_s(LPCSTR str1, LPCSTR str2);
132int TP_wcmp_s(LPCWSTR str1, LPCWSTR str2);
133error_t TP_getenv_s(size_t* pReturnValue, LPWSTR buffer, size_t sizeInWords, LPCWSTR varname);
134error_t TP_putenv_s(LPTSTR name, LPTSTR value);
135void TP_ZeroMemory(LPVOID buffer, size_t sizeInBytes);
136error_t TP_itow_s(int num, LPWSTR buffer, size_t sizeInCharacters, int radix);
137error_t TP_itoa_s(int num, LPSTR buffer, size_t sizeInCharacters, int radix);
138LPWSTR TP_sstr(LPWSTR str, LPWSTR searchStr);
139LPSTR HackyConvertToSTR(LPWSTR pwszInput);
140DWORD TP_CreateThread(THREAD_ID* tThread, LPTHREAD_START_ROUTINE worker, LPVOID lpParameter);
141void TP_JoinThread(THREAD_ID tThread);
142void TP_DebugBreak();
143DWORD TP_GetFullPathName(LPWSTR fileName, DWORD nBufferLength, LPWSTR lpBuffer);
144
145typedef WCHAR* BSTR;
146BSTR TP_SysAllocStringByteLen(LPCSTR psz, size_t len);
147void TP_SysFreeString(BSTR bstr);
148size_t TP_SysStringByteLen(BSTR bstr);
149BSTR TP_SysAllocStringLen(LPCWSTR psz, size_t len);
150BSTR TP_SysAllocString(LPCWSTR psz);
151DWORD TP_SysStringLen(BSTR bstr);
152
153//
154// Method redirects
155//
156#ifdef WINDOWS
157#define TP_LoadLibrary(l) LoadLibrary(l)
158#define TP_LoadLibraryW(l) LoadLibraryW(l)
159#define TP_LoadLibraryA(l) LoadLibraryA(l)
160#define TP_GetProcAddress(m,e) GetProcAddress(m,e)
161#define TP_CoTaskMemAlloc(t) CoTaskMemAlloc(t)
162#define TP_CoTaskMemFree(t) CoTaskMemFree(t)
163#define TP_DebugBreak() DebugBreak()
164#define TP_rand rand
165#define TP_srand srand
166#else
167#define fopen_s(FILEHANDLE, FILENAME, MODE) *(FILEHANDLE) = fopen(FILENAME, MODE)
168#define _fsopen(FILENAME, MODE, ACCESS) fopen(FILENAME, MODE)
169#define GetCurrentDirectory(BUFSIZ, BUF) getcwd(BUF, BUFSIZ)
170#define DeleteFile unlink
171#define GlobalFree free
172#define sprintf_s snprintf
173#define fwscanf_s fwscanf
174#define strcat_s(DST,SIZ,SRC) strlcat(DST,SRC,SIZ)
175#define TP_LoadLibrary(l) dlopen(l, 0)
176#define TP_LoadLibraryW(l) dlopen(l, 0)
177#define TP_LoadLibraryA(l) dlopen(l, 0)
178#define TP_GetProcAddress(m,e) dlsym(m,e)
179#define TP_CoTaskMemAlloc(t) malloc(t)
180#define TP_CoTaskMemFree(t) free(t)
181#define TP_rand arc4random
182#define TP_srand srandom
183#define wcscpy_s TP_scpy_s
184#define wcscat_s TP_scat_s
185#define GetFullPathNameW(fname,buflen,buf,filepart) TP_GetFullPathName(fname,buflen,buf)
186#define wcslen TP_slen
187#define _wgetenv_s TP_getenv_s
188#define _putenv_s TP_putenv_s
189#define ZeroMemory TP_ZeroMemory
190#define _itow_s TP_itow_s
191#define _itoa_s TP_itoa_s
192#define wcsstr TP_sstr
193#define strcmp TP_scmp_s
194#define wcscmp TP_wcmp_s
195#endif
196
197#endif
198
199