WinFunctions.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //===-- WinFunctions.cpp - Windows Functions for other platforms --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines Windows-specific functions used in the codebase for
  11. // non-Windows platforms.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef _WIN32
  15. #include <fcntl.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include "dxc/Support/WinFunctions.h"
  19. HRESULT StringCchPrintfA(char *dst, size_t dstSize, const char *format, ...) {
  20. va_list args;
  21. va_start(args, format);
  22. // C++11 snprintf can return the size of the resulting string if it was to be
  23. // constructed.
  24. size_t size = snprintf(nullptr, 0, format, args) + 1; // Extra space for '\0'
  25. if (size > dstSize) {
  26. *dst = '\0';
  27. } else {
  28. snprintf(dst, size, format, args);
  29. }
  30. va_end(args);
  31. return S_OK;
  32. }
  33. HRESULT UIntAdd(UINT uAugend, UINT uAddend, UINT *puResult) {
  34. HRESULT hr;
  35. if ((uAugend + uAddend) >= uAugend) {
  36. *puResult = (uAugend + uAddend);
  37. hr = S_OK;
  38. } else {
  39. *puResult = 0xffffffff;
  40. hr = ERROR_ARITHMETIC_OVERFLOW;
  41. }
  42. return hr;
  43. }
  44. HRESULT IntToUInt(int in, UINT *out) {
  45. HRESULT hr;
  46. if (in >= 0) {
  47. *out = (UINT)in;
  48. hr = S_OK;
  49. } else {
  50. *out = 0xffffffff;
  51. hr = ERROR_ARITHMETIC_OVERFLOW;
  52. }
  53. return hr;
  54. }
  55. HRESULT SizeTToInt(size_t in, int *out) {
  56. HRESULT hr;
  57. if(in <= INT_MAX) {
  58. *out = (int)in;
  59. hr = S_OK;
  60. }
  61. else {
  62. *out = 0xffffffff;
  63. hr = ERROR_ARITHMETIC_OVERFLOW;
  64. }
  65. return hr;
  66. }
  67. HRESULT UInt32Mult(UINT a, UINT b, UINT *out) {
  68. uint64_t result = (uint64_t)a * (uint64_t)b;
  69. if (result > uint64_t(UINT_MAX))
  70. return ERROR_ARITHMETIC_OVERFLOW;
  71. *out = (uint32_t)result;
  72. return S_OK;
  73. }
  74. int _stricmp(const char *str1, const char *str2) {
  75. size_t i = 0;
  76. for (; str1[i] && str2[i]; ++i) {
  77. int d = std::tolower(str1[i]) - std::tolower(str2[i]);
  78. if (d != 0)
  79. return d;
  80. }
  81. return str1[i] - str2[i];
  82. }
  83. HRESULT CoGetMalloc(DWORD dwMemContext, IMalloc **ppMalloc) {
  84. *ppMalloc = new IMalloc;
  85. (*ppMalloc)->AddRef();
  86. return S_OK;
  87. }
  88. HANDLE CreateFile2(_In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess,
  89. _In_ DWORD dwShareMode, _In_ DWORD dwCreationDisposition,
  90. _In_opt_ void *pCreateExParams) {
  91. return CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, pCreateExParams,
  92. dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, nullptr);
  93. }
  94. HANDLE CreateFileW(_In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess,
  95. _In_ DWORD dwShareMode, _In_opt_ void *lpSecurityAttributes,
  96. _In_ DWORD dwCreationDisposition,
  97. _In_ DWORD dwFlagsAndAttributes,
  98. _In_opt_ HANDLE hTemplateFile) {
  99. CW2A pUtf8FileName(lpFileName);
  100. size_t fd = -1;
  101. int flags = 0;
  102. if (dwDesiredAccess & GENERIC_WRITE)
  103. if (dwDesiredAccess & GENERIC_READ)
  104. flags |= O_RDWR;
  105. else
  106. flags |= O_WRONLY;
  107. else // dwDesiredAccess may be 0, but open() demands something here. This is mostly harmless
  108. flags |= O_RDONLY;
  109. if (dwCreationDisposition == CREATE_ALWAYS)
  110. flags |= (O_CREAT | O_TRUNC);
  111. if (dwCreationDisposition == OPEN_ALWAYS)
  112. flags |= O_CREAT;
  113. else if (dwCreationDisposition == CREATE_NEW)
  114. flags |= (O_CREAT | O_EXCL);
  115. else if (dwCreationDisposition == TRUNCATE_EXISTING)
  116. flags |= O_TRUNC;
  117. // OPEN_EXISTING represents default open() behavior
  118. // Catch Implementation limitations.
  119. assert(!lpSecurityAttributes && "security attributes not supported in CreateFileW yet");
  120. assert(!hTemplateFile && "template file not supported in CreateFileW yet");
  121. assert(dwFlagsAndAttributes == FILE_ATTRIBUTE_NORMAL &&
  122. "Attributes other than NORMAL not supported in CreateFileW yet");
  123. fd = open(pUtf8FileName, flags);
  124. return (HANDLE)fd;
  125. }
  126. BOOL GetFileSizeEx(_In_ HANDLE hFile, _Out_ PLARGE_INTEGER lpFileSize) {
  127. int fd = (size_t)hFile;
  128. struct stat fdstat;
  129. int rv = fstat(fd, &fdstat);
  130. if (!rv) {
  131. lpFileSize->QuadPart = (LONGLONG)fdstat.st_size;
  132. return true;
  133. }
  134. return false;
  135. }
  136. BOOL ReadFile(_In_ HANDLE hFile, _Out_ LPVOID lpBuffer,
  137. _In_ DWORD nNumberOfBytesToRead,
  138. _Out_opt_ LPDWORD lpNumberOfBytesRead,
  139. _Inout_opt_ void *lpOverlapped) {
  140. size_t fd = (size_t)hFile;
  141. ssize_t rv = -1;
  142. // Implementation limitation
  143. assert(!lpOverlapped && "Overlapping not supported in ReadFile yet.");
  144. rv = read(fd, lpBuffer, nNumberOfBytesToRead);
  145. if (rv < 0)
  146. return false;
  147. *lpNumberOfBytesRead = rv;
  148. return true;
  149. }
  150. BOOL WriteFile(_In_ HANDLE hFile, _In_ LPCVOID lpBuffer,
  151. _In_ DWORD nNumberOfBytesToWrite,
  152. _Out_opt_ LPDWORD lpNumberOfBytesWritten,
  153. _Inout_opt_ void *lpOverlapped) {
  154. size_t fd = (size_t)hFile;
  155. ssize_t rv = -1;
  156. // Implementation limitation
  157. assert(!lpOverlapped && "Overlapping not supported in WriteFile yet.");
  158. rv = write(fd, lpBuffer, nNumberOfBytesToWrite);
  159. if (rv < 0)
  160. return false;
  161. *lpNumberOfBytesWritten = rv;
  162. return true;
  163. }
  164. BOOL CloseHandle(_In_ HANDLE hObject) {
  165. int fd = (size_t)hObject;
  166. return !close(fd);
  167. }
  168. #endif // _WIN32