• 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: wcsncat_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  */
SecDoCatLimitW(wchar_t * strDest,size_t destMax,const wchar_t * strSrc,size_t count)20 SECUREC_INLINE errno_t SecDoCatLimitW(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
21 {
22     /* To calculate the length of a wide character, the parameter must be a wide character */
23     size_t destLen;
24     size_t srcLen;
25     SECUREC_CALC_WSTR_LEN(strDest, destMax, &destLen);
26     SECUREC_CALC_WSTR_LEN(strSrc, count, &srcLen);
27 
28     if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
29         strDest[0] = L'\0';
30         if (strDest + destLen <= strSrc && destLen == destMax) {
31             SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s");
32             return EINVAL_AND_RESET;
33         }
34         SECUREC_ERROR_BUFFER_OVERLAP("wcsncat_s");
35         return EOVERLAP_AND_RESET;
36     }
37     if (srcLen + destLen >= destMax || strDest == strSrc) {
38         strDest[0] = L'\0';
39         if (destLen == destMax) {
40             SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s");
41             return EINVAL_AND_RESET;
42         }
43         SECUREC_ERROR_INVALID_RANGE("wcsncat_s");
44         return ERANGE_AND_RESET;
45     }
46     SECUREC_MEMCPY_WARP_OPT(strDest + destLen, strSrc, srcLen * sizeof(wchar_t)); /* no  terminator */
47     *(strDest + destLen + srcLen) = L'\0';
48     return EOK;
49 }
50 
51 /*
52  * <FUNCTION DESCRIPTION>
53  *    The wcsncat_s function appends not more than n successive wide characters
54  *     (not including the terminating null wide character)
55  *     from the array pointed to by strSrc to the end of the wide string pointed to by strDest.
56  *
57  *    The wcsncat_s function try to append the first D characters of strSrc to
58  *    the end of strDest, where D is the lesser of count and the length of strSrc.
59  *    If appending those D characters will fit within strDest (whose size is
60  *    given as destMax) and still leave room for a null terminator, then those
61  *    characters are appended, starting at the original terminating null of
62  *    strDest, and a new terminating null is appended; otherwise, strDest[0] is
63  *    set to the null character.
64  *
65  * <INPUT PARAMETERS>
66  *    strDest               Null-terminated destination string.
67  *    destMax               Size of the destination buffer.
68  *    strSrc                Null-terminated source string.
69  *    count                 Number of character to append, or truncate.
70  *
71  * <OUTPUT PARAMETERS>
72  *    strDest               is updated
73  *
74  * <RETURN VALUE>
75  *    EOK                   Success
76  *    EINVAL                strDest is  NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
77  *    EINVAL_AND_RESET      (strDest unterminated and all other parameters are valid) or
78  *                          (strDest != NULL and strSrc is NULL and destMax != 0 and
79  *                           destMax <= SECUREC_WCHAR_STRING_MAX_LEN)
80  *    ERANGE                destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
81  *    ERANGE_AND_RESET      strDest have not enough space  and all other parameters are valid  and not overlap
82  *    EOVERLAP_AND_RESET     dest buffer and source buffer are overlapped and all  parameters are valid
83  *
84  *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
85  */
wcsncat_s(wchar_t * strDest,size_t destMax,const wchar_t * strSrc,size_t count)86 errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
87 {
88     if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
89         SECUREC_ERROR_INVALID_RANGE("wcsncat_s");
90         return ERANGE;
91     }
92     if (strDest == NULL || strSrc == NULL) {
93         SECUREC_ERROR_INVALID_PARAMTER("wcsncat_s");
94         if (strDest != NULL) {
95             strDest[0] = L'\0';
96             return EINVAL_AND_RESET;
97         }
98         return EINVAL;
99     }
100     if (count > SECUREC_WCHAR_STRING_MAX_LEN) {
101 #ifdef  SECUREC_COMPATIBLE_WIN_FORMAT
102         if (count == ((size_t)(-1))) {
103             /* Windows internal functions may pass in -1 when calling this function */
104             return SecDoCatLimitW(strDest, destMax, strSrc, destMax);
105         }
106 #endif
107         strDest[0] = L'\0';
108         SECUREC_ERROR_INVALID_RANGE("wcsncat_s");
109         return ERANGE_AND_RESET;
110     }
111     return SecDoCatLimitW(strDest, destMax, strSrc, count);
112 }
113 
114