• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "securecutil.h"
17 
SecDoWcscpy(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)18 static errno_t SecDoWcscpy(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
19 {
20     wchar_t *tmpDest = strDest;
21     const wchar_t *tmpSrc = strSrc;
22     size_t maxSize = destMax;
23     SECUREC_IN_REGISTER const wchar_t *overlapGuard = NULL;
24 
25     if (tmpDest < tmpSrc) {
26         overlapGuard = tmpSrc;
27         while ((*(tmpDest++) = *(tmpSrc++)) != '\0' && --maxSize > 0) {
28             if (tmpDest == overlapGuard) {
29                 strDest[0] = '\0';
30                 SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s");
31                 return EOVERLAP_AND_RESET;
32             }
33         }
34     } else {
35         overlapGuard = tmpDest;
36         while ((*(tmpDest++) = *(tmpSrc++)) != '\0' && --maxSize > 0) {
37             if (tmpSrc == overlapGuard) {
38                 strDest[0] = '\0';
39                 SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s");
40                 return EOVERLAP_AND_RESET;
41             }
42         }
43     }
44 
45     if (maxSize == 0) {
46         strDest[0] = '\0';
47         SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
48         return ERANGE_AND_RESET;
49     }
50     return EOK;
51 }
52 
53 /*******************************************************************************
54  * <FUNCTION DESCRIPTION>
55  *   The wcscpy_s function copies the wide string pointed to by strSrc
56  *   (including theterminating null wide character) into the array pointed to by strDest
57 
58  * <INPUT PARAMETERS>
59  *    strDest               Destination string buffer
60  *    destMax               Size of the destination string buffer.
61  *    strSrc                Null-terminated source string buffer.
62  *
63  * <OUTPUT PARAMETERS>
64  *    strDest               is updated.
65  *
66  * <RETURN VALUE>
67  *    EOK                   Success
68  *    EINVAL                strDest is  NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
69  *    EINVAL_AND_RESET      strDest != NULL and strSrc is NULL and destMax != 0
70  *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
71  *    ERANGE                destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
72  *    ERANGE_AND_RESET      destMax <= length of strSrc and strDest != strSrc
73  *                          and strDest != NULL and strSrc != NULL and destMax != 0 and
74  *                                           destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap
75  *    EOVERLAP_AND_RESET    dest buffer and source buffer are overlapped and destMax != 0
76  *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN and
77  *                                           strDest != NULL and strSrc !=NULL and strDest != strSrc
78  *
79  *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
80  *******************************************************************************
81  */
wcscpy_s(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)82 errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
83 {
84     if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
85         SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
86         return ERANGE;
87     }
88     if (strDest == NULL || strSrc == NULL) {
89         SECUREC_ERROR_INVALID_PARAMTER("wcscpy_s");
90         if (strDest != NULL) {
91             strDest[0] = L'\0';
92             return EINVAL_AND_RESET;
93         }
94 
95         return EINVAL;
96     }
97     if (strDest == strSrc) {
98         return EOK;
99     }
100     return SecDoWcscpy(strDest, destMax, strSrc);
101 }
102 
103 
104