win_util.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2023 Amazon Inc. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "platform_common.h"
  6. #include "win_util.h"
  7. // From 1601-01-01 to 1970-01-01 there are 134774 days.
  8. static const uint64_t NT_to_UNIX_epoch_in_ns =
  9. 134774ull * 86400ull * 1000ull * 1000ull * 1000ull;
  10. __wasi_timestamp_t
  11. convert_filetime_to_wasi_timestamp(LPFILETIME filetime)
  12. {
  13. ULARGE_INTEGER temp = { .HighPart = filetime->dwHighDateTime,
  14. .LowPart = filetime->dwLowDateTime };
  15. // WASI timestamps are measured in nanoseconds whereas FILETIME structs are
  16. // represented in terms 100-nanosecond intervals.
  17. return (temp.QuadPart * 100ull) - NT_to_UNIX_epoch_in_ns;
  18. }
  19. FILETIME
  20. convert_wasi_timestamp_to_filetime(__wasi_timestamp_t timestamp)
  21. {
  22. ULARGE_INTEGER temp = { .QuadPart =
  23. (timestamp + NT_to_UNIX_epoch_in_ns) / 100ull };
  24. FILETIME ret = { .dwLowDateTime = temp.LowPart,
  25. .dwHighDateTime = temp.HighPart };
  26. return ret;
  27. }
  28. __wasi_errno_t
  29. convert_windows_error_code(DWORD windows_error_code)
  30. {
  31. switch (windows_error_code) {
  32. case ERROR_INVALID_PARAMETER:
  33. case ERROR_INVALID_HANDLE:
  34. case ERROR_NEGATIVE_SEEK:
  35. return __WASI_EINVAL;
  36. case ERROR_SHARING_VIOLATION:
  37. case ERROR_PIPE_BUSY:
  38. return __WASI_EBUSY;
  39. case ERROR_ACCESS_DENIED:
  40. return __WASI_EACCES;
  41. case ERROR_ALREADY_EXISTS:
  42. case ERROR_FILE_EXISTS:
  43. return __WASI_EEXIST;
  44. case ERROR_NO_MORE_FILES:
  45. case ERROR_FILE_NOT_FOUND:
  46. case ERROR_INVALID_NAME:
  47. return __WASI_ENOENT;
  48. case ERROR_PRIVILEGE_NOT_HELD:
  49. return __WASI_EPERM;
  50. case ERROR_NOT_ENOUGH_MEMORY:
  51. return __WASI_ENOMEM;
  52. case ERROR_NOACCESS:
  53. return __WASI_EFAULT;
  54. case ERROR_DIR_NOT_EMPTY:
  55. return __WASI_ENOTEMPTY;
  56. case ERROR_DIRECTORY:
  57. return __WASI_ENOTDIR;
  58. case ERROR_IO_PENDING:
  59. case ERROR_INSUFFICIENT_BUFFER:
  60. case ERROR_INVALID_FLAGS:
  61. case ERROR_NO_UNICODE_TRANSLATION:
  62. default:
  63. return __WASI_EINVAL;
  64. }
  65. }
  66. #ifdef UWP_DEFAULT_VPRINTF
  67. int
  68. uwp_print_to_debugger(const char *format, va_list ap)
  69. {
  70. // Provide a stack buffer which should be large enough for any realistic
  71. // string so we avoid making an allocation on every printf call.
  72. char stack_buf[2048];
  73. char *buf = stack_buf;
  74. int ret = vsnprintf(stack_buf, sizeof(stack_buf), format, ap);
  75. if ((size_t)ret >= sizeof(stack_buf)) {
  76. // Allocate an extra byte for the null terminator.
  77. char *heap_buf = BH_MALLOC((unsigned int)(ret) + 1);
  78. buf = heap_buf;
  79. if (heap_buf == NULL) {
  80. // Output as much as we can to the debugger if allocating a buffer
  81. // fails.
  82. OutputDebugStringA(stack_buf);
  83. return ret;
  84. }
  85. ret = vsnprintf(heap_buf, (size_t)ret + 1, format, ap);
  86. }
  87. if (ret >= 0)
  88. OutputDebugStringA(buf);
  89. if (buf != stack_buf)
  90. BH_FREE(buf);
  91. return ret;
  92. }
  93. #endif
  94. __wasi_errno_t
  95. convert_winsock_error_code(int error_code)
  96. {
  97. switch (error_code) {
  98. case WSASYSNOTREADY:
  99. case WSAEWOULDBLOCK:
  100. return __WASI_EAGAIN;
  101. case WSAVERNOTSUPPORTED:
  102. return __WASI_ENOTSUP;
  103. case WSAEINPROGRESS:
  104. return __WASI_EINPROGRESS;
  105. case WSAEPROCLIM:
  106. return __WASI_EBUSY;
  107. case WSAEFAULT:
  108. return __WASI_EFAULT;
  109. case WSAENETDOWN:
  110. return __WASI_ENETDOWN;
  111. case WSAENOTSOCK:
  112. return __WASI_ENOTSOCK;
  113. case WSAEINTR:
  114. return __WASI_EINTR;
  115. case WSAEAFNOSUPPORT:
  116. return __WASI_EAFNOSUPPORT;
  117. case WSAEMFILE:
  118. return __WASI_ENFILE;
  119. case WSAEINVAL:
  120. return __WASI_EINVAL;
  121. case WSAENOBUFS:
  122. return __WASI_ENOBUFS;
  123. case WSAEPROTONOSUPPORT:
  124. return __WASI_EPROTONOSUPPORT;
  125. case WSAEPROTOTYPE:
  126. return __WASI_EPROTOTYPE;
  127. case WSAESOCKTNOSUPPORT:
  128. return __WASI_ENOTSUP;
  129. case WSAECONNABORTED:
  130. return __WASI_ECONNABORTED;
  131. case WSAECONNRESET:
  132. return __WASI_ECONNRESET;
  133. case WSAENOTCONN:
  134. return __WASI_ENOTCONN;
  135. case WSAEINVALIDPROCTABLE:
  136. case WSAEINVALIDPROVIDER:
  137. case WSAEPROVIDERFAILEDINIT:
  138. case WSANOTINITIALISED:
  139. default:
  140. return __WASI_EINVAL;
  141. }
  142. }