OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006, Google Inc. | |
2 // All rights reserved. | |
3 // | |
4 // Redistribution and use in source and binary forms, with or without | |
5 // modification, are permitted provided that the following conditions are | |
6 // met: | |
7 // | |
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 | |
13 // distribution. | |
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. | |
17 // | |
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. | |
29 | |
30 // string_utils-inl.h: Safer string manipulation on Windows, supporting | |
31 // pre-MSVC8 environments. | |
32 | |
33 #ifndef COMMON_WINDOWS_STRING_UTILS_INL_H__ | |
34 #define COMMON_WINDOWS_STRING_UTILS_INL_H__ | |
35 | |
36 #include <stdarg.h> | |
37 #include <wchar.h> | |
38 | |
39 #include <string> | |
40 | |
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 | |
45 // 64 bits wide. | |
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 | |
51 | |
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 #else | |
60 // For MSVC8 and newer, swprintf_s is the recommended method. Conveniently, | |
61 // it takes the same argument list as swprintf. | |
62 #define swprintf swprintf_s | |
63 #endif // MSC_VER < 1400 | |
64 | |
65 namespace google_breakpad { | |
66 | |
67 using std::string; | |
68 using std::wstring; | |
69 | |
70 class WindowsStringUtils { | |
71 public: | |
72 // Roughly equivalent to MSVC8's wcscpy_s, except pre-MSVC8, this does | |
73 // not fail if source is longer than destination_size. The destination | |
74 // buffer is always 0-terminated. | |
75 static void safe_wcscpy(wchar_t *destination, size_t destination_size, | |
76 const wchar_t *source); | |
77 | |
78 // Roughly equivalent to MSVC8's wcsncpy_s, except that _TRUNCATE cannot | |
79 // be passed directly, and pre-MSVC8, this will not fail if source or count | |
80 // are longer than destination_size. The destination buffer is always | |
81 // 0-terminated. | |
82 static void safe_wcsncpy(wchar_t *destination, size_t destination_size, | |
83 const wchar_t *source, size_t count); | |
84 | |
85 // Performs multi-byte to wide character conversion on C++ strings, using | |
86 // mbstowcs_s (MSVC8) or mbstowcs (pre-MSVC8). Returns false on failure, | |
87 // without setting wcs. | |
88 static bool safe_mbstowcs(const string &mbs, wstring *wcs); | |
89 | |
90 // Returns the base name of a file, e.g. strips off the path. | |
91 static wstring GetBaseName(const wstring &filename); | |
92 | |
93 private: | |
94 // Disallow instantiation and other object-based operations. | |
95 WindowsStringUtils(); | |
96 WindowsStringUtils(const WindowsStringUtils&); | |
97 ~WindowsStringUtils(); | |
98 void operator=(const WindowsStringUtils&); | |
99 }; | |
100 | |
101 // static | |
102 inline void WindowsStringUtils::safe_wcscpy(wchar_t *destination, | |
103 size_t destination_size, | |
104 const wchar_t *source) { | |
105 #if _MSC_VER >= 1400 // MSVC 2005/8 | |
106 wcscpy_s(destination, destination_size, source); | |
107 #else // _MSC_VER >= 1400 | |
108 // Pre-MSVC 2005/8 doesn't have wcscpy_s. Simulate it with wcsncpy. | |
109 // wcsncpy doesn't 0-terminate the destination buffer if the source string | |
110 // is longer than size. Ensure that the destination is 0-terminated. | |
111 wcsncpy(destination, source, destination_size); | |
112 if (destination && destination_size) | |
113 destination[destination_size - 1] = 0; | |
114 #endif // _MSC_VER >= 1400 | |
115 } | |
116 | |
117 // static | |
118 inline void WindowsStringUtils::safe_wcsncpy(wchar_t *destination, | |
119 size_t destination_size, | |
120 const wchar_t *source, | |
121 size_t count) { | |
122 #if _MSC_VER >= 1400 // MSVC 2005/8 | |
123 wcsncpy_s(destination, destination_size, source, count); | |
124 #else // _MSC_VER >= 1400 | |
125 // Pre-MSVC 2005/8 doesn't have wcsncpy_s. Simulate it with wcsncpy. | |
126 // wcsncpy doesn't 0-terminate the destination buffer if the source string | |
127 // is longer than size. Ensure that the destination is 0-terminated. | |
128 if (destination_size < count) | |
129 count = destination_size; | |
130 | |
131 wcsncpy(destination, source, count); | |
132 if (destination && count) | |
133 destination[count - 1] = 0; | |
134 #endif // _MSC_VER >= 1400 | |
135 } | |
136 | |
137 } // namespace google_breakpad | |
138 | |
139 #endif // COMMON_WINDOWS_STRING_UTILS_INL_H__ | |
OLD | NEW |