• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: vswscanf_s  function
12  * Create: 2014-02-25
13  */
14 
15 #ifndef SECUREC_FOR_WCHAR
16 #define SECUREC_FOR_WCHAR
17 #endif
18 
19 #include "secinput.h"
20 
SecWcslen(const wchar_t * s)21 SECUREC_INLINE size_t SecWcslen(const wchar_t *s)
22 {
23     const wchar_t *end = s;
24     while (*end != L'\0') {
25         ++end;
26     }
27     return ((size_t)((end - s)));
28 }
29 
30 /*
31  * <FUNCTION DESCRIPTION>
32  *    The  vswscanf_s  function  is  the  wide-character  equivalent  of the vsscanf_s function
33  *    The vsscanf_s function reads data from buffer into the location given by
34  *    each argument. Every argument must be a pointer to a variable with a type
35  *    that corresponds to a type specifier in format.
36  *    The format argument controls the interpretation of the input fields and
37  *    has the same form and function as the format argument for the scanf function.
38  *    If copying takes place between strings that overlap, the behavior is undefined.
39  *
40  * <INPUT PARAMETERS>
41  *    buffer                Stored data
42  *    format                Format control string, see Format Specifications.
43  *    argList               pointer to list of arguments
44  *
45  * <OUTPUT PARAMETERS>
46  *    argList               the converted value stored in user assigned address
47  *
48  * <RETURN VALUE>
49  *    Each of these functions returns the number of fields successfully converted
50  *    and assigned; the return value does not include fields that were read but
51  *    not assigned. A return value of 0 indicates that no fields were assigned.
52  *    return -1 if an error occurs.
53  */
vswscanf_s(const wchar_t * buffer,const wchar_t * format,va_list argList)54 int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list argList)
55 {
56     size_t count; /* If initialization causes  e838 */
57     SecFileStream fStr;
58     int retVal;
59 
60     /* Validation section */
61     if (buffer == NULL || format == NULL) {
62         SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
63         return SECUREC_SCANF_EINVAL;
64     }
65     count = SecWcslen(buffer);
66     if (count == 0 || count > SECUREC_WCHAR_STRING_MAX_LEN) {
67         SecClearDestBufW(buffer, format, argList);
68         SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
69         return SECUREC_SCANF_EINVAL;
70     }
71     SECUREC_FILE_STREAM_FROM_STRING(&fStr, (const char *)buffer, count * sizeof(wchar_t));
72     retVal = SecInputSW(&fStr, format, argList);
73     if (retVal < 0) {
74         SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
75         return SECUREC_SCANF_EINVAL;
76     }
77     return retVal;
78 }
79 
80