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