• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: wcscat_s  function
12  * Create: 2014-02-25
13  */
14 
15 #include "securecutil.h"
16 
17 /*
18  * Befor this function, the basic parameter checking has been done
19  */
SecDoCatW(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)20 SECUREC_INLINE errno_t SecDoCatW(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
21 {
22     size_t destLen;
23     size_t srcLen;
24     size_t maxCount; /* Store the maximum available count */
25 
26     /* To calculate the length of a wide character, the parameter must be a wide character */
27     SECUREC_CALC_WSTR_LEN(strDest, destMax, &destLen);
28     maxCount = destMax - destLen;
29     SECUREC_CALC_WSTR_LEN(strSrc, maxCount, &srcLen);
30 
31     if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
32         strDest[0] = L'\0';
33         if (strDest + destLen <= strSrc && destLen == destMax) {
34             SECUREC_ERROR_INVALID_PARAMTER("wcscat_s");
35             return EINVAL_AND_RESET;
36         }
37         SECUREC_ERROR_BUFFER_OVERLAP("wcscat_s");
38         return EOVERLAP_AND_RESET;
39     }
40     if (srcLen + destLen >= destMax || strDest == strSrc) {
41         strDest[0] = L'\0';
42         if (destLen == destMax) {
43             SECUREC_ERROR_INVALID_PARAMTER("wcscat_s");
44             return EINVAL_AND_RESET;
45         }
46         SECUREC_ERROR_INVALID_RANGE("wcscat_s");
47         return ERANGE_AND_RESET;
48     }
49     /* Copy single character length  include \0 */
50     SECUREC_MEMCPY_WARP_OPT(strDest + destLen, strSrc, (srcLen + 1) * sizeof(wchar_t));
51     return EOK;
52 }
53 
54 /*
55  * <FUNCTION DESCRIPTION>
56  *    The wcscat_s function appends a copy of the wide string pointed to by strSrc
57 *      (including the terminating null wide character)
58  *     to the end of the wide string pointed to by strDest.
59  *    The arguments and return value of wcscat_s are wide-character strings.
60  *
61  *    The wcscat_s function appends strSrc to strDest and terminates the resulting
62  *    string with a null character. The initial character of strSrc overwrites the
63  *    terminating null character of strDest. wcscat_s will return EOVERLAP_AND_RESET if the
64  *    source and destination strings overlap.
65  *
66  *    Note that the second parameter is the total size of the buffer, not the
67  *    remaining size.
68  *
69  * <INPUT PARAMETERS>
70  *    strDest              Null-terminated destination string buffer.
71  *    destMax              Size of the destination string buffer.
72  *    strSrc               Null-terminated source string buffer.
73  *
74  * <OUTPUT PARAMETERS>
75  *    strDest               is updated
76  *
77  * <RETURN VALUE>
78  *    EOK                   Success
79  *    EINVAL                strDest is  NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
80  *    EINVAL_AND_RESET      (strDest unterminated and all other parameters are valid) or
81  *                          (strDest != NULL and strSrc is NULL and destMax != 0
82  *                           and destMax <= SECUREC_WCHAR_STRING_MAX_LEN)
83  *    ERANGE                destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
84  *    ERANGE_AND_RESET      strDest have not enough space  and all other parameters are valid  and not overlap
85  *    EOVERLAP_AND_RESET     dest buffer and source buffer are overlapped and all  parameters are valid
86  *
87  *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
88  */
wcscat_s(wchar_t * strDest,size_t destMax,const wchar_t * strSrc)89 errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
90 {
91     if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
92         SECUREC_ERROR_INVALID_RANGE("wcscat_s");
93         return ERANGE;
94     }
95 
96     if (strDest == NULL || strSrc == NULL) {
97         SECUREC_ERROR_INVALID_PARAMTER("wcscat_s");
98         if (strDest != NULL) {
99             strDest[0] = L'\0';
100             return EINVAL_AND_RESET;
101         }
102         return EINVAL;
103     }
104 
105     return SecDoCatW(strDest, destMax, strSrc);
106 }
107 
108