• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define SECUREC_INLINE_DO_MEMCPY 1
18 
19 #include "securecutil.h"
20 
SecDoWcscpy(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)21 static errno_t SecDoWcscpy(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
22 {
23     size_t srcStrLen;
24 
25     SECUREC_CALC_WSTR_LEN(strSrc, destMax, &srcStrLen);
26     if (srcStrLen == destMax) {
27         strDest[0] = '\0';
28         SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
29         return ERANGE_AND_RESET;
30     }
31     if (strDest == strSrc) {
32         return EOK;
33     }
34 
35     if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, srcStrLen)) {
36         /* performance optimization srcStrLen include '\0' */
37         SecDoMemcpy(strDest, strSrc, (srcStrLen + 1) * sizeof(wchar_t)); /* single character length  include \0 */
38         return EOK;
39     } else {
40         strDest[0] = L'\0';
41         SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s");
42         return EOVERLAP_AND_RESET;
43     }
44 }
45 
46 /*
47  * <FUNCTION DESCRIPTION>
48  *   The wcscpy_s function copies the wide string pointed to by strSrc
49  *   (including theterminating null wide character) into the array pointed to by strDest
50 
51  * <INPUT PARAMETERS>
52  *    strDest               Destination string buffer
53  *    destMax               Size of the destination string buffer.
54  *    strSrc                Null-terminated source string buffer.
55  *
56  * <OUTPUT PARAMETERS>
57  *    strDest               is updated.
58  *
59  * <RETURN VALUE>
60  *    EOK                   Success
61  *    EINVAL                strDest is  NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
62  *    EINVAL_AND_RESET      strDest != NULL and strSrc is NULLL and destMax != 0
63  *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
64  *    ERANGE                destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
65  *    ERANGE_AND_RESET      destMax <= length of strSrc and strDest != strSrc
66  *                          and strDest != NULL and strSrc != NULL and destMax != 0
67  *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap
68  *    EOVERLAP_AND_RESET    dest buffer and source buffer are overlapped and destMax != 0
69  *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
70  *                          and strDest != NULL and strSrc !=NULL and strDest != strSrc
71  *
72  *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
73  */
wcscpy_s(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)74 errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
75 {
76     if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
77         SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
78         return ERANGE;
79     }
80     if (strDest == NULL || strSrc == NULL) {
81         SECUREC_ERROR_INVALID_PARAMTER("wcscpy_s");
82         if (strDest != NULL) {
83             strDest[0] = L'\0';
84             return EINVAL_AND_RESET;
85         }
86         return EINVAL;
87     }
88     return SecDoWcscpy(strDest, destMax, strSrc);
89 }
90 
91 
92