13d58139fSopenharmony_ci/*
23d58139fSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
33d58139fSopenharmony_ci * Licensed under Mulan PSL v2.
43d58139fSopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
53d58139fSopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
63d58139fSopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
73d58139fSopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
83d58139fSopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
93d58139fSopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
103d58139fSopenharmony_ci * See the Mulan PSL v2 for more details.
113d58139fSopenharmony_ci * Description: vsprintf_s  function
123d58139fSopenharmony_ci * Create: 2014-02-25
133d58139fSopenharmony_ci */
143d58139fSopenharmony_ci
153d58139fSopenharmony_ci#include "secureprintoutput.h"
163d58139fSopenharmony_ci
173d58139fSopenharmony_ci/*
183d58139fSopenharmony_ci * <FUNCTION DESCRIPTION>
193d58139fSopenharmony_ci *    The vsprintf_s function is equivalent to the vsprintf function
203d58139fSopenharmony_ci *    except for the parameter destMax and the explicit runtime-constraints violation
213d58139fSopenharmony_ci *    The vsprintf_s function takes a pointer to an argument list, and then formats
223d58139fSopenharmony_ci *    and writes the given data to the memory pointed to by strDest.
233d58139fSopenharmony_ci *    The function differ from the non-secure versions only in that the secure
243d58139fSopenharmony_ci *    versions support positional parameters.
253d58139fSopenharmony_ci *
263d58139fSopenharmony_ci * <INPUT PARAMETERS>
273d58139fSopenharmony_ci *    strDest                Storage location for the output.
283d58139fSopenharmony_ci *    destMax                Size of strDest
293d58139fSopenharmony_ci *    format                 Format specification.
303d58139fSopenharmony_ci *    argList                pointer to list of arguments
313d58139fSopenharmony_ci *
323d58139fSopenharmony_ci * <OUTPUT PARAMETERS>
333d58139fSopenharmony_ci *    strDest                is updated
343d58139fSopenharmony_ci *
353d58139fSopenharmony_ci * <RETURN VALUE>
363d58139fSopenharmony_ci *    return  the number of characters written, not including the terminating null character,
373d58139fSopenharmony_ci *    return -1  if an  error occurs.
383d58139fSopenharmony_ci *
393d58139fSopenharmony_ci * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
403d58139fSopenharmony_ci */
413d58139fSopenharmony_ciint vsprintf_s(char *strDest, size_t destMax, const char *format, va_list argList)
423d58139fSopenharmony_ci{
433d58139fSopenharmony_ci    int retVal;               /* If initialization causes  e838 */
443d58139fSopenharmony_ci
453d58139fSopenharmony_ci    if (SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, SECUREC_STRING_MAX_LEN)) {
463d58139fSopenharmony_ci        SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, SECUREC_STRING_MAX_LEN);
473d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
483d58139fSopenharmony_ci        return -1;
493d58139fSopenharmony_ci    }
503d58139fSopenharmony_ci
513d58139fSopenharmony_ci    retVal = SecVsnprintfImpl(strDest, destMax, format, argList);
523d58139fSopenharmony_ci    if (retVal < 0) {
533d58139fSopenharmony_ci        strDest[0] = '\0';
543d58139fSopenharmony_ci        if (retVal == SECUREC_PRINTF_TRUNCATE) {
553d58139fSopenharmony_ci            /* Buffer is too small */
563d58139fSopenharmony_ci            SECUREC_ERROR_INVALID_RANGE("vsprintf_s");
573d58139fSopenharmony_ci        }
583d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
593d58139fSopenharmony_ci        return -1;
603d58139fSopenharmony_ci    }
613d58139fSopenharmony_ci
623d58139fSopenharmony_ci    return retVal;
633d58139fSopenharmony_ci}
643d58139fSopenharmony_ci#if SECUREC_EXPORT_KERNEL_SYMBOL
653d58139fSopenharmony_ciEXPORT_SYMBOL(vsprintf_s);
663d58139fSopenharmony_ci#endif
673d58139fSopenharmony_ci
68