return EINVAL;
#endif
-/* MBCS handling: change these definitions if you do not need to support mbcs strings */
-#if !defined(_SAFECRT__ISMBBLEAD)
-#define _SAFECRT__ISMBBLEAD(_Character) \
- _ismbblead(_Character)
-#endif
-
-#if !defined(_SAFECRT__MBSDEC)
-#define _SAFECRT__MBSDEC(_String, _Current) \
- _mbsdec(_String, _Current)
-#endif
-
_SAFECRT__EXTERN_C
void __cdecl _invalid_parameter(const WCHAR *_Message, const WCHAR *_FunctionName, const WCHAR *_FileName, unsigned int _LineNumber, uintptr_t _Reserved);
#if _SAFECRT_DEFINE_TCS_MACROS
/* _tcs macros */
-#if !defined(_UNICODE) && !defined(UNICODE) && !defined(_MBCS)
+#if !defined(_UNICODE) && !defined(UNICODE)
#define _tcscpy_s strcpy_s
#define _tcsncpy_s strncpy_s
#define _tcsset_s _strset_s
#define _tcsnset_s _strnset_s
#define _tcstok_s strtok_s
-#define _tmakepath_s _makepath_s
-#define _tsplitpath_s _splitpath_s
-#define _stprintf_s sprintf_s
-#define _sntprintf_s _snprintf_s
#define _vsntprintf_s _vsnprintf_s
-#define _tscanf_s scanf_s
-#define _tsscanf_s sscanf_s
#elif defined(_UNICODE) || defined(UNICODE)
#define _tscanf_s wscanf_s
#define _tsscanf_s swscanf_s
-#elif defined(_MBCS)
-
-#define _tcscpy_s _mbscpy_s
-#define _tcsncpy_s _mbsnbcpy_s
-#define _tcscat_s _mbscat_s
-#define _tcsncat_s _mbsnbcat_s
-#define _tcsset_s _mbsset_s
-#define _tcsnset_s _mbsnbset_s
-#define _tcstok_s _mbstok_s
-#define _tmakepath_s _makepath_s
-#define _tsplitpath_s _splitpath_s
-#define _stprintf_s sprintf_s
-#define _sntprintf_s _snprintf_s
-#define _tscanf_s scanf_s
-#define _tsscanf_s sscanf_s
-
#else
#error We should not get here...
#endif
-/* _mbscpy_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbscpy_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbscpy_s(unsigned char (&_Dst)[_SizeInBytes], const unsigned char *_Src)
-{
- return _mbscpy_s(_Dst, _SizeInBytes, _Src);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbscpy_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src)
-{
- unsigned char *p;
- size_t available;
-
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
- _SAFECRT__VALIDATE_POINTER_RESET_STRING(_Src, _Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- while ((*p++ = *_Src++) != 0 && --available > 0)
- {
- }
-
- if (available == 0)
- {
- if (*_Src == 0 && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-1] = 0;
- return 0;
- }
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- }
- if (available < _SizeInBytes && _SAFECRT__ISMBBLEAD(p[-2]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-2] = 0;
- available++;
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- return 0;
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
/* strncpy_s */
/*
* strncpy_s, wcsncpy_s copy at max _Count characters from string _Src into _Dst;
#endif
-/* _mbsnbcpy_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbsnbcpy_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInBytes);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbsnbcpy_s(unsigned char (&_Dst)[_SizeInBytes], const unsigned char *_Src, size_t _CountInBytes)
-{
- return _mbsnbcpy_s(_Dst, _SizeInBytes, _Src, _CountInBytes);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbsnbcpy_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInBytes)
-{
- unsigned char *p;
- size_t available;
-
- if (_CountInBytes == 0 && _Dst == nullptr && _SizeInBytes == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
-
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
- if (_CountInBytes == 0)
- {
- /* notice that the source string pointer can be nullptr in this case */
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- return 0;
- }
- _SAFECRT__VALIDATE_POINTER_RESET_STRING(_Src, _Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- if (_CountInBytes == _TRUNCATE)
- {
- while ((*p++ = *_Src++) != 0 && --available > 0)
- {
- }
- }
- else
- {
- while ((*p++ = *_Src++) != 0 && --available > 0 && --_CountInBytes > 0)
- {
- }
- if (_CountInBytes == 0)
- {
- *p++ = 0;
- }
- }
-
- if (available == 0)
- {
- if ((*_Src == 0 || _CountInBytes == 1) && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-1] = 0;
- return 0;
- }
- if (_CountInBytes == _TRUNCATE)
- {
- if (_SizeInBytes > 1 && _SAFECRT__ISMBBLEAD(_Dst[_SizeInBytes - 2]))
- {
- _Dst[_SizeInBytes - 2] = 0;
- _SAFECRT__FILL_BYTE(_Dst[_SizeInBytes - 1]);
- }
- else
- {
- _Dst[_SizeInBytes - 1] = 0;
- }
- return STRUNCATE;
- }
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- }
- if (available < _SizeInBytes && _SAFECRT__ISMBBLEAD(p[-2]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-2] = 0;
- available++;
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- return 0;
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
-/* _mbsncpy_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbsncpy_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInChars);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbsncpy_s(unsigned char (&_Dst)[_SizeInBytes], const unsigned char *_Src, size_t _CountInChars)
-{
- return _mbsncpy_s(_Dst, _SizeInBytes, _Src, _CountInChars);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbsncpy_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInChars)
-{
- unsigned char *p;
- size_t available;
-
- if (_CountInChars == 0 && _Dst == nullptr && _SizeInBytes == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
-
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
- if (_CountInChars == 0)
- {
- /* notice that the source string pointer can be nullptr in this case */
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- return 0;
- }
- _SAFECRT__VALIDATE_POINTER_RESET_STRING(_Src, _Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- if (_CountInChars == _TRUNCATE)
- {
- while ((*p++ = *_Src++) != 0 && --available > 0)
- {
- }
- }
- else
- {
- do
- {
- if (_SAFECRT__ISMBBLEAD(*_Src))
- {
- if (_Src[1] == 0)
- {
- /* the source string ended with a lead byte: we remove it */
- *p = 0;
- break;
- }
- if (available <= 2)
- {
- /* not enough space */
- available = 0;
- break;
- }
- *p++ = *_Src++;
- *p++ = *_Src++;
- available -= 2;
- }
- else
- {
- if ((*p++ = *_Src++) == 0 || --available == 0)
- {
- break;
- }
- }
- }
- while (--_CountInChars > 0);
- if (_CountInChars == 0)
- {
- *p++ = 0;
- }
- }
-
- if (available == 0)
- {
- if (_CountInChars == _TRUNCATE)
- {
- if (_SizeInBytes > 1 && _SAFECRT__ISMBBLEAD(_Dst[_SizeInBytes - 2]))
- {
- _Dst[_SizeInBytes - 2] = 0;
- _SAFECRT__FILL_BYTE(_Dst[_SizeInBytes - 1]);
- }
- else
- {
- _Dst[_SizeInBytes - 1] = 0;
- }
- return STRUNCATE;
- }
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- return 0;
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
/* strcat_s */
/*
* strcat_s, wcscat_s append string _Src to _Dst;
#endif
-/* _mbscat_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbscat_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbscat_s(unsigned char (&_Dst)[_SizeInBytes], const unsigned char *_Src)
-{
- return _mbscat_s(_Dst, _SizeInBytes, _Src);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbscat_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src)
-{
- unsigned char *p;
- size_t available;
-
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
- _SAFECRT__VALIDATE_POINTER_RESET_STRING(_Src, _Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- while (available > 0 && *p != 0)
- {
- p++;
- available--;
- }
-
- if (available == 0)
- {
- if (*p == 0 && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the original string ended with a lead byte: we remove it */
- p--;
- *p = 0;
- available = 1;
- }
- else
- {
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
- }
- }
- if (available < _SizeInBytes && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the original string ended with a lead byte: we remove it */
- p--;
- *p = 0;
- available++;
- }
-
- while ((*p++ = *_Src++) != 0 && --available > 0)
- {
- }
-
- if (available == 0)
- {
- if (*_Src == 0 && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-1] = 0;
- return 0;
- }
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- }
- if (available < _SizeInBytes && _SAFECRT__ISMBBLEAD(p[-2]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-2] = 0;
- available++;
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- return 0;
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
/* strncat_s */
/*
* strncat_s, wcsncat_s append at max _Count characters from string _Src to _Dst;
#endif
-/* _mbsnbcat_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbsnbcat_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInBytes);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbsnbcat_s(unsigned char (&_Dst)[_SizeInBytes], const unsigned char *_Src, size_t _CountInBytes)
-{
- return _mbsnbcat_s(_Dst, _SizeInBytes, _Src, size_t _CountInBytes);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbsnbcat_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInBytes)
-{
- unsigned char *p;
- size_t available;
- if (_CountInBytes == 0 && _Dst == nullptr && _SizeInBytes == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
- if (_CountInBytes != 0)
- {
- _SAFECRT__VALIDATE_POINTER_RESET_STRING(_Src, _Dst, _SizeInBytes);
- }
-
- p = _Dst;
- available = _SizeInBytes;
- while (available > 0 && *p != 0)
- {
- p++;
- available--;
- }
-
- if (available == 0)
- {
- if (*p == 0 && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the original string ended with a lead byte: we remove it */
- p--;
- *p = 0;
- available = 1;
- }
- else
- {
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
- }
- }
- if (available < _SizeInBytes && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the original string ended with a lead byte: we remove it */
- p--;
- *p = 0;
- available++;
- }
-
- if (_CountInBytes == _TRUNCATE)
- {
- while ((*p++ = *_Src++) != 0 && --available > 0)
- {
- }
- }
- else
- {
- while (_CountInBytes > 0 && (*p++ = *_Src++) != 0 && --available > 0)
- {
- _CountInBytes--;
- }
- if (_CountInBytes == 0)
- {
- *p++ = 0;
- }
- }
-
- if (available == 0)
- {
- if ((*_Src == 0 || _CountInBytes == 1) && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-1] = 0;
- return 0;
- }
- if (_CountInBytes == _TRUNCATE)
- {
- if (_SizeInBytes > 1 && _SAFECRT__ISMBBLEAD(_Dst[_SizeInBytes - 2]))
- {
- _Dst[_SizeInBytes - 2] = 0;
- _SAFECRT__FILL_BYTE(_Dst[_SizeInBytes - 1]);
- }
- else
- {
- _Dst[_SizeInBytes - 1] = 0;
- }
- return STRUNCATE;
- }
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- }
- if (available < _SizeInBytes && _SAFECRT__ISMBBLEAD(p[-2]))
- {
- /* the source string ended with a lead byte: we remove it */
- p[-2] = 0;
- available++;
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- return 0;
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
-/* _mbsncat_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbsncat_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInChars);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbsncat_s(unsigned char (&_Dst)[_SizeInBytes], const unsigned char *_Src, size_t _CountInChars)
-{
- return _mbsncat_s(_Dst, _SizeInBytes, _Src, size_t _CountInChars);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbsncat_s(unsigned char *_Dst, size_t _SizeInBytes, const unsigned char *_Src, size_t _CountInChars)
-{
- unsigned char *p;
- size_t available;
- if (_CountInChars == 0 && _Dst == nullptr && _SizeInBytes == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
- if (_CountInChars != 0)
- {
- _SAFECRT__VALIDATE_POINTER_RESET_STRING(_Src, _Dst, _SizeInBytes);
- }
-
- p = _Dst;
- available = _SizeInBytes;
- while (available > 0 && *p != 0)
- {
- p++;
- available--;
- }
-
- if (available == 0)
- {
- if (*p == 0 && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the original string ended with a lead byte: we remove it */
- p--;
- *p = 0;
- available = 1;
- }
- else
- {
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
- }
- }
- if (available < _SizeInBytes && _SAFECRT__ISMBBLEAD(p[-1]))
- {
- /* the original string ended with a lead byte: we remove it */
- p--;
- *p = 0;
- available++;
- }
-
- if (_CountInChars == _TRUNCATE)
- {
- while ((*p++ = *_Src++) != 0 && --available > 0)
- {
- }
- }
- else
- {
- while (_CountInChars > 0)
- {
- if (_SAFECRT__ISMBBLEAD(*_Src))
- {
- if (_Src[1] == 0)
- {
- /* the source string ended with a lead byte: we remove it */
- *p = 0;
- break;
- }
- if (available <= 2)
- {
- /* not enough space */
- available = 0;
- break;
- }
- *p++ = *_Src++;
- *p++ = *_Src++;
- available -= 2;
- }
- else
- {
- if ((*p++ = *_Src++) == 0 || --available == 0)
- {
- break;
- }
- }
- _CountInChars--;
- }
- if (_CountInChars == 0)
- {
- *p++ = 0;
- }
- }
-
- if (available == 0)
- {
- if (_CountInChars == _TRUNCATE)
- {
- if (_SizeInBytes > 1 && _SAFECRT__ISMBBLEAD(_Dst[_SizeInBytes - 2]))
- {
- _Dst[_SizeInBytes - 2] = 0;
- _SAFECRT__FILL_BYTE(_Dst[_SizeInBytes - 1]);
- }
- else
- {
- _Dst[_SizeInBytes - 1] = 0;
- }
- return STRUNCATE;
- }
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- return 0;
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
/* _strset_s */
/*
#endif
-/* _mbsset_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbsset_s(unsigned char *_Dst, size_t _SizeInBytes, unsigned int _Value);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbsset_s(unsigned char (&_Dst)[_SizeInBytes], unsigned int _Value)
-{
- return _mbsset_s(_Dst, _SizeInBytes, _Value);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbsset_s(unsigned char *_Dst, size_t _SizeInBytes, unsigned int _Value)
-{
- int mbcs_error = 0;
- unsigned char *p;
- size_t available;
- unsigned char highval, lowval;
-
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- highval = (unsigned char)(_Value >> 8);
- lowval = (unsigned char)(_Value & 0x00ff);
- if (highval != 0)
- {
- if (_SAFECRT__ISMBBLEAD(highval) && lowval != 0)
- {
- while (*p != 0 && --available > 0)
- {
- if (p[1] == 0)
- {
- /* do not orphan leadbyte */
- *p++ = ' ';
- break;
- }
- *p++ = highval;
- if (--available == 0)
- {
- break;
- }
- *p++ = lowval;
- }
- }
- else
- {
- mbcs_error = 1;
- highval = 0;
- lowval = ' ';
- }
- }
- else
- {
- if (_SAFECRT__ISMBBLEAD(lowval))
- {
- mbcs_error = 1;
- lowval = ' ';
- }
- }
- if (highval == 0)
- {
- while (*p != 0 && --available > 0)
- {
- *p++ = lowval;
- }
- }
-
- if (available == 0)
- {
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- if (mbcs_error)
- {
- _SAFECRT__SET_ERRNO(EILSEQ); return EILSEQ;
- }
- else
- {
- return 0;
- }
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
/* _strnset_s */
/*
* _strnset_s, _wcsnset_s ;
#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
_SAFECRT__INLINE
-errno_t __cdecl _strnset_s(char *_Dst, size_t _SizeInBytes, int _Value, size_t _Count)
-{
- char *p;
- size_t available;
-
- /* validation section */
- if (_Count == 0 && _Dst == nullptr && _SizeInBytes == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- while (*p != 0 && _Count > 0 && --available > 0)
- {
- *p++ = (char)_Value;
- --_Count;
- }
-
- if (available == 0)
- {
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
- }
- if (_Count == 0)
- {
- *p = 0;
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- return 0;
-}
-
-#endif
-
-/* _wcsnset_s */
-_SAFECRT__EXTERN_C
-errno_t __cdecl _wcsnset_s(WCHAR *_Dst, size_t _SizeInWords, WCHAR _Value, size_t _Count);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInWords>
-inline
-errno_t __cdecl _wcsnset_s(WCHAR (&_Dst)[_SizeInWords], WCHAR _Value, size_t _Count)
-{
- return _wcsnset_s(_Dst, _SizeInWords, _Value, _Count);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _wcsnset_s(WCHAR *_Dst, size_t _SizeInWords, WCHAR _Value, size_t _Count)
-{
- WCHAR *p;
- size_t available;
-
- /* validation section */
- if (_Count == 0 && _Dst == nullptr && _SizeInWords == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInWords);
-
- p = _Dst;
- available = _SizeInWords;
- while (*p != 0 && _Count > 0 && --available > 0)
- {
- *p++ = (WCHAR)_Value;
- --_Count;
- }
-
- if (available == 0)
- {
- _SAFECRT__RESET_STRING(_Dst, _SizeInWords);
- _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInWords);
- }
- if (_Count == 0)
- {
- *p = 0;
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInWords, _SizeInWords - available + 1);
- return 0;
-}
-
-#endif
-
-/* _mbsnbset_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbsnbset_s(unsigned char *_Dst, size_t _SizeInBytes, unsigned int _Value, size_t _CountInBytes);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbsnbset_s(unsigned char (&_Dst)[_SizeInBytes], unsigned int _Value, size_t _CountInBytes)
-{
- return _mbsnbset_s(_Dst, _SizeInBytes, _Value, _CountInBytes);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbsnbset_s(unsigned char *_Dst, size_t _SizeInBytes, unsigned int _Value, size_t _CountInBytes)
-{
- int mbcs_error = 0;
- unsigned char *p;
- size_t available;
- unsigned char highval, lowval;
-
- /* validation section */
- if (_CountInBytes == 0 && _Dst == nullptr && _SizeInBytes == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- highval = (unsigned char)(_Value >> 8);
- lowval = (unsigned char)(_Value & 0x00ff);
- if (highval != 0)
- {
- if (_SAFECRT__ISMBBLEAD(highval) && lowval != 0)
- {
- while (*p != 0 && _CountInBytes > 0 && --available > 0)
- {
- if (_CountInBytes == 1 || p[1] == 0)
- {
- /* do not orphan leadbyte */
- *p++ = ' ';
- --_CountInBytes;
- break;
- }
- *p++ = highval;
- if (--available == 0)
- {
- break;
- }
- *p++ = lowval;
- _CountInBytes -= 2;
- }
- }
- else
- {
- mbcs_error = 1;
- highval = 0;
- lowval = ' ';
- }
- }
- else
- {
- if (_SAFECRT__ISMBBLEAD(lowval))
- {
- mbcs_error = 1;
- lowval = ' ';
- }
- }
- if (highval == 0)
- {
- while (*p != 0 && available > 0 && _CountInBytes > 0)
- {
- *p++ = lowval;
- --available;
- --_CountInBytes;
- }
- }
- if (available == 0)
- {
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
- }
- if (_CountInBytes == 0)
- {
- *p = 0;
- }
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- if (mbcs_error)
- {
- _SAFECRT__SET_ERRNO(EILSEQ); return EILSEQ;
- }
- else
- {
- return 0;
- }
-}
-
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
-/* _mbsnset_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-errno_t __cdecl _mbsnset_s(unsigned char *_Dst, size_t _SizeInBytes, unsigned int _Value, size_t _CountInChars);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _mbsnset_s(unsigned char (&_Dst)[_SizeInBytes], unsigned int _Value, size_t _CountInChars)
-{
- return _mbsnset_s(_Dst, _SizeInBytes, _Value, _CountInChars);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _mbsnset_s(unsigned char *_Dst, size_t _SizeInBytes, unsigned int _Value, size_t _CountInChars)
-{
- int mbcs_error = 0;
- unsigned char *p;
- size_t available;
- unsigned char highval, lowval;
-
- /* validation section */
- if (_CountInChars == 0 && _Dst == nullptr && _SizeInBytes == 0)
- {
- /* this case is allowed; nothing to do */
- return 0;
- }
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
-
- p = _Dst;
- available = _SizeInBytes;
- highval = (unsigned char)(_Value >> 8);
- lowval = (unsigned char)(_Value & 0x00ff);
- if (highval != 0)
- {
- if (_SAFECRT__ISMBBLEAD(highval) && lowval != 0)
- {
- while (*p != 0 && _CountInChars > 0 && --available > 0)
- {
- if (p[1] == 0)
- {
- /* do not orphan leadbyte */
- *p++ = ' ';
- break;
- }
- *p++ = highval;
- if (--available == 0)
- {
- break;
- }
- *p++ = lowval;
- --_CountInChars;
- }
- }
- else
- {
- mbcs_error = 1;
- highval = 0;
- lowval = ' ';
- }
- }
- else
+errno_t __cdecl _strnset_s(char *_Dst, size_t _SizeInBytes, int _Value, size_t _Count)
+{
+ char *p;
+ size_t available;
+
+ /* validation section */
+ if (_Count == 0 && _Dst == nullptr && _SizeInBytes == 0)
{
- if (_SAFECRT__ISMBBLEAD(lowval))
- {
- mbcs_error = 1;
- lowval = ' ';
- }
+ /* this case is allowed; nothing to do */
+ return 0;
}
- if (highval == 0)
+ _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
+
+ p = _Dst;
+ available = _SizeInBytes;
+ while (*p != 0 && _Count > 0 && --available > 0)
{
- while (*p != 0 && available > 0 && _CountInChars > 0)
- {
- *p++ = lowval;
- --available;
- --_CountInChars;
- }
+ *p++ = (char)_Value;
+ --_Count;
}
+
if (available == 0)
{
_SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
_SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInBytes);
}
- if (_CountInChars == 0)
+ if (_Count == 0)
{
*p = 0;
}
_SAFECRT__FILL_STRING(_Dst, _SizeInBytes, _SizeInBytes - available + 1);
- if (mbcs_error)
- {
- _SAFECRT__SET_ERRNO(EILSEQ); return EILSEQ;
- }
- else
- {
- return 0;
- }
+ return 0;
}
#endif
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
-/* _mbccpy_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
+/* _wcsnset_s */
_SAFECRT__EXTERN_C
-errno_t __cdecl _mbccpy_s(unsigned char *_Dst, size_t _SizeInBytes, int *_PCopied, const unsigned char *_Src);
+errno_t __cdecl _wcsnset_s(WCHAR *_Dst, size_t _SizeInWords, WCHAR _Value, size_t _Count);
#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
+template <size_t _SizeInWords>
inline
-errno_t __cdecl _mbccpy_s(unsigned char (&_Dst)[_SizeInBytes], int *_PCopied, const unsigned char *_Src)
+errno_t __cdecl _wcsnset_s(WCHAR (&_Dst)[_SizeInWords], WCHAR _Value, size_t _Count)
{
- return _mbccpy_s(_Dst, _SizeInBytes, _PCopied, _Src);
+ return _wcsnset_s(_Dst, _SizeInWords, _Value, _Count);
}
#endif
#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
_SAFECRT__INLINE
-errno_t __cdecl _mbccpy_s(unsigned char *_Dst, size_t _SizeInBytes, int *_PCopied, const unsigned char *_Src)
+errno_t __cdecl _wcsnset_s(WCHAR *_Dst, size_t _SizeInWords, WCHAR _Value, size_t _Count)
{
+ WCHAR *p;
+ size_t available;
+
/* validation section */
- if (_PCopied != nullptr) { *_PCopied = 0; };
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
- if (_Src == nullptr)
+ if (_Count == 0 && _Dst == nullptr && _SizeInWords == 0)
{
- *_Dst = '\0';
- _SAFECRT__RETURN_EINVAL;
+ /* this case is allowed; nothing to do */
+ return 0;
}
+ _SAFECRT__VALIDATE_STRING(_Dst, _SizeInWords);
- /* copy */
- if (_SAFECRT__ISMBBLEAD(*_Src))
+ p = _Dst;
+ available = _SizeInWords;
+ while (*p != 0 && _Count > 0 && --available > 0)
{
- if (_Src[1] == '\0')
- {
- /* the source string contained a lead byte followed by the null terminator:
- we copy only the null terminator and return EILSEQ to indicate the
- malformed char */
- *_Dst = '\0';
- if (_PCopied != nullptr) { *_PCopied = 1; };
- _SAFECRT__SET_ERRNO(EILSEQ); return EILSEQ;
- }
- if (_SizeInBytes < 2)
- {
- *_Dst = '\0';
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- }
- *_Dst++ = *_Src++;
- *_Dst = *_Src;
- if (_PCopied != nullptr) { *_PCopied = 2; };
+ *p++ = (WCHAR)_Value;
+ --_Count;
}
- else
+
+ if (available == 0)
{
- *_Dst = *_Src;
- if (_PCopied != nullptr) { *_PCopied = 1; };
+ _SAFECRT__RESET_STRING(_Dst, _SizeInWords);
+ _SAFECRT__RETURN_DEST_NOT_NULL_TERMINATED(_Dst, _SizeInWords);
}
-
+ if (_Count == 0)
+ {
+ *p = 0;
+ }
+ _SAFECRT__FILL_STRING(_Dst, _SizeInWords, _SizeInWords - available + 1);
return 0;
}
-#endif
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
+#endif
/* strtok_s */
/*
}
#endif
-/* _mbstok_s */
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-
-_SAFECRT__EXTERN_C
-unsigned char * __cdecl _mbstok_s(unsigned char *_String, const unsigned char *_Control, unsigned char **_Context);
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-unsigned char * __cdecl _mbstok_s(unsigned char *_String, const unsigned char *_Control, unsigned char **_Context)
-{
- unsigned char *token;
- const unsigned char *ctl;
- int dbc;
-
- /* validation section */
- _SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Context, EINVAL, nullptr);
- _SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Control, EINVAL, nullptr);
- _SAFECRT__VALIDATE_CONDITION_ERROR_RETURN(_String != nullptr || *_Context != nullptr, EINVAL, nullptr);
-
- /* If string==nullptr, continue with previous string */
- if (!_String)
- {
- _String = *_Context;
- }
-
- /* Find beginning of token (skip over leading delimiters). Note that
- * there is no token iff this loop sets string to point to the terminal null. */
- for ( ; *_String != 0; _String++)
- {
- for (ctl = _Control; *ctl != 0; ctl++)
- {
- if (_SAFECRT__ISMBBLEAD(*ctl))
- {
- if (*ctl == *_String && (ctl[1] == 0 || ctl[1] == _String[1]))
- {
- break;
- }
- ctl++;
- }
- else
- {
- if (*ctl == *_String)
- {
- break;
- }
- }
- }
- if (*ctl == 0)
- {
- break;
- }
- if (_SAFECRT__ISMBBLEAD(*_String))
- {
- _String++;
- if (*_String == 0)
- {
- break;
- }
- }
- }
-
- token = _String;
-
- /* Find the end of the token. If it is not the end of the string,
- * put a null there. */
- for ( ; *_String != 0; _String++)
- {
- for (ctl = _Control, dbc = 0; *ctl != 0; ctl++)
- {
- if (_SAFECRT__ISMBBLEAD(*ctl))
- {
- if (*ctl == *_String && (ctl[1] == 0 || ctl[1] == _String[1]))
- {
- dbc = 1;
- break;
- }
- ctl++;
- }
- else
- {
- if (*ctl == *_String)
- {
- break;
- }
- }
- }
- if (*ctl != 0)
- {
- *_String++ = 0;
- if (dbc && ctl[1] != 0)
- {
- *_String++ = 0;
- }
- break;
- }
- if (_SAFECRT__ISMBBLEAD(*_String))
- {
- _String++;
- if (*_String == 0)
- {
- break;
- }
- }
- }
-
- /* Update the context */
- *_Context = _String;
-
- /* Determine if a token has been found. */
- if (token == _String)
- {
- return nullptr;
- }
- else
- {
- return token;
- }
-}
-#endif
-
-#endif /* _SAFECRT_DEFINE_MBS_FUNCTIONS */
-
#ifndef PAL_STDCPP_COMPAT
/* strnlen */
/*
#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-_SAFECRT__INLINE
-size_t __cdecl wcsnlen(const WCHAR *inString, size_t inMaxSize)
-{
- size_t n;
-
- /* Note that we do not check if s == nullptr, because we do not
- * return errno_t...
- */
-
- for (n = 0; n < inMaxSize && *inString; n++, inString++)
- ;
-
- return n;
-}
-
-#endif
-#endif // PAL_STDCPP_COMPAT
-
-/* _makepath_s */
-/*
- * _makepath_s, _wmakepath_s build up a path starting from the specified components;
- * will call _SAFECRT_INVALID_PARAMETER if there is not enough space in _Dst;
- * any of _Drive, _Dir, _Filename and _Ext can be nullptr
- */
-_SAFECRT__EXTERN_C
-errno_t __cdecl _makepath_s(char *_Dst, size_t _SizeInBytes, const char *_Drive, const char *_Dir, const char *_Filename, const char *_Ext);
-
-#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
-template <size_t _SizeInBytes>
-inline
-errno_t __cdecl _makepath_s(char (&_Dst)[_SizeInBytes], const char *_Drive, const char *_Dir, const char *_Filename, const char *_Ext)
-{
- return _makepath_s(_Dst, _SizeInBytes, _Drive, _Dir, _Filename, _Ext);
-}
-#endif
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _makepath_s(char *_Dst, size_t _SizeInBytes, const char *_Drive, const char *_Dir, const char *_Filename, const char *_Ext)
-{
- size_t written;
- const char *p;
- char *d;
-
- /* validation section */
- _SAFECRT__VALIDATE_STRING(_Dst, _SizeInBytes);
-
- /* copy drive */
- written = 0;
- d = _Dst;
- if (_Drive != nullptr && *_Drive != 0)
- {
- written += 2;
- if(written >= _SizeInBytes)
- {
- goto error_return;
- }
- *d++ = *_Drive;
- *d++ = ':';
- }
-
- /* copy dir */
- p = _Dir;
- if (p != nullptr && *p != 0)
- {
- do {
- if(++written >= _SizeInBytes)
- {
- goto error_return;
- }
- *d++ = *p++;
- } while (*p != 0);
-
- p = (const char *)_SAFECRT__MBSDEC((const unsigned char *)_Dir, (const unsigned char *)p);
- if (*p != '/' && *p != '\\')
- {
- if(++written >= _SizeInBytes)
- {
- goto error_return;
- }
- *d++ = '\\';
- }
- }
-
- /* copy fname */
- p = _Filename;
- if (p != nullptr)
- {
- while (*p != 0)
- {
- if(++written >= _SizeInBytes)
- {
- goto error_return;
- }
- *d++ = *p++;
- }
- }
-
- /* copy extension; check to see if a '.' needs to be inserted */
- p = _Ext;
- if (p != nullptr)
- {
- if (*p != 0 && *p != '.')
- {
- if(++written >= _SizeInBytes)
- {
- goto error_return;
- }
- *d++ = '.';
- }
- while (*p != 0)
- {
- if(++written >= _SizeInBytes)
- {
- goto error_return;
- }
- *d++ = *p++;
- }
- }
+_SAFECRT__INLINE
+size_t __cdecl wcsnlen(const WCHAR *inString, size_t inMaxSize)
+{
+ size_t n;
- if(++written > _SizeInBytes)
- {
- goto error_return;
- }
- *d = 0;
- _SAFECRT__FILL_STRING(_Dst, _SizeInBytes, written);
- return 0;
+ /* Note that we do not check if s == nullptr, because we do not
+ * return errno_t...
+ */
-error_return:
- _SAFECRT__RESET_STRING(_Dst, _SizeInBytes);
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Dst, _SizeInBytes);
- /* should never happen, but compiler can't tell */
- return EINVAL;
+ for (n = 0; n < inMaxSize && *inString; n++, inString++)
+ ;
+
+ return n;
}
+
#endif
+#endif // PAL_STDCPP_COMPAT
/* _wmakepath_s */
_SAFECRT__EXTERN_C
}
#endif
-/* _splitpath_s */
-/*
- * _splitpath_s, _wsplitpath_s decompose a path into the specified components;
- * will call _SAFECRT_INVALID_PARAMETER if there is not enough space in
- * any of _Drive, _Dir, _Filename and _Ext;
- * any of _Drive, _Dir, _Filename and _Ext can be nullptr, but the correspondent size must
- * be set to 0, e.g. (_Drive == nullptr && _DriveSize == 0) is allowed, but
- * (_Drive == nullptr && _DriveSize != 0) is considered an invalid parameter
- */
-_SAFECRT__EXTERN_C
-errno_t __cdecl _splitpath_s(
- const char *_Path,
- char *_Drive, size_t _DriveSize,
- char *_Dir, size_t _DirSize,
- char *_Filename, size_t _FilenameSize,
- char *_Ext, size_t _ExtSize
-);
-
-/* no C++ overload for _splitpath_s */
-
-#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL
-
-_SAFECRT__INLINE
-errno_t __cdecl _splitpath_s(
- const char *_Path,
- char *_Drive, size_t _DriveSize,
- char *_Dir, size_t _DirSize,
- char *_Filename, size_t _FilenameSize,
- char *_Ext, size_t _ExtSize
-)
-{
- const char *tmp;
- const char *last_slash;
- const char *dot;
- int drive_set = 0;
- size_t length = 0;
- int bEinval = 0;
-
- /* validation section */
- _SAFECRT__VALIDATE_POINTER(_Path);
- if ((_Drive == nullptr && _DriveSize != 0) || (_Drive != nullptr && _DriveSize == 0))
- {
- goto error_einval;
- }
- if ((_Dir == nullptr && _DirSize != 0) || (_Dir != nullptr && _DirSize == 0))
- {
- goto error_einval;
- }
- if ((_Filename == nullptr && _FilenameSize != 0) || (_Filename != nullptr && _FilenameSize == 0))
- {
- goto error_einval;
- }
- if ((_Ext == nullptr && _ExtSize != 0) || (_Ext != nullptr && _ExtSize == 0))
- {
- goto error_einval;
- }
-
- /* check if _Path begins with the longpath prefix */
- if (_Path[0] == '\\' && _Path[1] == '\\' && _Path[2] == '?' && _Path[3] == '\\')
- {
- _Path += 4;
- }
-
- /* extract drive letter and ':', if any */
- if (!drive_set)
- {
- size_t skip = _MAX_DRIVE - 2;
- tmp = _Path;
- while (skip > 0 && *tmp != 0)
- {
- skip--;
- tmp++;
- }
- if (*tmp == ':')
- {
- if (_Drive != nullptr)
- {
- if (_DriveSize < _MAX_DRIVE)
- {
- goto error_erange;
- }
- strncpy_s(_Drive, _DriveSize, _Path, _MAX_DRIVE - 1);
- }
- _Path = tmp + 1;
- }
- else
- {
- if (_Drive != nullptr)
- {
- _SAFECRT__RESET_STRING(_Drive, _DriveSize);
- }
- }
- }
-
- /* extract path string, if any. _Path now points to the first character
- * of the path, if any, or the filename or extension, if no path was
- * specified. Scan ahead for the last occurence, if any, of a '/' or
- * '\' path separator character. If none is found, there is no path.
- * We will also note the last '.' character found, if any, to aid in
- * handling the extension.
- */
- last_slash = nullptr;
- dot = nullptr;
- tmp = _Path;
- for (; *tmp != 0; ++tmp)
- {
-#if _SAFECRT_DEFINE_MBS_FUNCTIONS
-#pragma warning(push)
-#pragma warning(disable:4127)
- if (_SAFECRT__ISMBBLEAD(*tmp))
-#pragma warning(pop)
-#else
- if (0)
-#endif
- {
- tmp++;
- }
- else
- {
- if (*tmp == '/' || *tmp == '\\')
- {
- /* point to one beyond for later copy */
- last_slash = tmp + 1;
- }
- else if (*tmp == '.')
- {
- dot = tmp;
- }
- }
- }
-
- if (last_slash != nullptr)
- {
- /* found a path - copy up through last_slash or max characters
- * allowed, whichever is smaller
- */
- if (_Dir != nullptr) {
- length = (size_t)(last_slash - _Path);
- if (_DirSize <= length)
- {
- goto error_erange;
- }
- strncpy_s(_Dir, _DirSize, _Path, length);
- }
- _Path = last_slash;
- }
- else
- {
- /* there is no path */
- if (_Dir != nullptr)
- {
- _SAFECRT__RESET_STRING(_Dir, _DirSize);
- }
- }
-
- /* extract file name and extension, if any. Path now points to the
- * first character of the file name, if any, or the extension if no
- * file name was given. Dot points to the '.' beginning the extension,
- * if any.
- */
- if (dot != nullptr && (dot >= _Path))
- {
- /* found the marker for an extension - copy the file name up to the '.' */
- if (_Filename)
- {
- length = (size_t)(dot - _Path);
- if (_FilenameSize <= length)
- {
- goto error_erange;
- }
- strncpy_s(_Filename, _FilenameSize, _Path, length);
- }
- /* now we can get the extension - remember that tmp still points
- * to the terminating nullptr character of path.
- */
- if (_Ext)
- {
- length = (size_t)(tmp - dot);
- if (_ExtSize <= length)
- {
- goto error_erange;
- }
- strncpy_s(_Ext, _ExtSize, dot, length);
- }
- }
- else
- {
- /* found no extension, give empty extension and copy rest of
- * string into fname.
- */
- if (_Filename)
- {
- length = (size_t)(tmp - _Path);
- if (_FilenameSize <= length)
- {
- goto error_erange;
- }
- strncpy_s(_Filename, _FilenameSize, _Path, length);
- }
- if (_Ext)
- {
- _SAFECRT__RESET_STRING(_Ext, _ExtSize);
- }
- }
-
- return 0;
-
-error_einval:
- bEinval = 1;
-
-error_erange:
- if (_Drive != nullptr && _DriveSize > 0)
- {
- _SAFECRT__RESET_STRING(_Drive, _DriveSize);
- }
- if (_Dir != nullptr && _DirSize > 0)
- {
- _SAFECRT__RESET_STRING(_Dir, _DirSize);
- }
- if (_Filename != nullptr && _FilenameSize > 0)
- {
- _SAFECRT__RESET_STRING(_Filename, _FilenameSize);
- }
- if (_Ext != nullptr && _ExtSize > 0)
- {
- _SAFECRT__RESET_STRING(_Ext, _ExtSize);
- }
-
- if (bEinval)
- {
- _SAFECRT__RETURN_EINVAL;
- }
-
- _SAFECRT__RETURN_BUFFER_TOO_SMALL(_Strings, _StringSizes);
- /* should never happen, but compiler can't tell */
- return EINVAL;
-}
-#endif
-
/* _wsplitpath_s */
_SAFECRT__EXTERN_C
errno_t __cdecl _wsplitpath_s(
}
#endif
-/* sprintf_s, vsprintf_s */
+/* vsprintf_s */
/*
- * sprintf_s, swprintf_s, vsprintf_s, vswprintf_s format a string and copy it into _Dst;
+ * swprintf_s, vsprintf_s, vswprintf_s format a string and copy it into _Dst;
* need safecrt.lib and msvcrt.dll;
* will call _SAFECRT_INVALID_PARAMETER if there is not enough space in _Dst;
* will call _SAFECRT_INVALID_PARAMETER if the format string is malformed;
* cannot be used without safecrt.lib
*/
_SAFECRT__EXTERN_C
-int __cdecl sprintf_s(char *_Dst, size_t _SizeInBytes, const char *_Format, ...);
-_SAFECRT__EXTERN_C
int __cdecl vsprintf_s(char *_Dst, size_t _SizeInBytes, const char *_Format, va_list _ArgList);
#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
template <size_t _SizeInBytes>
inline
-int __cdecl sprintf_s(char (&_Dst)[_SizeInBytes], const char *_Format, ...)
-{
- int ret;
- va_list _ArgList;
- va_start(_ArgList, _Format);
- ret = vsprintf_s(_Dst, _SizeInBytes, _Format, _ArgList);
- va_end(_ArgList);
- return ret;
-}
-
-template <size_t _SizeInBytes>
-inline
int __cdecl vsprintf_s(char (&_Dst)[_SizeInBytes], const char *_Format, va_list _ArgList)
{
return vsprintf_s(_Dst, _SizeInBytes, _Format, _ArgList);
}
#endif
-/* no inline version of sprintf_s, vsprintf_s */
+/* no inline version of vsprintf_s */
/* swprintf_s, vswprintf_s */
_SAFECRT__EXTERN_C
/* no inline version of swprintf_s, vswprintf_s */
-/* _snprintf_s, _vsnprintf_s */
+/* _vsnprintf_s */
/*
- * _snprintf_s, _snwprintf_s, _vsnprintf_s, _vsnwprintf_s format a string and copy at max _Count characters into _Dst;
+ * _snwprintf_s, _vsnprintf_s, _vsnwprintf_s format a string and copy at max _Count characters into _Dst;
* need safecrt.lib and msvcrt.dll;
* string _Dst will always be null-terminated;
* will call _SAFECRT_INVALID_PARAMETER if there is not enough space in _Dst;
#if defined(__cplusplus) && _SAFECRT_USE_CPP_OVERLOADS
template <size_t _SizeInBytes>
inline
-int __cdecl _snprintf_s(char (&_Dst)[_SizeInBytes], size_t _Count, const char *_Format, ...)
-{
- int ret;
- va_list _ArgList;
- va_start(_ArgList, _Format);
- ret = _vsnprintf_s(_Dst, _SizeInBytes, _Count, _Format, _ArgList);
- va_end(_ArgList);
- return ret;
-}
-
-template <size_t _SizeInBytes>
-inline
int __cdecl _vsnprintf_s(char (&_Dst)[_SizeInBytes], size_t _Count, const char *_Format, va_list _ArgList)
{
return _vsnprintf_s(_Dst, _SizeInBytes, _Count, _Format, _ArgList);
}
#endif
-/* no inline version of _snprintf_s, _vsnprintf_s */
+/* no inline version of _vsnprintf_s */
/* _snwprintf_s, _vsnwprintf_s */
_SAFECRT__EXTERN_C
/* no inline version of _snwprintf_s, _vsnwprintf_s */
-/* scanf_s */
-/*
- * read formatted data from the standard input stream;
- * need safecrt.lib and msvcrt.dll;
- * will call _SAFECRT_INVALID_PARAMETER if the format string is malformed;
- * for format types %s, %S, %[, %c and %C, in the argument list the buffer pointer
- * need to be followed by the size of the buffer, e.g.:
- * #define BUFFSIZE 100
- * char buff[BUFFSIZE];
- * scanf_s("%s", buff, BUFFSIZE);
- * as scanf, returns the number of fields successfully converted and assigned;
- * if a buffer field is too small, scanf set the buffer to the empty string and returns.
- * do not support floating-point, for now
- */
-_SAFECRT__EXTERN_C
-int __cdecl scanf_s(const char *_Format, ...);
-
-/* no C++ overload for scanf_s */
-
-/* no inline version of scanf_s */
-
/* wscanf_s */
_SAFECRT__EXTERN_C
int __cdecl wscanf_s(const WCHAR *_Format, ...);
/* no inline version of wscanf_s */
-/* sscanf_s */
-_SAFECRT__EXTERN_C
-int __cdecl sscanf_s(const char *_String, const char *_Format, ...);
-
-/* no C++ overload for sscanf_s */
-
-/* no inline version of sscanf_s */
-
/* swscanf_s */
_SAFECRT__EXTERN_C
int __cdecl swscanf_s(const WCHAR *_String, const WCHAR *_Format, ...);