|
31 | 31 | #include "svn_pools.h"
|
32 | 32 | #include "svn_xml.h"
|
33 | 33 | #include "svn_error.h"
|
34 |
| -#include "svn_ctype.h" |
35 | 34 |
|
36 |
| -#include "private/svn_utf_private.h" |
37 | 35 | #include "private/svn_subr_private.h"
|
38 | 36 |
|
39 | 37 | #ifdef SVN_HAVE_OLD_EXPAT
|
@@ -98,251 +96,6 @@ struct svn_xml_parser_t
|
98 | 96 |
|
99 | 97 | };
|
100 | 98 |
|
101 |
| - |
102 |
| -/*** XML character validation ***/ |
103 |
| - |
104 |
| -svn_boolean_t |
105 |
| -svn_xml_is_xml_safe(const char *data, apr_size_t len) |
106 |
| -{ |
107 |
| - const char *end = data + len; |
108 |
| - const char *p; |
109 |
| - |
110 |
| - if (! svn_utf__is_valid(data, len)) |
111 |
| - return FALSE; |
112 |
| - |
113 |
| - for (p = data; p < end; p++) |
114 |
| - { |
115 |
| - unsigned char c = *p; |
116 |
| - |
117 |
| - if (svn_ctype_iscntrl(c)) |
118 |
| - { |
119 |
| - if ((c != SVN_CTYPE_ASCII_TAB) |
120 |
| - && (c != SVN_CTYPE_ASCII_LINEFEED) |
121 |
| - && (c != SVN_CTYPE_ASCII_CARRIAGERETURN) |
122 |
| - && (c != SVN_CTYPE_ASCII_DELETE)) |
123 |
| - return FALSE; |
124 |
| - } |
125 |
| - } |
126 |
| - return TRUE; |
127 |
| -} |
128 |
| - |
129 |
| - |
130 |
| - |
131 |
| - |
132 |
| - |
133 |
| -/*** XML escaping. ***/ |
134 |
| - |
135 |
| -/* ### ...? |
136 |
| - * |
137 |
| - * If *OUTSTR is @c NULL, set *OUTSTR to a new stringbuf allocated |
138 |
| - * in POOL, else append to the existing stringbuf there. |
139 |
| - */ |
140 |
| -static void |
141 |
| -xml_escape_cdata(svn_stringbuf_t **outstr, |
142 |
| - const char *data, |
143 |
| - apr_size_t len, |
144 |
| - apr_pool_t *pool) |
145 |
| -{ |
146 |
| - const char *end = data + len; |
147 |
| - const char *p = data, *q; |
148 |
| - |
149 |
| - if (*outstr == NULL) |
150 |
| - *outstr = svn_stringbuf_create_empty(pool); |
151 |
| - |
152 |
| - while (1) |
153 |
| - { |
154 |
| - /* Find a character which needs to be quoted and append bytes up |
155 |
| - to that point. Strictly speaking, '>' only needs to be |
156 |
| - quoted if it follows "]]", but it's easier to quote it all |
157 |
| - the time. |
158 |
| -
|
159 |
| - So, why are we escaping '\r' here? Well, according to the |
160 |
| - XML spec, '\r\n' gets converted to '\n' during XML parsing. |
161 |
| - Also, any '\r' not followed by '\n' is converted to '\n'. By |
162 |
| - golly, if we say we want to escape a '\r', we want to make |
163 |
| - sure it remains a '\r'! */ |
164 |
| - q = p; |
165 |
| - while (q < end && *q != '&' && *q != '<' && *q != '>' && *q != '\r') |
166 |
| - q++; |
167 |
| - svn_stringbuf_appendbytes(*outstr, p, q - p); |
168 |
| - |
169 |
| - /* We may already be a winner. */ |
170 |
| - if (q == end) |
171 |
| - break; |
172 |
| - |
173 |
| - /* Append the entity reference for the character. */ |
174 |
| - if (*q == '&') |
175 |
| - svn_stringbuf_appendcstr(*outstr, "&"); |
176 |
| - else if (*q == '<') |
177 |
| - svn_stringbuf_appendcstr(*outstr, "<"); |
178 |
| - else if (*q == '>') |
179 |
| - svn_stringbuf_appendcstr(*outstr, ">"); |
180 |
| - else if (*q == '\r') |
181 |
| - svn_stringbuf_appendcstr(*outstr, " "); |
182 |
| - |
183 |
| - p = q + 1; |
184 |
| - } |
185 |
| -} |
186 |
| - |
187 |
| -/* Essentially the same as xml_escape_cdata, with the addition of |
188 |
| - whitespace and quote characters. */ |
189 |
| -static void |
190 |
| -xml_escape_attr(svn_stringbuf_t **outstr, |
191 |
| - const char *data, |
192 |
| - apr_size_t len, |
193 |
| - apr_pool_t *pool) |
194 |
| -{ |
195 |
| - const char *end = data + len; |
196 |
| - const char *p = data, *q; |
197 |
| - |
198 |
| - if (*outstr == NULL) |
199 |
| - *outstr = svn_stringbuf_create_ensure(len, pool); |
200 |
| - |
201 |
| - while (1) |
202 |
| - { |
203 |
| - /* Find a character which needs to be quoted and append bytes up |
204 |
| - to that point. */ |
205 |
| - q = p; |
206 |
| - while (q < end && *q != '&' && *q != '<' && *q != '>' |
207 |
| - && *q != '"' && *q != '\'' && *q != '\r' |
208 |
| - && *q != '\n' && *q != '\t') |
209 |
| - q++; |
210 |
| - svn_stringbuf_appendbytes(*outstr, p, q - p); |
211 |
| - |
212 |
| - /* We may already be a winner. */ |
213 |
| - if (q == end) |
214 |
| - break; |
215 |
| - |
216 |
| - /* Append the entity reference for the character. */ |
217 |
| - if (*q == '&') |
218 |
| - svn_stringbuf_appendcstr(*outstr, "&"); |
219 |
| - else if (*q == '<') |
220 |
| - svn_stringbuf_appendcstr(*outstr, "<"); |
221 |
| - else if (*q == '>') |
222 |
| - svn_stringbuf_appendcstr(*outstr, ">"); |
223 |
| - else if (*q == '"') |
224 |
| - svn_stringbuf_appendcstr(*outstr, """); |
225 |
| - else if (*q == '\'') |
226 |
| - svn_stringbuf_appendcstr(*outstr, "'"); |
227 |
| - else if (*q == '\r') |
228 |
| - svn_stringbuf_appendcstr(*outstr, " "); |
229 |
| - else if (*q == '\n') |
230 |
| - svn_stringbuf_appendcstr(*outstr, " "); |
231 |
| - else if (*q == '\t') |
232 |
| - svn_stringbuf_appendcstr(*outstr, "	"); |
233 |
| - |
234 |
| - p = q + 1; |
235 |
| - } |
236 |
| -} |
237 |
| - |
238 |
| - |
239 |
| -void |
240 |
| -svn_xml_escape_cdata_stringbuf(svn_stringbuf_t **outstr, |
241 |
| - const svn_stringbuf_t *string, |
242 |
| - apr_pool_t *pool) |
243 |
| -{ |
244 |
| - xml_escape_cdata(outstr, string->data, string->len, pool); |
245 |
| -} |
246 |
| - |
247 |
| - |
248 |
| -void |
249 |
| -svn_xml_escape_cdata_string(svn_stringbuf_t **outstr, |
250 |
| - const svn_string_t *string, |
251 |
| - apr_pool_t *pool) |
252 |
| -{ |
253 |
| - xml_escape_cdata(outstr, string->data, string->len, pool); |
254 |
| -} |
255 |
| - |
256 |
| - |
257 |
| -void |
258 |
| -svn_xml_escape_cdata_cstring(svn_stringbuf_t **outstr, |
259 |
| - const char *string, |
260 |
| - apr_pool_t *pool) |
261 |
| -{ |
262 |
| - xml_escape_cdata(outstr, string, (apr_size_t) strlen(string), pool); |
263 |
| -} |
264 |
| - |
265 |
| - |
266 |
| -void |
267 |
| -svn_xml_escape_attr_stringbuf(svn_stringbuf_t **outstr, |
268 |
| - const svn_stringbuf_t *string, |
269 |
| - apr_pool_t *pool) |
270 |
| -{ |
271 |
| - xml_escape_attr(outstr, string->data, string->len, pool); |
272 |
| -} |
273 |
| - |
274 |
| - |
275 |
| -void |
276 |
| -svn_xml_escape_attr_string(svn_stringbuf_t **outstr, |
277 |
| - const svn_string_t *string, |
278 |
| - apr_pool_t *pool) |
279 |
| -{ |
280 |
| - xml_escape_attr(outstr, string->data, string->len, pool); |
281 |
| -} |
282 |
| - |
283 |
| - |
284 |
| -void |
285 |
| -svn_xml_escape_attr_cstring(svn_stringbuf_t **outstr, |
286 |
| - const char *string, |
287 |
| - apr_pool_t *pool) |
288 |
| -{ |
289 |
| - xml_escape_attr(outstr, string, (apr_size_t) strlen(string), pool); |
290 |
| -} |
291 |
| - |
292 |
| - |
293 |
| -const char * |
294 |
| -svn_xml_fuzzy_escape(const char *string, apr_pool_t *pool) |
295 |
| -{ |
296 |
| - const char *end = string + strlen(string); |
297 |
| - const char *p = string, *q; |
298 |
| - svn_stringbuf_t *outstr; |
299 |
| - char escaped_char[6]; /* ? \ u u u \0 */ |
300 |
| - |
301 |
| - for (q = p; q < end; q++) |
302 |
| - { |
303 |
| - if (svn_ctype_iscntrl(*q) |
304 |
| - && ! ((*q == '\n') || (*q == '\r') || (*q == '\t'))) |
305 |
| - break; |
306 |
| - } |
307 |
| - |
308 |
| - /* Return original string if no unsafe characters found. */ |
309 |
| - if (q == end) |
310 |
| - return string; |
311 |
| - |
312 |
| - outstr = svn_stringbuf_create_empty(pool); |
313 |
| - while (1) |
314 |
| - { |
315 |
| - q = p; |
316 |
| - |
317 |
| - /* Traverse till either unsafe character or eos. */ |
318 |
| - while ((q < end) |
319 |
| - && ((! svn_ctype_iscntrl(*q)) |
320 |
| - || (*q == '\n') || (*q == '\r') || (*q == '\t'))) |
321 |
| - q++; |
322 |
| - |
323 |
| - /* copy chunk before marker */ |
324 |
| - svn_stringbuf_appendbytes(outstr, p, q - p); |
325 |
| - |
326 |
| - if (q == end) |
327 |
| - break; |
328 |
| - |
329 |
| - /* Append an escaped version of the unsafe character. |
330 |
| -
|
331 |
| - ### This format was chosen for consistency with |
332 |
| - ### svn_utf__cstring_from_utf8_fuzzy(). The two functions |
333 |
| - ### should probably share code, even though they escape |
334 |
| - ### different characters. |
335 |
| - */ |
336 |
| - apr_snprintf(escaped_char, sizeof(escaped_char), "?\\%03u", |
337 |
| - (unsigned char) *q); |
338 |
| - svn_stringbuf_appendcstr(outstr, escaped_char); |
339 |
| - |
340 |
| - p = q + 1; |
341 |
| - } |
342 |
| - |
343 |
| - return outstr->data; |
344 |
| -} |
345 |
| - |
346 | 99 |
|
347 | 100 | /*** Map from the Expat callback types to the SVN XML types. ***/
|
348 | 101 |
|
|
0 commit comments