diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-02-07 15:47:16 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-02-07 15:47:16 +0000 |
commit | 11034d766c94f50f7c21ab6e9cffb07da736ea8f (patch) | |
tree | eae79e0f8ca077035efcb5a939a751eb6bf60cd2 | |
parent | c56696612bb1e20fe907f6285866c4cb841a38e4 (diff) | |
download | rspamd-11034d766c94f50f7c21ab6e9cffb07da736ea8f.tar.gz rspamd-11034d766c94f50f7c21ab6e9cffb07da736ea8f.zip |
[Minor] Add rspamd_null_safe_copy routine
-rw-r--r-- | src/libutil/str_util.c | 26 | ||||
-rw-r--r-- | src/libutil/str_util.h | 12 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 7078415d3..25c88acee 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -391,6 +391,32 @@ rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz) return (d - dst); } +gsize +rspamd_null_safe_copy (const gchar *src, gsize srclen, + gchar *dest, gsize destlen) +{ + gsize copied = 0, si = 0, di = 0; + + if (destlen == 0) { + return 0; + } + + while (si < srclen && di + 1 < destlen) { + if (src[si] != '\0') { + dest[di++] = src[si++]; + copied ++; + } + else { + si ++; + } + } + + dest[di] = '\0'; + + return copied; +} + + size_t rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz) { diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 72e0ab56f..46b74001b 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -89,6 +89,18 @@ gsize rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz); # define rspamd_strlcpy rspamd_strlcpy_fast #endif +/** + * Copies `srclen` characters from `src` to `dst` ignoring \0 + * @param src + * @param srclen + * @param dest + * @param destlen + * @return number of bytes copied + */ +gsize +rspamd_null_safe_copy (const gchar *src, gsize srclen, + gchar *dest, gsize destlen); + /* * Try to convert string of length to long */ |