1 /* 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2024, STMicroelectronics 5 * 6 */ 7 8 #include <string.h> 9 10 /** 11 * @internal 12 * 13 * @brief Copies a string to a destination buffer with size limitation and returns the length of 14 * the destination string. 15 * 16 * This function copies up to `s_size - 1` characters from the source string `src` 17 * to the destination buffer `dst`, ensuring that the destination buffer is 18 * null-terminated. The function returns the length of the `dst` string. 19 * If the length of `src` string is greater than or equal to `d_size`, the destination 20 * buffer will be truncated. 21 * 22 * @param dst Destination buffer where the string will be copied. 23 * @param d_size Size of the destination buffer. 24 * @param src Source string to be copied. 25 * @param s_size Size of the source buffer. 26 * @return The length of the string contained in the `dst` buffer. 27 * 28 * @note If the size of the destination buffer is 0, the function does not copy any characters and 29 * the destination buffer is not null-terminated. 30 * @note The function ensures that the destination buffer is always null-terminated if `size` is 31 * greater than 0. 32 * @note The function ensures that no data is read past the end of the 'src' buffer. 33 */ 34 size_t safe_strcpy(char *dst, size_t d_size, const char *src, size_t s_size); 35 36