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: strncpy_s  function
12 * Create: 2014-02-25
13 */
14/*
15 * [Standardize-exceptions] Use unsafe function: Performance-sensitive
16 * [reason] Always used in the performance critical path,
17 *          and sufficient input validation is performed before calling
18 */
19
20#include "securecutil.h"
21
22#if defined(SECUREC_COMPATIBLE_WIN_FORMAT)
23#define SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count) \
24    (((destMax) > 0 && (destMax) <= SECUREC_STRING_MAX_LEN && (strDest) != NULL && (strSrc) != NULL && \
25    ((count) <= SECUREC_STRING_MAX_LEN || (count) == ((size_t)(-1))) && (count) > 0))
26#else
27#define SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count) \
28    (((destMax) > 0 && (destMax) <= SECUREC_STRING_MAX_LEN && (strDest) != NULL && (strSrc) != NULL && \
29    (count) <= SECUREC_STRING_MAX_LEN && (count) > 0))
30#endif
31
32/*
33 * Check Src Count Range
34 */
35SECUREC_INLINE errno_t CheckSrcCountRange(char *strDest, size_t destMax, const char *strSrc, size_t count)
36{
37    size_t tmpDestMax = destMax;
38    size_t tmpCount = count;
39    const char *endPos = strSrc;
40
41    /* Use destMax and  count as boundary checker and destMax must be greater than zero */
42    while (*(endPos) != '\0' && tmpDestMax > 0 && tmpCount > 0) {
43        ++endPos;
44        --tmpCount;
45        --tmpDestMax;
46    }
47    if (tmpDestMax == 0) {
48        strDest[0] = '\0';
49        SECUREC_ERROR_INVALID_RANGE("strncpy_s");
50        return ERANGE_AND_RESET;
51    }
52    return EOK;
53}
54
55/*
56 * Handling errors, when dest equal src return EOK
57 */
58errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count)
59{
60    if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
61        SECUREC_ERROR_INVALID_RANGE("strncpy_s");
62        return ERANGE;
63    }
64    if (strDest == NULL || strSrc == NULL) {
65        SECUREC_ERROR_INVALID_PARAMTER("strncpy_s");
66        if (strDest != NULL) {
67            strDest[0] = '\0';
68            return EINVAL_AND_RESET;
69        }
70        return EINVAL;
71    }
72    if (count > SECUREC_STRING_MAX_LEN) {
73        strDest[0] = '\0';      /* Clear dest string */
74        SECUREC_ERROR_INVALID_RANGE("strncpy_s");
75        return ERANGE_AND_RESET;
76    }
77    if (count == 0) {
78        strDest[0] = '\0';
79        return EOK;
80    }
81    return CheckSrcCountRange(strDest, destMax, strSrc, count);
82}
83
84/*
85 * <FUNCTION DESCRIPTION>
86 *   The strncpy_s function copies not more than n successive characters (not including the terminating null character)
87 *                     from the array pointed to by strSrc to the array pointed to by strDest.
88 *
89 * <INPUT PARAMETERS>
90 *    strDest                          Destination string.
91 *    destMax                          The size of the destination string, in characters.
92 *    strSrc                            Source string.
93 *    count                              Number of characters to be copied.
94 *
95 * <OUTPUT PARAMETERS>
96 *    strDest                           is updated
97 *
98 * <RETURN VALUE>
99 *    EOK                               Success
100 *    EINVAL                          strDest is  NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
101 *    EINVAL_AND_RESET       strDest !=  NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
102 *    ERANGE                         destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
103 *    ERANGE_AND_RESET      strDest have not enough space  and all other parameters are valid  and not overlap
104 *    EOVERLAP_AND_RESET   dest buffer and source buffer are overlapped and all  parameters are valid
105 *
106 *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
107 */
108errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count)
109{
110    if (SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count)) {
111        size_t minCpLen;        /* Use it to store the maxi length limit */
112        if (count < destMax) {
113            SECUREC_CALC_STR_LEN(strSrc, count, &minCpLen); /* No ending terminator */
114        } else {
115            size_t tmpCount = destMax;
116#ifdef  SECUREC_COMPATIBLE_WIN_FORMAT
117            if (count == ((size_t)(-1))) {
118                tmpCount = destMax - 1;
119            }
120#endif
121            SECUREC_CALC_STR_LEN(strSrc, tmpCount, &minCpLen); /* No ending terminator */
122            if (minCpLen == destMax) {
123                strDest[0] = '\0';
124                SECUREC_ERROR_INVALID_RANGE("strncpy_s");
125                return ERANGE_AND_RESET;
126            }
127        }
128        if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, minCpLen) || strDest == strSrc) {
129            /* Not overlap */
130            SECUREC_MEMCPY_WARP_OPT(strDest, strSrc, minCpLen);    /* Copy string without terminator */
131            strDest[minCpLen] = '\0';
132            return EOK;
133        } else {
134            strDest[0] = '\0';
135            SECUREC_ERROR_BUFFER_OVERLAP("strncpy_s");
136            return EOVERLAP_AND_RESET;
137        }
138    }
139    return strncpy_error(strDest, destMax, strSrc, count);
140}
141
142#if SECUREC_EXPORT_KERNEL_SYMBOL
143EXPORT_SYMBOL(strncpy_s);
144#endif
145
146