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: wcsncpy_s function
12 * Create: 2014-02-25
13 */
14
15 #include "securecutil.h"
16
SecDoCpyLimitW(wchar_t * strDest,size_t destMax,const wchar_t * strSrc,size_t count)17 SECUREC_INLINE errno_t SecDoCpyLimitW(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
18 {
19 size_t srcStrLen;
20 if (count < destMax) {
21 SECUREC_CALC_WSTR_LEN(strSrc, count, &srcStrLen);
22 } else {
23 SECUREC_CALC_WSTR_LEN(strSrc, destMax, &srcStrLen);
24 }
25 if (srcStrLen == destMax) {
26 strDest[0] = L'\0';
27 SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
28 return ERANGE_AND_RESET;
29 }
30 if (strDest == strSrc) {
31 return EOK;
32 }
33 if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, srcStrLen)) {
34 /* Performance optimization srcStrLen not include '\0' */
35 SECUREC_MEMCPY_WARP_OPT(strDest, strSrc, srcStrLen * sizeof(wchar_t));
36 *(strDest + srcStrLen) = L'\0';
37 return EOK;
38 } else {
39 strDest[0] = L'\0';
40 SECUREC_ERROR_BUFFER_OVERLAP("wcsncpy_s");
41 return EOVERLAP_AND_RESET;
42 }
43 }
44
45 /*
46 * <FUNCTION DESCRIPTION>
47 * The wcsncpy_s function copies not more than n successive wide characters
48 * (not including the terminating null wide character)
49 * from the array pointed to by strSrc to the array pointed to by strDest
50 *
51 * <INPUT PARAMETERS>
52 * strDest Destination string.
53 * destMax The size of the destination string, in characters.
54 * strSrc Source string.
55 * count Number of characters to be copied.
56 *
57 * <OUTPUT PARAMETERS>
58 * strDest is updated
59 *
60 * <RETURN VALUE>
61 * EOK Success
62 * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
63 * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0
64 * and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
65 * ERANGE destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
66 * ERANGE_AND_RESET count > SECUREC_WCHAR_STRING_MAX_LEN or
67 * (destMax <= length of strSrc and destMax <= count and strDest != strSrc
68 * and strDest != NULL and strSrc != NULL and destMax != 0 and
69 * destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap)
70 * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
71 *
72 *
73 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
74 */
wcsncpy_s(wchar_t * strDest,size_t destMax,const wchar_t * strSrc,size_t count)75 errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count)
76 {
77 if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
78 SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
79 return ERANGE;
80 }
81 if (strDest == NULL || strSrc == NULL) {
82 SECUREC_ERROR_INVALID_PARAMTER("wcsncpy_s");
83 if (strDest != NULL) {
84 strDest[0] = L'\0';
85 return EINVAL_AND_RESET;
86 }
87 return EINVAL;
88 }
89 if (count > SECUREC_WCHAR_STRING_MAX_LEN) {
90 #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
91 if (count == (size_t)(-1)) {
92 return SecDoCpyLimitW(strDest, destMax, strSrc, destMax - 1);
93 }
94 #endif
95 strDest[0] = L'\0'; /* Clear dest string */
96 SECUREC_ERROR_INVALID_RANGE("wcsncpy_s");
97 return ERANGE_AND_RESET;
98 }
99
100 if (count == 0) {
101 strDest[0] = L'\0';
102 return EOK;
103 }
104
105 return SecDoCpyLimitW(strDest, destMax, strSrc, count);
106 }
107
108