1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/windows/string_utils-inl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +// Copyright (c) 2006, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 + 1.33 +// string_utils-inl.h: Safer string manipulation on Windows, supporting 1.34 +// pre-MSVC8 environments. 1.35 + 1.36 +#ifndef COMMON_WINDOWS_STRING_UTILS_INL_H__ 1.37 +#define COMMON_WINDOWS_STRING_UTILS_INL_H__ 1.38 + 1.39 +#include <stdarg.h> 1.40 +#include <wchar.h> 1.41 + 1.42 +#include <string> 1.43 + 1.44 +// The "ll" printf format size specifier corresponding to |long long| was 1.45 +// intrudced in MSVC8. Earlier versions did not provide this size specifier, 1.46 +// but "I64" can be used to print 64-bit types. Don't use "I64" where "ll" 1.47 +// is available, in the event of oddball systems where |long long| is not 1.48 +// 64 bits wide. 1.49 +#if _MSC_VER >= 1400 // MSVC 2005/8 1.50 +#define WIN_STRING_FORMAT_LL "ll" 1.51 +#else // MSC_VER >= 1400 1.52 +#define WIN_STRING_FORMAT_LL "I64" 1.53 +#endif // MSC_VER >= 1400 1.54 + 1.55 +// A nonconforming version of swprintf, without the length argument, was 1.56 +// included with the CRT prior to MSVC8. Although a conforming version was 1.57 +// also available via an overload, it is not reliably chosen. _snwprintf 1.58 +// behaves as a standards-confirming swprintf should, so force the use of 1.59 +// _snwprintf when using older CRTs. 1.60 +#if _MSC_VER < 1400 // MSVC 2005/8 1.61 +#define swprintf _snwprintf 1.62 +#else 1.63 +// For MSVC8 and newer, swprintf_s is the recommended method. Conveniently, 1.64 +// it takes the same argument list as swprintf. 1.65 +#define swprintf swprintf_s 1.66 +#endif // MSC_VER < 1400 1.67 + 1.68 +namespace google_breakpad { 1.69 + 1.70 +using std::string; 1.71 +using std::wstring; 1.72 + 1.73 +class WindowsStringUtils { 1.74 + public: 1.75 + // Roughly equivalent to MSVC8's wcscpy_s, except pre-MSVC8, this does 1.76 + // not fail if source is longer than destination_size. The destination 1.77 + // buffer is always 0-terminated. 1.78 + static void safe_wcscpy(wchar_t *destination, size_t destination_size, 1.79 + const wchar_t *source); 1.80 + 1.81 + // Roughly equivalent to MSVC8's wcsncpy_s, except that _TRUNCATE cannot 1.82 + // be passed directly, and pre-MSVC8, this will not fail if source or count 1.83 + // are longer than destination_size. The destination buffer is always 1.84 + // 0-terminated. 1.85 + static void safe_wcsncpy(wchar_t *destination, size_t destination_size, 1.86 + const wchar_t *source, size_t count); 1.87 + 1.88 + // Performs multi-byte to wide character conversion on C++ strings, using 1.89 + // mbstowcs_s (MSVC8) or mbstowcs (pre-MSVC8). Returns false on failure, 1.90 + // without setting wcs. 1.91 + static bool safe_mbstowcs(const string &mbs, wstring *wcs); 1.92 + 1.93 + // The inverse of safe_mbstowcs. 1.94 + static bool safe_wcstombs(const wstring &wcs, string *mbs); 1.95 + 1.96 + // Returns the base name of a file, e.g. strips off the path. 1.97 + static wstring GetBaseName(const wstring &filename); 1.98 + 1.99 + private: 1.100 + // Disallow instantiation and other object-based operations. 1.101 + WindowsStringUtils(); 1.102 + WindowsStringUtils(const WindowsStringUtils&); 1.103 + ~WindowsStringUtils(); 1.104 + void operator=(const WindowsStringUtils&); 1.105 +}; 1.106 + 1.107 +// static 1.108 +inline void WindowsStringUtils::safe_wcscpy(wchar_t *destination, 1.109 + size_t destination_size, 1.110 + const wchar_t *source) { 1.111 +#if _MSC_VER >= 1400 // MSVC 2005/8 1.112 + wcscpy_s(destination, destination_size, source); 1.113 +#else // _MSC_VER >= 1400 1.114 + // Pre-MSVC 2005/8 doesn't have wcscpy_s. Simulate it with wcsncpy. 1.115 + // wcsncpy doesn't 0-terminate the destination buffer if the source string 1.116 + // is longer than size. Ensure that the destination is 0-terminated. 1.117 + wcsncpy(destination, source, destination_size); 1.118 + if (destination && destination_size) 1.119 + destination[destination_size - 1] = 0; 1.120 +#endif // _MSC_VER >= 1400 1.121 +} 1.122 + 1.123 +// static 1.124 +inline void WindowsStringUtils::safe_wcsncpy(wchar_t *destination, 1.125 + size_t destination_size, 1.126 + const wchar_t *source, 1.127 + size_t count) { 1.128 +#if _MSC_VER >= 1400 // MSVC 2005/8 1.129 + wcsncpy_s(destination, destination_size, source, count); 1.130 +#else // _MSC_VER >= 1400 1.131 + // Pre-MSVC 2005/8 doesn't have wcsncpy_s. Simulate it with wcsncpy. 1.132 + // wcsncpy doesn't 0-terminate the destination buffer if the source string 1.133 + // is longer than size. Ensure that the destination is 0-terminated. 1.134 + if (destination_size < count) 1.135 + count = destination_size; 1.136 + 1.137 + wcsncpy(destination, source, count); 1.138 + if (destination && count) 1.139 + destination[count - 1] = 0; 1.140 +#endif // _MSC_VER >= 1400 1.141 +} 1.142 + 1.143 +} // namespace google_breakpad 1.144 + 1.145 +#endif // COMMON_WINDOWS_STRING_UTILS_INL_H__