1
/*********************************************************
2
* Copyright (C) 1998 VMware, Inc. All rights reserved.
4
* The contents of this file are subject to the terms of the Common
5
* Development and Distribution License (the "License") version 1.0
6
* and no later version. You may not use this file except in
7
* compliance with the License.
9
* You can obtain a copy of the License at
10
* http://www.opensource.org/licenses/cddl1.php
12
* See the License for the specific language governing permissions
13
* and limitations under the License.
15
*********************************************************/
20
* string wrapping functions
26
#define INCLUDE_ALLOW_MODULE
27
#define INCLUDE_ALLOW_USERLEVEL
28
#define INCLUDE_ALLOW_VMCORE
29
#include "includeCheck.h"
31
#if defined(GLIBC_VERSION_22)
38
#include "compat/compat_stdarg.h" // Provides stdarg.h plus va_copy
40
#include "vm_basic_types.h"
44
* These platforms use bsd_vsnprintf().
45
* This does not mean it has bsd_vsnwprintf().
47
#if defined _WIN32 && !defined STR_NO_WIN32_LIBS || \
48
defined __linux__ && !defined N_PLAT_NLM || defined __APPLE__
49
#define HAS_BSD_PRINTF 1
53
* And these platforms/setups use bsd_vsnwprintf()
55
#if (defined _WIN32 && !defined STR_NO_WIN32_LIBS) || \
56
(defined __GNUC__ && (__GNUC__ < 2 \
58
&& __GNUC_MINOR__ < 96)))
59
#define HAS_BSD_WPRINTF 1
63
* ASCII/UTF-8 versions
65
* NOTE: All size_t arguments and integer returns values are in bytes.
67
* NOTE: Str_Asprintf/Str_Vasprintf return NULL on failure, while
68
* Str_SafeAsprintf/Str_SafeVasprintf ASSERT_NOT_IMPLEMENTED.
70
* NOTE: "%s" refers to strings of "char" units, while "%S" refers to
71
* strings of "wchar_t" units, regardless of platform.
74
EXTERN int Str_Sprintf(char *buf, size_t max,
75
const char *fmt, ...) PRINTF_DECL(3, 4);
76
EXTERN int Str_Snprintf(char *buf, size_t len,
77
const char *fmt, ...) PRINTF_DECL(3, 4);
78
EXTERN int Str_Vsnprintf(char *buf, size_t len,
79
const char *fmt, va_list args);
80
EXTERN size_t Str_Strlen(const char *src, size_t maxLen);
81
EXTERN char *Str_Strnstr(const char *src, const char *sub, size_t n);
82
EXTERN char *Str_Strcpy(char *dst, const char *src, size_t maxLen);
83
EXTERN char *Str_Strcat(char *dst, const char *src, size_t maxLen);
84
EXTERN char *Str_Strncat(char *buf, size_t bufSize, const char *src, size_t n);
86
EXTERN char *Str_Asprintf(size_t *length,
87
const char *format, ...) PRINTF_DECL(2, 3);
88
EXTERN char *Str_Vasprintf(size_t *length, const char *format,
90
EXTERN char *Str_SafeAsprintf(size_t *length,
91
const char *format, ...) PRINTF_DECL(2, 3);
92
EXTERN char *Str_SafeVasprintf(size_t *length, const char *format,
95
#if defined(_WIN32) || defined(GLIBC_VERSION_22) // {
100
* NOTE: All size_t arguments and integer return values are in
101
* wchar_ts, not bytes.
103
* NOTE: Str_Aswprintf/Str_Vaswprintf return NULL on failure, while
104
* Str_SafeAswprintf/Str_SafeVaswprintf ASSERT_NOT_IMPLEMENTED.
106
* NOTE: "%s" refers to strings of "char" units, while "%S" refers to
107
* strings of "wchar_t" units, regardless of platform.
110
EXTERN int Str_Swprintf(wchar_t *buf, size_t max,
111
const wchar_t *fmt, ...);
112
EXTERN int Str_Snwprintf(wchar_t *buf, size_t len,
113
const wchar_t *fmt, ...);
114
EXTERN int Str_Vsnwprintf(wchar_t *buf, size_t len,
115
const wchar_t *fmt, va_list args);
116
EXTERN wchar_t *Str_Wcscpy(wchar_t *dst, const wchar_t *src, size_t maxLen);
117
EXTERN wchar_t *Str_Wcscat(wchar_t *dst, const wchar_t *src, size_t maxLen);
118
EXTERN wchar_t *Str_Wcsncat(wchar_t *buf, size_t bufSize, const wchar_t *src,
121
EXTERN wchar_t *Str_Aswprintf(size_t *length,
122
const wchar_t *format, ...);
123
EXTERN wchar_t *Str_Vaswprintf(size_t *length, const wchar_t *format,
125
EXTERN wchar_t *Str_SafeAswprintf(size_t *length,
126
const wchar_t *format, ...);
127
EXTERN wchar_t *Str_SafeVaswprintf(size_t *length, const wchar_t *format,
130
unsigned char *Str_Mbscpy(char *buf, const char *src,
132
unsigned char *Str_Mbscat(char *buf, const char *src,
136
* These are handly for Windows programmers. They are like
137
* the _tcs functions, but with Str_Strcpy-style bounds checking.
139
* We don't have Str_Mbsncat() because it has some odd semantic
140
* ambiguity (whether to truncate in the middle of a multibyte
141
* sequence) that I want to stay away from. -- edward
146
#define Str_Tcscpy(s1, s2, n) Str_Wcscpy(s1, s2, n)
147
#define Str_Tcscat(s1, s2, n) Str_Wcscat(s1, s2, n)
149
#define Str_Tcscpy(s1, s2, n) Str_Mbscpy(s1, s2, n)
150
#define Str_Tcscat(s1, s2, n) Str_Mbscat(s1, s2, n)
154
#endif // } defined(_WIN32) || defined(GLIBC_VERSION_22)
158
* Wrappers for standard string functions
160
* These are either for Windows-Posix compatibility,
161
* or just gratuitous wrapping for consistency.
164
#define Str_Strcmp(s1, s2) strcmp(s1, s2)
165
#define Str_Strncmp(s1, s2, n) strncmp(s1, s2, n)
167
#define Str_Strchr(s, c) strchr(s, c)
168
#define Str_Strrchr(s, c) strrchr(s, c)
169
#define Str_Strspn(s1, s2) strspn(s1, s2)
170
#define Str_Strcspn(s1, s2) strcspn(s1, s2)
173
#define Str_Strcasecmp(s1, s2) _stricmp(s1, s2)
174
#define Str_Strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
175
#define Str_ToUpper(s) _strupr(s)
176
#define Str_ToLower(s) _strlwr(s)
177
#elif defined(N_PLAT_NLM)
178
#define Str_Strcasecmp(s1, s2) stricmp(s1, s2)
179
#define Str_Strncasecmp(s1, s2, n) strnicmp(s1, s2, n)
180
char *Str_ToUpper(char *string);
181
char *Str_ToLower(char *string);
183
#define Str_Strcasecmp(s1, s2) strcasecmp(s1, s2)
184
#define Str_Strncasecmp(s1, s2, n) strncasecmp(s1, s2, n)
185
char *Str_ToUpper(char *string);
186
char *Str_ToLower(char *string);
190
#define Str_Tcscmp(s1, s2) _tcscmp(s1, s2)
191
#define Str_Tcsncmp(s1, s2, n) _tcsncmp(s1, s2, n)
192
#define Str_Tcsicmp(s1, s2) _tcsicmp(s1, s2)
193
#define Str_Tcsnicmp(s1, s2, n) _tcsnicmp(s1, s2, n)
194
#define Str_Tcschr(s, c) _tcschr(s, c)
195
#define Str_Tcsrchr(s, c) _tcsrchr(s, c)
196
#define Str_Tcsspn(s1, s2) _tcsspn(s1, s2)
197
#define Str_Tcscspn(s1, s2) _tcscspn(s1, s2)
198
#define Str_Tcsupr(s) _tcsupr(s)
199
#define Str_Tcslwr(s) _tcslwr(s)