7 #if defined(STRSAFE_NO_CCH_FUNCTIONS) && defined(STRSAFE_NO_CB_FUNCTIONS)
8 #error Both STRSAFE_NO_CCH_FUNCTIONS and STRSAFE_NO_CB_FUNCTIONS are defined
12 #define SUCCEEDED(Status) ((HRESULT)(Status) >= 0)
14 #define STRSAFE_MAX_CCH 2147483647
15 #define STRSAFE_E_INVALID_PARAMETER ((HRESULT)0x80070057L)
16 #define STRSAFE_E_INSUFFICIENT_BUFFER ((HRESULT)0x8007007AL)
17 #define STRSAFE_E_END_OF_FILE ((HRESULT)0x80070026L)
19 #define STRSAFE_FILL_BEHIND_NULL 0x00000200
20 #define STRSAFE_IGNORE_NULLS 0x00000200
21 #define STRSAFE_FILL_ON_FAILURE 0x00000400
22 #define STRSAFE_NULL_ON_FAILURE 0x00000800
23 #define STRSAFE_NO_TRUNCATION 0x00001000
26 #define S_OK ((HRESULT)0x00000000L)
29 #define STRSAFE_MIN(a,b) (((a) < (b))?(a):(b))
31 #ifndef _HRESULT_DEFINED
32 #define _HRESULT_DEFINED
36 typedef char * STRSAFE_LPSTR
;
37 typedef const char * STRSAFE_LPCSTR
;
38 typedef wchar_t * STRSAFE_LPWSTR
;
39 typedef const wchar_t * STRSAFE_LPCWSTR
;
40 typedef unsigned long STRSAFE_DWORD
;
44 /* Implement Cb functions for ansi and unicode */
45 #ifndef STRSAFE_NO_CB_FUNCTIONS
47 #define STRSAFE_UNICODE 0
49 #undef STRSAFE_UNICODE
50 #define STRSAFE_UNICODE 1
52 #undef STRSAFE_UNICODE
54 #endif // !STRSAFE_NO_CB_FUNCTIONS
56 /* Implement Cch functions for ansi and unicode */
57 #ifndef STRSAFE_NO_CCH_FUNCTIONS
58 #define STRSAFE_UNICODE 0
60 #undef STRSAFE_UNICODE
61 #define STRSAFE_UNICODE 1
63 #undef STRSAFE_UNICODE
64 #endif // !STRSAFE_NO_CCH_FUNCTIONS
68 /* Now define the functions depending on UNICODE */
70 # define STRSAFE_UNICODE 1
72 # define STRSAFE_UNICODE 0
75 #undef STRSAFE_UNICODE
77 #endif // !__STRSAFE_H_
79 /*****************************************************************************/
81 #if defined(STRSAFE_UNICODE)
82 #if (STRSAFE_UNICODE == 1)
84 #define STRSAFE_LPTSTR STRSAFE_LPWSTR
85 #define STRSAFE_LPCTSTR STRSAFE_LPCWSTR
86 #define STRSAFE_TCHAR wchar_t
88 #define StringCbCat StringCbCatW
89 #define StringCbCatEx StringCbCatExW
90 #define StringCbCatN StringCbCatNW
91 #define StringCbCatNEx StringCbCatNExW
92 #define StringCbCatWorker StringCxxCatWorkerW
93 #define StringCbCopy StringCbCopyW
94 #define StringCbCopyEx StringCbCopyExW
95 #define StringCbCopyN StringCbCopyNW
96 #define StringCbCopyNEx StringCbCopyNExW
97 #define StringCbGets StringCbGetsW
98 #define StringCbGetsEx StringCbGetsExW
99 #define StringCbLength StringCbLengthW
100 #define StringCbPrintf StringCbPrintfW
101 #define StringCbPrintfEx StringCbPrintfExW
102 #define StringCbVPrintf StringCbVPrintfW
103 #define StringCbVPrintfEx StringCbVPrintfExW
104 #define StringCchCat StringCchCatW
105 #define StringCchCatEx StringCchCatExW
106 #define StringCchCatN StringCchCatNW
107 #define StringCchCatNEx StringCchCatNExW
108 #define StringCchCatWorker StringCchCatWorkerW
109 #define StringCchCopy StringCchCopyW
110 #define StringCchCopyEx StringCchCopyExW
111 #define StringCchCopyN StringCchCopyNW
112 #define StringCchCopyNEx StringCchCopyNExW
113 #define StringCchGets StringCchGetsW
114 #define StringCchGetsEx StringCchGetsExW
115 #define StringCchLength StringCchLengthW
116 #define StringCchPrintf StringCchPrintfW
117 #define StringCchPrintfEx StringCchPrintfExW
118 #define StringCchVPrintf StringCchVPrintfW
119 #define StringCchVPrintfEx StringCchVPrintfExW
121 #else // (STRSAFE_UNICODE != 1)
123 #define STRSAFE_LPTSTR STRSAFE_LPSTR
124 #define STRSAFE_LPCTSTR STRSAFE_LPCSTR
125 #define STRSAFE_TCHAR char
127 #define StringCbCat StringCbCatA
128 #define StringCbCatEx StringCbCatExA
129 #define StringCbCatN StringCbCatNA
130 #define StringCbCatNEx StringCbCatNExA
131 #define StringCbCatWorker StringCxxCatWorkerA
132 #define StringCbCopy StringCbCopyA
133 #define StringCbCopyEx StringCbCopyExA
134 #define StringCbCopyN StringCbCopyNA
135 #define StringCbCopyNEx StringCbCopyNExA
136 #define StringCbGets StringCbGetsA
137 #define StringCbGetsEx StringCbGetsExA
138 #define StringCbLength StringCbLengthA
139 #define StringCbPrintf StringCbPrintfA
140 #define StringCbPrintfEx StringCbPrintfExA
141 #define StringCbVPrintf StringCbVPrintfA
142 #define StringCbVPrintfEx StringCbVPrintfExA
143 #define StringCchCat StringCchCatA
144 #define StringCchCatEx StringCchCatExA
145 #define StringCchCatN StringCchCatNA
146 #define StringCchCatNEx StringCchCatNExA
147 #define StringCchCatWorker StringCchCatWorkerA
148 #define StringCchCopy StringCchCopyA
149 #define StringCchCopyEx StringCchCopyExA
150 #define StringCchCopyN StringCchCopyNA
151 #define StringCchCopyNEx StringCchCopyNExA
152 #define StringCchGets StringCchGetsA
153 #define StringCchGetsEx StringCchGetsExA
154 #define StringCchLength StringCchLengthA
155 #define StringCchPrintf StringCchPrintfA
156 #define StringCchPrintfEx StringCchPrintfExA
157 #define StringCchVPrintf StringCchVPrintfA
158 #define StringCchVPrintfEx StringCchVPrintfExA
160 #endif // (STRSAFE_UNICODE != 1)
161 #endif // defined(STRSAFE_UNICODE)
163 /*****************************************************************************/
165 #if defined (STRSAFE_PASS2)
167 #if defined(STRSAFE_CB)
169 #define STRSAFE_CXXtoCB(x) (x)
170 #define STRSAFE_CBtoCXX(x) (x)
171 #define STRSAFE_CXXtoCCH(x) (x)*sizeof(STRSAFE_TCHAR)
172 #define STRSAFE_CCHtoCXX(x) (x)/sizeof(STRSAFE_TCHAR)
173 #define StringCxxCat StringCbCat
174 #define StringCxxCatEx StringCbCatEx
175 #define StringCxxCatN StringCbCatN
176 #define StringCxxCatNEx StringCbCatNEx
177 #define StringCxxCatWorker StringCbCatWorker
178 #define StringCxxCopy StringCbCopy
179 #define StringCxxCopyEx StringCbCopyEx
180 #define StringCxxCopyN StringCbCopyN
181 #define StringCxxCopyNEx StringCbCopyNEx
182 #define StringCxxGets StringCbGets
183 #define StringCxxGetsEx StringCbGetsEx
184 #define StringCxxLength StringCbLength
185 #define StringCxxPrintf StringCbPrintf
186 #define StringCxxPrintfEx StringCbPrintfEx
187 #define StringCxxVPrintf StringCbVPrintf
188 #define StringCxxVPrintfEx StringCbVPrintfEx
192 #define STRSAFE_CXXtoCB(x) (x)/sizeof(STRSAFE_TCHAR)
193 #define STRSAFE_CBtoCXX(x) (x)*sizeof(STRSAFE_TCHAR)
194 #define STRSAFE_CXXtoCCH(x) (x)
195 #define STRSAFE_CCHtoCXX(x) (x)
196 #define StringCxxCat StringCchCat
197 #define StringCxxCatEx StringCchCatEx
198 #define StringCxxCatN StringCchCatN
199 #define StringCxxCatNEx StringCchCatNEx
200 #define StringCxxCatWorker StringCchCatWorker
201 #define StringCxxCopy StringCchCopy
202 #define StringCxxCopyEx StringCchCopyEx
203 #define StringCxxCopyN StringCchCopyN
204 #define StringCxxCopyNEx StringCchCopyNEx
205 #define StringCxxGets StringCchGets
206 #define StringCxxGetsEx StringCchGetsEx
207 #define StringCxxLength StringCchLength
208 #define StringCxxPrintf StringCchPrintf
209 #define StringCxxPrintfEx StringCchPrintfEx
210 #define StringCxxVPrintf StringCchVPrintf
211 #define StringCxxVPrintfEx StringCchVPrintfEx
213 #endif // !STRSAFE_CB
217 /* Normal function prototypes only */
218 #define STRSAFEAPI HRESULT __stdcall
220 STRSAFEAPI
StringCxxCat(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
);
221 STRSAFEAPI
StringCxxCatEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
);
222 STRSAFEAPI
StringCxxCatN(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbMaxAppend
);
223 STRSAFEAPI
StringCxxCatNEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbMaxAppend
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
);
224 STRSAFEAPI
StringCxxCopy(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
);
225 STRSAFEAPI
StringCxxCopyEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
);
226 STRSAFEAPI
StringCxxCopyN(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbSrc
);
227 STRSAFEAPI
StringCxxCopyNEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbSrc
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
);
228 STRSAFEAPI
StringCxxGets(STRSAFE_LPTSTR pszDest
, size_t cxDest
);
229 STRSAFEAPI
StringCxxGetsEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
);
230 STRSAFEAPI
StringCxxLength(STRSAFE_LPCTSTR psz
, size_t cxMax
, size_t *pcb
);
231 STRSAFEAPI
StringCxxPrintf(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszFormat
, ...);
232 STRSAFEAPI
StringCxxPrintfEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
, STRSAFE_LPCTSTR pszFormat
, ...);
233 STRSAFEAPI
StringCxxVPrintf(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszFormat
, va_list args
);
234 STRSAFEAPI
StringCxxVPrintfEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
, LPCTSTR pszFormat
, va_list args
);
236 #else // !STRSAFE_LIB
238 /* Create inlined versions */
239 #define STRSAFEAPI HRESULT static __inline__
241 STRSAFEAPI
StringCxxCatWorker(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, size_t cxMaxAppend
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
, int UseN
)
244 STRSAFE_LPTSTR psz
= pszDest
;
245 size_t cch
= STRSAFE_CXXtoCCH(cxDest
);
247 if (!pszDest
|| !pszSrc
|| cch
> STRSAFE_MAX_CCH
|| cch
== 0)
249 return STRSAFE_E_INVALID_PARAMETER
;
252 for (--psz
; *(++psz
) != 0 && --cch
> 0;);
255 return STRSAFE_E_INSUFFICIENT_BUFFER
;
260 cch
= STRSAFE_MIN(cxDest
, STRSAFE_CXXtoCCH(cxMaxAppend
));
263 for (--pszSrc
, --psz
; (*(++psz
) = *(++pszSrc
)) != 0 && --cch
> 0;);
266 result
= STRSAFE_E_INSUFFICIENT_BUFFER
;
278 *pcbRemaining
= STRSAFE_CCHtoCXX(cch
);
281 if (dwFlags
& STRSAFE_FILL_BEHIND_NULL
)
283 for (--psz
, ++cch
; --cch
; *(++psz
) = dwFlags
& 0xff);
286 if (!SUCCEEDED(result
))
288 if (dwFlags
& STRSAFE_FILL_ON_FAILURE
)
290 cch
= STRSAFE_CXXtoCCH(cxDest
);
291 for (--pszDest
, ++cch
; --cch
; *(++pszDest
) = dwFlags
& 0xff);
294 if (dwFlags
& STRSAFE_NULL_ON_FAILURE
)
303 STRSAFEAPI
StringCxxCatEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
)
305 return StringCxxCatWorker(pszDest
, cxDest
, pszSrc
, 0, ppszDestEnd
, pcbRemaining
, dwFlags
, 0);
308 STRSAFEAPI
StringCxxCat(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
)
310 return StringCxxCatWorker(pszDest
, cxDest
, pszSrc
, 0, NULL
, NULL
, 0, 0);
313 STRSAFEAPI
StringCxxCatN(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbMaxAppend
)
315 return StringCxxCatWorker(pszDest
, cxDest
, pszSrc
, cbMaxAppend
, NULL
, NULL
, 0, 1);
318 STRSAFEAPI
StringCxxCatNEx(STRSAFE_LPTSTR pszDest
, size_t cxDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbMaxAppend
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
)
320 return StringCxxCatWorker(pszDest
, cxDest
, pszSrc
, cbMaxAppend
, ppszDestEnd
, pcbRemaining
, dwFlags
, 1);
323 STRSAFEAPI
StringCxxCopy(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPCTSTR pszSrc
)
328 STRSAFEAPI
StringCxxCopyEx(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPCTSTR pszSrc
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
)
333 STRSAFEAPI
StringCxxCopyN(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbSrc
)
338 STRSAFEAPI
StringCxxCopyNEx(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPCTSTR pszSrc
, size_t cbSrc
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
)
343 STRSAFEAPI
StringCxxGets(STRSAFE_LPTSTR pszDest
, size_t cbDest
)
348 STRSAFEAPI
StringCxxGetsEx(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
)
353 STRSAFEAPI
StringCxxLength(STRSAFE_LPCTSTR psz
, size_t cxMax
, size_t *pcx
)
355 size_t cch
= STRSAFE_CXXtoCCH(cxMax
);
357 /* Default return on error */
361 if (!psz
|| cch
> STRSAFE_MAX_CCH
|| cch
== 0)
363 return STRSAFE_E_INVALID_PARAMETER
;
366 for (--psz
; *(++psz
) != 0 && --cch
> 0;);
370 return STRSAFE_E_INVALID_PARAMETER
;
374 *pcx
= cxMax
- STRSAFE_CCHtoCXX(cch
);
379 STRSAFEAPI
StringCxxVPrintf(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPCTSTR pszFormat
, va_list args
)
384 STRSAFEAPI
StringCxxVPrintfEx(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
, STRSAFE_LPCTSTR pszFormat
, va_list args
)
389 STRSAFEAPI
StringCxxPrintf(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPCTSTR pszFormat
, ...)
393 va_start(args
, pszFormat
);
394 result
= StringCxxVPrintf(pszDest
, cbDest
, pszFormat
, args
);
399 STRSAFEAPI
StringCxxPrintfEx(STRSAFE_LPTSTR pszDest
, size_t cbDest
, STRSAFE_LPTSTR
*ppszDestEnd
, size_t *pcbRemaining
, STRSAFE_DWORD dwFlags
, STRSAFE_LPCTSTR pszFormat
, ...)
403 va_start(args
, pszFormat
);
404 result
= StringCxxVPrintfEx(pszDest
, cbDest
, ppszDestEnd
, pcbRemaining
, dwFlags
, pszFormat
, args
);
409 #endif // !STRSAFE_LIB
411 /* Functions are implemented or defined, clear #defines for next pass */
413 #undef StringCxxCatEx
415 #undef StringCxxCatNEx
416 #undef StringCxxCatWorker
418 #undef StringCxxCopyEx
419 #undef StringCxxCopyN
420 #undef StringCxxCopyNEx
422 #undef StringCxxGetsEx
423 #undef StringCxxLength
424 #undef StringCxxPrintf
425 #undef StringCxxPrintfEx
426 #undef StringCxxVPrintf
427 #undef StringCxxVPrintfEx
432 #undef StringCbCatNEx
433 #undef StringCbCatWorker
435 #undef StringCbCopyEx
437 #undef StringCbCopyNEx
439 #undef StringCbGetsEx
440 #undef StringCbLength
441 #undef StringCbPrintf
442 #undef StringCbPrintfEx
443 #undef StringCbVPrintf
444 #undef StringCbVPrintfEx
446 #undef StringCchCatEx
448 #undef StringCchCatNEx
449 #undef StringCchCatWorker
451 #undef StringCchCopyEx
452 #undef StringCchCopyN
453 #undef StringCchCopyNEx
455 #undef StringCchGetsEx
456 #undef StringCchLength
457 #undef StringCchPrintf
458 #undef StringCchPrintfEx
459 #undef StringCchVPrintf
460 #undef StringCchVPrintfEx
462 #undef STRSAFE_LPTSTR
463 #undef STRSAFE_LPCTSTR
466 #undef STRSAFE_CXXtoCB
467 #undef STRSAFE_CBtoCXX
468 #undef STRSAFE_CXXtoCCH
469 #undef STRSAFE_CCHtoCXX
471 #endif // defined (STRSAFE_PASS2)