From: Florian Forster Date: Sat, 20 Sep 2014 05:11:17 +0000 (+0200) Subject: common.[ch]: Move escape_string() to here. X-Git-Tag: collectd-5.5.0~182 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=c62cf94a06c708594aa98698edc22ae1445a6d82;p=collectd.git common.[ch]: Move escape_string() to here. --- diff --git a/src/csv.c b/src/csv.c index e9a409d8..b6e535ee 100644 --- a/src/csv.c +++ b/src/csv.c @@ -25,7 +25,6 @@ #include "plugin.h" #include "common.h" #include "utils_cache.h" -#include "utils_parse_option.h" /* * Private variables diff --git a/src/daemon/common.c b/src/daemon/common.c index 5386739f..535dfad3 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -373,6 +373,60 @@ int strsubstitute (char *str, char c_from, char c_to) return (ret); } /* int strsubstitute */ +int escape_string (char *buffer, size_t buffer_size) +{ + char *temp; + size_t i; + size_t j; + + /* Check if we need to escape at all first */ + temp = strpbrk (buffer, " \t\"\\"); + if (temp == NULL) + return (0); + + if (buffer_size < 3) + return (EINVAL); + + temp = (char *) malloc (buffer_size); + if (temp == NULL) + return (ENOMEM); + memset (temp, 0, buffer_size); + + temp[0] = '"'; + j = 1; + + for (i = 0; i < buffer_size; i++) + { + if (buffer[i] == 0) + { + break; + } + else if ((buffer[i] == '"') || (buffer[i] == '\\')) + { + if (j > (buffer_size - 4)) + break; + temp[j] = '\\'; + temp[j + 1] = buffer[i]; + j += 2; + } + else + { + if (j > (buffer_size - 3)) + break; + temp[j] = buffer[i]; + j++; + } + } + + assert ((j + 1) < buffer_size); + temp[j] = '"'; + temp[j + 1] = 0; + + sstrncpy (buffer, temp, buffer_size); + sfree (temp); + return (0); +} /* int escape_string */ + int strunescape (char *buf, size_t buf_len) { size_t i; diff --git a/src/daemon/common.h b/src/daemon/common.h index 434ed019..da21cad9 100644 --- a/src/daemon/common.h +++ b/src/daemon/common.h @@ -186,6 +186,27 @@ int strjoin (char *dst, size_t dst_len, char **fields, size_t fields_num, const */ int escape_slashes (char *buffer, size_t buffer_size); +/** + * NAME + * escape_string + * + * DESCRIPTION + * escape_string quotes and escapes a string to be usable with collectd's + * plain text protocol. "simple" strings are left as they are, for example if + * buffer is 'simple' before the call, it will remain 'simple'. However, if + * buffer contains 'more "complex"' before the call, the returned buffer will + * contain '"more \"complex\""'. + * + * If the buffer is too small to contain the escaped string, the string will + * be truncated. However, leading and trailing double quotes, as well as an + * ending null byte are guaranteed. + * + * RETURN VALUE + * Returns zero on success, even if the string was truncated. Non-zero on + * failure. + */ +int escape_string (char *buffer, size_t buffer_size); + /* * NAME * replace_special diff --git a/src/daemon/utils_parse_option.c b/src/daemon/utils_parse_option.c index 56e65ea5..7f06f295 100644 --- a/src/daemon/utils_parse_option.c +++ b/src/daemon/utils_parse_option.c @@ -155,55 +155,4 @@ int parse_option (char **ret_buffer, char **ret_key, char **ret_value) return (0); } /* int parse_option */ -int escape_string (char *buffer, size_t buffer_size) -{ - char *temp; - size_t i; - size_t j; - - /* Check if we need to escape at all first */ - temp = strpbrk (buffer, " \t\"\\"); - if (temp == NULL) - return (0); - - temp = (char *) malloc (buffer_size); - if (temp == NULL) - return (-1); - memset (temp, 0, buffer_size); - - temp[0] = '"'; - j = 1; - - for (i = 0; i < buffer_size; i++) - { - if (buffer[i] == 0) - { - break; - } - else if ((buffer[i] == '"') || (buffer[i] == '\\')) - { - if (j > (buffer_size - 4)) - break; - temp[j] = '\\'; - temp[j + 1] = buffer[i]; - j += 2; - } - else - { - if (j > (buffer_size - 3)) - break; - temp[j] = buffer[i]; - j++; - } - } - - assert ((j + 1) < buffer_size); - temp[j] = '"'; - temp[j + 1] = 0; - - sstrncpy (buffer, temp, buffer_size); - sfree (temp); - return (0); -} /* int escape_string */ - /* vim: set sw=2 ts=8 tw=78 et : */ diff --git a/src/daemon/utils_parse_option.h b/src/daemon/utils_parse_option.h index 01b73d16..885a6a33 100644 --- a/src/daemon/utils_parse_option.h +++ b/src/daemon/utils_parse_option.h @@ -30,8 +30,6 @@ int parse_string (char **ret_buffer, char **ret_string); int parse_option (char **ret_buffer, char **ret_key, char **ret_value); -int escape_string (char *buffer, size_t buffer_size); - #endif /* UTILS_PARSE_OPTION */ /* vim: set sw=2 ts=8 tw=78 et : */ diff --git a/src/utils_format_json.c b/src/utils_format_json.c index 31b83c37..699c74e2 100644 --- a/src/utils_format_json.c +++ b/src/utils_format_json.c @@ -31,7 +31,7 @@ #include "utils_cache.h" #include "utils_format_json.h" -static int escape_string (char *buffer, size_t buffer_size, /* {{{ */ +static int json_escape_string (char *buffer, size_t buffer_size, /* {{{ */ const char *string) { size_t src_pos; @@ -75,7 +75,7 @@ static int escape_string (char *buffer, size_t buffer_size, /* {{{ */ #undef BUFFER_ADD return (0); -} /* }}} int escape_string */ +} /* }}} int json_escape_string */ static int values_to_json (char *buffer, size_t buffer_size, /* {{{ */ const data_set_t *ds, const value_list_t *vl, int store_rates) @@ -274,7 +274,7 @@ static int meta_data_to_json (char *buffer, size_t buffer_size, /* {{{ */ if (meta_data_get_string (meta, key, &value) == 0) { char temp[512] = ""; - escape_string (temp, sizeof (temp), value); + json_escape_string (temp, sizeof (temp), value); sfree (value); BUFFER_ADD (",\"%s\":%s", key, temp); } @@ -362,7 +362,7 @@ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */ BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval)); #define BUFFER_ADD_KEYVAL(key, value) do { \ - status = escape_string (temp, sizeof (temp), (value)); \ + status = json_escape_string (temp, sizeof (temp), (value)); \ if (status != 0) \ return (status); \ BUFFER_ADD (",\"%s\":%s", (key), temp); \ diff --git a/src/write_http.c b/src/write_http.c index 7a1fbd04..d97bc9b5 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -27,7 +27,6 @@ #include "plugin.h" #include "common.h" #include "utils_cache.h" -#include "utils_parse_option.h" #include "utils_format_json.h" #if HAVE_PTHREAD_H diff --git a/src/write_tsdb.c b/src/write_tsdb.c index 2eca77e1..aeed6350 100644 --- a/src/write_tsdb.c +++ b/src/write_tsdb.c @@ -47,7 +47,6 @@ #include "configfile.h" #include "utils_cache.h" -#include "utils_parse_option.h" #include #include