aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c94
1 files changed, 88 insertions, 6 deletions
diff --git a/src/util.c b/src/util.c
index eb1b77e0c..78be8cbaf 100644
--- a/src/util.c
+++ b/src/util.c
@@ -224,7 +224,7 @@ make_unix_socket (const gchar *path, struct sockaddr_un *addr, gboolean is_serve
addr->sun_family = AF_UNIX;
- g_strlcpy (addr->sun_path, path, sizeof (addr->sun_path));
+ rspamd_strlcpy (addr->sun_path, path, sizeof (addr->sun_path));
#ifdef FREEBSD
addr->sun_len = SUN_LEN (addr);
#endif
@@ -1124,17 +1124,57 @@ rspamd_snprintf (gchar *buf, size_t max, const gchar *fmt, ...)
return p - buf;
}
+gchar *
+rspamd_escape_string (gchar *dst, const gchar *src, gsize len)
+{
+ gchar *buf = dst, *last = dst + len;
+ guint8 c;
+ const gchar *p = src;
+
+ while (*p && buf < last) {
+ c = *p++;
+ if (G_UNLIKELY ((c & 0x80))) {
+ c &= 0x7F;
+ if (last - buf >= 3) {
+ *buf++ = 'M';
+ *buf++ = '-';
+ }
+ }
+
+ if (G_UNLIKELY ( g_ascii_iscntrl (c))) {
+ if (c == '\n') {
+ *buf++ = ' ';
+ }
+ else if (c == '\t') {
+ *buf++ = '\t';
+ }
+ else {
+ *buf++ = '^';
+ if (buf != last) {
+ *buf++ = c ^ 0100;
+ }
+ }
+ }
+ else {
+ *buf++ = c;
+ }
+ }
+
+ *buf = '\0';
+
+ return buf;
+}
gchar *
rspamd_vsnprintf (gchar *buf, size_t max, const gchar *fmt, va_list args)
{
- gchar *p, zero, *last;
- gint d;
+ gchar *p, zero, *last;
+ gint d;
long double f, scale;
size_t len, slen;
- gint64 i64;
- guint64 ui64;
- guint width, sign, hex, max_width, frac_width, i;
+ gint64 i64;
+ guint64 ui64;
+ guint width, sign, hex, max_width, frac_width, i;
f_str_t *v;
if (max == 0) {
@@ -1253,6 +1293,25 @@ rspamd_vsnprintf (gchar *buf, size_t max, const gchar *fmt, va_list args)
continue;
+ case 'S':
+ p = va_arg(args, gchar *);
+ if (p == NULL) {
+ p = "(NULL)";
+ }
+
+ if (slen == (size_t) -1) {
+ buf = rspamd_escape_string (buf, p, last - buf);
+
+ } else {
+ len = (buf + slen < last) ? slen : (size_t) (last - buf);
+
+ buf = rspamd_escape_string (buf, p, len);
+ }
+
+ fmt++;
+
+ continue;
+
case 'O':
i64 = (gint64) va_arg (args, off_t);
sign = 1;
@@ -1559,6 +1618,29 @@ g_ptr_array_unref (GPtrArray *array)
}
#endif
+gsize
+rspamd_strlcpy (gchar *dst, const gchar *src, gsize siz)
+{
+ gchar *d = dst;
+ const gchar *s = src;
+ gsize n = siz;
+
+ /* Copy as many bytes as will fit */
+ if (n != 0) {
+ while (--n != 0) {
+ if ((*d++ = *s++) == '\0') {
+ break;
+ }
+ }
+ }
+
+ if (n == 0 && siz != 0) {
+ *d = '\0';
+ }
+
+ return (s - src - 1); /* count does not include NUL */
+}
+
/*
* vi:ts=4
*/