/src/libxslt/libexslt/strings.c
Line | Count | Source (jump to first uncovered line) |
1 | | #define IN_LIBEXSLT |
2 | | #include "libexslt/libexslt.h" |
3 | | |
4 | | #include <libxml/tree.h> |
5 | | #include <libxml/xpath.h> |
6 | | #include <libxml/xpathInternals.h> |
7 | | #include <libxml/parser.h> |
8 | | #include <libxml/encoding.h> |
9 | | #include <libxml/uri.h> |
10 | | |
11 | | #include <libxslt/xsltutils.h> |
12 | | #include <libxslt/xsltInternals.h> |
13 | | #include <libxslt/extensions.h> |
14 | | |
15 | | #include "exslt.h" |
16 | | |
17 | | /** |
18 | | * exsltStrTokenizeFunction: |
19 | | * @ctxt: an XPath parser context |
20 | | * @nargs: the number of arguments |
21 | | * |
22 | | * Splits up a string on the characters of the delimiter string and returns a |
23 | | * node set of token elements, each containing one token from the string. |
24 | | */ |
25 | | static void |
26 | | exsltStrTokenizeFunction(xmlXPathParserContextPtr ctxt, int nargs) |
27 | 8.87k | { |
28 | 8.87k | xsltTransformContextPtr tctxt; |
29 | 8.87k | xmlChar *str, *delimiters, *cur; |
30 | 8.87k | const xmlChar *token, *delimiter; |
31 | 8.87k | xmlNodePtr node; |
32 | 8.87k | xmlDocPtr container; |
33 | 8.87k | xmlXPathObjectPtr ret = NULL; |
34 | 8.87k | int clen; |
35 | | |
36 | 8.87k | if ((nargs < 1) || (nargs > 2)) { |
37 | 29 | xmlXPathSetArityError(ctxt); |
38 | 29 | return; |
39 | 29 | } |
40 | | |
41 | 8.84k | if (nargs == 2) { |
42 | 6.03k | delimiters = xmlXPathPopString(ctxt); |
43 | 6.03k | if (xmlXPathCheckError(ctxt)) |
44 | 0 | return; |
45 | 6.03k | } else { |
46 | 2.81k | delimiters = xmlStrdup((const xmlChar *) "\t\r\n "); |
47 | 2.81k | } |
48 | 8.84k | if (delimiters == NULL) |
49 | 0 | return; |
50 | | |
51 | 8.84k | str = xmlXPathPopString(ctxt); |
52 | 8.84k | if (xmlXPathCheckError(ctxt) || (str == NULL)) { |
53 | 0 | xmlFree(delimiters); |
54 | 0 | return; |
55 | 0 | } |
56 | | |
57 | | /* Return a result tree fragment */ |
58 | 8.84k | tctxt = xsltXPathGetTransformContext(ctxt); |
59 | 8.84k | if (tctxt == NULL) { |
60 | 0 | xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL, |
61 | 0 | "exslt:tokenize : internal error tctxt == NULL\n"); |
62 | 0 | goto fail; |
63 | 0 | } |
64 | | |
65 | 8.84k | container = xsltCreateRVT(tctxt); |
66 | 8.84k | if (container != NULL) { |
67 | 8.84k | xsltRegisterLocalRVT(tctxt, container); |
68 | 8.84k | ret = xmlXPathNewNodeSet(NULL); |
69 | 8.84k | if (ret != NULL) { |
70 | 2.20M | for (cur = str, token = str; *cur != 0; cur += clen) { |
71 | 2.19M | clen = xmlUTF8Strsize(cur, 1); |
72 | 2.19M | if (*delimiters == 0) { /* empty string case */ |
73 | 637k | xmlChar ctmp; |
74 | 637k | ctmp = *(cur+clen); |
75 | 637k | *(cur+clen) = 0; |
76 | 637k | node = xmlNewDocRawNode(container, NULL, |
77 | 637k | (const xmlChar *) "token", cur); |
78 | 637k | xmlAddChild((xmlNodePtr) container, node); |
79 | 637k | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
80 | 637k | *(cur+clen) = ctmp; /* restore the changed byte */ |
81 | 637k | token = cur + clen; |
82 | 7.11M | } else for (delimiter = delimiters; *delimiter != 0; |
83 | 6.04M | delimiter += xmlUTF8Strsize(delimiter, 1)) { |
84 | 6.04M | if (!xmlUTF8Charcmp(cur, delimiter)) { |
85 | 491k | if (cur == token) { |
86 | | /* discard empty tokens */ |
87 | 470k | token = cur + clen; |
88 | 470k | break; |
89 | 470k | } |
90 | 21.6k | *cur = 0; /* terminate the token */ |
91 | 21.6k | node = xmlNewDocRawNode(container, NULL, |
92 | 21.6k | (const xmlChar *) "token", token); |
93 | 21.6k | xmlAddChild((xmlNodePtr) container, node); |
94 | 21.6k | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
95 | 21.6k | *cur = *delimiter; /* restore the changed byte */ |
96 | 21.6k | token = cur + clen; |
97 | 21.6k | break; |
98 | 491k | } |
99 | 6.04M | } |
100 | 2.19M | } |
101 | 8.84k | if (token != cur) { |
102 | 4.35k | node = xmlNewDocRawNode(container, NULL, |
103 | 4.35k | (const xmlChar *) "token", token); |
104 | 4.35k | xmlAddChild((xmlNodePtr) container, node); |
105 | 4.35k | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
106 | 4.35k | } |
107 | 8.84k | } |
108 | 8.84k | } |
109 | | |
110 | 8.84k | fail: |
111 | 8.84k | if (str != NULL) |
112 | 8.84k | xmlFree(str); |
113 | 8.84k | if (delimiters != NULL) |
114 | 8.84k | xmlFree(delimiters); |
115 | 8.84k | if (ret != NULL) |
116 | 8.84k | valuePush(ctxt, ret); |
117 | 0 | else |
118 | 0 | valuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
119 | 8.84k | } |
120 | | |
121 | | /** |
122 | | * exsltStrSplitFunction: |
123 | | * @ctxt: an XPath parser context |
124 | | * @nargs: the number of arguments |
125 | | * |
126 | | * Splits up a string on a delimiting string and returns a node set of token |
127 | | * elements, each containing one token from the string. |
128 | | */ |
129 | | static void |
130 | 119k | exsltStrSplitFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
131 | 119k | xsltTransformContextPtr tctxt; |
132 | 119k | xmlChar *str, *delimiter, *cur; |
133 | 119k | const xmlChar *token; |
134 | 119k | xmlNodePtr node; |
135 | 119k | xmlDocPtr container; |
136 | 119k | xmlXPathObjectPtr ret = NULL; |
137 | 119k | int delimiterLength; |
138 | | |
139 | 119k | if ((nargs < 1) || (nargs > 2)) { |
140 | 149 | xmlXPathSetArityError(ctxt); |
141 | 149 | return; |
142 | 149 | } |
143 | | |
144 | 119k | if (nargs == 2) { |
145 | 76.2k | delimiter = xmlXPathPopString(ctxt); |
146 | 76.2k | if (xmlXPathCheckError(ctxt)) |
147 | 0 | return; |
148 | 76.2k | } else { |
149 | 43.0k | delimiter = xmlStrdup((const xmlChar *) " "); |
150 | 43.0k | } |
151 | 119k | if (delimiter == NULL) |
152 | 0 | return; |
153 | 119k | delimiterLength = xmlStrlen (delimiter); |
154 | | |
155 | 119k | str = xmlXPathPopString(ctxt); |
156 | 119k | if (xmlXPathCheckError(ctxt) || (str == NULL)) { |
157 | 0 | xmlFree(delimiter); |
158 | 0 | return; |
159 | 0 | } |
160 | | |
161 | | /* Return a result tree fragment */ |
162 | 119k | tctxt = xsltXPathGetTransformContext(ctxt); |
163 | 119k | if (tctxt == NULL) { |
164 | 0 | xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL, |
165 | 0 | "exslt:tokenize : internal error tctxt == NULL\n"); |
166 | 0 | goto fail; |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | * OPTIMIZE TODO: We are creating an xmlDoc for every split! |
171 | | */ |
172 | 119k | container = xsltCreateRVT(tctxt); |
173 | 119k | if (container != NULL) { |
174 | 119k | xsltRegisterLocalRVT(tctxt, container); |
175 | 119k | ret = xmlXPathNewNodeSet(NULL); |
176 | 119k | if (ret != NULL) { |
177 | 145M | for (cur = str, token = str; *cur != 0; cur++) { |
178 | 145M | if (delimiterLength == 0) { |
179 | 29.6M | if (cur != token) { |
180 | 29.5M | xmlChar tmp = *cur; |
181 | 29.5M | *cur = 0; |
182 | 29.5M | node = xmlNewDocRawNode(container, NULL, |
183 | 29.5M | (const xmlChar *) "token", token); |
184 | 29.5M | xmlAddChild((xmlNodePtr) container, node); |
185 | 29.5M | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
186 | 29.5M | *cur = tmp; |
187 | 29.5M | token++; |
188 | 29.5M | } |
189 | 29.6M | } |
190 | 115M | else if (!xmlStrncasecmp(cur, delimiter, delimiterLength)) { |
191 | 12.4M | if (cur == token) { |
192 | | /* discard empty tokens */ |
193 | 8.41M | cur = cur + delimiterLength - 1; |
194 | 8.41M | token = cur + 1; |
195 | 8.41M | continue; |
196 | 8.41M | } |
197 | 4.02M | *cur = 0; |
198 | 4.02M | node = xmlNewDocRawNode(container, NULL, |
199 | 4.02M | (const xmlChar *) "token", token); |
200 | 4.02M | xmlAddChild((xmlNodePtr) container, node); |
201 | 4.02M | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
202 | 4.02M | *cur = *delimiter; |
203 | 4.02M | cur = cur + delimiterLength - 1; |
204 | 4.02M | token = cur + 1; |
205 | 4.02M | } |
206 | 145M | } |
207 | 119k | if (token != cur) { |
208 | 102k | node = xmlNewDocRawNode(container, NULL, |
209 | 102k | (const xmlChar *) "token", token); |
210 | 102k | xmlAddChild((xmlNodePtr) container, node); |
211 | 102k | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
212 | 102k | } |
213 | 119k | } |
214 | 119k | } |
215 | | |
216 | 119k | fail: |
217 | 119k | if (str != NULL) |
218 | 119k | xmlFree(str); |
219 | 119k | if (delimiter != NULL) |
220 | 119k | xmlFree(delimiter); |
221 | 119k | if (ret != NULL) |
222 | 119k | valuePush(ctxt, ret); |
223 | 0 | else |
224 | 0 | valuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
225 | 119k | } |
226 | | |
227 | | /** |
228 | | * exsltStrEncodeUriFunction: |
229 | | * @ctxt: an XPath parser context |
230 | | * @nargs: the number of arguments |
231 | | * |
232 | | * URI-Escapes a string |
233 | | */ |
234 | | static void |
235 | 2.91k | exsltStrEncodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
236 | 2.91k | int escape_all = 1, str_len = 0; |
237 | 2.91k | xmlChar *str = NULL, *ret = NULL, *tmp; |
238 | | |
239 | 2.91k | if ((nargs < 2) || (nargs > 3)) { |
240 | 46 | xmlXPathSetArityError(ctxt); |
241 | 46 | return; |
242 | 46 | } |
243 | | |
244 | 2.86k | if (nargs >= 3) { |
245 | | /* check for UTF-8 if encoding was explicitly given; |
246 | | we don't support anything else yet */ |
247 | 65 | tmp = xmlXPathPopString(ctxt); |
248 | 65 | if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) { |
249 | 65 | xmlXPathReturnEmptyString(ctxt); |
250 | 65 | xmlFree(tmp); |
251 | 65 | return; |
252 | 65 | } |
253 | 0 | xmlFree(tmp); |
254 | 0 | } |
255 | | |
256 | 2.79k | escape_all = xmlXPathPopBoolean(ctxt); |
257 | | |
258 | 2.79k | str = xmlXPathPopString(ctxt); |
259 | 2.79k | str_len = xmlUTF8Strlen(str); |
260 | | |
261 | 2.79k | if (str_len <= 0) { |
262 | 234 | if (str_len < 0) |
263 | 213 | xsltGenericError(xsltGenericErrorContext, |
264 | 213 | "exsltStrEncodeUriFunction: invalid UTF-8\n"); |
265 | 234 | xmlXPathReturnEmptyString(ctxt); |
266 | 234 | xmlFree(str); |
267 | 234 | return; |
268 | 234 | } |
269 | | |
270 | 2.56k | ret = xmlURIEscapeStr(str,(const xmlChar *)(escape_all?"-_.!~*'()":"-_.!~*'();/?:@&=+$,[]")); |
271 | 2.56k | xmlXPathReturnString(ctxt, ret); |
272 | | |
273 | 2.56k | if (str != NULL) |
274 | 2.56k | xmlFree(str); |
275 | 2.56k | } |
276 | | |
277 | | /** |
278 | | * exsltStrDecodeUriFunction: |
279 | | * @ctxt: an XPath parser context |
280 | | * @nargs: the number of arguments |
281 | | * |
282 | | * reverses URI-Escaping of a string |
283 | | */ |
284 | | static void |
285 | 3.24k | exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
286 | 3.24k | int str_len = 0; |
287 | 3.24k | xmlChar *str = NULL, *ret = NULL, *tmp; |
288 | | |
289 | 3.24k | if ((nargs < 1) || (nargs > 2)) { |
290 | 32 | xmlXPathSetArityError(ctxt); |
291 | 32 | return; |
292 | 32 | } |
293 | | |
294 | 3.21k | if (nargs >= 2) { |
295 | | /* check for UTF-8 if encoding was explicitly given; |
296 | | we don't support anything else yet */ |
297 | 37 | tmp = xmlXPathPopString(ctxt); |
298 | 37 | if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) { |
299 | 37 | xmlXPathReturnEmptyString(ctxt); |
300 | 37 | xmlFree(tmp); |
301 | 37 | return; |
302 | 37 | } |
303 | 0 | xmlFree(tmp); |
304 | 0 | } |
305 | | |
306 | 3.17k | str = xmlXPathPopString(ctxt); |
307 | 3.17k | str_len = xmlUTF8Strlen(str); |
308 | | |
309 | 3.17k | if (str_len <= 0) { |
310 | 908 | if (str_len < 0) |
311 | 906 | xsltGenericError(xsltGenericErrorContext, |
312 | 906 | "exsltStrDecodeUriFunction: invalid UTF-8\n"); |
313 | 908 | xmlXPathReturnEmptyString(ctxt); |
314 | 908 | xmlFree(str); |
315 | 908 | return; |
316 | 908 | } |
317 | | |
318 | 2.26k | ret = (xmlChar *) xmlURIUnescapeString((const char *)str,0,NULL); |
319 | 2.26k | if (!xmlCheckUTF8(ret)) { |
320 | | /* FIXME: instead of throwing away the whole URI, we should |
321 | | only discard the invalid sequence(s). How to do that? */ |
322 | 1.16k | xmlXPathReturnEmptyString(ctxt); |
323 | 1.16k | xmlFree(str); |
324 | 1.16k | xmlFree(ret); |
325 | 1.16k | return; |
326 | 1.16k | } |
327 | | |
328 | 1.10k | xmlXPathReturnString(ctxt, ret); |
329 | | |
330 | 1.10k | if (str != NULL) |
331 | 1.10k | xmlFree(str); |
332 | 1.10k | } |
333 | | |
334 | | /** |
335 | | * exsltStrPaddingFunction: |
336 | | * @ctxt: an XPath parser context |
337 | | * @nargs: the number of arguments |
338 | | * |
339 | | * Creates a padding string of a certain length. |
340 | | */ |
341 | | static void |
342 | 446 | exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
343 | 446 | int number, str_len = 0, str_size = 0; |
344 | 446 | double floatval; |
345 | 446 | xmlChar *str = NULL; |
346 | 446 | xmlBufferPtr buf; |
347 | | |
348 | 446 | if ((nargs < 1) || (nargs > 2)) { |
349 | 20 | xmlXPathSetArityError(ctxt); |
350 | 20 | return; |
351 | 20 | } |
352 | | |
353 | 426 | if (nargs == 2) { |
354 | 350 | str = xmlXPathPopString(ctxt); |
355 | 350 | str_len = xmlUTF8Strlen(str); |
356 | 350 | str_size = xmlStrlen(str); |
357 | 350 | } |
358 | | |
359 | 426 | floatval = xmlXPathPopNumber(ctxt); |
360 | | |
361 | 426 | if (str_len <= 0) { |
362 | 153 | if (str_len < 0) { |
363 | 57 | xsltGenericError(xsltGenericErrorContext, |
364 | 57 | "exsltStrPaddingFunction: invalid UTF-8\n"); |
365 | 57 | xmlXPathReturnEmptyString(ctxt); |
366 | 57 | xmlFree(str); |
367 | 57 | return; |
368 | 57 | } |
369 | 96 | if (str != NULL) xmlFree(str); |
370 | 96 | str = xmlStrdup((const xmlChar *) " "); |
371 | 96 | str_len = 1; |
372 | 96 | str_size = 1; |
373 | 96 | } |
374 | | |
375 | 369 | if (xmlXPathIsNaN(floatval) || floatval < 0.0) { |
376 | 50 | number = 0; |
377 | 319 | } else if (floatval >= 100000.0) { |
378 | 33 | number = 100000; |
379 | 33 | } |
380 | 286 | else { |
381 | 286 | number = (int) floatval; |
382 | 286 | } |
383 | | |
384 | 369 | if (number <= 0) { |
385 | 84 | xmlXPathReturnEmptyString(ctxt); |
386 | 84 | xmlFree(str); |
387 | 84 | return; |
388 | 84 | } |
389 | | |
390 | 285 | buf = xmlBufferCreateSize(number); |
391 | 285 | if (buf == NULL) { |
392 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
393 | 0 | xmlFree(str); |
394 | 0 | return; |
395 | 0 | } |
396 | 285 | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
397 | | |
398 | 1.25M | while (number >= str_len) { |
399 | 1.25M | xmlBufferAdd(buf, str, str_size); |
400 | 1.25M | number -= str_len; |
401 | 1.25M | } |
402 | 285 | if (number > 0) { |
403 | 196 | str_size = xmlUTF8Strsize(str, number); |
404 | 196 | xmlBufferAdd(buf, str, str_size); |
405 | 196 | } |
406 | | |
407 | 285 | xmlXPathReturnString(ctxt, xmlBufferDetach(buf)); |
408 | | |
409 | 285 | xmlBufferFree(buf); |
410 | 285 | if (str != NULL) |
411 | 285 | xmlFree(str); |
412 | 285 | } |
413 | | |
414 | | /** |
415 | | * exsltStrAlignFunction: |
416 | | * @ctxt: an XPath parser context |
417 | | * @nargs: the number of arguments |
418 | | * |
419 | | * Aligns a string within another string. |
420 | | */ |
421 | | static void |
422 | 4.16k | exsltStrAlignFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
423 | 4.16k | xmlChar *str, *padding, *alignment, *ret; |
424 | 4.16k | int str_l, padding_l; |
425 | | |
426 | 4.16k | if ((nargs < 2) || (nargs > 3)) { |
427 | 117 | xmlXPathSetArityError(ctxt); |
428 | 117 | return; |
429 | 117 | } |
430 | | |
431 | 4.04k | if (nargs == 3) |
432 | 1.11k | alignment = xmlXPathPopString(ctxt); |
433 | 2.93k | else |
434 | 2.93k | alignment = NULL; |
435 | | |
436 | 4.04k | padding = xmlXPathPopString(ctxt); |
437 | 4.04k | str = xmlXPathPopString(ctxt); |
438 | | |
439 | 4.04k | str_l = xmlUTF8Strlen (str); |
440 | 4.04k | padding_l = xmlUTF8Strlen (padding); |
441 | | |
442 | 4.04k | if (str_l < 0 || padding_l < 0) { |
443 | 2.12k | xsltGenericError(xsltGenericErrorContext, |
444 | 2.12k | "exsltStrAlignFunction: invalid UTF-8\n"); |
445 | 2.12k | xmlXPathReturnEmptyString(ctxt); |
446 | 2.12k | xmlFree(str); |
447 | 2.12k | xmlFree(padding); |
448 | 2.12k | xmlFree(alignment); |
449 | 2.12k | return; |
450 | 2.12k | } |
451 | | |
452 | 1.92k | if (str_l == padding_l) { |
453 | 628 | xmlXPathReturnString (ctxt, str); |
454 | 628 | xmlFree(padding); |
455 | 628 | xmlFree(alignment); |
456 | 628 | return; |
457 | 628 | } |
458 | | |
459 | 1.29k | if (str_l > padding_l) { |
460 | 437 | ret = xmlUTF8Strndup (str, padding_l); |
461 | 857 | } else { |
462 | 857 | if (xmlStrEqual(alignment, (const xmlChar *) "right")) { |
463 | 162 | ret = xmlUTF8Strndup (padding, padding_l - str_l); |
464 | 162 | ret = xmlStrcat (ret, str); |
465 | 695 | } else if (xmlStrEqual(alignment, (const xmlChar *) "center")) { |
466 | 151 | int left = (padding_l - str_l) / 2; |
467 | 151 | int right_start; |
468 | | |
469 | 151 | ret = xmlUTF8Strndup (padding, left); |
470 | 151 | ret = xmlStrcat (ret, str); |
471 | | |
472 | 151 | right_start = xmlUTF8Strsize (padding, left + str_l); |
473 | 151 | ret = xmlStrcat (ret, padding + right_start); |
474 | 544 | } else { |
475 | 544 | int str_s; |
476 | | |
477 | 544 | str_s = xmlUTF8Strsize(padding, str_l); |
478 | 544 | ret = xmlStrdup (str); |
479 | 544 | ret = xmlStrcat (ret, padding + str_s); |
480 | 544 | } |
481 | 857 | } |
482 | | |
483 | 1.29k | xmlXPathReturnString (ctxt, ret); |
484 | | |
485 | 1.29k | xmlFree(str); |
486 | 1.29k | xmlFree(padding); |
487 | 1.29k | xmlFree(alignment); |
488 | 1.29k | } |
489 | | |
490 | | /** |
491 | | * exsltStrConcatFunction: |
492 | | * @ctxt: an XPath parser context |
493 | | * @nargs: the number of arguments |
494 | | * |
495 | | * Takes a node set and returns the concatenation of the string values |
496 | | * of the nodes in that node set. If the node set is empty, it |
497 | | * returns an empty string. |
498 | | */ |
499 | | static void |
500 | 359 | exsltStrConcatFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
501 | 359 | xmlXPathObjectPtr obj; |
502 | 359 | xmlBufferPtr buf; |
503 | 359 | int i; |
504 | | |
505 | 359 | if (nargs != 1) { |
506 | 20 | xmlXPathSetArityError(ctxt); |
507 | 20 | return; |
508 | 20 | } |
509 | | |
510 | 339 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
511 | 19 | xmlXPathSetTypeError(ctxt); |
512 | 19 | return; |
513 | 19 | } |
514 | | |
515 | 320 | obj = valuePop (ctxt); |
516 | | |
517 | 320 | if (xmlXPathNodeSetIsEmpty(obj->nodesetval)) { |
518 | 47 | xmlXPathFreeObject(obj); |
519 | 47 | xmlXPathReturnEmptyString(ctxt); |
520 | 47 | return; |
521 | 47 | } |
522 | | |
523 | 273 | buf = xmlBufferCreate(); |
524 | 273 | if (buf == NULL) { |
525 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
526 | 0 | xmlXPathFreeObject(obj); |
527 | 0 | return; |
528 | 0 | } |
529 | 273 | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
530 | | |
531 | 175k | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
532 | 174k | xmlChar *tmp; |
533 | 174k | tmp = xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
534 | | |
535 | 174k | xmlBufferCat(buf, tmp); |
536 | | |
537 | 174k | xmlFree(tmp); |
538 | 174k | } |
539 | | |
540 | 273 | xmlXPathFreeObject (obj); |
541 | | |
542 | 273 | xmlXPathReturnString(ctxt, xmlBufferDetach(buf)); |
543 | 273 | xmlBufferFree(buf); |
544 | 273 | } |
545 | | |
546 | | /** |
547 | | * exsltStrReturnString: |
548 | | * @ctxt: an XPath parser context |
549 | | * @str: a string |
550 | | * @len: length of string |
551 | | * |
552 | | * Returns a string as a node set. |
553 | | */ |
554 | | static int |
555 | | exsltStrReturnString(xmlXPathParserContextPtr ctxt, const xmlChar *str, |
556 | | int len) |
557 | 2.14k | { |
558 | 2.14k | xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt); |
559 | 2.14k | xmlDocPtr container; |
560 | 2.14k | xmlNodePtr text_node; |
561 | 2.14k | xmlXPathObjectPtr ret; |
562 | | |
563 | 2.14k | container = xsltCreateRVT(tctxt); |
564 | 2.14k | if (container == NULL) { |
565 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
566 | 0 | return(-1); |
567 | 0 | } |
568 | 2.14k | xsltRegisterLocalRVT(tctxt, container); |
569 | | |
570 | 2.14k | text_node = xmlNewTextLen(str, len); |
571 | 2.14k | if (text_node == NULL) { |
572 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
573 | 0 | return(-1); |
574 | 0 | } |
575 | 2.14k | xmlAddChild((xmlNodePtr) container, text_node); |
576 | | |
577 | 2.14k | ret = xmlXPathNewNodeSet(text_node); |
578 | 2.14k | if (ret == NULL) { |
579 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
580 | 0 | return(-1); |
581 | 0 | } |
582 | | |
583 | 2.14k | valuePush(ctxt, ret); |
584 | | |
585 | 2.14k | return(0); |
586 | 2.14k | } |
587 | | |
588 | | /** |
589 | | * exsltStrReplaceFunction: |
590 | | * @ctxt: an XPath parser context |
591 | | * @nargs: the number of arguments |
592 | | * |
593 | | * Takes a string, and two node sets and returns the string with all strings in |
594 | | * the first node set replaced by all strings in the second node set. |
595 | | */ |
596 | | static void |
597 | 2.34k | exsltStrReplaceFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
598 | 2.34k | int i, i_empty, n, slen0, rlen0, *slen, *rlen; |
599 | 2.34k | void *mem = NULL; |
600 | 2.34k | const xmlChar *src, *start; |
601 | 2.34k | xmlChar *string, *search_str = NULL, *replace_str = NULL; |
602 | 2.34k | xmlChar **search, **replace; |
603 | 2.34k | xmlNodeSetPtr search_set = NULL, replace_set = NULL; |
604 | 2.34k | xmlBufferPtr buf; |
605 | | |
606 | 2.34k | if (nargs != 3) { |
607 | 198 | xmlXPathSetArityError(ctxt); |
608 | 198 | return; |
609 | 198 | } |
610 | | |
611 | | /* get replace argument */ |
612 | | |
613 | 2.14k | if (!xmlXPathStackIsNodeSet(ctxt)) |
614 | 543 | replace_str = xmlXPathPopString(ctxt); |
615 | 1.60k | else |
616 | 1.60k | replace_set = xmlXPathPopNodeSet(ctxt); |
617 | | |
618 | 2.14k | if (xmlXPathCheckError(ctxt)) |
619 | 0 | goto fail_replace; |
620 | | |
621 | | /* get search argument */ |
622 | | |
623 | 2.14k | if (!xmlXPathStackIsNodeSet(ctxt)) { |
624 | 322 | search_str = xmlXPathPopString(ctxt); |
625 | 322 | n = 1; |
626 | 322 | } |
627 | 1.82k | else { |
628 | 1.82k | search_set = xmlXPathPopNodeSet(ctxt); |
629 | 1.82k | n = search_set != NULL ? search_set->nodeNr : 0; |
630 | 1.82k | } |
631 | | |
632 | 2.14k | if (xmlXPathCheckError(ctxt)) |
633 | 0 | goto fail_search; |
634 | | |
635 | | /* get string argument */ |
636 | | |
637 | 2.14k | string = xmlXPathPopString(ctxt); |
638 | 2.14k | if (xmlXPathCheckError(ctxt)) |
639 | 0 | goto fail_string; |
640 | | |
641 | | /* check for empty search node list */ |
642 | | |
643 | 2.14k | if (n <= 0) { |
644 | 113 | exsltStrReturnString(ctxt, string, xmlStrlen(string)); |
645 | 113 | goto done_empty_search; |
646 | 113 | } |
647 | | |
648 | | /* allocate memory for string pointer and length arrays */ |
649 | | |
650 | 2.03k | if (n == 1) { |
651 | 515 | search = &search_str; |
652 | 515 | replace = &replace_str; |
653 | 515 | slen = &slen0; |
654 | 515 | rlen = &rlen0; |
655 | 515 | } |
656 | 1.52k | else { |
657 | 1.52k | mem = xmlMalloc(2 * n * (sizeof(const xmlChar *) + sizeof(int))); |
658 | 1.52k | if (mem == NULL) { |
659 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
660 | 0 | goto fail_malloc; |
661 | 0 | } |
662 | 1.52k | search = (xmlChar **) mem; |
663 | 1.52k | replace = search + n; |
664 | 1.52k | slen = (int *) (replace + n); |
665 | 1.52k | rlen = slen + n; |
666 | 1.52k | } |
667 | | |
668 | | /* process arguments */ |
669 | | |
670 | 2.03k | i_empty = -1; |
671 | | |
672 | 190k | for (i=0; i<n; ++i) { |
673 | 188k | if (search_set != NULL) { |
674 | 187k | search[i] = xmlXPathCastNodeToString(search_set->nodeTab[i]); |
675 | 187k | if (search[i] == NULL) { |
676 | 0 | n = i; |
677 | 0 | goto fail_process_args; |
678 | 0 | } |
679 | 187k | } |
680 | | |
681 | 188k | slen[i] = xmlStrlen(search[i]); |
682 | 188k | if (i_empty < 0 && slen[i] == 0) |
683 | 220 | i_empty = i; |
684 | | |
685 | 188k | if (replace_set != NULL) { |
686 | 95.6k | if (i < replace_set->nodeNr) { |
687 | 7.64k | replace[i] = xmlXPathCastNodeToString(replace_set->nodeTab[i]); |
688 | 7.64k | if (replace[i] == NULL) { |
689 | 0 | n = i + 1; |
690 | 0 | goto fail_process_args; |
691 | 0 | } |
692 | 7.64k | } |
693 | 87.9k | else |
694 | 87.9k | replace[i] = NULL; |
695 | 95.6k | } |
696 | 92.5k | else { |
697 | 92.5k | if (i == 0) |
698 | 587 | replace[i] = replace_str; |
699 | 91.9k | else |
700 | 91.9k | replace[i] = NULL; |
701 | 92.5k | } |
702 | | |
703 | 188k | if (replace[i] == NULL) |
704 | 179k | rlen[i] = 0; |
705 | 8.14k | else |
706 | 8.14k | rlen[i] = xmlStrlen(replace[i]); |
707 | 188k | } |
708 | | |
709 | 2.03k | if (i_empty >= 0 && rlen[i_empty] == 0) |
710 | 23 | i_empty = -1; |
711 | | |
712 | | /* replace operation */ |
713 | | |
714 | 2.03k | buf = xmlBufferCreate(); |
715 | 2.03k | if (buf == NULL) { |
716 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
717 | 0 | goto fail_buffer; |
718 | 0 | } |
719 | 2.03k | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
720 | 2.03k | src = string; |
721 | 2.03k | start = string; |
722 | | |
723 | 4.39M | while (*src != 0) { |
724 | 4.39M | int max_len = 0, i_match = 0; |
725 | | |
726 | 53.6M | for (i=0; i<n; ++i) { |
727 | 49.2M | if (*src == search[i][0] && |
728 | 49.2M | slen[i] > max_len && |
729 | 49.2M | xmlStrncmp(src, search[i], slen[i]) == 0) |
730 | 1.30M | { |
731 | 1.30M | i_match = i; |
732 | 1.30M | max_len = slen[i]; |
733 | 1.30M | } |
734 | 49.2M | } |
735 | | |
736 | 4.39M | if (max_len == 0) { |
737 | 3.08M | if (i_empty >= 0 && start < src) { |
738 | 92.3k | if (xmlBufferAdd(buf, start, src - start) || |
739 | 92.3k | xmlBufferAdd(buf, replace[i_empty], rlen[i_empty])) |
740 | 0 | { |
741 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
742 | 0 | goto fail_buffer_add; |
743 | 0 | } |
744 | 92.3k | start = src; |
745 | 92.3k | } |
746 | | |
747 | 3.08M | src += xmlUTF8Strsize(src, 1); |
748 | 3.08M | } |
749 | 1.30M | else { |
750 | 1.30M | if ((start < src && |
751 | 1.30M | xmlBufferAdd(buf, start, src - start)) || |
752 | 1.30M | (rlen[i_match] && |
753 | 1.30M | xmlBufferAdd(buf, replace[i_match], rlen[i_match]))) |
754 | 0 | { |
755 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
756 | 0 | goto fail_buffer_add; |
757 | 0 | } |
758 | | |
759 | 1.30M | src += slen[i_match]; |
760 | 1.30M | start = src; |
761 | 1.30M | } |
762 | 4.39M | } |
763 | | |
764 | 2.03k | if (start < src && xmlBufferAdd(buf, start, src - start)) { |
765 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
766 | 0 | goto fail_buffer_add; |
767 | 0 | } |
768 | | |
769 | | /* create result node set */ |
770 | | |
771 | 2.03k | exsltStrReturnString(ctxt, xmlBufferContent(buf), xmlBufferLength(buf)); |
772 | | |
773 | | /* clean up */ |
774 | | |
775 | 2.03k | fail_buffer_add: |
776 | 2.03k | xmlBufferFree(buf); |
777 | | |
778 | 2.03k | fail_buffer: |
779 | 2.03k | fail_process_args: |
780 | 2.03k | if (search_set != NULL) { |
781 | 189k | for (i=0; i<n; ++i) |
782 | 187k | xmlFree(search[i]); |
783 | 1.71k | } |
784 | 2.03k | if (replace_set != NULL) { |
785 | 97.0k | for (i=0; i<n; ++i) { |
786 | 95.6k | if (replace[i] != NULL) |
787 | 7.64k | xmlFree(replace[i]); |
788 | 95.6k | } |
789 | 1.44k | } |
790 | | |
791 | 2.03k | if (mem != NULL) |
792 | 1.52k | xmlFree(mem); |
793 | | |
794 | 2.03k | fail_malloc: |
795 | 2.14k | done_empty_search: |
796 | 2.14k | xmlFree(string); |
797 | | |
798 | 2.14k | fail_string: |
799 | 2.14k | if (search_set != NULL) |
800 | 1.79k | xmlXPathFreeNodeSet(search_set); |
801 | 353 | else |
802 | 353 | xmlFree(search_str); |
803 | | |
804 | 2.14k | fail_search: |
805 | 2.14k | if (replace_set != NULL) |
806 | 1.50k | xmlXPathFreeNodeSet(replace_set); |
807 | 642 | else |
808 | 642 | xmlFree(replace_str); |
809 | | |
810 | 2.14k | fail_replace: |
811 | 2.14k | return; |
812 | 2.14k | } |
813 | | |
814 | | /** |
815 | | * exsltStrRegister: |
816 | | * |
817 | | * Registers the EXSLT - Strings module |
818 | | */ |
819 | | |
820 | | void |
821 | 7.71k | exsltStrRegister (void) { |
822 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "tokenize", |
823 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
824 | 7.71k | exsltStrTokenizeFunction); |
825 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "split", |
826 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
827 | 7.71k | exsltStrSplitFunction); |
828 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "encode-uri", |
829 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
830 | 7.71k | exsltStrEncodeUriFunction); |
831 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "decode-uri", |
832 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
833 | 7.71k | exsltStrDecodeUriFunction); |
834 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "padding", |
835 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
836 | 7.71k | exsltStrPaddingFunction); |
837 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "align", |
838 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
839 | 7.71k | exsltStrAlignFunction); |
840 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "concat", |
841 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
842 | 7.71k | exsltStrConcatFunction); |
843 | 7.71k | xsltRegisterExtModuleFunction ((const xmlChar *) "replace", |
844 | 7.71k | EXSLT_STRINGS_NAMESPACE, |
845 | 7.71k | exsltStrReplaceFunction); |
846 | 7.71k | } |
847 | | |
848 | | /** |
849 | | * exsltStrXpathCtxtRegister: |
850 | | * |
851 | | * Registers the EXSLT - Strings module for use outside XSLT |
852 | | */ |
853 | | int |
854 | | exsltStrXpathCtxtRegister (xmlXPathContextPtr ctxt, const xmlChar *prefix) |
855 | 0 | { |
856 | 0 | if (ctxt |
857 | 0 | && prefix |
858 | 0 | && !xmlXPathRegisterNs(ctxt, |
859 | 0 | prefix, |
860 | 0 | (const xmlChar *) EXSLT_STRINGS_NAMESPACE) |
861 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
862 | 0 | (const xmlChar *) "encode-uri", |
863 | 0 | (const xmlChar *) EXSLT_STRINGS_NAMESPACE, |
864 | 0 | exsltStrEncodeUriFunction) |
865 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
866 | 0 | (const xmlChar *) "decode-uri", |
867 | 0 | (const xmlChar *) EXSLT_STRINGS_NAMESPACE, |
868 | 0 | exsltStrDecodeUriFunction) |
869 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
870 | 0 | (const xmlChar *) "padding", |
871 | 0 | (const xmlChar *) EXSLT_STRINGS_NAMESPACE, |
872 | 0 | exsltStrPaddingFunction) |
873 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
874 | 0 | (const xmlChar *) "align", |
875 | 0 | (const xmlChar *) EXSLT_STRINGS_NAMESPACE, |
876 | 0 | exsltStrAlignFunction) |
877 | 0 | && !xmlXPathRegisterFuncNS(ctxt, |
878 | 0 | (const xmlChar *) "concat", |
879 | 0 | (const xmlChar *) EXSLT_STRINGS_NAMESPACE, |
880 | 0 | exsltStrConcatFunction)) { |
881 | 0 | return 0; |
882 | 0 | } |
883 | 0 | return -1; |
884 | 0 | } |