• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
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 wmemmove_s function copies n successive wide characters from the object pointed
25  *   to by src into the object pointed to by dest.
26  *
27  * <INPUT PARAMETERS>
28  *    dest                     Destination buffer.
29  *    destMax                  Size of the destination buffer.
30  *    src                      Source object.
31  *    count                    Number of bytes or character to copy.
32  *
33  * <OUTPUT PARAMETERS>
34  *    dest                     is updated.
35  *
36  * <RETURN VALUE>
37  *    EOK                      Success
38  *    EINVAL                   dest is  NULL and destMax != 0 and destMax <= SECUREC_WCHAR_MEM_MAX_LEN
39  *                             and count <= destMax
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  *
48  *
49  *     If an error occurred, dest will  be filled with 0 when dest and destMax valid.
50  *     If some regions of the source area and the destination overlap, wmemmove_s
51  *     ensures that the original source bytes in the overlapping region are copied
52  *     before being overwritten
53  ********************************************************************************
54  */
wmemmove_s(wchar_t * dest,size_t destMax,const wchar_t * src,size_t count)55 errno_t wmemmove_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("wmemmove_s");
59         return ERANGE;
60     }
61     if (count > destMax) {
62         SECUREC_ERROR_INVALID_PARAMTER("wmemmove_s");
63         if (dest != NULL) {
64             (void)memset(dest, 0, destMax * sizeof(wchar_t));
65             return ERANGE_AND_RESET;
66         }
67         return ERANGE;
68     }
69     return memmove_s(dest, destMax * sizeof(wchar_t), src, count * sizeof(wchar_t));
70 }
71 
72 
73