Merge from master into 4.next
[exim.git] / src / src / lookups / lf_quote.c
CommitLineData
0756eb3c
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
0a49a7a4 5/* Copyright (c) University of Cambridge 1995 - 2009 */
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
18strings, to handle the quoting of the data. It adds "name=" to the result,
19followed by appropriately quoted data, followed by a single space.
20
21Arguments:
22 name the field name
23 value the data value
24 vlength the data length
25 result the result pointer
26 asize points to the size variable
27 aoffset points to the offset variable
28
29Returns: the result pointer (possibly updated)
30*/
31
32uschar *
33lf_quote(uschar *name, uschar *value, int vlength, uschar *result, int *asize,
34 int *aoffset)
35{
36result = string_append(result, asize, aoffset, 2, name, US"=");
37
38/* NULL is handled as an empty string */
39
4fab92fb
HSHR
40if (!value)
41 {
42 value = US"";
43 vlength = 0;
44 }
0756eb3c
PH
45
46/* Quote the value if it is empty, contains white space, or starts with a quote
47character. */
48
49if (value[0] == 0 || Ustrpbrk(value, " \t\n\r") != NULL || value[0] == '\"')
50 {
51 int j;
4fab92fb 52 result = string_catn(result, asize, aoffset, US"\"", 1);
0756eb3c
PH
53 for (j = 0; j < vlength; j++)
54 {
55 if (value[j] == '\"' || value[j] == '\\')
4fab92fb
HSHR
56 result = string_catn(result, asize, aoffset, US"\\", 1);
57 result = string_catn(result, asize, aoffset, US value+j, 1);
0756eb3c 58 }
4fab92fb 59 result = string_catn(result, asize, aoffset, US"\"", 1);
0756eb3c
PH
60 }
61else
4fab92fb 62 result = string_catn(result, asize, aoffset, US value, vlength);
0756eb3c 63
4fab92fb 64return string_catn(result, asize, aoffset, US" ", 1);
0756eb3c
PH
65}
66
67/* End of lf_quote.c */