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