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