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