michael@0: // Copyright (c) 2006, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // string_utils-inl.h: Safer string manipulation on Windows, supporting michael@0: // pre-MSVC8 environments. michael@0: michael@0: #ifndef COMMON_WINDOWS_STRING_UTILS_INL_H__ michael@0: #define COMMON_WINDOWS_STRING_UTILS_INL_H__ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: michael@0: // The "ll" printf format size specifier corresponding to |long long| was michael@0: // intrudced in MSVC8. Earlier versions did not provide this size specifier, michael@0: // but "I64" can be used to print 64-bit types. Don't use "I64" where "ll" michael@0: // is available, in the event of oddball systems where |long long| is not michael@0: // 64 bits wide. michael@0: #if _MSC_VER >= 1400 // MSVC 2005/8 michael@0: #define WIN_STRING_FORMAT_LL "ll" michael@0: #else // MSC_VER >= 1400 michael@0: #define WIN_STRING_FORMAT_LL "I64" michael@0: #endif // MSC_VER >= 1400 michael@0: michael@0: // A nonconforming version of swprintf, without the length argument, was michael@0: // included with the CRT prior to MSVC8. Although a conforming version was michael@0: // also available via an overload, it is not reliably chosen. _snwprintf michael@0: // behaves as a standards-confirming swprintf should, so force the use of michael@0: // _snwprintf when using older CRTs. michael@0: #if _MSC_VER < 1400 // MSVC 2005/8 michael@0: #define swprintf _snwprintf michael@0: #else michael@0: // For MSVC8 and newer, swprintf_s is the recommended method. Conveniently, michael@0: // it takes the same argument list as swprintf. michael@0: #define swprintf swprintf_s michael@0: #endif // MSC_VER < 1400 michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: using std::string; michael@0: using std::wstring; michael@0: michael@0: class WindowsStringUtils { michael@0: public: michael@0: // Roughly equivalent to MSVC8's wcscpy_s, except pre-MSVC8, this does michael@0: // not fail if source is longer than destination_size. The destination michael@0: // buffer is always 0-terminated. michael@0: static void safe_wcscpy(wchar_t *destination, size_t destination_size, michael@0: const wchar_t *source); michael@0: michael@0: // Roughly equivalent to MSVC8's wcsncpy_s, except that _TRUNCATE cannot michael@0: // be passed directly, and pre-MSVC8, this will not fail if source or count michael@0: // are longer than destination_size. The destination buffer is always michael@0: // 0-terminated. michael@0: static void safe_wcsncpy(wchar_t *destination, size_t destination_size, michael@0: const wchar_t *source, size_t count); michael@0: michael@0: // Performs multi-byte to wide character conversion on C++ strings, using michael@0: // mbstowcs_s (MSVC8) or mbstowcs (pre-MSVC8). Returns false on failure, michael@0: // without setting wcs. michael@0: static bool safe_mbstowcs(const string &mbs, wstring *wcs); michael@0: michael@0: // The inverse of safe_mbstowcs. michael@0: static bool safe_wcstombs(const wstring &wcs, string *mbs); michael@0: michael@0: // Returns the base name of a file, e.g. strips off the path. michael@0: static wstring GetBaseName(const wstring &filename); michael@0: michael@0: private: michael@0: // Disallow instantiation and other object-based operations. michael@0: WindowsStringUtils(); michael@0: WindowsStringUtils(const WindowsStringUtils&); michael@0: ~WindowsStringUtils(); michael@0: void operator=(const WindowsStringUtils&); michael@0: }; michael@0: michael@0: // static michael@0: inline void WindowsStringUtils::safe_wcscpy(wchar_t *destination, michael@0: size_t destination_size, michael@0: const wchar_t *source) { michael@0: #if _MSC_VER >= 1400 // MSVC 2005/8 michael@0: wcscpy_s(destination, destination_size, source); michael@0: #else // _MSC_VER >= 1400 michael@0: // Pre-MSVC 2005/8 doesn't have wcscpy_s. Simulate it with wcsncpy. michael@0: // wcsncpy doesn't 0-terminate the destination buffer if the source string michael@0: // is longer than size. Ensure that the destination is 0-terminated. michael@0: wcsncpy(destination, source, destination_size); michael@0: if (destination && destination_size) michael@0: destination[destination_size - 1] = 0; michael@0: #endif // _MSC_VER >= 1400 michael@0: } michael@0: michael@0: // static michael@0: inline void WindowsStringUtils::safe_wcsncpy(wchar_t *destination, michael@0: size_t destination_size, michael@0: const wchar_t *source, michael@0: size_t count) { michael@0: #if _MSC_VER >= 1400 // MSVC 2005/8 michael@0: wcsncpy_s(destination, destination_size, source, count); michael@0: #else // _MSC_VER >= 1400 michael@0: // Pre-MSVC 2005/8 doesn't have wcsncpy_s. Simulate it with wcsncpy. michael@0: // wcsncpy doesn't 0-terminate the destination buffer if the source string michael@0: // is longer than size. Ensure that the destination is 0-terminated. michael@0: if (destination_size < count) michael@0: count = destination_size; michael@0: michael@0: wcsncpy(destination, source, count); michael@0: if (destination && count) michael@0: destination[count - 1] = 0; michael@0: #endif // _MSC_VER >= 1400 michael@0: } michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // COMMON_WINDOWS_STRING_UTILS_INL_H__