1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "secureprintoutput.h"
17 #ifndef __MINGW32__
SecVswprintfImpl(wchar_t * string,size_t sizeInWchar,const wchar_t * format,va_list arglist)18 static int SecVswprintfImpl(wchar_t *string, size_t sizeInWchar, const wchar_t *format, va_list arglist)
19 {
20 SecPrintfStream str;
21 int retVal; /* If initialization causes e838 */
22
23 str.cur = (char *)string;
24 str.count = (int)(sizeInWchar * sizeof(wchar_t));
25
26 retVal = SecOutputSW(&str, format, arglist);
27 if ((retVal >= 0) && SecPutWcharStrEndingZero(&str, (int)sizeof(wchar_t))) {
28 return (retVal);
29 } else if (str.count < 0) {
30 /* the buffer was too small; we return truncation */
31 string[sizeInWchar - 1] = 0;
32 return SECUREC_PRINTF_TRUNCATE;
33 }
34 return -1;
35
36 }
37
38 /*******************************************************************************
39 * <FUNCTION DESCRIPTION>
40 * The vswprintf_s function is the wide-character equivalent of the vsprintf_s function
41 *
42 * <INPUT PARAMETERS>
43 * strDest Storage location for the output.
44 * destMax Size of strDest
45 * format Format specification.
46 * arglist pointer to list of arguments
47 *
48 * <OUTPUT PARAMETERS>
49 * strDest is updated
50 *
51 * <RETURN VALUE>
52 * return the number of wide characters stored in strDest, not counting the terminating null wide character.
53 * return -1 if an error occurred.
54 *
55 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
56 *******************************************************************************
57 */
58
vswprintf_s(wchar_t * strDest,size_t destMax,const wchar_t * format,va_list arglist)59 int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list arglist)
60 {
61 int retVal; /* If initialization causes e838 */
62
63 if (format == NULL || strDest == NULL || destMax == 0 || destMax > (SECUREC_WCHAR_STRING_MAX_LEN)) {
64 if (strDest != NULL && destMax > 0) {
65 strDest[0] = '\0';
66 }
67 SECUREC_ERROR_INVALID_PARAMTER("vswprintf_s");
68 return -1;
69 }
70
71 retVal = SecVswprintfImpl(strDest, destMax, format, arglist);
72
73 if (retVal < 0) {
74 strDest[0] = '\0';
75 if (retVal == SECUREC_PRINTF_TRUNCATE) {
76 /* Buffer too small */
77 SECUREC_ERROR_INVALID_RANGE("vswprintf_s");
78 }
79 SECUREC_ERROR_INVALID_PARAMTER("vswprintf_s");
80 return -1;
81 }
82
83 return retVal;
84 }
85 #endif
86
87