diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 17:43:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 17:43:51 +0000 |
commit | be58c81aff4cd4c0ccf43dbd7998da4a6a08c03b (patch) | |
tree | 779c248fb61c83f65d1f0dc867f2053d76b4e03a /lib/libc/memcpy_s.c | |
parent | Initial commit. (diff) | |
download | arm-trusted-firmware-be58c81aff4cd4c0ccf43dbd7998da4a6a08c03b.tar.xz arm-trusted-firmware-be58c81aff4cd4c0ccf43dbd7998da4a6a08c03b.zip |
Adding upstream version 2.10.0+dfsg.upstream/2.10.0+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/libc/memcpy_s.c')
-rw-r--r-- | lib/libc/memcpy_s.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/libc/memcpy_s.c b/lib/libc/memcpy_s.c new file mode 100644 index 0000000..26953bf --- /dev/null +++ b/lib/libc/memcpy_s.c @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved. + * Copyright (c) 2023, Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include <errno.h> +#include <stddef.h> +#include <string.h> + +int memcpy_s(void *dst, size_t dsize, void *src, size_t ssize) +{ + unsigned int *s = (unsigned int *)src; + unsigned int *d = (unsigned int *)dst; + + /* + * Check source and destination size is NULL + */ + if ((dst == NULL) || (src == NULL)) { + return -ENOMEM; + } + + /* + * Check source and destination size validity + */ + if ((dsize == 0) || (ssize == 0)) { + return -ERANGE; + } + + /* + * Check both source and destination size range + */ + if ((ssize > dsize) || (dsize > ssize)) { + return -EINVAL; + } + + /* + * Check both source and destination address overlapping + * When (s > d < s + ssize) + * Or (d > s < d + dsize) + */ + + if (d > s) { + if ((d) < (s + ssize)) { + return -EOPNOTSUPP; + } + } + + if (s > d) { + if ((s) < (d + dsize)) { + return -EOPNOTSUPP; + } + } + + /* + * Start copy process when there is no error + */ + while (ssize--) { + d[ssize] = s[ssize]; + } + + return 0; +} |