winrt.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Snippets extracted from https://github.com/Microsoft/openssl/blob/ec7e430e06e4e3ac87c183dee33cb216814cf980/ms/winrt.cpp
  2. * Adapted for Godot definitions
  3. */
  4. /* winrt.cpp
  5. * Copyright 2014 Microsoft Corporation
  6. * C++/CX Entropy/shims for Windows Phone/Windows Store platform
  7. * written by Alejandro Jimenez Martinez
  8. * ([email protected]) for the OpenSSL project 2014.
  9. */
  10. #include <windows.h>
  11. #if defined(WINAPI_FAMILY)
  12. extern "C" {
  13. unsigned entropyRT(BYTE *buffer, unsigned len);
  14. void RAND_add(const void *buf, int num, double entropy);
  15. int RAND_poll(void);
  16. }
  17. #endif
  18. unsigned entropyRT(BYTE *buffer, unsigned len) {
  19. using namespace Platform;
  20. using namespace Windows::Foundation;
  21. using namespace Windows::Foundation::Collections;
  22. using namespace Windows::Security::Cryptography;
  23. using namespace Windows::Storage::Streams;
  24. IBuffer ^ buf = CryptographicBuffer::GenerateRandom(len);
  25. Array<unsigned char> ^ arr;
  26. CryptographicBuffer::CopyToByteArray(buf, &arr);
  27. unsigned arrayLen = arr->Length;
  28. // Make sure not to overflow the copy
  29. arrayLen = (arrayLen > len) ? len : arrayLen;
  30. memcpy(buffer, arr->Data, arrayLen);
  31. return arrayLen;
  32. }
  33. int RAND_poll(void) {
  34. BYTE buf[60];
  35. unsigned collected = entropyRT(buf, sizeof(buf));
  36. RAND_add(buf, collected, collected);
  37. return 1;
  38. }
  39. #if defined(WINRT_ENABLED)
  40. extern "C" {
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. void *GetModuleHandle(
  45. _In_opt_ LPCTSTR lpModuleName) {
  46. return NULL;
  47. }
  48. //no log for phone
  49. int RegisterEventSource(
  50. _In_ LPCTSTR lpUNCServerName,
  51. _In_ LPCTSTR lpSourceName) {
  52. return NULL;
  53. }
  54. int ReportEvent(
  55. _In_ HANDLE hEventLog,
  56. _In_ WORD wType,
  57. _In_ WORD wCategory,
  58. _In_ DWORD dwEventID,
  59. _In_ PSID lpUserSid,
  60. _In_ WORD wNumStrings,
  61. _In_ DWORD dwDataSize,
  62. _In_ LPCTSTR *lpStrings,
  63. _In_ LPVOID lpRawData) {
  64. return 0;
  65. }
  66. int MessageBox(
  67. _In_opt_ HWND hWnd,
  68. _In_opt_ LPCTSTR lpText,
  69. _In_opt_ LPCTSTR lpCaption,
  70. _In_ UINT uType) {
  71. return 0;
  72. }
  73. int __cdecl GetProcessWindowStation(void) {
  74. return NULL;
  75. }
  76. BOOL __cdecl GetUserObjectInformationW(
  77. _In_ HANDLE hObj,
  78. _In_ int nIndex,
  79. _Out_opt_ PVOID pvInfo,
  80. _In_ DWORD nLength,
  81. _Out_opt_ LPDWORD lpnLengthNeeded) {
  82. return 0;
  83. }
  84. #ifndef STD_ERROR_HANDLE
  85. HANDLE __cdecl GetStdHandle(
  86. _In_ DWORD nStdHandle) {
  87. return 0;
  88. }
  89. #endif
  90. BOOL DeregisterEventSource(
  91. _Inout_ HANDLE hEventLog) {
  92. return 0;
  93. }
  94. #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_WIN10_RS4)
  95. char *getenv(
  96. const char *varname) {
  97. //hardcoded environmental variables used for the appx testing application for store/phone
  98. if (!strcmp(varname, "OPENSSL_CONF")) {
  99. return "./openssl.cnf";
  100. }
  101. return 0;
  102. }
  103. int setenv(const char *envname, const char *envval, int overwrite) {
  104. return -1;
  105. }
  106. #endif
  107. int _getch(void) {
  108. return 0;
  109. }
  110. int _kbhit() {
  111. return 0;
  112. }
  113. #if !defined(NTDDI_VERSION) || (NTDDI_VERSION < NTDDI_WIN10_RS4)
  114. BOOL __cdecl FlushConsoleInputBuffer(
  115. _In_ HANDLE hConsoleInput) {
  116. return 0;
  117. }
  118. #endif
  119. int winrt_GetTickCount(void) {
  120. LARGE_INTEGER t;
  121. return (int)(QueryPerformanceCounter(&t) ? t.QuadPart : 0);
  122. }
  123. void *OPENSSL_UplinkTable[26] = { 0 };
  124. } //extern C
  125. #endif /*defined(WINRT_ENABLED)*/