Commit | Line | Data |
---|---|---|
0756eb3c PH |
1 | /************************************************* |
2 | * Exim - an Internet mail transport agent * | |
3 | *************************************************/ | |
4 | ||
f9ba5e22 | 5 | /* Copyright (c) University of Cambridge 1995 - 2018 */ |
0756eb3c PH |
6 | /* See the file NOTICE for conditions of use and distribution. */ |
7 | ||
8 | ||
9 | #include "../exim.h" | |
10 | #include "lf_functions.h" | |
11 | ||
12 | ||
13 | /************************************************* | |
14 | * Add string to result, quoting if necessary * | |
15 | *************************************************/ | |
16 | ||
17 | /* This function is called by some lookups that create name=value result | |
18 | strings, to handle the quoting of the data. It adds "name=" to the result, | |
19 | followed by appropriately quoted data, followed by a single space. | |
20 | ||
21 | Arguments: | |
22 | name the field name | |
23 | value the data value | |
24 | vlength the data length | |
acec9514 | 25 | result the result expanding-string |
0756eb3c PH |
26 | |
27 | Returns: the result pointer (possibly updated) | |
28 | */ | |
29 | ||
acec9514 JH |
30 | gstring * |
31 | lf_quote(uschar *name, uschar *value, int vlength, gstring * result) | |
0756eb3c | 32 | { |
acec9514 | 33 | result = string_append(result, 2, name, US"="); |
0756eb3c PH |
34 | |
35 | /* NULL is handled as an empty string */ | |
36 | ||
a5bdc7ee JH |
37 | if (!value) |
38 | { | |
39 | value = US""; | |
40 | vlength = 0; | |
41 | } | |
0756eb3c PH |
42 | |
43 | /* Quote the value if it is empty, contains white space, or starts with a quote | |
44 | character. */ | |
45 | ||
46 | if (value[0] == 0 || Ustrpbrk(value, " \t\n\r") != NULL || value[0] == '\"') | |
47 | { | |
acec9514 | 48 | result = string_catn(result, US"\"", 1); |
d7978c0f | 49 | for (int j = 0; j < vlength; j++) |
0756eb3c PH |
50 | { |
51 | if (value[j] == '\"' || value[j] == '\\') | |
acec9514 JH |
52 | result = string_catn(result, US"\\", 1); |
53 | result = string_catn(result, US value+j, 1); | |
0756eb3c | 54 | } |
acec9514 | 55 | result = string_catn(result, US"\"", 1); |
0756eb3c PH |
56 | } |
57 | else | |
acec9514 | 58 | result = string_catn(result, US value, vlength); |
0756eb3c | 59 | |
acec9514 | 60 | return string_catn(result, US" ", 1); |
0756eb3c PH |
61 | } |
62 | ||
63 | /* End of lf_quote.c */ |