toolkit/crashreporter/google-breakpad/src/common/windows/string_utils-inl.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // Copyright (c) 2006, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 // string_utils-inl.h: Safer string manipulation on Windows, supporting
michael@0 31 // pre-MSVC8 environments.
michael@0 32
michael@0 33 #ifndef COMMON_WINDOWS_STRING_UTILS_INL_H__
michael@0 34 #define COMMON_WINDOWS_STRING_UTILS_INL_H__
michael@0 35
michael@0 36 #include <stdarg.h>
michael@0 37 #include <wchar.h>
michael@0 38
michael@0 39 #include <string>
michael@0 40
michael@0 41 // The "ll" printf format size specifier corresponding to |long long| was
michael@0 42 // intrudced in MSVC8. Earlier versions did not provide this size specifier,
michael@0 43 // but "I64" can be used to print 64-bit types. Don't use "I64" where "ll"
michael@0 44 // is available, in the event of oddball systems where |long long| is not
michael@0 45 // 64 bits wide.
michael@0 46 #if _MSC_VER >= 1400 // MSVC 2005/8
michael@0 47 #define WIN_STRING_FORMAT_LL "ll"
michael@0 48 #else // MSC_VER >= 1400
michael@0 49 #define WIN_STRING_FORMAT_LL "I64"
michael@0 50 #endif // MSC_VER >= 1400
michael@0 51
michael@0 52 // A nonconforming version of swprintf, without the length argument, was
michael@0 53 // included with the CRT prior to MSVC8. Although a conforming version was
michael@0 54 // also available via an overload, it is not reliably chosen. _snwprintf
michael@0 55 // behaves as a standards-confirming swprintf should, so force the use of
michael@0 56 // _snwprintf when using older CRTs.
michael@0 57 #if _MSC_VER < 1400 // MSVC 2005/8
michael@0 58 #define swprintf _snwprintf
michael@0 59 #else
michael@0 60 // For MSVC8 and newer, swprintf_s is the recommended method. Conveniently,
michael@0 61 // it takes the same argument list as swprintf.
michael@0 62 #define swprintf swprintf_s
michael@0 63 #endif // MSC_VER < 1400
michael@0 64
michael@0 65 namespace google_breakpad {
michael@0 66
michael@0 67 using std::string;
michael@0 68 using std::wstring;
michael@0 69
michael@0 70 class WindowsStringUtils {
michael@0 71 public:
michael@0 72 // Roughly equivalent to MSVC8's wcscpy_s, except pre-MSVC8, this does
michael@0 73 // not fail if source is longer than destination_size. The destination
michael@0 74 // buffer is always 0-terminated.
michael@0 75 static void safe_wcscpy(wchar_t *destination, size_t destination_size,
michael@0 76 const wchar_t *source);
michael@0 77
michael@0 78 // Roughly equivalent to MSVC8's wcsncpy_s, except that _TRUNCATE cannot
michael@0 79 // be passed directly, and pre-MSVC8, this will not fail if source or count
michael@0 80 // are longer than destination_size. The destination buffer is always
michael@0 81 // 0-terminated.
michael@0 82 static void safe_wcsncpy(wchar_t *destination, size_t destination_size,
michael@0 83 const wchar_t *source, size_t count);
michael@0 84
michael@0 85 // Performs multi-byte to wide character conversion on C++ strings, using
michael@0 86 // mbstowcs_s (MSVC8) or mbstowcs (pre-MSVC8). Returns false on failure,
michael@0 87 // without setting wcs.
michael@0 88 static bool safe_mbstowcs(const string &mbs, wstring *wcs);
michael@0 89
michael@0 90 // The inverse of safe_mbstowcs.
michael@0 91 static bool safe_wcstombs(const wstring &wcs, string *mbs);
michael@0 92
michael@0 93 // Returns the base name of a file, e.g. strips off the path.
michael@0 94 static wstring GetBaseName(const wstring &filename);
michael@0 95
michael@0 96 private:
michael@0 97 // Disallow instantiation and other object-based operations.
michael@0 98 WindowsStringUtils();
michael@0 99 WindowsStringUtils(const WindowsStringUtils&);
michael@0 100 ~WindowsStringUtils();
michael@0 101 void operator=(const WindowsStringUtils&);
michael@0 102 };
michael@0 103
michael@0 104 // static
michael@0 105 inline void WindowsStringUtils::safe_wcscpy(wchar_t *destination,
michael@0 106 size_t destination_size,
michael@0 107 const wchar_t *source) {
michael@0 108 #if _MSC_VER >= 1400 // MSVC 2005/8
michael@0 109 wcscpy_s(destination, destination_size, source);
michael@0 110 #else // _MSC_VER >= 1400
michael@0 111 // Pre-MSVC 2005/8 doesn't have wcscpy_s. Simulate it with wcsncpy.
michael@0 112 // wcsncpy doesn't 0-terminate the destination buffer if the source string
michael@0 113 // is longer than size. Ensure that the destination is 0-terminated.
michael@0 114 wcsncpy(destination, source, destination_size);
michael@0 115 if (destination && destination_size)
michael@0 116 destination[destination_size - 1] = 0;
michael@0 117 #endif // _MSC_VER >= 1400
michael@0 118 }
michael@0 119
michael@0 120 // static
michael@0 121 inline void WindowsStringUtils::safe_wcsncpy(wchar_t *destination,
michael@0 122 size_t destination_size,
michael@0 123 const wchar_t *source,
michael@0 124 size_t count) {
michael@0 125 #if _MSC_VER >= 1400 // MSVC 2005/8
michael@0 126 wcsncpy_s(destination, destination_size, source, count);
michael@0 127 #else // _MSC_VER >= 1400
michael@0 128 // Pre-MSVC 2005/8 doesn't have wcsncpy_s. Simulate it with wcsncpy.
michael@0 129 // wcsncpy doesn't 0-terminate the destination buffer if the source string
michael@0 130 // is longer than size. Ensure that the destination is 0-terminated.
michael@0 131 if (destination_size < count)
michael@0 132 count = destination_size;
michael@0 133
michael@0 134 wcsncpy(destination, source, count);
michael@0 135 if (destination && count)
michael@0 136 destination[count - 1] = 0;
michael@0 137 #endif // _MSC_VER >= 1400
michael@0 138 }
michael@0 139
michael@0 140 } // namespace google_breakpad
michael@0 141
michael@0 142 #endif // COMMON_WINDOWS_STRING_UTILS_INL_H__

mercurial