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