win_util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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