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