diff options
author | 0x41CEA55 <167377970+41CEA55@users.noreply.github.com> | 2024-04-19 18:16:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-19 17:16:40 +0200 |
commit | 1b2e2cd968876ff4e7a772bf8df2517d583d9956 (patch) | |
tree | 8676f5651e41aa935b373a740477184d2b6b23e2 /src | |
parent | e75d7a620e60fd35385793f09209f8a857d4b325 (diff) |
Add strlcpy implementation (#2395)
Diffstat (limited to 'src')
-rw-r--r-- | src/include/ndpi_api.h | 12 | ||||
-rw-r--r-- | src/lib/ndpi_main.c | 17 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index acc01fb0f..ca1855532 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -2245,6 +2245,18 @@ extern "C" { void* ndpi_memmem(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len); + /** + * Copies up to -dst_len - 1- characters from -src- to -dst-, ensuring the result is + * null-terminated. Measures -src- length and handles potential null pointers. + * + * @par dst = destination buffer + * @par src = source string + * @par dst_len = size of the destination buffer, includes the null terminator + * + * @return Length of `src`. If greater or equal to -dst_len-, -dst- has been truncated. + */ + size_t ndpi_strlcpy(char* dst, const char* src, size_t dst_len); + #ifdef __cplusplus } #endif diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index ec848a0d2..815aa2e1c 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -11504,3 +11504,20 @@ void* ndpi_memmem(const void* haystack, size_t haystack_len, const void* needle, return NULL; } + +size_t ndpi_strlcpy(char *dst, const char* src, size_t dst_len) +{ + if (!dst || !src) { + return 0; + } + + size_t src_len = strlen(src); + + if (dst_len != 0) { + size_t len = (src_len < dst_len - 1) ? src_len : dst_len - 1; + memcpy(dst, src, len); + dst[len] = '\0'; + } + + return src_len; +} |