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