From be58c81aff4cd4c0ccf43dbd7998da4a6a08c03b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 19:43:51 +0200 Subject: Adding upstream version 2.10.0+dfsg. Signed-off-by: Daniel Baumann --- lib/libc/memcpy_s.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 lib/libc/memcpy_s.c (limited to 'lib/libc/memcpy_s.c') 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 +#include +#include + +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; +} -- cgit v1.2.3