1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_BASE_WIN32_H_
12 #define WEBRTC_BASE_WIN32_H_
13 
14 #if defined(WEBRTC_WIN)
15 
16 #ifndef WIN32_LEAN_AND_MEAN
17 #define WIN32_LEAN_AND_MEAN
18 #endif
19 
20 // Make sure we don't get min/max macros
21 #ifndef NOMINMAX
22 #define NOMINMAX
23 #endif
24 
25 #include <winsock2.h>
26 #include <windows.h>
27 
28 #ifndef SECURITY_MANDATORY_LABEL_AUTHORITY
29 // Add defines that we use if we are compiling against older sdks
30 #define SECURITY_MANDATORY_MEDIUM_RID               (0x00002000L)
31 #define TokenIntegrityLevel static_cast<TOKEN_INFORMATION_CLASS>(0x19)
32 typedef struct _TOKEN_MANDATORY_LABEL {
33     SID_AND_ATTRIBUTES Label;
34 } TOKEN_MANDATORY_LABEL, *PTOKEN_MANDATORY_LABEL;
35 #endif  // SECURITY_MANDATORY_LABEL_AUTHORITY
36 
37 #undef SetPort
38 
39 #include <string>
40 
41 #include "webrtc/base/stringutils.h"
42 #include "webrtc/base/basictypes.h"
43 
44 namespace rtc {
45 
46 const char* win32_inet_ntop(int af, const void *src, char* dst, socklen_t size);
47 int win32_inet_pton(int af, const char* src, void *dst);
48 
ToUtf16(const char * utf8,size_t len)49 inline std::wstring ToUtf16(const char* utf8, size_t len) {
50   int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
51                                     NULL, 0);
52   wchar_t* ws = STACK_ARRAY(wchar_t, len16);
53   ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16);
54   return std::wstring(ws, len16);
55 }
56 
ToUtf16(const std::string & str)57 inline std::wstring ToUtf16(const std::string& str) {
58   return ToUtf16(str.data(), str.length());
59 }
60 
ToUtf8(const wchar_t * wide,size_t len)61 inline std::string ToUtf8(const wchar_t* wide, size_t len) {
62   int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
63                                    NULL, 0, NULL, NULL);
64   char* ns = STACK_ARRAY(char, len8);
65   ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8,
66                         NULL, NULL);
67   return std::string(ns, len8);
68 }
69 
ToUtf8(const wchar_t * wide)70 inline std::string ToUtf8(const wchar_t* wide) {
71   return ToUtf8(wide, wcslen(wide));
72 }
73 
ToUtf8(const std::wstring & wstr)74 inline std::string ToUtf8(const std::wstring& wstr) {
75   return ToUtf8(wstr.data(), wstr.length());
76 }
77 
78 // Convert FILETIME to time_t
79 void FileTimeToUnixTime(const FILETIME& ft, time_t* ut);
80 
81 // Convert time_t to FILETIME
82 void UnixTimeToFileTime(const time_t& ut, FILETIME * ft);
83 
84 // Convert a Utf8 path representation to a non-length-limited Unicode pathname.
85 bool Utf8ToWindowsFilename(const std::string& utf8, std::wstring* filename);
86 
87 // Convert a FILETIME to a UInt64
ToUInt64(const FILETIME & ft)88 inline uint64_t ToUInt64(const FILETIME& ft) {
89   ULARGE_INTEGER r = {{ft.dwLowDateTime, ft.dwHighDateTime}};
90   return r.QuadPart;
91 }
92 
93 enum WindowsMajorVersions {
94   kWindows2000 = 5,
95   kWindowsVista = 6,
96 };
97 bool GetOsVersion(int* major, int* minor, int* build);
98 
IsWindowsVistaOrLater()99 inline bool IsWindowsVistaOrLater() {
100   int major;
101   return (GetOsVersion(&major, NULL, NULL) && major >= kWindowsVista);
102 }
103 
IsWindowsXpOrLater()104 inline bool IsWindowsXpOrLater() {
105   int major, minor;
106   return (GetOsVersion(&major, &minor, NULL) &&
107           (major >= kWindowsVista ||
108           (major == kWindows2000 && minor >= 1)));
109 }
110 
IsWindows8OrLater()111 inline bool IsWindows8OrLater() {
112   int major, minor;
113   return (GetOsVersion(&major, &minor, NULL) &&
114           (major > kWindowsVista ||
115           (major == kWindowsVista && minor >= 2)));
116 }
117 
118 // Determine the current integrity level of the process.
119 bool GetCurrentProcessIntegrityLevel(int* level);
120 
IsCurrentProcessLowIntegrity()121 inline bool IsCurrentProcessLowIntegrity() {
122   int level;
123   return (GetCurrentProcessIntegrityLevel(&level) &&
124       level < SECURITY_MANDATORY_MEDIUM_RID);
125 }
126 
127 bool AdjustCurrentProcessPrivilege(const TCHAR* privilege, bool to_enable);
128 
129 }  // namespace rtc
130 
131 #endif  // WEBRTC_WIN
132 #endif  // WEBRTC_BASE_WIN32_H_
133