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: vsscanf_s  function
123d58139fSopenharmony_ci * Create: 2014-02-25
133d58139fSopenharmony_ci */
143d58139fSopenharmony_ci
153d58139fSopenharmony_ci#include "secinput.h"
163d58139fSopenharmony_ci#if defined(SECUREC_VXWORKS_PLATFORM) && !SECUREC_IN_KERNEL && \
173d58139fSopenharmony_ci    (!defined(SECUREC_SYSAPI4VXWORKS) && !defined(SECUREC_CTYPE_MACRO_ADAPT))
183d58139fSopenharmony_ci#include <ctype.h>
193d58139fSopenharmony_ci#endif
203d58139fSopenharmony_ci
213d58139fSopenharmony_ci/*
223d58139fSopenharmony_ci * <NAME>
233d58139fSopenharmony_ci *    vsscanf_s
243d58139fSopenharmony_ci *
253d58139fSopenharmony_ci *
263d58139fSopenharmony_ci * <FUNCTION DESCRIPTION>
273d58139fSopenharmony_ci *    The vsscanf_s function is equivalent to sscanf_s, with the variable argument list replaced by argList
283d58139fSopenharmony_ci *    The vsscanf_s function reads data from buffer into the location given by
293d58139fSopenharmony_ci *    each argument. Every argument must be a pointer to a variable with a type
303d58139fSopenharmony_ci *    that corresponds to a type specifier in format. The format argument controls
313d58139fSopenharmony_ci *    the interpretation of the input fields and has the same form and function
323d58139fSopenharmony_ci *    as the format argument for the scanf function.
333d58139fSopenharmony_ci *    If copying takes place between strings that overlap, the behavior is undefined.
343d58139fSopenharmony_ci *
353d58139fSopenharmony_ci * <INPUT PARAMETERS>
363d58139fSopenharmony_ci *    buffer                Stored data
373d58139fSopenharmony_ci *    format                Format control string, see Format Specifications.
383d58139fSopenharmony_ci *    argList               pointer to list of arguments
393d58139fSopenharmony_ci *
403d58139fSopenharmony_ci * <OUTPUT PARAMETERS>
413d58139fSopenharmony_ci *    argList               the converted value stored in user assigned address
423d58139fSopenharmony_ci *
433d58139fSopenharmony_ci * <RETURN VALUE>
443d58139fSopenharmony_ci *    Each of these functions returns the number of fields successfully converted
453d58139fSopenharmony_ci *    and assigned; the return value does not include fields that were read but
463d58139fSopenharmony_ci *    not assigned. A return value of 0 indicates that no fields were assigned.
473d58139fSopenharmony_ci *    return -1 if an error occurs.
483d58139fSopenharmony_ci */
493d58139fSopenharmony_ciint vsscanf_s(const char *buffer, const char *format, va_list argList)
503d58139fSopenharmony_ci{
513d58139fSopenharmony_ci    size_t count;               /* If initialization causes  e838 */
523d58139fSopenharmony_ci    int retVal;
533d58139fSopenharmony_ci    SecFileStream fStr;
543d58139fSopenharmony_ci
553d58139fSopenharmony_ci    /* Validation section */
563d58139fSopenharmony_ci    if (buffer == NULL || format == NULL) {
573d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("vsscanf_s");
583d58139fSopenharmony_ci        return SECUREC_SCANF_EINVAL;
593d58139fSopenharmony_ci    }
603d58139fSopenharmony_ci    count = strlen(buffer);
613d58139fSopenharmony_ci    if (count == 0 || count > SECUREC_STRING_MAX_LEN) {
623d58139fSopenharmony_ci        SecClearDestBuf(buffer, format, argList);
633d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("vsscanf_s");
643d58139fSopenharmony_ci        return SECUREC_SCANF_EINVAL;
653d58139fSopenharmony_ci    }
663d58139fSopenharmony_ci#if defined(SECUREC_VXWORKS_PLATFORM) && !SECUREC_IN_KERNEL
673d58139fSopenharmony_ci    /*
683d58139fSopenharmony_ci     * On vxworks platform when buffer is white string, will set first %s argument to zero.Like following usage:
693d58139fSopenharmony_ci     * "   \v\f\t\r\n", "%s", str, strSize
703d58139fSopenharmony_ci     * Do not check all character, just first and last character then consider it is white string
713d58139fSopenharmony_ci     */
723d58139fSopenharmony_ci    if (isspace((int)(unsigned char)buffer[0]) != 0 && isspace((int)(unsigned char)buffer[count - 1]) != 0) {
733d58139fSopenharmony_ci        SecClearDestBuf(buffer, format, argList);
743d58139fSopenharmony_ci    }
753d58139fSopenharmony_ci#endif
763d58139fSopenharmony_ci    SECUREC_FILE_STREAM_FROM_STRING(&fStr, buffer, count);
773d58139fSopenharmony_ci    retVal = SecInputS(&fStr, format, argList);
783d58139fSopenharmony_ci    if (retVal < 0) {
793d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("vsscanf_s");
803d58139fSopenharmony_ci        return SECUREC_SCANF_EINVAL;
813d58139fSopenharmony_ci    }
823d58139fSopenharmony_ci    return retVal;
833d58139fSopenharmony_ci}
843d58139fSopenharmony_ci#if SECUREC_EXPORT_KERNEL_SYMBOL
853d58139fSopenharmony_ciEXPORT_SYMBOL(vsscanf_s);
863d58139fSopenharmony_ci#endif
873d58139fSopenharmony_ci
88