summaryrefslogtreecommitdiffstats
path: root/src/lookups/lf_quote.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:16:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 16:16:13 +0000
commite90fcc54809db2591dc083f43ef54c6ec8c60847 (patch)
treef20bc206c3c2d5d59d37c46c5cf5d53a20642556 /src/lookups/lf_quote.c
parentInitial commit. (diff)
downloadexim4-upstream.tar.xz
exim4-upstream.zip
Adding upstream version 4.96.upstream/4.96upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lookups/lf_quote.c')
-rw-r--r--src/lookups/lf_quote.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/lookups/lf_quote.c b/src/lookups/lf_quote.c
new file mode 100644
index 0000000..6f4143d
--- /dev/null
+++ b/src/lookups/lf_quote.c
@@ -0,0 +1,63 @@
+/*************************************************
+* Exim - an Internet mail transport agent *
+*************************************************/
+
+/* Copyright (c) University of Cambridge 1995 - 2018 */
+/* See the file NOTICE for conditions of use and distribution. */
+
+
+#include "../exim.h"
+#include "lf_functions.h"
+
+
+/*************************************************
+* Add string to result, quoting if necessary *
+*************************************************/
+
+/* This function is called by some lookups that create name=value result
+strings, to handle the quoting of the data. It adds "name=" to the result,
+followed by appropriately quoted data, followed by a single space.
+
+Arguments:
+ name the field name
+ value the data value
+ vlength the data length
+ result the result expanding-string
+
+Returns: the result pointer (possibly updated)
+*/
+
+gstring *
+lf_quote(uschar *name, uschar *value, int vlength, gstring * result)
+{
+result = string_append(result, 2, name, US"=");
+
+/* NULL is handled as an empty string */
+
+if (!value)
+ {
+ value = US"";
+ vlength = 0;
+ }
+
+/* Quote the value if it is empty, contains white space, or starts with a quote
+character. */
+
+if (value[0] == 0 || Ustrpbrk(value, " \t\n\r") != NULL || value[0] == '\"')
+ {
+ result = string_catn(result, US"\"", 1);
+ for (int j = 0; j < vlength; j++)
+ {
+ if (value[j] == '\"' || value[j] == '\\')
+ result = string_catn(result, US"\\", 1);
+ result = string_catn(result, US value+j, 1);
+ }
+ result = string_catn(result, US"\"", 1);
+ }
+else
+ result = string_catn(result, US value, vlength);
+
+return string_catn(result, US" ", 1);
+}
+
+/* End of lf_quote.c */