Merge "feat(lib): implement memcpy_s in lib" into integration

This commit is contained in:
Manish Pandey 2023-06-20 15:34:24 +02:00 committed by TrustedFirmware Code Review
commit 3a95b5d5ea
3 changed files with 67 additions and 0 deletions
include/lib/libc
lib/libc

View file

@ -5,6 +5,7 @@
*/
/*
* Portions copyright (c) 2018-2020, ARM Limited and Contributors.
* Portions copyright (c) 2023, Intel Corporation. All rights reserved.
* All rights reserved.
*/
@ -14,6 +15,7 @@
#include <stddef.h>
void *memcpy(void *dst, const void *src, size_t len);
int memcpy_s(void *dst, size_t dsize, void *src, size_t ssize);
void *memmove(void *dst, const void *src, size_t len);
int memcmp(const void *s1, const void *s2, size_t len);
int strcmp(const char *s1, const char *s2);

View file

@ -11,6 +11,7 @@ LIBC_SRCS := $(addprefix lib/libc/, \
memchr.c \
memcmp.c \
memcpy.c \
memcpy_s.c \
memmove.c \
memrchr.c \
memset.c \

64
lib/libc/memcpy_s.c Normal file
View file

@ -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;
}