From f4e620080580367907a984e368eaa360348b6d16 Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Sat, 16 Mar 2013 13:40:28 +0000 Subject: [PATCH] [CRT] Implement [v]s[nw]printf_s [MSVCRT] Enable [v]s[nw]printf_s exports svn path=/trunk/; revision=58515 --- reactos/dll/win32/msvcrt/msvcrt.spec | 16 +++--- reactos/lib/sdk/crt/crt.cmake | 8 +++ reactos/lib/sdk/crt/printf/_snprintf_s.c | 13 +++++ reactos/lib/sdk/crt/printf/_snwprintf_s.c | 14 +++++ reactos/lib/sdk/crt/printf/_sxprintf.c | 65 ++++++++++++++++++++-- reactos/lib/sdk/crt/printf/_vsnprintf_s.c | 14 +++++ reactos/lib/sdk/crt/printf/_vsnwprintf_s.c | 15 +++++ reactos/lib/sdk/crt/printf/sprintf_s.c | 13 +++++ reactos/lib/sdk/crt/printf/swprintf_s.c | 14 +++++ reactos/lib/sdk/crt/printf/vsprintf_s.c | 14 +++++ reactos/lib/sdk/crt/printf/vswprintf_s.c | 15 +++++ 11 files changed, 188 insertions(+), 13 deletions(-) create mode 100644 reactos/lib/sdk/crt/printf/_snprintf_s.c create mode 100644 reactos/lib/sdk/crt/printf/_snwprintf_s.c create mode 100644 reactos/lib/sdk/crt/printf/_vsnprintf_s.c create mode 100644 reactos/lib/sdk/crt/printf/_vsnwprintf_s.c create mode 100644 reactos/lib/sdk/crt/printf/sprintf_s.c create mode 100644 reactos/lib/sdk/crt/printf/swprintf_s.c create mode 100644 reactos/lib/sdk/crt/printf/vsprintf_s.c create mode 100644 reactos/lib/sdk/crt/printf/vswprintf_s.c diff --git a/reactos/dll/win32/msvcrt/msvcrt.spec b/reactos/dll/win32/msvcrt/msvcrt.spec index 23ca3b7c17f..2336c22d95f 100644 --- a/reactos/dll/win32/msvcrt/msvcrt.spec +++ b/reactos/dll/win32/msvcrt/msvcrt.spec @@ -832,7 +832,7 @@ # stub _snprintf_c # stub _snprintf_c_l # stub _snprintf_l -# stub _snprintf_s +@ varargs _snprintf_s(ptr long long ptr) # stub _snprintf_s_l # stub _snscanf # stub _snscanf_l @@ -840,7 +840,7 @@ # stub _snscanf_s_l @ varargs _snwprintf(ptr long wstr) # stub _snwprintf_l -# stub _snwprintf_s +@ varargs _snwprintf_s(ptr long long ptr) # stub _snwprintf_s_l # stub _snwscanf # stub _snwscanf_l @@ -985,11 +985,11 @@ @ cdecl _vsnprintf_c(ptr long str ptr) _vsnprintf # @ cdecl _vsnprintf_c_l(ptr long str ptr ptr) _vsnprintf_l # @ cdecl _vsnprintf_l(ptr long str ptr ptr) -# @ cdecl _vsnprintf_s(ptr long long str ptr) +@ cdecl _vsnprintf_s(ptr long long str ptr) # @ cdecl _vsnprintf_s_l(ptr long long str ptr ptr) @ cdecl _vsnwprintf(ptr long wstr ptr) # @ cdecl _vsnwprintf_l(ptr long wstr ptr ptr) -# @ cdecl _vsnwprintf_s(ptr long long wstr ptr) +@ cdecl _vsnwprintf_s(ptr long long wstr ptr) # @ cdecl _vsnwprintf_s_l(ptr long long wstr ptr ptr) # stub _vsprintf_l @ cdecl _vsprintf_p(ptr long str ptr) @@ -1313,7 +1313,7 @@ @ cdecl -arch=x86_64 sinf(long) @ cdecl sinh(double) @ varargs sprintf(ptr str) -# @ varargs sprintf_s(ptr long str) +@ varargs sprintf_s(ptr long str) @ cdecl sqrt(double) @ cdecl -arch=x86_64 sqrtf(long) @ cdecl srand(long) @@ -1348,7 +1348,7 @@ @ cdecl strtoul(str ptr long) @ cdecl strxfrm(ptr str long) @ varargs swprintf(ptr wstr) -# @ varargs swprintf_s(ptr long wstr) +@ varargs swprintf_s(ptr long wstr) @ varargs swscanf(wstr wstr) # @ varargs swscanf_s(wstr wstr) @ cdecl system(str) @@ -1374,9 +1374,9 @@ @ cdecl vprintf_s(str ptr) # stub vsnprintf @ cdecl vsprintf(ptr str ptr) -# @ cdecl vsprintf_s(ptr long str ptr) +@ cdecl vsprintf_s(ptr long str ptr) @ cdecl vswprintf(ptr wstr ptr) -# @ cdecl vswprintf_s(ptr long wstr ptr) +@ cdecl vswprintf_s(ptr long wstr ptr) @ cdecl vwprintf(wstr ptr) @ cdecl vwprintf_s(wstr ptr) # stub wcrtomb diff --git a/reactos/lib/sdk/crt/crt.cmake b/reactos/lib/sdk/crt/crt.cmake index eaf22f2c1c8..66d8a057fe1 100644 --- a/reactos/lib/sdk/crt/crt.cmake +++ b/reactos/lib/sdk/crt/crt.cmake @@ -135,13 +135,17 @@ list(APPEND CRT_SOURCE printf/_cprintf.c printf/_cwprintf.c printf/_snprintf.c + printf/_snprintf_s.c printf/_snwprintf.c + printf/_snwprintf_s.c printf/_vcprintf.c printf/_vcwprintf.c printf/_vscprintf.c printf/_vscwprintf.c printf/_vsnprintf.c + printf/_vsnprintf_s.c printf/_vsnwprintf.c + printf/_vsnwprintf_s.c printf/_vsprintf_p.c printf/fprintf.c printf/fprintf_s.c @@ -150,8 +154,10 @@ list(APPEND CRT_SOURCE printf/printf.c printf/printf_s.c printf/sprintf.c + printf/sprintf_s.c printf/streamout.c printf/swprintf.c + printf/swprintf_s.c printf/vfprintf.c printf/vfprintf_s.c printf/vfwprintf.c @@ -159,7 +165,9 @@ list(APPEND CRT_SOURCE printf/vprintf.c printf/vprintf_s.c printf/vsprintf.c + printf/vsprintf_s.c printf/vswprintf.c + printf/vswprintf_s.c printf/vwprintf.c printf/vwprintf_s.c printf/wprintf.c diff --git a/reactos/lib/sdk/crt/printf/_snprintf_s.c b/reactos/lib/sdk/crt/printf/_snprintf_s.c new file mode 100644 index 00000000000..249a8b3936f --- /dev/null +++ b/reactos/lib/sdk/crt/printf/_snprintf_s.c @@ -0,0 +1,13 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/_snprintf_s.c + * PURPOSE: Implementation of _snprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf _snprintf_s +#define USE_COUNT 1 +#define IS_SECAPI 1 + +#include "_sxprintf.c" diff --git a/reactos/lib/sdk/crt/printf/_snwprintf_s.c b/reactos/lib/sdk/crt/printf/_snwprintf_s.c new file mode 100644 index 00000000000..cd60e135466 --- /dev/null +++ b/reactos/lib/sdk/crt/printf/_snwprintf_s.c @@ -0,0 +1,14 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/_snwprintf_s.c + * PURPOSE: Implementation of _snwprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf _snwprintf_s +#define USE_COUNT 1 +#define _UNICODE +#define IS_SECAPI 1 + +#include "_sxprintf.c" diff --git a/reactos/lib/sdk/crt/printf/_sxprintf.c b/reactos/lib/sdk/crt/printf/_sxprintf.c index 44d35ecafff..28d77aa2fd7 100644 --- a/reactos/lib/sdk/crt/printf/_sxprintf.c +++ b/reactos/lib/sdk/crt/printf/_sxprintf.c @@ -10,6 +10,9 @@ #include #include #include +#if IS_SECAPI +#include +#endif #ifdef _UNICODE #define _tstreamout wstreamout @@ -17,6 +20,8 @@ #define _tstreamout streamout #endif +#define min(a,b) (((a) < (b)) ? (a) : (b)) + int _cdecl _tstreamout(FILE *stream, const TCHAR *format, va_list argptr); int @@ -27,6 +32,9 @@ _cdecl #endif _sxprintf( TCHAR *buffer, +#if IS_SECAPI + size_t sizeOfBuffer, +#endif #if USE_COUNT size_t count, #endif @@ -37,20 +45,42 @@ _sxprintf( ...) #endif { +#if !USE_COUNT + const size_t count = INT_MAX; +#endif +#if !IS_SECAPI + const size_t sizeOfBuffer = count; +#endif #if !USE_VARARGS va_list argptr; #endif int result; FILE stream; + /* Check trivial case */ + if ((buffer == NULL) && (count == 0) && (sizeOfBuffer == 0)) + { + return 0; + } + +#if IS_SECAPI + /* Validate parameters */ + if (MSVCRT_CHECK_PMT(((buffer == NULL) || (format == NULL) || (sizeOfBuffer <= 0)))) + { + errno = EINVAL; + return -1; + } + + /* Limit output to count + 1 characters */ + if (count != -1) + sizeOfBuffer = min(sizeOfBuffer, count + 1); +#endif + + /* Setup the FILE structure */ stream._base = (char*)buffer; stream._ptr = stream._base; stream._charbuf = 0; -#if USE_COUNT - stream._cnt = (int)(count * sizeof(TCHAR)); -#else - stream._cnt = INT_MAX; -#endif + stream._cnt = (int)(sizeOfBuffer * sizeof(TCHAR)); stream._bufsiz = 0; stream._flag = _IOSTRG | _IOWRT; stream._tmpfname = 0; @@ -63,8 +93,33 @@ _sxprintf( va_end(argptr); #endif +#if IS_SECAPI + /* Check for failure or unterminated string */ + if ((result < 0) || (result == sizeOfBuffer)) + { + /* Null-terminate the buffer at the end */ + buffer[sizeOfBuffer-1] = _T('\0'); + + /* Check if we can truncate */ + if (count != _TRUNCATE) + { + /* We can't, invoke invalid parameter handler */ + MSVCRT_INVALID_PMT("Buffer is too small"); + + /* If we came back, set the buffer to an empty string */ + *buffer = 0; + } + + /* Return failure */ + return -1; + } + + /* Null-terminate the buffer after the string */ + buffer[result] = _T('\0'); +#else /* Only zero terminate if there is enough space left */ if (stream._cnt >= sizeof(TCHAR)) *(TCHAR*)stream._ptr = _T('\0'); +#endif return result; } diff --git a/reactos/lib/sdk/crt/printf/_vsnprintf_s.c b/reactos/lib/sdk/crt/printf/_vsnprintf_s.c new file mode 100644 index 00000000000..7355406e798 --- /dev/null +++ b/reactos/lib/sdk/crt/printf/_vsnprintf_s.c @@ -0,0 +1,14 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/_vsnprintf_s.c + * PURPOSE: Implementation of _vsnprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf _vsnprintf_s +#define USE_COUNT 1 +#define USE_VARARGS 1 +#define IS_SECAPI 1 + +#include "_sxprintf.c" diff --git a/reactos/lib/sdk/crt/printf/_vsnwprintf_s.c b/reactos/lib/sdk/crt/printf/_vsnwprintf_s.c new file mode 100644 index 00000000000..fc7585cdf2f --- /dev/null +++ b/reactos/lib/sdk/crt/printf/_vsnwprintf_s.c @@ -0,0 +1,15 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/_vsnwprintf_s.c + * PURPOSE: Implementation of _vsnwprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf _vsnwprintf_s +#define USE_COUNT 1 +#define USE_VARARGS 1 +#define _UNICODE +#define IS_SECAPI 1 + +#include "_sxprintf.c" diff --git a/reactos/lib/sdk/crt/printf/sprintf_s.c b/reactos/lib/sdk/crt/printf/sprintf_s.c new file mode 100644 index 00000000000..671747391f2 --- /dev/null +++ b/reactos/lib/sdk/crt/printf/sprintf_s.c @@ -0,0 +1,13 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/sprintf_s.c + * PURPOSE: Implementation of sprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf sprintf_s +#define USE_COUNT 0 +#define IS_SECAPI 1 + +#include "_sxprintf.c" diff --git a/reactos/lib/sdk/crt/printf/swprintf_s.c b/reactos/lib/sdk/crt/printf/swprintf_s.c new file mode 100644 index 00000000000..a77136c6849 --- /dev/null +++ b/reactos/lib/sdk/crt/printf/swprintf_s.c @@ -0,0 +1,14 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/swprintf_s.c + * PURPOSE: Implementation of swprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf swprintf_s +#define USE_COUNT 0 +#define USE_VARARGS 0 +#define IS_SECAPI 1 + +#include "_sxprintf.c" diff --git a/reactos/lib/sdk/crt/printf/vsprintf_s.c b/reactos/lib/sdk/crt/printf/vsprintf_s.c new file mode 100644 index 00000000000..b049afaecd3 --- /dev/null +++ b/reactos/lib/sdk/crt/printf/vsprintf_s.c @@ -0,0 +1,14 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/vsprintf_s.c + * PURPOSE: Implementation of vsprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf vsprintf_s +#define USE_COUNT 0 +#define USE_VARARGS 1 +#define IS_SECAPI 1 + +#include "_sxprintf.c" diff --git a/reactos/lib/sdk/crt/printf/vswprintf_s.c b/reactos/lib/sdk/crt/printf/vswprintf_s.c new file mode 100644 index 00000000000..68702ba9f75 --- /dev/null +++ b/reactos/lib/sdk/crt/printf/vswprintf_s.c @@ -0,0 +1,15 @@ +/* + * COPYRIGHT: GNU GPL, see COPYING in the top level directory + * PROJECT: ReactOS crt library + * FILE: lib/sdk/crt/printf/vswprintf_s.c + * PURPOSE: Implementation of vswprintf_s + * PROGRAMMER: Timo Kreuzer + */ + +#define _sxprintf vswprintf_s +#define USE_COUNT 0 +#define USE_VARARGS 1 +#define _UNICODE +#define IS_SECAPI 1 + +#include "_sxprintf.c"