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