]> git.ipfire.org Git - thirdparty/squid.git/blob - src/format/Quoting.cc
Changed increment operators from postfix to prefix form.
[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;
51 *buf_cursor = 'r';
52 ++buf_cursor;
53 } else if (c == '\n') {
54 *buf_cursor = '\\';
55 ++buf_cursor;
56 *buf_cursor = 'n';
57 ++buf_cursor;
58 } else if (c <= 0x1F
59 || c >= 0x7F
60 || c == '%'
61 || c == ' ') {
62 *buf_cursor = '%';
63 ++buf_cursor;
64 i = c * 2;
65 *buf_cursor = c2x[i];
66 ++buf_cursor;
67 *buf_cursor = c2x[i + 1];
68 ++buf_cursor;
69 } else {
70 *buf_cursor = (char) c;
71 ++buf_cursor;
72 }
73 }
74
75 *buf_cursor = '\0';
76 return buf;
77 }
78 #endif // DEAD
79
80 char *
81 Format::QuoteUrlEncodeUsername(const char *name)
82 {
83 if (NULL == name)
84 return NULL;
85
86 if (name[0] == '\0')
87 return NULL;
88
89 return QuoteMimeBlob(name);
90 // return username_quote(name);
91 }
92
93 char *
94 Format::QuoteMimeBlob(const char *header)
95 {
96 int c;
97 int i;
98 char *buf;
99 char *buf_cursor;
100
101 if (header == NULL) {
102 buf = static_cast<char *>(xcalloc(1, 1));
103 *buf = '\0';
104 return buf;
105 }
106
107 buf = static_cast<char *>(xcalloc(1, (strlen(header) * 3) + 1));
108 buf_cursor = buf;
109 /**
110 * Whe OLD_LOG_MIME is defined we escape: \x00-\x1F"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF
111 * which is the default escape list for the CPAN Perl5 URI module
112 * modulo the inclusion of space (x40) to make the raw logs a bit
113 * more readable.
114 */
115
116 while ((c = *(const unsigned char *) header++) != '\0') {
117 #if !OLD_LOG_MIME
118 if (c == '\r') {
119 *buf_cursor++ = '\\';
120 *buf_cursor++ = 'r';
121 } else if (c == '\n') {
122 *buf_cursor++ = '\\';
123 *buf_cursor++ = 'n';
124 } else
125 #endif
126 if (c <= 0x1F
127 || c >= 0x7F
128 || c == '%'
129 #if OLD_LOG_MIME
130 || c == '"'
131 || c == '#'
132 || c == ';'
133 || c == '<'
134 || c == '>'
135 || c == '?'
136 || c == '{'
137 || c == '}'
138 || c == '|'
139 || c == '\\'
140 || c == '^'
141 || c == '~'
142 || c == '`'
143 #endif
144 || c == '['
145 || c == ']') {
146 *buf_cursor++ = '%';
147 i = c * 2;
148 *buf_cursor++ = c2x[i];
149 *buf_cursor++ = c2x[i + 1];
150 #if !OLD_LOG_MIME
151
152 } else if (c == '\\') {
153 *buf_cursor++ = '\\';
154 *buf_cursor++ = '\\';
155 #endif
156
157 } else {
158 *buf_cursor++ = (char) c;
159 }
160 }
161
162 *buf_cursor = '\0';
163 return buf;
164 }