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: wmemmove_s  function
123d58139fSopenharmony_ci * Create: 2014-02-25
133d58139fSopenharmony_ci */
143d58139fSopenharmony_ci/*
153d58139fSopenharmony_ci * [Standardize-exceptions] Use unsafe function: Portability
163d58139fSopenharmony_ci * [reason] Use unsafe function to implement security function to maintain platform compatibility.
173d58139fSopenharmony_ci *          And sufficient input validation is performed before calling
183d58139fSopenharmony_ci */
193d58139fSopenharmony_ci
203d58139fSopenharmony_ci#include "securecutil.h"
213d58139fSopenharmony_ci
223d58139fSopenharmony_ci/*
233d58139fSopenharmony_ci * <FUNCTION DESCRIPTION>
243d58139fSopenharmony_ci *   The wmemmove_s function copies n successive wide characters from the object pointed
253d58139fSopenharmony_ci *   to by src into the object pointed to by dest.
263d58139fSopenharmony_ci *
273d58139fSopenharmony_ci * <INPUT PARAMETERS>
283d58139fSopenharmony_ci *    dest                     Destination buffer.
293d58139fSopenharmony_ci *    destMax                  Size of the destination buffer.
303d58139fSopenharmony_ci *    src                      Source object.
313d58139fSopenharmony_ci *    count                    Number of bytes or character to copy.
323d58139fSopenharmony_ci *
333d58139fSopenharmony_ci * <OUTPUT PARAMETERS>
343d58139fSopenharmony_ci *    dest                     is updated.
353d58139fSopenharmony_ci *
363d58139fSopenharmony_ci * <RETURN VALUE>
373d58139fSopenharmony_ci *    EOK                      Success
383d58139fSopenharmony_ci *    EINVAL                   dest is  NULL and destMax != 0 and count <= destMax
393d58139fSopenharmony_ci *                             and destMax <= SECUREC_WCHAR_MEM_MAX_LEN
403d58139fSopenharmony_ci *    EINVAL_AND_RESET         dest != NULL and src is NULL and destMax != 0
413d58139fSopenharmony_ci *                             and destMax <= SECUREC_WCHAR_MEM_MAX_LEN and count <= destMax
423d58139fSopenharmony_ci *    ERANGE                   destMax > SECUREC_WCHAR_MEM_MAX_LEN or destMax is 0 or
433d58139fSopenharmony_ci *                             (count > destMax and dest is  NULL and destMax != 0
443d58139fSopenharmony_ci *                             and destMax <= SECUREC_WCHAR_MEM_MAX_LEN)
453d58139fSopenharmony_ci *    ERANGE_AND_RESET        count > destMax and dest  !=  NULL and destMax != 0
463d58139fSopenharmony_ci *                             and destMax <= SECUREC_WCHAR_MEM_MAX_LEN
473d58139fSopenharmony_ci *
483d58139fSopenharmony_ci *
493d58139fSopenharmony_ci *     If an error occurred, dest will  be filled with 0 when dest and destMax valid.
503d58139fSopenharmony_ci *     If some regions of the source area and the destination overlap, wmemmove_s
513d58139fSopenharmony_ci *     ensures that the original source bytes in the overlapping region are copied
523d58139fSopenharmony_ci *     before being overwritten
533d58139fSopenharmony_ci */
543d58139fSopenharmony_cierrno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count)
553d58139fSopenharmony_ci{
563d58139fSopenharmony_ci    if (destMax == 0 || destMax > SECUREC_WCHAR_MEM_MAX_LEN) {
573d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("wmemmove_s");
583d58139fSopenharmony_ci        return ERANGE;
593d58139fSopenharmony_ci    }
603d58139fSopenharmony_ci    if (count > destMax) {
613d58139fSopenharmony_ci        SECUREC_ERROR_INVALID_PARAMTER("wmemmove_s");
623d58139fSopenharmony_ci        if (dest != NULL) {
633d58139fSopenharmony_ci            (void)SECUREC_MEMSET_FUNC_OPT(dest, 0, destMax * sizeof(wchar_t));
643d58139fSopenharmony_ci            return ERANGE_AND_RESET;
653d58139fSopenharmony_ci        }
663d58139fSopenharmony_ci        return ERANGE;
673d58139fSopenharmony_ci    }
683d58139fSopenharmony_ci    return memmove_s(dest, destMax * sizeof(wchar_t), src, count * sizeof(wchar_t));
693d58139fSopenharmony_ci}
703d58139fSopenharmony_ci
71