1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "secinput.h"
17
SecWcslen(const wchar_t * s)18 static size_t SecWcslen(const wchar_t *s)
19 {
20 const wchar_t *end = s;
21 while (*end != L'\0') {
22 ++end;
23 }
24 return ((size_t)((end - s)));
25 }
26
27 /*******************************************************************************
28 * <FUNCTION DESCRIPTION>
29 * The vswscanf_s function is the wide-character equivalent of the vsscanf_s function
30 * The vsscanf_s function reads data from buffer into the location given by
31 * each argument. Every argument must be a pointer to a variable with a type
32 * that corresponds to a type specifier in format.
33 * The format argument controls the interpretation of the input fields and
34 * has the same form and function as the format argument for the scanf function.
35 * If copying takes place between strings that overlap, the behavior is undefined.
36 *
37 * <INPUT PARAMETERS>
38 * buffer Stored data
39 * format Format control string, see Format Specifications.
40 * arglist pointer to list of arguments
41 *
42 * <OUTPUT PARAMETERS>
43 * arglist the converted value stored in user assigned address
44 *
45 * <RETURN VALUE>
46 * Each of these functions returns the number of fields successfully converted
47 * and assigned; the return value does not include fields that were read but
48 * not assigned. A return value of 0 indicates that no fields were assigned.
49 * return -1 if an error occurs.
50 *******************************************************************************
51 */
vswscanf_s(const wchar_t * buffer,const wchar_t * format,va_list arglist)52 int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list arglist)
53 {
54 SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM;
55 int retVal; /* If initialization causes e838 */
56 size_t count; /* If initialization causes e838 */
57
58 /* validation section */
59 if (buffer == NULL || format == NULL) {
60 SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
61 return SECUREC_SCANF_EINVAL;
62 }
63 count = SecWcslen(buffer);
64 if (count == 0 || count > SECUREC_WCHAR_STRING_MAX_LEN) {
65 SecClearDestBufW(buffer, format, arglist);
66 SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
67 return SECUREC_SCANF_EINVAL;
68 }
69
70 fStr.flag = SECUREC_MEM_STR_FLAG;
71 fStr.cur = (const char *)buffer;
72 fStr.count = (int)count *((int)sizeof(wchar_t));
73
74 retVal = SecInputSW(&fStr, format, arglist);
75 if (retVal < 0) {
76 SECUREC_ERROR_INVALID_PARAMTER("vswscanf_s");
77 return SECUREC_SCANF_EINVAL;
78 }
79 return retVal;
80 }
81
82
83