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: wcscpy_s function
12 * Create: 2014-02-25
13 */
14
15 #include "securecutil.h"
16
SecDoCpyW(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)17 SECUREC_INLINE errno_t SecDoCpyW(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
18 {
19 size_t srcStrLen;
20 SECUREC_CALC_WSTR_LEN(strSrc, destMax, &srcStrLen);
21
22 if (srcStrLen == destMax) {
23 strDest[0] = L'\0';
24 SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
25 return ERANGE_AND_RESET;
26 }
27 if (strDest == strSrc) {
28 return EOK;
29 }
30
31 if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, srcStrLen)) {
32 /* Performance optimization, srcStrLen is single character length include '\0' */
33 SECUREC_MEMCPY_WARP_OPT(strDest, strSrc, (srcStrLen + 1) * sizeof(wchar_t));
34 return EOK;
35 } else {
36 strDest[0] = L'\0';
37 SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s");
38 return EOVERLAP_AND_RESET;
39 }
40 }
41
42 /*
43 * <FUNCTION DESCRIPTION>
44 * The wcscpy_s function copies the wide string pointed to by strSrc
45 * (including the terminating null wide character) into the array pointed to by strDest
46
47 * <INPUT PARAMETERS>
48 * strDest Destination string buffer
49 * destMax Size of the destination string buffer.
50 * strSrc Null-terminated source string buffer.
51 *
52 * <OUTPUT PARAMETERS>
53 * strDest is updated.
54 *
55 * <RETURN VALUE>
56 * EOK Success
57 * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
58 * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0
59 * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
60 * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
61 * ERANGE_AND_RESET destMax <= length of strSrc and strDest != strSrc
62 * and strDest != NULL and strSrc != NULL and destMax != 0
63 * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap
64 * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and destMax != 0
65 * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
66 * and strDest != NULL and strSrc !=NULL and strDest != strSrc
67 *
68 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
69 */
wcscpy_s(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)70 errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
71 {
72 if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
73 SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
74 return ERANGE;
75 }
76 if (strDest == NULL || strSrc == NULL) {
77 SECUREC_ERROR_INVALID_PARAMTER("wcscpy_s");
78 if (strDest != NULL) {
79 strDest[0] = L'\0';
80 return EINVAL_AND_RESET;
81 }
82 return EINVAL;
83 }
84 return SecDoCpyW(strDest, destMax, strSrc);
85 }
86
87