1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
3 * Licensed under Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 *          http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 * Description: vswprintf_s  function
12 * Create: 2014-02-25
13 */
14
15#ifndef SECUREC_FOR_WCHAR
16#define SECUREC_FOR_WCHAR
17#endif
18
19#include "secureprintoutput.h"
20
21/*
22 * <FUNCTION DESCRIPTION>
23 *    The  vswprintf_s  function  is  the  wide-character  equivalent  of the vsprintf_s function
24 *
25 * <INPUT PARAMETERS>
26 *    strDest                  Storage location for the output.
27 *    destMax                  Maximum number of characters to store
28 *    format                   Format specification.
29 *    argList                  pointer to list of arguments
30 *
31 * <OUTPUT PARAMETERS>
32 *    strDest                 is updated
33 *
34 * <RETURN VALUE>
35 *    return  the number of wide characters stored in strDest, not  counting the terminating null wide character.
36 *    return -1  if an error occurred.
37 *
38 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
39 */
40int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list argList)
41{
42    int retVal;               /* If initialization causes  e838 */
43    if (SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, SECUREC_WCHAR_STRING_MAX_LEN)) {
44        SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, SECUREC_WCHAR_STRING_MAX_LEN);
45        SECUREC_ERROR_INVALID_PARAMTER("vswprintf_s");
46        return -1;
47    }
48
49    retVal = SecVswprintfImpl(strDest, destMax, format, argList);
50    if (retVal < 0) {
51        strDest[0] = L'\0';
52        if (retVal == SECUREC_PRINTF_TRUNCATE) {
53            /* Buffer too small */
54            SECUREC_ERROR_INVALID_RANGE("vswprintf_s");
55        }
56        SECUREC_ERROR_INVALID_PARAMTER("vswprintf_s");
57        return -1;
58    }
59
60    return retVal;
61}
62
63