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