1 //===- WindowsSupport.h - Common Windows Include File -----------*- 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 things specific to Windows implementations.  In addition to
11 // providing some helpers for working with win32 APIs, this header wraps
12 // <windows.h> with some portability macros.  Always include WindowsSupport.h
13 // instead of including <windows.h> directly.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only generic Win32 code that
19 //===          is guaranteed to work on *all* Win32 variants.
20 //===----------------------------------------------------------------------===//
21 
22 #ifndef LLVM_SUPPORT_WINDOWSSUPPORT_H
23 #define LLVM_SUPPORT_WINDOWSSUPPORT_H
24 
25 // mingw-w64 tends to define it as 0x0502 in its headers.
26 #undef _WIN32_WINNT
27 #undef _WIN32_IE
28 
29 // Require at least Windows 7 API.
30 #define _WIN32_WINNT 0x0601
31 #define _WIN32_IE    0x0800 // MinGW at it again. FIXME: verify if still needed.
32 #define WIN32_LEAN_AND_MEAN
33 #ifndef NOMINMAX
34 #define NOMINMAX
35 #endif
36 
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/ADT/Twine.h"
41 #include "llvm/Config/config.h" // Get build system configuration settings
42 #include "llvm/Support/Chrono.h"
43 #include "llvm/Support/Compiler.h"
44 #include <cassert>
45 #include <string>
46 #include <system_error>
47 #include <windows.h>
48 
49 // Must be included after windows.h
50 #include <wincrypt.h>
51 
52 /// Determines if the program is running on Windows 8 or newer. This
53 /// reimplements one of the helpers in the Windows 8.1 SDK, which are intended
54 /// to supercede raw calls to GetVersionEx. Old SDKs, Cygwin, and MinGW don't
55 /// yet have VersionHelpers.h, so we have our own helper.
56 inline bool RunningWindows8OrGreater() {
57   // Windows 8 is version 6.2, service pack 0.
58   OSVERSIONINFOEXW osvi = {};
59   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
60   osvi.dwMajorVersion = 6;
61   osvi.dwMinorVersion = 2;
62   osvi.wServicePackMajor = 0;
63 
64   DWORDLONG Mask = 0;
65   Mask = VerSetConditionMask(Mask, VER_MAJORVERSION, VER_GREATER_EQUAL);
66   Mask = VerSetConditionMask(Mask, VER_MINORVERSION, VER_GREATER_EQUAL);
67   Mask = VerSetConditionMask(Mask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
68 
69   return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION |
70                                        VER_SERVICEPACKMAJOR,
71                             Mask) != FALSE;
72 }
73 
74 inline bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix) {
75   if (!ErrMsg)
76     return true;
77   char *buffer = NULL;
78   DWORD LastError = GetLastError();
79   DWORD R = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
80                                FORMAT_MESSAGE_FROM_SYSTEM |
81                                FORMAT_MESSAGE_MAX_WIDTH_MASK,
82                            NULL, LastError, 0, (LPSTR)&buffer, 1, NULL);
83   if (R)
84     *ErrMsg = prefix + ": " + buffer;
85   else
86     *ErrMsg = prefix + ": Unknown error";
87   *ErrMsg += " (0x" + llvm::utohexstr(LastError) + ")";
88 
89   LocalFree(buffer);
90   return R != 0;
91 }
92 
93 template <typename HandleTraits>
94 class ScopedHandle {
95   typedef typename HandleTraits::handle_type handle_type;
96   handle_type Handle;
97 
98   ScopedHandle(const ScopedHandle &other); // = delete;
99   void operator=(const ScopedHandle &other); // = delete;
100 public:
101   ScopedHandle()
102     : Handle(HandleTraits::GetInvalid()) {}
103 
104   explicit ScopedHandle(handle_type h)
105     : Handle(h) {}
106 
107   ~ScopedHandle() {
108     if (HandleTraits::IsValid(Handle))
109       HandleTraits::Close(Handle);
110   }
111 
112   handle_type take() {
113     handle_type t = Handle;
114     Handle = HandleTraits::GetInvalid();
115     return t;
116   }
117 
118   ScopedHandle &operator=(handle_type h) {
119     if (HandleTraits::IsValid(Handle))
120       HandleTraits::Close(Handle);
121     Handle = h;
122     return *this;
123   }
124 
125   // True if Handle is valid.
126   explicit operator bool() const {
127     return HandleTraits::IsValid(Handle) ? true : false;
128   }
129 
130   operator handle_type() const {
131     return Handle;
132   }
133 };
134 
135 struct CommonHandleTraits {
136   typedef HANDLE handle_type;
137 
138   static handle_type GetInvalid() {
139     return INVALID_HANDLE_VALUE;
140   }
141 
142   static void Close(handle_type h) {
143     ::CloseHandle(h);
144   }
145 
146   static bool IsValid(handle_type h) {
147     return h != GetInvalid();
148   }
149 };
150 
151 struct JobHandleTraits : CommonHandleTraits {
152   static handle_type GetInvalid() {
153     return NULL;
154   }
155 };
156 
157 struct CryptContextTraits : CommonHandleTraits {
158   typedef HCRYPTPROV handle_type;
159 
160   static handle_type GetInvalid() {
161     return 0;
162   }
163 
164   static void Close(handle_type h) {
165     ::CryptReleaseContext(h, 0);
166   }
167 
168   static bool IsValid(handle_type h) {
169     return h != GetInvalid();
170   }
171 };
172 
173 struct RegTraits : CommonHandleTraits {
174   typedef HKEY handle_type;
175 
176   static handle_type GetInvalid() {
177     return NULL;
178   }
179 
180   static void Close(handle_type h) {
181     ::RegCloseKey(h);
182   }
183 
184   static bool IsValid(handle_type h) {
185     return h != GetInvalid();
186   }
187 };
188 
189 struct FindHandleTraits : CommonHandleTraits {
190   static void Close(handle_type h) {
191     ::FindClose(h);
192   }
193 };
194 
195 struct FileHandleTraits : CommonHandleTraits {};
196 
197 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
198 typedef ScopedHandle<FileHandleTraits>   ScopedFileHandle;
199 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
200 typedef ScopedHandle<RegTraits>          ScopedRegHandle;
201 typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
202 typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
203 
204 namespace llvm {
205 template <class T>
206 class SmallVectorImpl;
207 
208 template <class T>
209 typename SmallVectorImpl<T>::const_pointer
210 c_str(SmallVectorImpl<T> &str) {
211   str.push_back(0);
212   str.pop_back();
213   return str.data();
214 }
215 
216 namespace sys {
217 
218 inline std::chrono::nanoseconds toDuration(FILETIME Time) {
219   ULARGE_INTEGER TimeInteger;
220   TimeInteger.LowPart = Time.dwLowDateTime;
221   TimeInteger.HighPart = Time.dwHighDateTime;
222 
223   // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
224   return std::chrono::nanoseconds(100 * TimeInteger.QuadPart);
225 }
226 
227 inline TimePoint<> toTimePoint(FILETIME Time) {
228   ULARGE_INTEGER TimeInteger;
229   TimeInteger.LowPart = Time.dwLowDateTime;
230   TimeInteger.HighPart = Time.dwHighDateTime;
231 
232   // Adjust for different epoch
233   TimeInteger.QuadPart -= 11644473600ll * 10000000;
234 
235   // FILETIME's are # of 100 nanosecond ticks (1/10th of a microsecond)
236   return TimePoint<>(std::chrono::nanoseconds(100 * TimeInteger.QuadPart));
237 }
238 
239 inline FILETIME toFILETIME(TimePoint<> TP) {
240   ULARGE_INTEGER TimeInteger;
241   TimeInteger.QuadPart = TP.time_since_epoch().count() / 100;
242   TimeInteger.QuadPart += 11644473600ll * 10000000;
243 
244   FILETIME Time;
245   Time.dwLowDateTime = TimeInteger.LowPart;
246   Time.dwHighDateTime = TimeInteger.HighPart;
247   return Time;
248 }
249 
250 namespace path {
251 std::error_code widenPath(const Twine &Path8,
252                           SmallVectorImpl<wchar_t> &Path16);
253 } // end namespace path
254 
255 namespace windows {
256 std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl<wchar_t> &utf16);
257 std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
258                             SmallVectorImpl<char> &utf8);
259 /// Convert from UTF16 to the current code page used in the system
260 std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
261                              SmallVectorImpl<char> &utf8);
262 } // end namespace windows
263 } // end namespace sys
264 } // end namespace llvm.
265 
266 #endif
267