13d58139fSopenharmony_ci/*
23d58139fSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
33d58139fSopenharmony_ci * Licensed under Mulan PSL v2.
43d58139fSopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
53d58139fSopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
63d58139fSopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
73d58139fSopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
83d58139fSopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
93d58139fSopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
103d58139fSopenharmony_ci * See the Mulan PSL v2 for more details.
113d58139fSopenharmony_ci * Description: wcscpy_s  function
123d58139fSopenharmony_ci * Create: 2014-02-25
133d58139fSopenharmony_ci */
143d58139fSopenharmony_ci
153d58139fSopenharmony_ci#include "securecutil.h"
163d58139fSopenharmony_ci
173d58139fSopenharmony_ciSECUREC_INLINE errno_t SecDoCpyW(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
183d58139fSopenharmony_ci{
193d58139fSopenharmony_ci    size_t srcStrLen;
203d58139fSopenharmony_ci    SECUREC_CALC_WSTR_LEN(strSrc, destMax, &srcStrLen);
213d58139fSopenharmony_ci
223d58139fSopenharmony_ci    if (srcStrLen == destMax) {
233d58139fSopenharmony_ci        strDest[0] = L'\0';
243d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
253d58139fSopenharmony_ci        return ERANGE_AND_RESET;
263d58139fSopenharmony_ci    }
273d58139fSopenharmony_ci    if (strDest == strSrc) {
283d58139fSopenharmony_ci        return EOK;
293d58139fSopenharmony_ci    }
303d58139fSopenharmony_ci
313d58139fSopenharmony_ci    if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, srcStrLen)) {
323d58139fSopenharmony_ci        /* Performance optimization, srcStrLen is single character length  include '\0' */
333d58139fSopenharmony_ci        SECUREC_MEMCPY_WARP_OPT(strDest, strSrc, (srcStrLen + 1) * sizeof(wchar_t));
343d58139fSopenharmony_ci        return EOK;
353d58139fSopenharmony_ci    } else {
363d58139fSopenharmony_ci        strDest[0] = L'\0';
373d58139fSopenharmony_ci        SECUREC_ERROR_BUFFER_OVERLAP("wcscpy_s");
383d58139fSopenharmony_ci        return EOVERLAP_AND_RESET;
393d58139fSopenharmony_ci    }
403d58139fSopenharmony_ci}
413d58139fSopenharmony_ci
423d58139fSopenharmony_ci/*
433d58139fSopenharmony_ci * <FUNCTION DESCRIPTION>
443d58139fSopenharmony_ci *   The wcscpy_s function copies the wide string pointed to by strSrc
453d58139fSopenharmony_ci *   (including the terminating null wide character) into the array pointed to by strDest
463d58139fSopenharmony_ci
473d58139fSopenharmony_ci * <INPUT PARAMETERS>
483d58139fSopenharmony_ci *    strDest               Destination string buffer
493d58139fSopenharmony_ci *    destMax               Size of the destination string buffer.
503d58139fSopenharmony_ci *    strSrc                Null-terminated source string buffer.
513d58139fSopenharmony_ci *
523d58139fSopenharmony_ci * <OUTPUT PARAMETERS>
533d58139fSopenharmony_ci *    strDest               is updated.
543d58139fSopenharmony_ci *
553d58139fSopenharmony_ci * <RETURN VALUE>
563d58139fSopenharmony_ci *    EOK                   Success
573d58139fSopenharmony_ci *    EINVAL                strDest is  NULL and destMax != 0 and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
583d58139fSopenharmony_ci *    EINVAL_AND_RESET      strDest != NULL and strSrc is NULL and destMax != 0
593d58139fSopenharmony_ci *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
603d58139fSopenharmony_ci *    ERANGE                destMax > SECUREC_WCHAR_STRING_MAX_LEN or destMax is 0
613d58139fSopenharmony_ci *    ERANGE_AND_RESET      destMax <= length of strSrc and strDest != strSrc
623d58139fSopenharmony_ci *                          and strDest != NULL and strSrc != NULL and destMax != 0
633d58139fSopenharmony_ci *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN and not overlap
643d58139fSopenharmony_ci *    EOVERLAP_AND_RESET    dest buffer and source buffer are overlapped and destMax != 0
653d58139fSopenharmony_ci *                          and destMax <= SECUREC_WCHAR_STRING_MAX_LEN
663d58139fSopenharmony_ci *                          and strDest != NULL and strSrc !=NULL and strDest != strSrc
673d58139fSopenharmony_ci *
683d58139fSopenharmony_ci *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
693d58139fSopenharmony_ci */
703d58139fSopenharmony_cierrno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc)
713d58139fSopenharmony_ci{
723d58139fSopenharmony_ci    if (destMax == 0 || destMax > SECUREC_WCHAR_STRING_MAX_LEN) {
733d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_RANGE("wcscpy_s");
743d58139fSopenharmony_ci        return ERANGE;
753d58139fSopenharmony_ci    }
763d58139fSopenharmony_ci    if (strDest == NULL || strSrc == NULL) {
773d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("wcscpy_s");
783d58139fSopenharmony_ci        if (strDest != NULL) {
793d58139fSopenharmony_ci            strDest[0] = L'\0';
803d58139fSopenharmony_ci            return EINVAL_AND_RESET;
813d58139fSopenharmony_ci        }
823d58139fSopenharmony_ci        return EINVAL;
833d58139fSopenharmony_ci    }
843d58139fSopenharmony_ci    return SecDoCpyW(strDest, destMax, strSrc);
853d58139fSopenharmony_ci}
863d58139fSopenharmony_ci
87