1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_BASE_WIN32_H_
29#define TALK_BASE_WIN32_H_
30
31#ifdef WIN32
32
33#ifndef WIN32_LEAN_AND_MEAN
34#define WIN32_LEAN_AND_MEAN
35#endif
36
37// Make sure we don't get min/max macros
38#ifndef NOMINMAX
39#define NOMINMAX
40#endif
41
42#include <winsock2.h>
43#include <windows.h>
44
45#ifndef SECURITY_MANDATORY_LABEL_AUTHORITY
46// Add defines that we use if we are compiling against older sdks
47#define SECURITY_MANDATORY_MEDIUM_RID               (0x00002000L)
48#define TokenIntegrityLevel static_cast<TOKEN_INFORMATION_CLASS>(0x19)
49typedef struct _TOKEN_MANDATORY_LABEL {
50    SID_AND_ATTRIBUTES Label;
51} TOKEN_MANDATORY_LABEL, *PTOKEN_MANDATORY_LABEL;
52#endif  // SECURITY_MANDATORY_LABEL_AUTHORITY
53
54#undef SetPort
55
56#include <string>
57
58#include "talk/base/stringutils.h"
59#include "talk/base/basictypes.h"
60
61namespace talk_base {
62
63const char* win32_inet_ntop(int af, const void *src, char* dst, socklen_t size);
64int win32_inet_pton(int af, const char* src, void *dst);
65
66///////////////////////////////////////////////////////////////////////////////
67
68inline std::wstring ToUtf16(const char* utf8, size_t len) {
69  int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len),
70                                    NULL, 0);
71  wchar_t* ws = STACK_ARRAY(wchar_t, len16);
72  ::MultiByteToWideChar(CP_UTF8, 0, utf8, static_cast<int>(len), ws, len16);
73  return std::wstring(ws, len16);
74}
75
76inline std::wstring ToUtf16(const std::string& str) {
77  return ToUtf16(str.data(), str.length());
78}
79
80inline std::string ToUtf8(const wchar_t* wide, size_t len) {
81  int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len),
82                                   NULL, 0, NULL, NULL);
83  char* ns = STACK_ARRAY(char, len8);
84  ::WideCharToMultiByte(CP_UTF8, 0, wide, static_cast<int>(len), ns, len8,
85                        NULL, NULL);
86  return std::string(ns, len8);
87}
88
89inline std::string ToUtf8(const wchar_t* wide) {
90  return ToUtf8(wide, wcslen(wide));
91}
92
93inline std::string ToUtf8(const std::wstring& wstr) {
94  return ToUtf8(wstr.data(), wstr.length());
95}
96
97// Convert FILETIME to time_t
98void FileTimeToUnixTime(const FILETIME& ft, time_t* ut);
99
100// Convert time_t to FILETIME
101void UnixTimeToFileTime(const time_t& ut, FILETIME * ft);
102
103// Convert a Utf8 path representation to a non-length-limited Unicode pathname.
104bool Utf8ToWindowsFilename(const std::string& utf8, std::wstring* filename);
105
106// Convert a FILETIME to a UInt64
107inline uint64 ToUInt64(const FILETIME& ft) {
108  ULARGE_INTEGER r = {ft.dwLowDateTime, ft.dwHighDateTime};
109  return r.QuadPart;
110}
111
112enum WindowsMajorVersions {
113  kWindows2000 = 5,
114  kWindowsVista = 6,
115};
116bool GetOsVersion(int* major, int* minor, int* build);
117
118inline bool IsWindowsVistaOrLater() {
119  int major;
120  return (GetOsVersion(&major, NULL, NULL) && major >= kWindowsVista);
121}
122
123inline bool IsWindowsXpOrLater() {
124  int major, minor;
125  return (GetOsVersion(&major, &minor, NULL) &&
126          (major >= kWindowsVista ||
127          (major == kWindows2000 && minor >= 1)));
128}
129
130// Determine the current integrity level of the process.
131bool GetCurrentProcessIntegrityLevel(int* level);
132
133inline bool IsCurrentProcessLowIntegrity() {
134  int level;
135  return (GetCurrentProcessIntegrityLevel(&level) &&
136      level < SECURITY_MANDATORY_MEDIUM_RID);
137}
138
139bool AdjustCurrentProcessPrivilege(const TCHAR* privilege, bool to_enable);
140
141///////////////////////////////////////////////////////////////////////////////
142
143}  // namespace talk_base
144
145#endif  // WIN32
146#endif  // TALK_BASE_WIN32_H_
147