]> git.ipfire.org Git - thirdparty/squid.git/blob - src/format/Quoting.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / format / Quoting.cc
1 #include "squid.h"
2 #include "format/Quoting.h"
3
4 static const char c2x[] =
5 "000102030405060708090a0b0c0d0e0f"
6 "101112131415161718191a1b1c1d1e1f"
7 "202122232425262728292a2b2c2d2e2f"
8 "303132333435363738393a3b3c3d3e3f"
9 "404142434445464748494a4b4c4d4e4f"
10 "505152535455565758595a5b5c5d5e5f"
11 "606162636465666768696a6b6c6d6e6f"
12 "707172737475767778797a7b7c7d7e7f"
13 "808182838485868788898a8b8c8d8e8f"
14 "909192939495969798999a9b9c9d9e9f"
15 "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
16 "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
17 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
18 "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
19 "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
20 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
21
22 #if DEAD_USING_QUOTEMIMEBLOB
23 /** copy of Log::QuoteMimeBlob. Bugs there will be found here.
24 * This omits [] characters but is otherwise identical to Log::QuoteMimeBlob when OLD_LOG_MIME = 1
25 */
26 static char *
27 username_quote(const char *header)
28 {
29 int c;
30 int i;
31 char *buf;
32 char *buf_cursor;
33
34 if (header == NULL) {
35 buf = static_cast<char *>(xcalloc(1, 1));
36 *buf = '\0';
37 return buf;
38 }
39
40 buf = static_cast<char *>(xcalloc(1, (strlen(header) * 3) + 1));
41 buf_cursor = buf;
42 /*
43 * We escape: space \x00-\x1F and space (0x40) and \x7F-\xFF
44 * to prevent garbage in the logs. CR and LF are also there just in case.
45 */
46
47 while ((c = *(const unsigned char *) header++) != '\0') {
48 if (c == '\r') {
49 *buf_cursor++ = '\\';
50 *buf_cursor++ = 'r';
51 } else if (c == '\n') {
52 *buf_cursor++ = '\\';
53 *buf_cursor++ = 'n';
54 } else if (c <= 0x1F
55 || c >= 0x7F
56 || c == '%'
57 || c == ' ') {
58 *buf_cursor++ = '%';
59 i = c * 2;
60 *buf_cursor++ = c2x[i];
61 *buf_cursor++ = c2x[i + 1];
62 } else {
63 *buf_cursor++ = (char) c;
64 }
65 }
66
67 *buf_cursor = '\0';
68 return buf;
69 }
70 #endif // DEAD
71
72 char *
73 Format::QuoteUrlEncodeUsername(const char *name)
74 {
75 if (NULL == name)
76 return NULL;
77
78 if (name[0] == '\0')
79 return NULL;
80
81 return QuoteMimeBlob(name);
82 // return username_quote(name);
83 }
84
85 char *
86 Format::QuoteMimeBlob(const char *header)
87 {
88 int c;
89 int i;
90 char *buf;
91 char *buf_cursor;
92
93 if (header == NULL) {
94 buf = static_cast<char *>(xcalloc(1, 1));
95 *buf = '\0';
96 return buf;
97 }
98
99 buf = static_cast<char *>(xcalloc(1, (strlen(header) * 3) + 1));
100 buf_cursor = buf;
101 /**
102 * Whe OLD_LOG_MIME is defined we escape: \x00-\x1F"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF
103 * which is the default escape list for the CPAN Perl5 URI module
104 * modulo the inclusion of space (x40) to make the raw logs a bit
105 * more readable.
106 */
107
108 while ((c = *(const unsigned char *) header++) != '\0') {
109 #if !OLD_LOG_MIME
110 if (c == '\r') {
111 *buf_cursor++ = '\\';
112 *buf_cursor++ = 'r';
113 } else if (c == '\n') {
114 *buf_cursor++ = '\\';
115 *buf_cursor++ = 'n';
116 } else
117 #endif
118 if (c <= 0x1F
119 || c >= 0x7F
120 || c == '%'
121 #if OLD_LOG_MIME
122 || c == '"'
123 || c == '#'
124 || c == ';'
125 || c == '<'
126 || c == '>'
127 || c == '?'
128 || c == '{'
129 || c == '}'
130 || c == '|'
131 || c == '\\'
132 || c == '^'
133 || c == '~'
134 || c == '`'
135 #endif
136 || c == '['
137 || c == ']') {
138 *buf_cursor++ = '%';
139 i = c * 2;
140 *buf_cursor++ = c2x[i];
141 *buf_cursor++ = c2x[i + 1];
142 #if !OLD_LOG_MIME
143
144 } else if (c == '\\') {
145 *buf_cursor++ = '\\';
146 *buf_cursor++ = '\\';
147 #endif
148
149 } else {
150 *buf_cursor++ = (char) c;
151 }
152 }
153
154 *buf_cursor = '\0';
155 return buf;
156 }