| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*** |
| 6 | *memmove_s.c - contains memmove_s routine |
| 7 | * |
| 8 | |
| 9 | * |
| 10 | *Purpose: |
| 11 | * memmove_s() copies a source memory buffer to a destination buffer. |
| 12 | * Overlapping buffers are treated specially, to avoid propagation. |
| 13 | * |
| 14 | *Revision History: |
| 15 | * 10-07-03 AC Module created. |
| 16 | * 03-10-04 AC Return ERANGE when buffer is too small |
| 17 | * |
| 18 | *******************************************************************************/ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <errno.h> |
| 22 | #include "internal_securecrt.h" |
| 23 | #include "mbusafecrt_internal.h" |
| 24 | |
| 25 | /*** |
| 26 | *memmove - Copy source buffer to destination buffer |
| 27 | * |
| 28 | *Purpose: |
| 29 | * memmove() copies a source memory buffer to a destination memory buffer. |
| 30 | * This routine recognize overlapping buffers to avoid propagation. |
| 31 | * |
| 32 | * For cases where propagation is not a problem, memcpy_s() can be used. |
| 33 | * |
| 34 | *Entry: |
| 35 | * void *dst = pointer to destination buffer |
| 36 | * size_t sizeInBytes = size in bytes of the destination buffer |
| 37 | * const void *src = pointer to source buffer |
| 38 | * size_t count = number of bytes to copy |
| 39 | * |
| 40 | *Exit: |
| 41 | * Returns 0 if everything is ok, else return the error code. |
| 42 | * |
| 43 | *Exceptions: |
| 44 | * Input parameters are validated. Refer to the validation section of the function. |
| 45 | * On error, the error code is returned. Nothing is written to the destination buffer. |
| 46 | * |
| 47 | *******************************************************************************/ |
| 48 | |
| 49 | errno_t __cdecl memmove_s( |
| 50 | void * dst, |
| 51 | size_t sizeInBytes, |
| 52 | const void * src, |
| 53 | size_t count |
| 54 | ) |
| 55 | { |
| 56 | if (count == 0) |
| 57 | { |
| 58 | /* nothing to do */ |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | /* validation section */ |
| 63 | _VALIDATE_RETURN_ERRCODE(dst != NULL, EINVAL); |
| 64 | _VALIDATE_RETURN_ERRCODE(src != NULL, EINVAL); |
| 65 | _VALIDATE_RETURN_ERRCODE(sizeInBytes >= count, ERANGE); |
| 66 | |
| 67 | memmove(dst, src, count); |
| 68 | return 0; |
| 69 | } |
| 70 | |