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