• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: wmemcpy_s  function
12  * Create: 2014-02-25
13  */
14 /*
15  * [Standardize-exceptions] Use unsafe function: Portability
16  * [reason] Use unsafe function to implement security function to maintain platform compatibility.
17  *          And sufficient input validation is performed before calling
18  */
19 
20 #include "securecutil.h"
21 
22 /*
23  * <FUNCTION DESCRIPTION>
24  *   The wmemcpy_s function copies n successive wide characters
25  *    from the object pointed to by src into the object pointed to by dest.t.
26  *
27  * <INPUT PARAMETERS>
28  *    dest                   Destination buffer.
29  *    destMax                Size of the destination buffer.
30  *    src                     Buffer to copy from.
31  *    count                  Number of characters to copy.
32  *
33  * <OUTPUT PARAMETERS>
34  *    dest buffer            is updated.
35  *
36  * <RETURN VALUE>
37  *    EOK                    Success
38  *    EINVAL                 dest is  NULL  and destMax != 0 and count <= destMax
39  *                           and destMax <= SECUREC_WCHAR_MEM_MAX_LEN
40  *    EINVAL_AND_RESET       dest != NULL and src is NULL and destMax != 0
41  *                           and destMax <= SECUREC_WCHAR_MEM_MAX_LEN and count <= destMax
42  *    ERANGE                 destMax > SECUREC_WCHAR_MEM_MAX_LEN or destMax is 0 or
43  *                           (count > destMax and dest is  NULL and destMax != 0
44  *                            and destMax <= SECUREC_WCHAR_MEM_MAX_LEN)
45  *    ERANGE_AND_RESET       count > destMax and dest  !=  NULL and destMax != 0
46  *                           and destMax <= SECUREC_WCHAR_MEM_MAX_LEN
47  *    EOVERLAP_AND_RESET     dest buffer and source buffer are overlapped and
48  *                           count <= destMax destMax != 0 and destMax <= SECUREC_WCHAR_MEM_MAX_LEN
49  *                           and dest  !=  NULL  and src != NULL and dest != src
50  *
51  *    if an error occurred, dest will be filled with 0 when dest and destMax valid .
52  *    If the source and destination overlap, the behavior of wmemcpy_s is undefined.
53  *    Use wmemmove_s to handle overlapping regions.
54  */
wmemcpy_s(wchar_t * dest,size_t destMax,const wchar_t * src,size_t count)55 errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count)
56 {
57     if (destMax == 0 || destMax > SECUREC_WCHAR_MEM_MAX_LEN) {
58         SECUREC_ERROR_INVALID_PARAMTER("wmemcpy_s");
59         return ERANGE;
60     }
61     if (count > destMax) {
62         SECUREC_ERROR_INVALID_PARAMTER("wmemcpy_s");
63         if (dest != NULL) {
64             (void)SECUREC_MEMSET_FUNC_OPT(dest, 0, destMax * sizeof(wchar_t));
65             return ERANGE_AND_RESET;
66         }
67         return ERANGE;
68     }
69     return memcpy_s(dest, destMax * sizeof(wchar_t), src, count * sizeof(wchar_t));
70 }
71 
72