diff --git a/lib/include/internal/string.h b/lib/include/internal/string.h new file mode 100644 index 00000000000..17ac9af1e44 --- /dev/null +++ b/lib/include/internal/string.h @@ -0,0 +1,10 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2024, STMicroelectronics + * + */ + +#include + +size_t strlcpy(char *dest, const char *src, size_t size); diff --git a/lib/utils/CMakeLists.txt b/lib/utils/CMakeLists.txt new file mode 100644 index 00000000000..dd4c75935cf --- /dev/null +++ b/lib/utils/CMakeLists.txt @@ -0,0 +1 @@ +collect (PROJECT_LIB_SOURCES string.c) diff --git a/lib/utils/string.c b/lib/utils/string.c new file mode 100644 index 00000000000..e438321eb76 --- /dev/null +++ b/lib/utils/string.c @@ -0,0 +1,53 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2024, STMicroelectronics + * + */ + +#include + +/** + * @internal + * + * @brief Copies a string to a destination buffer with size limitation and returns the length of + * the source string. + * + * This function copies up to `size - 1` characters from the source string `src` + * to the destination buffer `dest`, ensuring that the destination buffer is + * null-terminated. The function returns the length of the source string `src`. + * If the length of `src` is greater than or equal to `size`, the destination + * buffer will be truncated. + * + * @param dst Destination buffer where the string will be copied. + * @param src Source string to be copied. + * @param size Size of the destination buffer. + * @return The length of the source string `src`. + * + * @note If the size of the destination buffer is 0, the function does not copy any characters and + * the destination buffer is not null-terminated. + * @note The function ensures that the destination buffer is always null-terminated if `size` is + * greater than 0. + * @note: this code is inspired from the strlcpy.c file from freeBSD . + */ + +size_t +strlcpy(char *dst, const char *src, size_t size) +{ + size_t nleft = size; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + *dst = *src++; + if (*dst++ == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0 && size != 0) + *dst = '\0'; /* NUL-terminate dst */ + + return strlen(src); +} diff --git a/open-amp/lib/CMakeLists.txt b/open-amp/lib/CMakeLists.txt index f8f9e2fcc6d..145116711db 100644 --- a/open-amp/lib/CMakeLists.txt +++ b/open-amp/lib/CMakeLists.txt @@ -10,6 +10,7 @@ collect (PROJECT_LIB_SOURCES version.c) add_subdirectory (virtio) add_subdirectory (rpmsg) add_subdirectory (remoteproc) +add_subdirectory (utils) if (WITH_VIRTIO_MMIO_DRV) add_subdirectory (virtio_mmio) endif (WITH_VIRTIO_MMIO_DRV) @@ -23,6 +24,7 @@ set (OPENAMP_LIB open_amp) configure_file(version.h.in ${PROJECT_BINARY_DIR}/include/generated/openamp/version_def.h) collect (PROJECT_INC_DIRS " ${PROJECT_BINARY_DIR}/include/generated/openamp") +collect (PROJECT_INC_DIRS " ${PROJECT_BINARY_DIR}/include/internal") if (NOT CMAKE_INSTALL_LIBDIR) set (CMAKE_INSTALL_LIBDIR "lib")