From: Victor Julien Date: Fri, 19 Jul 2019 13:07:46 +0000 (+0200) Subject: string: making shortening function global X-Git-Tag: suricata-5.0.0-rc1~172 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8a59ad60960fba187b78e88949bae37a664dbf94;p=thirdparty%2Fsuricata.git string: making shortening function global --- diff --git a/src/util-device.c b/src/util-device.c index 1187c62532..d6cc5b6b94 100644 --- a/src/util-device.c +++ b/src/util-device.c @@ -19,6 +19,7 @@ #include "conf.h" #include "util-device.h" #include "util-ioctl.h" +#include "util-misc.h" #include "device-storage.h" @@ -189,33 +190,6 @@ const char *LiveGetDeviceName(int number) return NULL; } -static void ShortenString(const char *input, - char *output, size_t output_size, char c) -{ - const size_t str_len = strlen(input); - size_t half = (output_size - 1) / 2; - - /* If the output size is an even number */ - if (half * 2 == (output_size - 1)) { - half = half - 1; - } - - size_t spaces = (output_size - 1) - (half * 2); - - /* Add the first half to the new string */ - snprintf(output, half+1, "%s", input); - - /* Add the amount of spaces wanted */ - size_t length = half; - for (size_t i = half; i < half + spaces; i++) { - char s[2] = ""; - snprintf(s, sizeof(s), "%c", c); - length = strlcat(output, s, output_size); - } - - snprintf(output + length, half + 1, "%s", input + (str_len - half)); -} - /** \internal * \brief Shorten a device name that is to long * diff --git a/src/util-misc.c b/src/util-misc.c index e41554a078..c706215a8f 100644 --- a/src/util-misc.c +++ b/src/util-misc.c @@ -217,6 +217,33 @@ int ParseSizeStringU64(const char *size, uint64_t *res) return 0; } +void ShortenString(const char *input, + char *output, size_t output_size, char c) +{ + const size_t str_len = strlen(input); + size_t half = (output_size - 1) / 2; + + /* If the output size is an even number */ + if (half * 2 == (output_size - 1)) { + half = half - 1; + } + + size_t spaces = (output_size - 1) - (half * 2); + + /* Add the first half to the new string */ + snprintf(output, half+1, "%s", input); + + /* Add the amount of spaces wanted */ + size_t length = half; + for (size_t i = half; i < half + spaces; i++) { + char s[2] = ""; + snprintf(s, sizeof(s), "%c", c); + length = strlcat(output, s, output_size); + } + + snprintf(output + length, half + 1, "%s", input + (str_len - half)); +} + /*********************************Unittests********************************/ #ifdef UNITTESTS diff --git a/src/util-misc.h b/src/util-misc.h index 2245f82933..e39e487952 100644 --- a/src/util-misc.h +++ b/src/util-misc.h @@ -52,4 +52,7 @@ void UtilMiscRegisterTests(void); void ParseSizeInit(void); void ParseSizeDeinit(void); +void ShortenString(const char *input, + char *output, size_t output_size, char c); + #endif /* __UTIL_MISC_H__ */