win_util.c 4.3 KB

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