/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 | 793 | { |
28 | 793 | xsltTransformContextPtr tctxt; |
29 | 793 | xmlChar *str, *delimiters, *cur; |
30 | 793 | const xmlChar *token, *delimiter; |
31 | 793 | xmlNodePtr node; |
32 | 793 | xmlDocPtr container; |
33 | 793 | xmlXPathObjectPtr ret = NULL; |
34 | 793 | int clen; |
35 | | |
36 | 793 | if ((nargs < 1) || (nargs > 2)) { |
37 | 2 | xmlXPathSetArityError(ctxt); |
38 | 2 | return; |
39 | 2 | } |
40 | | |
41 | 791 | if (nargs == 2) { |
42 | 543 | delimiters = xmlXPathPopString(ctxt); |
43 | 543 | if (xmlXPathCheckError(ctxt)) |
44 | 2 | return; |
45 | 543 | } else { |
46 | 248 | delimiters = xmlStrdup((const xmlChar *) "\t\r\n "); |
47 | 248 | } |
48 | 789 | if (delimiters == NULL) |
49 | 1 | return; |
50 | | |
51 | 788 | str = xmlXPathPopString(ctxt); |
52 | 788 | if (xmlXPathCheckError(ctxt) || (str == NULL)) { |
53 | 1 | xmlFree(delimiters); |
54 | 1 | return; |
55 | 1 | } |
56 | | |
57 | | /* Return a result tree fragment */ |
58 | 787 | tctxt = xsltXPathGetTransformContext(ctxt); |
59 | 787 | 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 | 787 | container = xsltCreateRVT(tctxt); |
66 | 787 | if (container != NULL) { |
67 | 785 | xsltRegisterLocalRVT(tctxt, container); |
68 | 785 | ret = xmlXPathNewNodeSet(NULL); |
69 | 785 | if (ret != NULL) { |
70 | 23.0k | for (cur = str, token = str; *cur != 0; cur += clen) { |
71 | 22.2k | clen = xmlUTF8Strsize(cur, 1); |
72 | 22.2k | if (*delimiters == 0) { /* empty string case */ |
73 | 313 | xmlChar ctmp; |
74 | 313 | ctmp = *(cur+clen); |
75 | 313 | *(cur+clen) = 0; |
76 | 313 | node = xmlNewDocRawNode(container, NULL, |
77 | 313 | (const xmlChar *) "token", cur); |
78 | 313 | xmlAddChild((xmlNodePtr) container, node); |
79 | 313 | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
80 | 313 | *(cur+clen) = ctmp; /* restore the changed byte */ |
81 | 313 | token = cur + clen; |
82 | 211k | } else for (delimiter = delimiters; *delimiter != 0; |
83 | 191k | delimiter += xmlUTF8Strsize(delimiter, 1)) { |
84 | 191k | if (!xmlUTF8Charcmp(cur, delimiter)) { |
85 | 1.95k | if (cur == token) { |
86 | | /* discard empty tokens */ |
87 | 784 | token = cur + clen; |
88 | 784 | break; |
89 | 784 | } |
90 | 1.16k | *cur = 0; /* terminate the token */ |
91 | 1.16k | node = xmlNewDocRawNode(container, NULL, |
92 | 1.16k | (const xmlChar *) "token", token); |
93 | 1.16k | xmlAddChild((xmlNodePtr) container, node); |
94 | 1.16k | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
95 | 1.16k | *cur = *delimiter; /* restore the changed byte */ |
96 | 1.16k | token = cur + clen; |
97 | 1.16k | break; |
98 | 1.95k | } |
99 | 191k | } |
100 | 22.2k | } |
101 | 782 | if (token != cur) { |
102 | 505 | node = xmlNewDocRawNode(container, NULL, |
103 | 505 | (const xmlChar *) "token", token); |
104 | 505 | xmlAddChild((xmlNodePtr) container, node); |
105 | 505 | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
106 | 505 | } |
107 | 782 | } |
108 | 785 | } |
109 | | |
110 | 787 | fail: |
111 | 787 | if (str != NULL) |
112 | 787 | xmlFree(str); |
113 | 787 | if (delimiters != NULL) |
114 | 787 | xmlFree(delimiters); |
115 | 787 | if (ret != NULL) |
116 | 782 | valuePush(ctxt, ret); |
117 | 5 | else |
118 | 5 | valuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
119 | 787 | } |
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 | 4.75k | exsltStrSplitFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
131 | 4.75k | xsltTransformContextPtr tctxt; |
132 | 4.75k | xmlChar *str, *delimiter, *cur; |
133 | 4.75k | const xmlChar *token; |
134 | 4.75k | xmlNodePtr node; |
135 | 4.75k | xmlDocPtr container; |
136 | 4.75k | xmlXPathObjectPtr ret = NULL; |
137 | 4.75k | int delimiterLength; |
138 | | |
139 | 4.75k | if ((nargs < 1) || (nargs > 2)) { |
140 | 431 | xmlXPathSetArityError(ctxt); |
141 | 431 | return; |
142 | 431 | } |
143 | | |
144 | 4.31k | if (nargs == 2) { |
145 | 3.69k | delimiter = xmlXPathPopString(ctxt); |
146 | 3.69k | if (xmlXPathCheckError(ctxt)) |
147 | 1 | return; |
148 | 3.69k | } else { |
149 | 624 | delimiter = xmlStrdup((const xmlChar *) " "); |
150 | 624 | } |
151 | 4.31k | if (delimiter == NULL) |
152 | 1 | return; |
153 | 4.31k | delimiterLength = xmlStrlen (delimiter); |
154 | | |
155 | 4.31k | str = xmlXPathPopString(ctxt); |
156 | 4.31k | if (xmlXPathCheckError(ctxt) || (str == NULL)) { |
157 | 1 | xmlFree(delimiter); |
158 | 1 | return; |
159 | 1 | } |
160 | | |
161 | | /* Return a result tree fragment */ |
162 | 4.31k | tctxt = xsltXPathGetTransformContext(ctxt); |
163 | 4.31k | 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 | 4.31k | container = xsltCreateRVT(tctxt); |
173 | 4.31k | if (container != NULL) { |
174 | 4.31k | xsltRegisterLocalRVT(tctxt, container); |
175 | 4.31k | ret = xmlXPathNewNodeSet(NULL); |
176 | 4.31k | if (ret != NULL) { |
177 | 36.5k | for (cur = str, token = str; *cur != 0; cur++) { |
178 | 32.2k | if (delimiterLength == 0) { |
179 | 22.3k | if (cur != token) { |
180 | 18.7k | xmlChar tmp = *cur; |
181 | 18.7k | *cur = 0; |
182 | 18.7k | node = xmlNewDocRawNode(container, NULL, |
183 | 18.7k | (const xmlChar *) "token", token); |
184 | 18.7k | xmlAddChild((xmlNodePtr) container, node); |
185 | 18.7k | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
186 | 18.7k | *cur = tmp; |
187 | 18.7k | token++; |
188 | 18.7k | } |
189 | 22.3k | } |
190 | 9.95k | else if (!xmlStrncasecmp(cur, delimiter, delimiterLength)) { |
191 | 3.21k | if (cur == token) { |
192 | | /* discard empty tokens */ |
193 | 2.50k | cur = cur + delimiterLength - 1; |
194 | 2.50k | token = cur + 1; |
195 | 2.50k | continue; |
196 | 2.50k | } |
197 | 714 | *cur = 0; |
198 | 714 | node = xmlNewDocRawNode(container, NULL, |
199 | 714 | (const xmlChar *) "token", token); |
200 | 714 | xmlAddChild((xmlNodePtr) container, node); |
201 | 714 | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
202 | 714 | *cur = *delimiter; |
203 | 714 | cur = cur + delimiterLength - 1; |
204 | 714 | token = cur + 1; |
205 | 714 | } |
206 | 32.2k | } |
207 | 4.31k | if (token != cur) { |
208 | 3.95k | node = xmlNewDocRawNode(container, NULL, |
209 | 3.95k | (const xmlChar *) "token", token); |
210 | 3.95k | xmlAddChild((xmlNodePtr) container, node); |
211 | 3.95k | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
212 | 3.95k | } |
213 | 4.31k | } |
214 | 4.31k | } |
215 | | |
216 | 4.31k | fail: |
217 | 4.31k | if (str != NULL) |
218 | 4.31k | xmlFree(str); |
219 | 4.31k | if (delimiter != NULL) |
220 | 4.31k | xmlFree(delimiter); |
221 | 4.31k | if (ret != NULL) |
222 | 4.31k | valuePush(ctxt, ret); |
223 | 3 | else |
224 | 3 | valuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
225 | 4.31k | } |
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 | 973 | exsltStrEncodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
236 | 973 | int escape_all = 1, str_len = 0; |
237 | 973 | xmlChar *str = NULL, *ret = NULL, *tmp; |
238 | | |
239 | 973 | if ((nargs < 2) || (nargs > 3)) { |
240 | 1 | xmlXPathSetArityError(ctxt); |
241 | 1 | return; |
242 | 1 | } |
243 | | |
244 | 972 | if (nargs >= 3) { |
245 | | /* check for UTF-8 if encoding was explicitly given; |
246 | | we don't support anything else yet */ |
247 | 2 | tmp = xmlXPathPopString(ctxt); |
248 | 2 | if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) { |
249 | 2 | xmlXPathReturnEmptyString(ctxt); |
250 | 2 | xmlFree(tmp); |
251 | 2 | return; |
252 | 2 | } |
253 | 0 | xmlFree(tmp); |
254 | 0 | } |
255 | | |
256 | 970 | escape_all = xmlXPathPopBoolean(ctxt); |
257 | | |
258 | 970 | str = xmlXPathPopString(ctxt); |
259 | 970 | str_len = xmlUTF8Strlen(str); |
260 | | |
261 | 970 | if (str_len <= 0) { |
262 | 231 | if (str_len < 0) |
263 | 2 | xsltGenericError(xsltGenericErrorContext, |
264 | 2 | "exsltStrEncodeUriFunction: invalid UTF-8\n"); |
265 | 231 | xmlXPathReturnEmptyString(ctxt); |
266 | 231 | xmlFree(str); |
267 | 231 | return; |
268 | 231 | } |
269 | | |
270 | 739 | ret = xmlURIEscapeStr(str,(const xmlChar *)(escape_all?"-_.!~*'()":"-_.!~*'();/?:@&=+$,[]")); |
271 | 739 | xmlXPathReturnString(ctxt, ret); |
272 | | |
273 | 739 | if (str != NULL) |
274 | 739 | xmlFree(str); |
275 | 739 | } |
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 | 167 | exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
286 | 167 | int str_len = 0; |
287 | 167 | xmlChar *str = NULL, *ret = NULL, *tmp; |
288 | | |
289 | 167 | if ((nargs < 1) || (nargs > 2)) { |
290 | 1 | xmlXPathSetArityError(ctxt); |
291 | 1 | return; |
292 | 1 | } |
293 | | |
294 | 166 | if (nargs >= 2) { |
295 | | /* check for UTF-8 if encoding was explicitly given; |
296 | | we don't support anything else yet */ |
297 | 2 | tmp = xmlXPathPopString(ctxt); |
298 | 2 | if (xmlUTF8Strlen(tmp) != 5 || xmlStrcmp((const xmlChar *)"UTF-8",tmp)) { |
299 | 2 | xmlXPathReturnEmptyString(ctxt); |
300 | 2 | xmlFree(tmp); |
301 | 2 | return; |
302 | 2 | } |
303 | 0 | xmlFree(tmp); |
304 | 0 | } |
305 | | |
306 | 164 | str = xmlXPathPopString(ctxt); |
307 | 164 | str_len = xmlUTF8Strlen(str); |
308 | | |
309 | 164 | if (str_len <= 0) { |
310 | 54 | if (str_len < 0) |
311 | 1 | xsltGenericError(xsltGenericErrorContext, |
312 | 1 | "exsltStrDecodeUriFunction: invalid UTF-8\n"); |
313 | 54 | xmlXPathReturnEmptyString(ctxt); |
314 | 54 | xmlFree(str); |
315 | 54 | return; |
316 | 54 | } |
317 | | |
318 | 110 | ret = (xmlChar *) xmlURIUnescapeString((const char *)str,0,NULL); |
319 | 110 | 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 | xmlXPathReturnEmptyString(ctxt); |
323 | 1 | xmlFree(str); |
324 | 1 | xmlFree(ret); |
325 | 1 | return; |
326 | 1 | } |
327 | | |
328 | 110 | xmlXPathReturnString(ctxt, ret); |
329 | | |
330 | 109 | if (str != NULL) |
331 | 109 | xmlFree(str); |
332 | 109 | } |
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 | 722 | exsltStrPaddingFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
343 | 722 | int number, str_len = 0, str_size = 0; |
344 | 722 | double floatval; |
345 | 722 | xmlChar *str = NULL; |
346 | 722 | xmlBufferPtr buf; |
347 | | |
348 | 722 | if ((nargs < 1) || (nargs > 2)) { |
349 | 1 | xmlXPathSetArityError(ctxt); |
350 | 1 | return; |
351 | 1 | } |
352 | | |
353 | 721 | if (nargs == 2) { |
354 | 539 | str = xmlXPathPopString(ctxt); |
355 | 539 | str_len = xmlUTF8Strlen(str); |
356 | 539 | str_size = xmlStrlen(str); |
357 | 539 | } |
358 | | |
359 | 721 | floatval = xmlXPathPopNumber(ctxt); |
360 | | |
361 | 721 | if (str_len <= 0) { |
362 | 265 | if (str_len < 0) { |
363 | 1 | xsltGenericError(xsltGenericErrorContext, |
364 | 1 | "exsltStrPaddingFunction: invalid UTF-8\n"); |
365 | 1 | xmlXPathReturnEmptyString(ctxt); |
366 | 1 | xmlFree(str); |
367 | 1 | return; |
368 | 1 | } |
369 | 264 | if (str != NULL) xmlFree(str); |
370 | 264 | str = xmlStrdup((const xmlChar *) " "); |
371 | 264 | str_len = 1; |
372 | 264 | str_size = 1; |
373 | 264 | } |
374 | | |
375 | 720 | if (xmlXPathIsNaN(floatval) || floatval < 0.0) { |
376 | 374 | number = 0; |
377 | 374 | } else if (floatval >= 100000.0) { |
378 | 79 | number = 100000; |
379 | 79 | } |
380 | 267 | else { |
381 | 267 | number = (int) floatval; |
382 | 267 | } |
383 | | |
384 | 720 | if (number <= 0) { |
385 | 453 | xmlXPathReturnEmptyString(ctxt); |
386 | 453 | xmlFree(str); |
387 | 453 | return; |
388 | 453 | } |
389 | | |
390 | 267 | buf = xmlBufferCreateSize(number); |
391 | 267 | if (buf == NULL) { |
392 | 2 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
393 | 2 | xmlFree(str); |
394 | 2 | return; |
395 | 2 | } |
396 | 265 | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
397 | | |
398 | 450k | while (number >= str_len) { |
399 | 450k | xmlBufferAdd(buf, str, str_size); |
400 | 450k | number -= str_len; |
401 | 450k | } |
402 | 265 | if (number > 0) { |
403 | 173 | str_size = xmlUTF8Strsize(str, number); |
404 | 173 | xmlBufferAdd(buf, str, str_size); |
405 | 173 | } |
406 | | |
407 | 265 | xmlXPathReturnString(ctxt, xmlBufferDetach(buf)); |
408 | | |
409 | 265 | xmlBufferFree(buf); |
410 | 265 | if (str != NULL) |
411 | 265 | xmlFree(str); |
412 | 265 | } |
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 | 1.23k | exsltStrAlignFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
423 | 1.23k | xmlChar *str, *padding, *alignment, *ret; |
424 | 1.23k | int str_l, padding_l; |
425 | | |
426 | 1.23k | if ((nargs < 2) || (nargs > 3)) { |
427 | 1 | xmlXPathSetArityError(ctxt); |
428 | 1 | return; |
429 | 1 | } |
430 | | |
431 | 1.23k | if (nargs == 3) |
432 | 1.11k | alignment = xmlXPathPopString(ctxt); |
433 | 113 | else |
434 | 113 | alignment = NULL; |
435 | | |
436 | 1.23k | padding = xmlXPathPopString(ctxt); |
437 | 1.23k | str = xmlXPathPopString(ctxt); |
438 | | |
439 | 1.23k | str_l = xmlUTF8Strlen (str); |
440 | 1.23k | padding_l = xmlUTF8Strlen (padding); |
441 | | |
442 | 1.23k | if (str_l < 0 || padding_l < 0) { |
443 | 1 | xsltGenericError(xsltGenericErrorContext, |
444 | 1 | "exsltStrAlignFunction: invalid UTF-8\n"); |
445 | 1 | xmlXPathReturnEmptyString(ctxt); |
446 | 1 | xmlFree(str); |
447 | 1 | xmlFree(padding); |
448 | 1 | xmlFree(alignment); |
449 | 1 | return; |
450 | 1 | } |
451 | | |
452 | 1.22k | if (str_l == padding_l) { |
453 | 360 | xmlXPathReturnString (ctxt, str); |
454 | 360 | xmlFree(padding); |
455 | 360 | xmlFree(alignment); |
456 | 360 | return; |
457 | 360 | } |
458 | | |
459 | 869 | if (str_l > padding_l) { |
460 | 332 | ret = xmlUTF8Strndup (str, padding_l); |
461 | 537 | } else { |
462 | 537 | if (xmlStrEqual(alignment, (const xmlChar *) "right")) { |
463 | 164 | ret = xmlUTF8Strndup (padding, padding_l - str_l); |
464 | 164 | ret = xmlStrcat (ret, str); |
465 | 373 | } else if (xmlStrEqual(alignment, (const xmlChar *) "center")) { |
466 | 179 | int left = (padding_l - str_l) / 2; |
467 | 179 | int right_start; |
468 | | |
469 | 179 | ret = xmlUTF8Strndup (padding, left); |
470 | 179 | ret = xmlStrcat (ret, str); |
471 | | |
472 | 179 | right_start = xmlUTF8Strsize (padding, left + str_l); |
473 | 179 | ret = xmlStrcat (ret, padding + right_start); |
474 | 194 | } else { |
475 | 194 | int str_s; |
476 | | |
477 | 194 | str_s = xmlUTF8Strsize(padding, str_l); |
478 | 194 | ret = xmlStrdup (str); |
479 | 194 | ret = xmlStrcat (ret, padding + str_s); |
480 | 194 | } |
481 | 537 | } |
482 | | |
483 | 869 | xmlXPathReturnString (ctxt, ret); |
484 | | |
485 | 869 | xmlFree(str); |
486 | 869 | xmlFree(padding); |
487 | 869 | xmlFree(alignment); |
488 | 869 | } |
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 | 0 | exsltStrConcatFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
501 | 0 | xmlXPathObjectPtr obj; |
502 | 0 | xmlBufferPtr buf; |
503 | 0 | int i; |
504 | |
|
505 | 0 | if (nargs != 1) { |
506 | 0 | xmlXPathSetArityError(ctxt); |
507 | 0 | return; |
508 | 0 | } |
509 | | |
510 | 0 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
511 | 0 | xmlXPathSetTypeError(ctxt); |
512 | 0 | return; |
513 | 0 | } |
514 | | |
515 | 0 | obj = valuePop (ctxt); |
516 | |
|
517 | 0 | if (xmlXPathNodeSetIsEmpty(obj->nodesetval)) { |
518 | 0 | xmlXPathFreeObject(obj); |
519 | 0 | xmlXPathReturnEmptyString(ctxt); |
520 | 0 | return; |
521 | 0 | } |
522 | | |
523 | 0 | buf = xmlBufferCreate(); |
524 | 0 | if (buf == NULL) { |
525 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
526 | 0 | xmlXPathFreeObject(obj); |
527 | 0 | return; |
528 | 0 | } |
529 | 0 | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
530 | |
|
531 | 0 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
532 | 0 | xmlChar *tmp; |
533 | 0 | tmp = xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
534 | |
|
535 | 0 | xmlBufferCat(buf, tmp); |
536 | |
|
537 | 0 | xmlFree(tmp); |
538 | 0 | } |
539 | |
|
540 | 0 | xmlXPathFreeObject (obj); |
541 | |
|
542 | 0 | xmlXPathReturnString(ctxt, xmlBufferDetach(buf)); |
543 | 0 | xmlBufferFree(buf); |
544 | 0 | } |
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 | 18.1k | { |
558 | 18.1k | xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt); |
559 | 18.1k | xmlDocPtr container; |
560 | 18.1k | xmlNodePtr text_node; |
561 | 18.1k | xmlXPathObjectPtr ret; |
562 | | |
563 | 18.1k | container = xsltCreateRVT(tctxt); |
564 | 18.1k | if (container == NULL) { |
565 | 1 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
566 | 1 | return(-1); |
567 | 1 | } |
568 | 18.1k | xsltRegisterLocalRVT(tctxt, container); |
569 | | |
570 | 18.1k | text_node = xmlNewTextLen(str, len); |
571 | 18.1k | if (text_node == NULL) { |
572 | 9 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
573 | 9 | return(-1); |
574 | 9 | } |
575 | 18.1k | xmlAddChild((xmlNodePtr) container, text_node); |
576 | | |
577 | 18.1k | ret = xmlXPathNewNodeSet(text_node); |
578 | 18.1k | if (ret == NULL) { |
579 | 6 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
580 | 6 | return(-1); |
581 | 6 | } |
582 | | |
583 | 18.1k | valuePush(ctxt, ret); |
584 | | |
585 | 18.1k | return(0); |
586 | 18.1k | } |
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 | 19.3k | exsltStrReplaceFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
598 | 19.3k | int i, i_empty, n, slen0, rlen0, *slen, *rlen; |
599 | 19.3k | void *mem = NULL; |
600 | 19.3k | const xmlChar *src, *start; |
601 | 19.3k | xmlChar *string, *search_str = NULL, *replace_str = NULL; |
602 | 19.3k | xmlChar **search, **replace; |
603 | 19.3k | xmlNodeSetPtr search_set = NULL, replace_set = NULL; |
604 | 19.3k | xmlBufferPtr buf; |
605 | | |
606 | 19.3k | if (nargs != 3) { |
607 | 1.07k | xmlXPathSetArityError(ctxt); |
608 | 1.07k | return; |
609 | 1.07k | } |
610 | | |
611 | | /* get replace argument */ |
612 | | |
613 | 18.2k | if (!xmlXPathStackIsNodeSet(ctxt)) |
614 | 6.79k | replace_str = xmlXPathPopString(ctxt); |
615 | 11.4k | else |
616 | 11.4k | replace_set = xmlXPathPopNodeSet(ctxt); |
617 | | |
618 | 18.2k | if (xmlXPathCheckError(ctxt)) |
619 | 2 | goto fail_replace; |
620 | | |
621 | | /* get search argument */ |
622 | | |
623 | 18.2k | if (!xmlXPathStackIsNodeSet(ctxt)) { |
624 | 4.77k | search_str = xmlXPathPopString(ctxt); |
625 | 4.77k | n = 1; |
626 | 4.77k | } |
627 | 13.4k | else { |
628 | 13.4k | search_set = xmlXPathPopNodeSet(ctxt); |
629 | 13.4k | n = search_set != NULL ? search_set->nodeNr : 0; |
630 | 13.4k | } |
631 | | |
632 | 18.2k | if (xmlXPathCheckError(ctxt)) |
633 | 1 | goto fail_search; |
634 | | |
635 | | /* get string argument */ |
636 | | |
637 | 18.2k | string = xmlXPathPopString(ctxt); |
638 | 18.2k | if (xmlXPathCheckError(ctxt)) |
639 | 1 | goto fail_string; |
640 | | |
641 | | /* check for empty search node list */ |
642 | | |
643 | 18.2k | if (n <= 0) { |
644 | 2.79k | exsltStrReturnString(ctxt, string, xmlStrlen(string)); |
645 | 2.79k | goto done_empty_search; |
646 | 2.79k | } |
647 | | |
648 | | /* allocate memory for string pointer and length arrays */ |
649 | | |
650 | 15.4k | if (n == 1) { |
651 | 6.05k | search = &search_str; |
652 | 6.05k | replace = &replace_str; |
653 | 6.05k | slen = &slen0; |
654 | 6.05k | rlen = &rlen0; |
655 | 6.05k | } |
656 | 9.37k | else { |
657 | 9.37k | mem = xmlMalloc(2 * n * (sizeof(const xmlChar *) + sizeof(int))); |
658 | 9.37k | if (mem == NULL) { |
659 | 1 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
660 | 1 | goto fail_malloc; |
661 | 1 | } |
662 | 9.37k | search = (xmlChar **) mem; |
663 | 9.37k | replace = search + n; |
664 | 9.37k | slen = (int *) (replace + n); |
665 | 9.37k | rlen = slen + n; |
666 | 9.37k | } |
667 | | |
668 | | /* process arguments */ |
669 | | |
670 | 15.4k | i_empty = -1; |
671 | | |
672 | 98.5k | for (i=0; i<n; ++i) { |
673 | 83.1k | if (search_set != NULL) { |
674 | 78.3k | search[i] = xmlXPathCastNodeToString(search_set->nodeTab[i]); |
675 | 78.3k | if (search[i] == NULL) { |
676 | 16 | n = i; |
677 | 16 | goto fail_process_args; |
678 | 16 | } |
679 | 78.3k | } |
680 | | |
681 | 83.1k | slen[i] = xmlStrlen(search[i]); |
682 | 83.1k | if (i_empty < 0 && slen[i] == 0) |
683 | 9.41k | i_empty = i; |
684 | | |
685 | 83.1k | if (replace_set != NULL) { |
686 | 51.2k | if (i < replace_set->nodeNr) { |
687 | 32.5k | replace[i] = xmlXPathCastNodeToString(replace_set->nodeTab[i]); |
688 | 32.5k | if (replace[i] == NULL) { |
689 | 2 | n = i + 1; |
690 | 2 | goto fail_process_args; |
691 | 2 | } |
692 | 32.5k | } |
693 | 18.7k | else |
694 | 18.7k | replace[i] = NULL; |
695 | 51.2k | } |
696 | 31.8k | else { |
697 | 31.8k | if (i == 0) |
698 | 3.99k | replace[i] = replace_str; |
699 | 27.8k | else |
700 | 27.8k | replace[i] = NULL; |
701 | 31.8k | } |
702 | | |
703 | 83.1k | if (replace[i] == NULL) |
704 | 46.6k | rlen[i] = 0; |
705 | 36.4k | else |
706 | 36.4k | rlen[i] = xmlStrlen(replace[i]); |
707 | 83.1k | } |
708 | | |
709 | 15.4k | if (i_empty >= 0 && rlen[i_empty] == 0) |
710 | 4.27k | i_empty = -1; |
711 | | |
712 | | /* replace operation */ |
713 | | |
714 | 15.4k | buf = xmlBufferCreate(); |
715 | 15.4k | if (buf == NULL) { |
716 | 3 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
717 | 3 | goto fail_buffer; |
718 | 3 | } |
719 | 15.4k | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
720 | 15.4k | src = string; |
721 | 15.4k | start = string; |
722 | | |
723 | 1.09M | while (*src != 0) { |
724 | 1.07M | int max_len = 0, i_match = 0; |
725 | | |
726 | 3.24M | for (i=0; i<n; ++i) { |
727 | 2.16M | if (*src == search[i][0] && |
728 | 2.16M | slen[i] > max_len && |
729 | 2.16M | xmlStrncmp(src, search[i], slen[i]) == 0) |
730 | 680k | { |
731 | 680k | i_match = i; |
732 | 680k | max_len = slen[i]; |
733 | 680k | } |
734 | 2.16M | } |
735 | | |
736 | 1.07M | if (max_len == 0) { |
737 | 398k | if (i_empty >= 0 && start < src) { |
738 | 73.2k | if (xmlBufferAdd(buf, start, src - start) || |
739 | 73.2k | xmlBufferAdd(buf, replace[i_empty], rlen[i_empty])) |
740 | 4 | { |
741 | 4 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
742 | 4 | goto fail_buffer_add; |
743 | 4 | } |
744 | 73.2k | start = src; |
745 | 73.2k | } |
746 | | |
747 | 398k | src += xmlUTF8Strsize(src, 1); |
748 | 398k | } |
749 | 680k | else { |
750 | 680k | if ((start < src && |
751 | 680k | xmlBufferAdd(buf, start, src - start)) || |
752 | 680k | (rlen[i_match] && |
753 | 680k | xmlBufferAdd(buf, replace[i_match], rlen[i_match]))) |
754 | 3 | { |
755 | 3 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
756 | 3 | goto fail_buffer_add; |
757 | 3 | } |
758 | | |
759 | 680k | src += slen[i_match]; |
760 | 680k | start = src; |
761 | 680k | } |
762 | 1.07M | } |
763 | | |
764 | 15.3k | if (start < src && xmlBufferAdd(buf, start, src - start)) { |
765 | 1 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
766 | 1 | goto fail_buffer_add; |
767 | 1 | } |
768 | | |
769 | | /* create result node set */ |
770 | | |
771 | 15.3k | exsltStrReturnString(ctxt, xmlBufferContent(buf), xmlBufferLength(buf)); |
772 | | |
773 | | /* clean up */ |
774 | | |
775 | 15.4k | fail_buffer_add: |
776 | 15.4k | xmlBufferFree(buf); |
777 | | |
778 | 15.4k | fail_buffer: |
779 | 15.4k | fail_process_args: |
780 | 15.4k | if (search_set != NULL) { |
781 | 89.0k | for (i=0; i<n; ++i) |
782 | 78.3k | xmlFree(search[i]); |
783 | 10.6k | } |
784 | 15.4k | if (replace_set != NULL) { |
785 | 62.7k | for (i=0; i<n; ++i) { |
786 | 51.2k | if (replace[i] != NULL) |
787 | 32.4k | xmlFree(replace[i]); |
788 | 51.2k | } |
789 | 11.4k | } |
790 | | |
791 | 15.4k | if (mem != NULL) |
792 | 9.37k | xmlFree(mem); |
793 | | |
794 | 15.4k | fail_malloc: |
795 | 18.2k | done_empty_search: |
796 | 18.2k | xmlFree(string); |
797 | | |
798 | 18.2k | fail_string: |
799 | 18.2k | if (search_set != NULL) |
800 | 13.4k | xmlXPathFreeNodeSet(search_set); |
801 | 4.77k | else |
802 | 4.77k | xmlFree(search_str); |
803 | | |
804 | 18.2k | fail_search: |
805 | 18.2k | if (replace_set != NULL) |
806 | 11.4k | xmlXPathFreeNodeSet(replace_set); |
807 | 6.78k | else |
808 | 6.78k | xmlFree(replace_str); |
809 | | |
810 | 18.2k | fail_replace: |
811 | 18.2k | return; |
812 | 18.2k | } |
813 | | |
814 | | /** |
815 | | * exsltStrRegister: |
816 | | * |
817 | | * Registers the EXSLT - Strings module |
818 | | */ |
819 | | |
820 | | void |
821 | 2 | exsltStrRegister (void) { |
822 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "tokenize", |
823 | 2 | EXSLT_STRINGS_NAMESPACE, |
824 | 2 | exsltStrTokenizeFunction); |
825 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "split", |
826 | 2 | EXSLT_STRINGS_NAMESPACE, |
827 | 2 | exsltStrSplitFunction); |
828 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "encode-uri", |
829 | 2 | EXSLT_STRINGS_NAMESPACE, |
830 | 2 | exsltStrEncodeUriFunction); |
831 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "decode-uri", |
832 | 2 | EXSLT_STRINGS_NAMESPACE, |
833 | 2 | exsltStrDecodeUriFunction); |
834 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "padding", |
835 | 2 | EXSLT_STRINGS_NAMESPACE, |
836 | 2 | exsltStrPaddingFunction); |
837 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "align", |
838 | 2 | EXSLT_STRINGS_NAMESPACE, |
839 | 2 | exsltStrAlignFunction); |
840 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "concat", |
841 | 2 | EXSLT_STRINGS_NAMESPACE, |
842 | 2 | exsltStrConcatFunction); |
843 | 2 | xsltRegisterExtModuleFunction ((const xmlChar *) "replace", |
844 | 2 | EXSLT_STRINGS_NAMESPACE, |
845 | 2 | exsltStrReplaceFunction); |
846 | 2 | } |
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 | } |