diff options
author | ronghuawu@chromium.org <ronghuawu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-28 21:12:59 +0000 |
---|---|---|
committer | ronghuawu@chromium.org <ronghuawu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-28 21:12:59 +0000 |
commit | 3aa8b49dc9d29a5528753962c0760e34f8d63832 (patch) | |
tree | 96b6d0c014f748c2475ec09837e3ba136963b146 /third_party/libjingle | |
parent | ef47f368d9aa9397ea0952bdd28d8a2cd24ddaf9 (diff) | |
download | chromium_src-3aa8b49dc9d29a5528753962c0760e34f8d63832.zip chromium_src-3aa8b49dc9d29a5528753962c0760e34f8d63832.tar.gz chromium_src-3aa8b49dc9d29a5528753962c0760e34f8d63832.tar.bz2 |
Add more files need to be changed to the overide folder.
The original files are from libjinge r117.
BUG=115501
TEST=N/A
Review URL: https://chromiumcodereview.appspot.com/9476031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@124023 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/libjingle')
-rw-r--r-- | third_party/libjingle/overrides/talk/base/byteorder.h | 172 | ||||
-rw-r--r-- | third_party/libjingle/overrides/talk/base/win32.cc | 440 | ||||
-rw-r--r-- | third_party/libjingle/overrides/talk/base/win32.h | 133 |
3 files changed, 745 insertions, 0 deletions
diff --git a/third_party/libjingle/overrides/talk/base/byteorder.h b/third_party/libjingle/overrides/talk/base/byteorder.h new file mode 100644 index 0000000..08094b8 --- /dev/null +++ b/third_party/libjingle/overrides/talk/base/byteorder.h @@ -0,0 +1,172 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TALK_BASE_BYTEORDER_H__ +#define TALK_BASE_BYTEORDER_H__ + +#ifdef POSIX +#include <arpa/inet.h> +#endif + +#ifdef WIN32 +#include <winsock2.h> +#endif + +#include "talk/base/basictypes.h" + +namespace talk_base { + +// Reading and writing of little and big-endian numbers from memory +// TODO: Add HostEndian #defines (HE) +// TODO: Consider NetworkEndian as synonym for BigEndian, for clarity in use. +// TODO: Consider creating optimized versions, such as direct read/writes of +// integers in host-endian format, when the platform supports it. + +inline void Set8(void* memory, size_t offset, uint8 v) { + static_cast<uint8*>(memory)[offset] = v; +} +inline uint8 Get8(const void* memory, size_t offset) { + return static_cast<const uint8*>(memory)[offset]; +} + +inline void SetBE16(void* memory, uint16 v) { + Set8(memory, 0, static_cast<uint8>(v >> 8)); + Set8(memory, 1, static_cast<uint8>(v >> 0)); +} +inline void SetBE32(void* memory, uint32 v) { + Set8(memory, 0, static_cast<uint8>(v >> 24)); + Set8(memory, 1, static_cast<uint8>(v >> 16)); + Set8(memory, 2, static_cast<uint8>(v >> 8)); + Set8(memory, 3, static_cast<uint8>(v >> 0)); +} +inline void SetBE64(void* memory, uint64 v) { + Set8(memory, 0, static_cast<uint8>(v >> 56)); + Set8(memory, 1, static_cast<uint8>(v >> 48)); + Set8(memory, 2, static_cast<uint8>(v >> 40)); + Set8(memory, 3, static_cast<uint8>(v >> 32)); + Set8(memory, 4, static_cast<uint8>(v >> 24)); + Set8(memory, 5, static_cast<uint8>(v >> 16)); + Set8(memory, 6, static_cast<uint8>(v >> 8)); + Set8(memory, 7, static_cast<uint8>(v >> 0)); +} +inline uint16 GetBE16(const void* memory) { + return (static_cast<uint16>(Get8(memory, 0)) << 8) + | (static_cast<uint16>(Get8(memory, 1)) << 0); +} +inline uint32 GetBE32(const void* memory) { + return (static_cast<uint32>(Get8(memory, 0)) << 24) + | (static_cast<uint32>(Get8(memory, 1)) << 16) + | (static_cast<uint32>(Get8(memory, 2)) << 8) + | (static_cast<uint32>(Get8(memory, 3)) << 0); +} +inline uint64 GetBE64(const void* memory) { + return (static_cast<uint64>(Get8(memory, 0)) << 56) + | (static_cast<uint64>(Get8(memory, 1)) << 48) + | (static_cast<uint64>(Get8(memory, 2)) << 40) + | (static_cast<uint64>(Get8(memory, 3)) << 32) + | (static_cast<uint64>(Get8(memory, 4)) << 24) + | (static_cast<uint64>(Get8(memory, 5)) << 16) + | (static_cast<uint64>(Get8(memory, 6)) << 8) + | (static_cast<uint64>(Get8(memory, 7)) << 0); +} + +inline void SetLE16(void* memory, uint16 v) { + Set8(memory, 1, static_cast<uint8>(v >> 8)); + Set8(memory, 0, static_cast<uint8>(v >> 0)); +} +inline void SetLE32(void* memory, uint32 v) { + Set8(memory, 3, static_cast<uint8>(v >> 24)); + Set8(memory, 2, static_cast<uint8>(v >> 16)); + Set8(memory, 1, static_cast<uint8>(v >> 8)); + Set8(memory, 0, static_cast<uint8>(v >> 0)); +} +inline void SetLE64(void* memory, uint64 v) { + Set8(memory, 7, static_cast<uint8>(v >> 56)); + Set8(memory, 6, static_cast<uint8>(v >> 48)); + Set8(memory, 5, static_cast<uint8>(v >> 40)); + Set8(memory, 4, static_cast<uint8>(v >> 32)); + Set8(memory, 3, static_cast<uint8>(v >> 24)); + Set8(memory, 2, static_cast<uint8>(v >> 16)); + Set8(memory, 1, static_cast<uint8>(v >> 8)); + Set8(memory, 0, static_cast<uint8>(v >> 0)); +} +inline uint16 GetLE16(const void* memory) { + return (static_cast<uint16>(Get8(memory, 1)) << 8) + | (static_cast<uint16>(Get8(memory, 0)) << 0); +} +inline uint32 GetLE32(const void* memory) { + return (static_cast<uint32>(Get8(memory, 3)) << 24) + | (static_cast<uint32>(Get8(memory, 2)) << 16) + | (static_cast<uint32>(Get8(memory, 1)) << 8) + | (static_cast<uint32>(Get8(memory, 0)) << 0); +} +inline uint64 GetLE64(const void* memory) { + return (static_cast<uint64>(Get8(memory, 7)) << 56) + | (static_cast<uint64>(Get8(memory, 6)) << 48) + | (static_cast<uint64>(Get8(memory, 5)) << 40) + | (static_cast<uint64>(Get8(memory, 4)) << 32) + | (static_cast<uint64>(Get8(memory, 3)) << 24) + | (static_cast<uint64>(Get8(memory, 2)) << 16) + | (static_cast<uint64>(Get8(memory, 1)) << 8) + | (static_cast<uint64>(Get8(memory, 0)) << 0); +} + +// Check if the current host is big endian. +inline bool IsHostBigEndian() { + static const int number = 1; + return (0 == *reinterpret_cast<const char*>(&number)); +} + +inline uint16 HostToNetwork16(uint16 n) { + return htons(n); +} + +inline uint32 HostToNetwork32(uint32 n) { + return htonl(n); +} + +inline uint64 HostToNetwork64(uint64 n) { + // If the host is little endian, GetBE64 converts n to big network endian. + return IsHostBigEndian() ? n : GetBE64(&n); +} + +inline uint16 NetworkToHost16(uint16 n) { + return ntohs(n); +} + +inline uint32 NetworkToHost32(uint32 n) { + return ntohl(n); +} + +inline uint64 NetworkToHost64(uint64 n) { + // If the host is little endian, GetBE64 converts n to little endian. + return IsHostBigEndian() ? n : GetBE64(&n); +} + +} // namespace talk_base + +#endif // TALK_BASE_BYTEORDER_H__ diff --git a/third_party/libjingle/overrides/talk/base/win32.cc b/third_party/libjingle/overrides/talk/base/win32.cc new file mode 100644 index 0000000..3937781 --- /dev/null +++ b/third_party/libjingle/overrides/talk/base/win32.cc @@ -0,0 +1,440 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "talk/base/win32.h" + +#include <winsock2.h> +#include <ws2tcpip.h> +#include <algorithm> + +#include "talk/base/basictypes.h" +#include "talk/base/common.h" +#include "talk/base/logging.h" + +namespace talk_base { + +// Helper function declarations for inet_ntop/inet_pton. +static const char* inet_ntop_v4(const void* src, char* dst, socklen_t size); +static const char* inet_ntop_v6(const void* src, char* dst, socklen_t size); +static int inet_pton_v4(const char* src, void* dst); +static int inet_pton_v6(const char* src, void* dst); + +// Implementation of inet_ntop (create a printable representation of an +// ip address). XP doesn't have its own inet_ntop, and +// WSAAddressToString requires both IPv6 to be installed and for Winsock +// to be initialized. +const char* win32_inet_ntop(int af, const void *src, + char* dst, socklen_t size) { + if (!src || !dst) { + return NULL; + } + switch (af) { + case AF_INET: { + return inet_ntop_v4(src, dst, size); + } + case AF_INET6: { + return inet_ntop_v6(src, dst, size); + } + } + return NULL; +} + +// As above, but for inet_pton. Wraps inet_addr for v4, and implements inet_pton +// for v6. Slightly more permissive than the RFC specified inet_pton, as it uses +// windows' inet_addr which permits octal and hexadecimal values in v4 +// addresses, while inet_pton only allows decimal. +// Note that our inet_ntop will output normal 'dotted' v4 addresses only. +int win32_inet_pton(int af, const char* src, void* dst) { + if (!src || !dst) { + return 0; + } + if (af == AF_INET) { + return inet_pton_v4(src, dst); + } else if (af == AF_INET6) { + return inet_pton_v6(src, dst); + } + return -1; +} + +// Helper function for inet_ntop for IPv4 addresses. +// Outputs "dotted-quad" decimal notation. +const char* inet_ntop_v4(const void* src, char* dst, socklen_t size) { + if (size < INET_ADDRSTRLEN) { + return NULL; + } + const struct in_addr* as_in_addr = + reinterpret_cast<const struct in_addr*>(src); + talk_base::sprintfn(dst, size, "%d.%d.%d.%d", + as_in_addr->S_un.S_un_b.s_b1, + as_in_addr->S_un.S_un_b.s_b2, + as_in_addr->S_un.S_un_b.s_b3, + as_in_addr->S_un.S_un_b.s_b4); + return dst; +} + +// Helper function for inet_ntop for IPv6 addresses. +const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) { + if (size < INET6_ADDRSTRLEN) { + return NULL; + } + const uint16* as_shorts = + reinterpret_cast<const uint16*>(src); + int runpos[8]; + int current = 1; + int max = 1; + int maxpos = -1; + int run_array_size = ARRAY_SIZE(runpos); + // Run over the address marking runs of 0s. + for (int i = 0; i < run_array_size; ++i) { + if (as_shorts[i] == 0) { + runpos[i] = current; + if (current > max) { + maxpos = i; + max = current; + } + ++current; + } else { + runpos[i] = -1; + current =1; + } + } + + if (max > 1) { + int tmpmax = maxpos; + // Run back through, setting -1 for all but the longest run. + for (int i = run_array_size - 1; i >= 0; i--) { + if (i > tmpmax) { + runpos[i] = -1; + } else if (runpos[i] == -1) { + // We're less than maxpos, we hit a -1, so the 'good' run is done. + // Setting tmpmax -1 means all remaining positions get set to -1. + tmpmax = -1; + } + } + } + + char* cursor = dst; + // Print IPv4 compatible and IPv4 mapped addresses using the IPv4 helper. + // These addresses have an initial run of either eight zero-bytes followed + // by 0xFFFF, or an initial run of ten zero-bytes. + if (runpos[0] == 1 && (maxpos == 5 || + (maxpos == 4 && as_shorts[5] == 0xFFFF))) { + *cursor++ = ':'; + *cursor++ = ':'; + if (maxpos == 4) { + cursor += talk_base::sprintfn(cursor, INET6_ADDRSTRLEN - 2, "ffff:"); + } + const struct in_addr* as_v4 = + reinterpret_cast<const struct in_addr*>(&(as_shorts[6])); + inet_ntop_v4(as_v4, cursor, (INET6_ADDRSTRLEN - (cursor - dst))); + } else { + for (int i = 0; i < run_array_size; ++i) { + if (runpos[i] == -1) { + cursor += talk_base::sprintfn(cursor, + INET6_ADDRSTRLEN - (cursor - dst), + "%x", ntohs(as_shorts[i])); + if (i != 7 && runpos[i + 1] != 1) { + *cursor++ = ':'; + } + } else if (runpos[i] == 1) { + // Entered the run; print the colons and skip the run. + *cursor++ = ':'; + *cursor++ = ':'; + i += (max - 1); + } + } + } + return dst; +} + +// Helper function for inet_pton for IPv4 addresses. +// Uses win32's inet_addr. +int inet_pton_v4(const char* src, void* dst) { + uint32 ip = inet_addr(src); + if (ip == 0xFFFFFFFF && strcmp(src, "255.255.255.255") != 0) { + return 0; + } + struct in_addr* dst_as_in_addr = reinterpret_cast<struct in_addr*>(dst); + dst_as_in_addr->s_addr = ip; + return 1; +} + +// Helper function for inet_pton for IPv6 addresses. +int inet_pton_v6(const char* src, void* dst) { + // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex. + // Check for literal x in the input string. + const char* readcursor = src; + char c = *readcursor++; + while (c) { + if (c == 'x') { + return 0; + } + c = *readcursor++; + } + readcursor = src; + + struct in6_addr an_addr; + memset(&an_addr, 0, sizeof(an_addr)); + + uint16* addr_cursor = reinterpret_cast<uint16*>(&an_addr.s6_addr[0]); + uint16* addr_end = reinterpret_cast<uint16*>(&an_addr.s6_addr[16]); + bool seencompressed = false; + + // Addresses that start with "::" (i.e., a run of initial zeros) or + // "::ffff:" can potentially be IPv4 mapped or compatibility addresses. + // These have dotted-style IPv4 addresses on the end (e.g. "::192.168.7.1"). + if (*readcursor == ':' && *(readcursor+1) == ':' && + *(readcursor + 2) != 0) { + // Check for periods, which we'll take as a sign of v4 addresses. + const char* addrstart = readcursor + 2; + if (talk_base::strchr(addrstart, ".")) { + const char* colon = talk_base::strchr(addrstart, "::"); + if (colon) { + uint16 a_short; + int bytesread = 0; + if (sscanf(addrstart, "%hx%n", &a_short, &bytesread) != 1 || + a_short != 0xFFFF || bytesread != 4) { + // Colons + periods means has to be ::ffff:a.b.c.d. But it wasn't. + return 0; + } else { + an_addr.s6_addr[10] = 0xFF; + an_addr.s6_addr[11] = 0xFF; + addrstart = colon + 1; + } + } + struct in_addr v4; + if (inet_pton_v4(addrstart, &v4.s_addr)) { + memcpy(&an_addr.s6_addr[12], &v4, sizeof(v4)); + memcpy(dst, &an_addr, sizeof(an_addr)); + return 1; + } else { + // Invalid v4 address. + return 0; + } + } + } + + // For addresses without a trailing IPv4 component ('normal' IPv6 addresses). + while (*readcursor != 0 && addr_cursor < addr_end) { + if (*readcursor == ':') { + if (*(readcursor + 1) == ':') { + if (seencompressed) { + // Can only have one compressed run of zeroes ("::") per address. + return 0; + } + // Hit a compressed run. Count colons to figure out how much of the + // address is skipped. + readcursor += 2; + const char* coloncounter = readcursor; + int coloncount = 0; + if (*coloncounter == 0) { + // Special case - trailing ::. + addr_cursor = addr_end; + } else { + while (*coloncounter) { + if (*coloncounter == ':') { + ++coloncount; + } + ++coloncounter; + } + // (coloncount + 1) is the number of shorts left in the address. + addr_cursor = addr_end - (coloncount + 1); + seencompressed = true; + } + } else { + ++readcursor; + } + } else { + uint16 word; + int bytesread = 0; + if (sscanf(readcursor, "%hx%n", &word, &bytesread) != 1) { + return 0; + } else { + *addr_cursor = htons(word); + ++addr_cursor; + readcursor += bytesread; + if (*readcursor != ':' && *readcursor != '\0') { + return 0; + } + } + } + } + + if (*readcursor != '\0' || addr_cursor < addr_end) { + // Catches addresses too short or too long. + return 0; + } + memcpy(dst, &an_addr, sizeof(an_addr)); + return 1; +} + +// +// Unix time is in seconds relative to 1/1/1970. So we compute the windows +// FILETIME of that time/date, then we add/subtract in appropriate units to +// convert to/from unix time. +// The units of FILETIME are 100ns intervals, so by multiplying by or dividing +// by 10000000, we can convert to/from seconds. +// +// FileTime = UnixTime*10000000 + FileTime(1970) +// UnixTime = (FileTime-FileTime(1970))/10000000 +// + +void FileTimeToUnixTime(const FILETIME& ft, time_t* ut) { + ASSERT(NULL != ut); + + // FILETIME has an earlier date base than time_t (1/1/1970), so subtract off + // the difference. + SYSTEMTIME base_st; + memset(&base_st, 0, sizeof(base_st)); + base_st.wDay = 1; + base_st.wMonth = 1; + base_st.wYear = 1970; + + FILETIME base_ft; + SystemTimeToFileTime(&base_st, &base_ft); + + ULARGE_INTEGER base_ul, current_ul; + memcpy(&base_ul, &base_ft, sizeof(FILETIME)); + memcpy(¤t_ul, &ft, sizeof(FILETIME)); + + // Divide by big number to convert to seconds, then subtract out the 1970 + // base date value. + const ULONGLONG RATIO = 10000000; + *ut = static_cast<time_t>((current_ul.QuadPart - base_ul.QuadPart) / RATIO); +} + +void UnixTimeToFileTime(const time_t& ut, FILETIME* ft) { + ASSERT(NULL != ft); + + // FILETIME has an earlier date base than time_t (1/1/1970), so add in + // the difference. + SYSTEMTIME base_st; + memset(&base_st, 0, sizeof(base_st)); + base_st.wDay = 1; + base_st.wMonth = 1; + base_st.wYear = 1970; + + FILETIME base_ft; + SystemTimeToFileTime(&base_st, &base_ft); + + ULARGE_INTEGER base_ul; + memcpy(&base_ul, &base_ft, sizeof(FILETIME)); + + // Multiply by big number to convert to 100ns units, then add in the 1970 + // base date value. + const ULONGLONG RATIO = 10000000; + ULARGE_INTEGER current_ul; + current_ul.QuadPart = base_ul.QuadPart + static_cast<int64>(ut) * RATIO; + memcpy(ft, ¤t_ul, sizeof(FILETIME)); +} + +bool Utf8ToWindowsFilename(const std::string& utf8, std::wstring* filename) { + // TODO: Integrate into fileutils.h + // TODO: Handle wide and non-wide cases via TCHAR? + // TODO: Skip \\?\ processing if the length is not > MAX_PATH? + // TODO: Write unittests + + // Convert to Utf16 + int wlen = ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), utf8.length() + 1, + NULL, 0); + if (0 == wlen) { + return false; + } + wchar_t* wfilename = STACK_ARRAY(wchar_t, wlen); + if (0 == ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), utf8.length() + 1, + wfilename, wlen)) { + return false; + } + // Replace forward slashes with backslashes + std::replace(wfilename, wfilename + wlen, L'/', L'\\'); + // Convert to complete filename + DWORD full_len = ::GetFullPathName(wfilename, 0, NULL, NULL); + if (0 == full_len) { + return false; + } + wchar_t* filepart = NULL; + wchar_t* full_filename = STACK_ARRAY(wchar_t, full_len + 6); + wchar_t* start = full_filename + 6; + if (0 == ::GetFullPathName(wfilename, full_len, start, &filepart)) { + return false; + } + // Add long-path prefix + const wchar_t kLongPathPrefix[] = L"\\\\?\\UNC"; + if ((start[0] != L'\\') || (start[1] != L'\\')) { + // Non-unc path: <pathname> + // Becomes: \\?\<pathname> + start -= 4; + ASSERT(start >= full_filename); + memcpy(start, kLongPathPrefix, 4 * sizeof(wchar_t)); + } else if (start[2] != L'?') { + // Unc path: \\<server>\<pathname> + // Becomes: \\?\UNC\<server>\<pathname> + start -= 6; + ASSERT(start >= full_filename); + memcpy(start, kLongPathPrefix, 7 * sizeof(wchar_t)); + } else { + // Already in long-path form. + } + filename->assign(start); + return true; +} + +bool GetOsVersion(int* major, int* minor, int* build) { + OSVERSIONINFO info = {0}; + info.dwOSVersionInfoSize = sizeof(info); + if (GetVersionEx(&info)) { + if (major) *major = info.dwMajorVersion; + if (minor) *minor = info.dwMinorVersion; + if (build) *build = info.dwBuildNumber; + return true; + } + return false; +} + +bool GetCurrentProcessIntegrityLevel(int* level) { + bool ret = false; + HANDLE process = ::GetCurrentProcess(), token; + if (OpenProcessToken(process, TOKEN_QUERY | TOKEN_QUERY_SOURCE, &token)) { + DWORD size; + if (!GetTokenInformation(token, TokenIntegrityLevel, NULL, 0, &size) && + GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + + char* buf = STACK_ARRAY(char, size); + TOKEN_MANDATORY_LABEL* til = + reinterpret_cast<TOKEN_MANDATORY_LABEL*>(buf); + if (GetTokenInformation(token, TokenIntegrityLevel, til, size, &size)) { + + DWORD count = *GetSidSubAuthorityCount(til->Label.Sid); + *level = *GetSidSubAuthority(til->Label.Sid, count - 1); + ret = true; + } + } + CloseHandle(token); + } + return ret; +} +} // namespace talk_base diff --git a/third_party/libjingle/overrides/talk/base/win32.h b/third_party/libjingle/overrides/talk/base/win32.h new file mode 100644 index 0000000..9c6ea3f --- /dev/null +++ b/third_party/libjingle/overrides/talk/base/win32.h @@ -0,0 +1,133 @@ +/* + * libjingle + * Copyright 2004--2005, Google Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef TALK_BASE_WIN32_H_ +#define TALK_BASE_WIN32_H_ + +#ifdef WIN32 + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include <winsock2.h> +#include <windows.h> + +#ifndef SECURITY_MANDATORY_LABEL_AUTHORITY +// Add defines that we use if we are compiling against older sdks +#define SECURITY_MANDATORY_MEDIUM_RID (0x00002000L) +#define TokenIntegrityLevel static_cast<TOKEN_INFORMATION_CLASS>(0x19) +typedef struct _TOKEN_MANDATORY_LABEL { + SID_AND_ATTRIBUTES Label; +} TOKEN_MANDATORY_LABEL, *PTOKEN_MANDATORY_LABEL; +#endif // SECURITY_MANDATORY_LABEL_AUTHORITY + +#undef SetPort + +#include <string> + +#include "talk/base/stringutils.h" +#include "talk/base/basictypes.h" + +namespace talk_base { + +const char* win32_inet_ntop(int af, const void *src, char* dst, socklen_t size); +int win32_inet_pton(int af, const char* src, void *dst); + +/////////////////////////////////////////////////////////////////////////////// + +inline std::wstring ToUtf16(const char* utf8, size_t len) { + int len16 = ::MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, 0); + wchar_t* ws = STACK_ARRAY(wchar_t, len16); + ::MultiByteToWideChar(CP_UTF8, 0, utf8, len, ws, len16); + return std::wstring(ws, len16); +} + +inline std::wstring ToUtf16(const std::string& str) { + return ToUtf16(str.data(), str.length()); +} + +inline std::string ToUtf8(const wchar_t* wide, size_t len) { + int len8 = ::WideCharToMultiByte(CP_UTF8, 0, wide, len, NULL, 0, NULL, NULL); + char* ns = STACK_ARRAY(char, len8); + ::WideCharToMultiByte(CP_UTF8, 0, wide, len, ns, len8, NULL, NULL); + return std::string(ns, len8); +} + +inline std::string ToUtf8(const std::wstring& wstr) { + return ToUtf8(wstr.data(), wstr.length()); +} + +// Convert FILETIME to time_t +void FileTimeToUnixTime(const FILETIME& ft, time_t* ut); + +// Convert time_t to FILETIME +void UnixTimeToFileTime(const time_t& ut, FILETIME * ft); + +// Convert a Utf8 path representation to a non-length-limited Unicode pathname. +bool Utf8ToWindowsFilename(const std::string& utf8, std::wstring* filename); + +// Convert a FILETIME to a UInt64 +inline uint64 ToUInt64(const FILETIME& ft) { + ULARGE_INTEGER r = {ft.dwLowDateTime, ft.dwHighDateTime}; + return r.QuadPart; +} + +enum WindowsMajorVersions { + kWindows2000 = 5, + kWindowsVista = 6, +}; +bool GetOsVersion(int* major, int* minor, int* build); + +inline bool IsWindowsVistaOrLater() { + int major; + return (GetOsVersion(&major, NULL, NULL) && major >= kWindowsVista); +} + +inline bool IsWindowsXpOrLater() { + int major, minor; + return (GetOsVersion(&major, &minor, NULL) && + (major >= kWindowsVista || + (major == kWindows2000 && minor >= 1))); +} + +// Determine the current integrity level of the process. +bool GetCurrentProcessIntegrityLevel(int* level); + +inline bool IsCurrentProcessLowIntegrity() { + int level; + return (GetCurrentProcessIntegrityLevel(&level) && + level < SECURITY_MANDATORY_MEDIUM_RID); +} + +bool AdjustCurrentProcessPrivilege(const TCHAR* privilege, bool to_enable); + +/////////////////////////////////////////////////////////////////////////////// + +} // namespace talk_base + +#endif // WIN32 +#endif // TALK_BASE_WIN32_H_ |