/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 | 0 | { |
28 | 0 | xsltTransformContextPtr tctxt; |
29 | 0 | xmlChar *str, *delimiters, *cur; |
30 | 0 | const xmlChar *token, *delimiter; |
31 | 0 | xmlNodePtr node; |
32 | 0 | xmlDocPtr container; |
33 | 0 | xmlXPathObjectPtr ret = NULL; |
34 | 0 | int clen; |
35 | |
|
36 | 0 | if ((nargs < 1) || (nargs > 2)) { |
37 | 0 | xmlXPathSetArityError(ctxt); |
38 | 0 | return; |
39 | 0 | } |
40 | | |
41 | 0 | if (nargs == 2) { |
42 | 0 | delimiters = xmlXPathPopString(ctxt); |
43 | 0 | if (xmlXPathCheckError(ctxt)) |
44 | 0 | return; |
45 | 0 | } else { |
46 | 0 | delimiters = xmlStrdup((const xmlChar *) "\t\r\n "); |
47 | 0 | } |
48 | 0 | if (delimiters == NULL) |
49 | 0 | return; |
50 | | |
51 | 0 | str = xmlXPathPopString(ctxt); |
52 | 0 | if (xmlXPathCheckError(ctxt) || (str == NULL)) { |
53 | 0 | xmlFree(delimiters); |
54 | 0 | return; |
55 | 0 | } |
56 | | |
57 | | /* Return a result tree fragment */ |
58 | 0 | tctxt = xsltXPathGetTransformContext(ctxt); |
59 | 0 | 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 | 0 | container = xsltCreateRVT(tctxt); |
66 | 0 | if (container != NULL) { |
67 | 0 | xsltRegisterLocalRVT(tctxt, container); |
68 | 0 | ret = xmlXPathNewNodeSet(NULL); |
69 | 0 | if (ret != NULL) { |
70 | 0 | for (cur = str, token = str; *cur != 0; cur += clen) { |
71 | 0 | clen = xmlUTF8Strsize(cur, 1); |
72 | 0 | if (*delimiters == 0) { /* empty string case */ |
73 | 0 | xmlChar ctmp; |
74 | 0 | ctmp = *(cur+clen); |
75 | 0 | *(cur+clen) = 0; |
76 | 0 | node = xmlNewDocRawNode(container, NULL, |
77 | 0 | (const xmlChar *) "token", cur); |
78 | 0 | xmlAddChild((xmlNodePtr) container, node); |
79 | 0 | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
80 | 0 | *(cur+clen) = ctmp; /* restore the changed byte */ |
81 | 0 | token = cur + clen; |
82 | 0 | } else for (delimiter = delimiters; *delimiter != 0; |
83 | 0 | delimiter += xmlUTF8Strsize(delimiter, 1)) { |
84 | 0 | if (!xmlUTF8Charcmp(cur, delimiter)) { |
85 | 0 | if (cur == token) { |
86 | | /* discard empty tokens */ |
87 | 0 | token = cur + clen; |
88 | 0 | break; |
89 | 0 | } |
90 | 0 | *cur = 0; /* terminate the token */ |
91 | 0 | node = xmlNewDocRawNode(container, NULL, |
92 | 0 | (const xmlChar *) "token", token); |
93 | 0 | xmlAddChild((xmlNodePtr) container, node); |
94 | 0 | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
95 | 0 | *cur = *delimiter; /* restore the changed byte */ |
96 | 0 | token = cur + clen; |
97 | 0 | break; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | } |
101 | 0 | if (token != cur) { |
102 | 0 | node = xmlNewDocRawNode(container, NULL, |
103 | 0 | (const xmlChar *) "token", token); |
104 | 0 | xmlAddChild((xmlNodePtr) container, node); |
105 | 0 | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
106 | 0 | } |
107 | 0 | } |
108 | 0 | } |
109 | |
|
110 | 0 | fail: |
111 | 0 | if (str != NULL) |
112 | 0 | xmlFree(str); |
113 | 0 | if (delimiters != NULL) |
114 | 0 | xmlFree(delimiters); |
115 | 0 | if (ret != NULL) |
116 | 0 | valuePush(ctxt, ret); |
117 | 0 | else |
118 | 0 | valuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
119 | 0 | } |
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 | 853 | exsltStrSplitFunction(xmlXPathParserContextPtr ctxt, int nargs) { |
131 | 853 | xsltTransformContextPtr tctxt; |
132 | 853 | xmlChar *str, *delimiter, *cur; |
133 | 853 | const xmlChar *token; |
134 | 853 | xmlNodePtr node; |
135 | 853 | xmlDocPtr container; |
136 | 853 | xmlXPathObjectPtr ret = NULL; |
137 | 853 | int delimiterLength; |
138 | | |
139 | 853 | if ((nargs < 1) || (nargs > 2)) { |
140 | 0 | xmlXPathSetArityError(ctxt); |
141 | 0 | return; |
142 | 0 | } |
143 | | |
144 | 853 | if (nargs == 2) { |
145 | 649 | delimiter = xmlXPathPopString(ctxt); |
146 | 649 | if (xmlXPathCheckError(ctxt)) |
147 | 0 | return; |
148 | 649 | } else { |
149 | 204 | delimiter = xmlStrdup((const xmlChar *) " "); |
150 | 204 | } |
151 | 853 | if (delimiter == NULL) |
152 | 0 | return; |
153 | 853 | delimiterLength = xmlStrlen (delimiter); |
154 | | |
155 | 853 | str = xmlXPathPopString(ctxt); |
156 | 853 | if (xmlXPathCheckError(ctxt) || (str == NULL)) { |
157 | 0 | xmlFree(delimiter); |
158 | 0 | return; |
159 | 0 | } |
160 | | |
161 | | /* Return a result tree fragment */ |
162 | 853 | tctxt = xsltXPathGetTransformContext(ctxt); |
163 | 853 | 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 | 853 | container = xsltCreateRVT(tctxt); |
173 | 853 | if (container != NULL) { |
174 | 853 | xsltRegisterLocalRVT(tctxt, container); |
175 | 853 | ret = xmlXPathNewNodeSet(NULL); |
176 | 853 | if (ret != NULL) { |
177 | 2.39M | for (cur = str, token = str; *cur != 0; cur++) { |
178 | 2.39M | if (delimiterLength == 0) { |
179 | 2.37M | if (cur != token) { |
180 | 2.37M | xmlChar tmp = *cur; |
181 | 2.37M | *cur = 0; |
182 | 2.37M | node = xmlNewDocRawNode(container, NULL, |
183 | 2.37M | (const xmlChar *) "token", token); |
184 | 2.37M | xmlAddChild((xmlNodePtr) container, node); |
185 | 2.37M | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
186 | 2.37M | *cur = tmp; |
187 | 2.37M | token++; |
188 | 2.37M | } |
189 | 2.37M | } |
190 | 18.8k | 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 | 2.39M | } |
207 | 853 | if (token != cur) { |
208 | 853 | node = xmlNewDocRawNode(container, NULL, |
209 | 853 | (const xmlChar *) "token", token); |
210 | 853 | xmlAddChild((xmlNodePtr) container, node); |
211 | 853 | xmlXPathNodeSetAddUnique(ret->nodesetval, node); |
212 | 853 | } |
213 | 853 | } |
214 | 853 | } |
215 | | |
216 | 853 | fail: |
217 | 853 | if (str != NULL) |
218 | 853 | xmlFree(str); |
219 | 853 | if (delimiter != NULL) |
220 | 853 | xmlFree(delimiter); |
221 | 853 | if (ret != NULL) |
222 | 853 | valuePush(ctxt, ret); |
223 | 0 | else |
224 | 0 | valuePush(ctxt, xmlXPathNewNodeSet(NULL)); |
225 | 853 | } |
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 | exsltStrEncodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
236 | 2 | int escape_all = 1, str_len = 0; |
237 | 2 | xmlChar *str = NULL, *ret = NULL, *tmp; |
238 | | |
239 | 2 | if ((nargs < 2) || (nargs > 3)) { |
240 | 0 | xmlXPathSetArityError(ctxt); |
241 | 0 | return; |
242 | 0 | } |
243 | | |
244 | 2 | 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 | 2 | escape_all = xmlXPathPopBoolean(ctxt); |
257 | | |
258 | 2 | str = xmlXPathPopString(ctxt); |
259 | 2 | str_len = xmlUTF8Strlen(str); |
260 | | |
261 | 2 | if (str_len <= 0) { |
262 | 0 | if (str_len < 0) |
263 | 0 | xsltGenericError(xsltGenericErrorContext, |
264 | 0 | "exsltStrEncodeUriFunction: invalid UTF-8\n"); |
265 | 0 | xmlXPathReturnEmptyString(ctxt); |
266 | 0 | xmlFree(str); |
267 | 0 | return; |
268 | 0 | } |
269 | | |
270 | 2 | ret = xmlURIEscapeStr(str,(const xmlChar *)(escape_all?"-_.!~*'()":"-_.!~*'();/?:@&=+$,[]")); |
271 | 2 | xmlXPathReturnString(ctxt, ret); |
272 | | |
273 | 2 | if (str != NULL) |
274 | 2 | xmlFree(str); |
275 | 2 | } |
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 | 1 | exsltStrDecodeUriFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
286 | 1 | int str_len = 0; |
287 | 1 | xmlChar *str = NULL, *ret = NULL, *tmp; |
288 | | |
289 | 1 | if ((nargs < 1) || (nargs > 2)) { |
290 | 0 | xmlXPathSetArityError(ctxt); |
291 | 0 | return; |
292 | 0 | } |
293 | | |
294 | 1 | 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 | 1 | str = xmlXPathPopString(ctxt); |
307 | 1 | str_len = xmlUTF8Strlen(str); |
308 | | |
309 | 1 | if (str_len <= 0) { |
310 | 1 | if (str_len < 0) |
311 | 1 | xsltGenericError(xsltGenericErrorContext, |
312 | 1 | "exsltStrDecodeUriFunction: invalid UTF-8\n"); |
313 | 1 | xmlXPathReturnEmptyString(ctxt); |
314 | 1 | xmlFree(str); |
315 | 1 | return; |
316 | 1 | } |
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 | 0 | exsltStrAlignFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
423 | 0 | xmlChar *str, *padding, *alignment, *ret; |
424 | 0 | int str_l, padding_l; |
425 | |
|
426 | 0 | if ((nargs < 2) || (nargs > 3)) { |
427 | 0 | xmlXPathSetArityError(ctxt); |
428 | 0 | return; |
429 | 0 | } |
430 | | |
431 | 0 | if (nargs == 3) |
432 | 0 | alignment = xmlXPathPopString(ctxt); |
433 | 0 | else |
434 | 0 | alignment = NULL; |
435 | |
|
436 | 0 | padding = xmlXPathPopString(ctxt); |
437 | 0 | str = xmlXPathPopString(ctxt); |
438 | |
|
439 | 0 | str_l = xmlUTF8Strlen (str); |
440 | 0 | padding_l = xmlUTF8Strlen (padding); |
441 | |
|
442 | 0 | if (str_l < 0 || padding_l < 0) { |
443 | 0 | xsltGenericError(xsltGenericErrorContext, |
444 | 0 | "exsltStrAlignFunction: invalid UTF-8\n"); |
445 | 0 | xmlXPathReturnEmptyString(ctxt); |
446 | 0 | xmlFree(str); |
447 | 0 | xmlFree(padding); |
448 | 0 | xmlFree(alignment); |
449 | 0 | return; |
450 | 0 | } |
451 | | |
452 | 0 | if (str_l == padding_l) { |
453 | 0 | xmlXPathReturnString (ctxt, str); |
454 | 0 | xmlFree(padding); |
455 | 0 | xmlFree(alignment); |
456 | 0 | return; |
457 | 0 | } |
458 | | |
459 | 0 | if (str_l > padding_l) { |
460 | 0 | ret = xmlUTF8Strndup (str, padding_l); |
461 | 0 | } else { |
462 | 0 | if (xmlStrEqual(alignment, (const xmlChar *) "right")) { |
463 | 0 | ret = xmlUTF8Strndup (padding, padding_l - str_l); |
464 | 0 | ret = xmlStrcat (ret, str); |
465 | 0 | } 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 | 0 | } else { |
475 | 0 | int str_s; |
476 | |
|
477 | 0 | str_s = xmlUTF8Strsize(padding, str_l); |
478 | 0 | ret = xmlStrdup (str); |
479 | 0 | ret = xmlStrcat (ret, padding + str_s); |
480 | 0 | } |
481 | 0 | } |
482 | |
|
483 | 0 | xmlXPathReturnString (ctxt, ret); |
484 | |
|
485 | 0 | xmlFree(str); |
486 | 0 | xmlFree(padding); |
487 | 0 | xmlFree(alignment); |
488 | 0 | } |
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 | 19 | exsltStrConcatFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
501 | 19 | xmlXPathObjectPtr obj; |
502 | 19 | xmlBufferPtr buf; |
503 | 19 | int i; |
504 | | |
505 | 19 | if (nargs != 1) { |
506 | 0 | xmlXPathSetArityError(ctxt); |
507 | 0 | return; |
508 | 0 | } |
509 | | |
510 | 19 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
511 | 0 | xmlXPathSetTypeError(ctxt); |
512 | 0 | return; |
513 | 0 | } |
514 | | |
515 | 19 | obj = valuePop (ctxt); |
516 | | |
517 | 19 | if (xmlXPathNodeSetIsEmpty(obj->nodesetval)) { |
518 | 0 | xmlXPathFreeObject(obj); |
519 | 0 | xmlXPathReturnEmptyString(ctxt); |
520 | 0 | return; |
521 | 0 | } |
522 | | |
523 | 19 | buf = xmlBufferCreate(); |
524 | 19 | if (buf == NULL) { |
525 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
526 | 0 | xmlXPathFreeObject(obj); |
527 | 0 | return; |
528 | 0 | } |
529 | 19 | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
530 | | |
531 | 38 | for (i = 0; i < obj->nodesetval->nodeNr; i++) { |
532 | 19 | xmlChar *tmp; |
533 | 19 | tmp = xmlXPathCastNodeToString(obj->nodesetval->nodeTab[i]); |
534 | | |
535 | 19 | xmlBufferCat(buf, tmp); |
536 | | |
537 | 19 | xmlFree(tmp); |
538 | 19 | } |
539 | | |
540 | 19 | xmlXPathFreeObject (obj); |
541 | | |
542 | 19 | xmlXPathReturnString(ctxt, xmlBufferDetach(buf)); |
543 | 19 | xmlBufferFree(buf); |
544 | 19 | } |
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 | 0 | { |
558 | 0 | xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt); |
559 | 0 | xmlDocPtr container; |
560 | 0 | xmlNodePtr text_node; |
561 | 0 | xmlXPathObjectPtr ret; |
562 | |
|
563 | 0 | container = xsltCreateRVT(tctxt); |
564 | 0 | if (container == NULL) { |
565 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
566 | 0 | return(-1); |
567 | 0 | } |
568 | 0 | xsltRegisterLocalRVT(tctxt, container); |
569 | |
|
570 | 0 | text_node = xmlNewTextLen(str, len); |
571 | 0 | if (text_node == NULL) { |
572 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
573 | 0 | return(-1); |
574 | 0 | } |
575 | 0 | xmlAddChild((xmlNodePtr) container, text_node); |
576 | |
|
577 | 0 | ret = xmlXPathNewNodeSet(text_node); |
578 | 0 | if (ret == NULL) { |
579 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
580 | 0 | return(-1); |
581 | 0 | } |
582 | | |
583 | 0 | valuePush(ctxt, ret); |
584 | |
|
585 | 0 | return(0); |
586 | 0 | } |
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 | 0 | exsltStrReplaceFunction (xmlXPathParserContextPtr ctxt, int nargs) { |
598 | 0 | int i, i_empty, n, slen0, rlen0, *slen, *rlen; |
599 | 0 | void *mem = NULL; |
600 | 0 | const xmlChar *src, *start; |
601 | 0 | xmlChar *string, *search_str = NULL, *replace_str = NULL; |
602 | 0 | xmlChar **search, **replace; |
603 | 0 | xmlNodeSetPtr search_set = NULL, replace_set = NULL; |
604 | 0 | xmlBufferPtr buf; |
605 | |
|
606 | 0 | if (nargs != 3) { |
607 | 0 | xmlXPathSetArityError(ctxt); |
608 | 0 | return; |
609 | 0 | } |
610 | | |
611 | | /* get replace argument */ |
612 | | |
613 | 0 | if (!xmlXPathStackIsNodeSet(ctxt)) |
614 | 0 | replace_str = xmlXPathPopString(ctxt); |
615 | 0 | else |
616 | 0 | replace_set = xmlXPathPopNodeSet(ctxt); |
617 | |
|
618 | 0 | if (xmlXPathCheckError(ctxt)) |
619 | 0 | goto fail_replace; |
620 | | |
621 | | /* get search argument */ |
622 | | |
623 | 0 | if (!xmlXPathStackIsNodeSet(ctxt)) { |
624 | 0 | search_str = xmlXPathPopString(ctxt); |
625 | 0 | n = 1; |
626 | 0 | } |
627 | 0 | else { |
628 | 0 | search_set = xmlXPathPopNodeSet(ctxt); |
629 | 0 | n = search_set != NULL ? search_set->nodeNr : 0; |
630 | 0 | } |
631 | |
|
632 | 0 | if (xmlXPathCheckError(ctxt)) |
633 | 0 | goto fail_search; |
634 | | |
635 | | /* get string argument */ |
636 | | |
637 | 0 | string = xmlXPathPopString(ctxt); |
638 | 0 | if (xmlXPathCheckError(ctxt)) |
639 | 0 | goto fail_string; |
640 | | |
641 | | /* check for empty search node list */ |
642 | | |
643 | 0 | 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 | 0 | if (n == 1) { |
651 | 0 | search = &search_str; |
652 | 0 | replace = &replace_str; |
653 | 0 | slen = &slen0; |
654 | 0 | rlen = &rlen0; |
655 | 0 | } |
656 | 0 | else { |
657 | 0 | mem = xmlMalloc(2 * n * (sizeof(const xmlChar *) + sizeof(int))); |
658 | 0 | if (mem == NULL) { |
659 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
660 | 0 | goto fail_malloc; |
661 | 0 | } |
662 | 0 | search = (xmlChar **) mem; |
663 | 0 | replace = search + n; |
664 | 0 | slen = (int *) (replace + n); |
665 | 0 | rlen = slen + n; |
666 | 0 | } |
667 | | |
668 | | /* process arguments */ |
669 | | |
670 | 0 | i_empty = -1; |
671 | |
|
672 | 0 | for (i=0; i<n; ++i) { |
673 | 0 | if (search_set != NULL) { |
674 | 0 | search[i] = xmlXPathCastNodeToString(search_set->nodeTab[i]); |
675 | 0 | if (search[i] == NULL) { |
676 | 0 | n = i; |
677 | 0 | goto fail_process_args; |
678 | 0 | } |
679 | 0 | } |
680 | | |
681 | 0 | slen[i] = xmlStrlen(search[i]); |
682 | 0 | if (i_empty < 0 && slen[i] == 0) |
683 | 0 | i_empty = i; |
684 | |
|
685 | 0 | if (replace_set != NULL) { |
686 | 0 | 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 | 0 | else |
694 | 0 | replace[i] = NULL; |
695 | 0 | } |
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 | 0 | if (replace[i] == NULL) |
704 | 0 | rlen[i] = 0; |
705 | 0 | else |
706 | 0 | rlen[i] = xmlStrlen(replace[i]); |
707 | 0 | } |
708 | | |
709 | 0 | if (i_empty >= 0 && rlen[i_empty] == 0) |
710 | 0 | i_empty = -1; |
711 | | |
712 | | /* replace operation */ |
713 | |
|
714 | 0 | buf = xmlBufferCreate(); |
715 | 0 | if (buf == NULL) { |
716 | 0 | xmlXPathSetError(ctxt, XPATH_MEMORY_ERROR); |
717 | 0 | goto fail_buffer; |
718 | 0 | } |
719 | 0 | xmlBufferSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
720 | 0 | src = string; |
721 | 0 | start = string; |
722 | |
|
723 | 0 | while (*src != 0) { |
724 | 0 | int max_len = 0, i_match = 0; |
725 | |
|
726 | 0 | for (i=0; i<n; ++i) { |
727 | 0 | if (*src == search[i][0] && |
728 | 0 | slen[i] > max_len && |
729 | 0 | xmlStrncmp(src, search[i], slen[i]) == 0) |
730 | 0 | { |
731 | 0 | i_match = i; |
732 | 0 | max_len = slen[i]; |
733 | 0 | } |
734 | 0 | } |
735 | |
|
736 | 0 | if (max_len == 0) { |
737 | 0 | 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 | 0 | src += xmlUTF8Strsize(src, 1); |
748 | 0 | } |
749 | 0 | else { |
750 | 0 | if ((start < src && |
751 | 0 | xmlBufferAdd(buf, start, src - start)) || |
752 | 0 | (rlen[i_match] && |
753 | 0 | 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 | 0 | src += slen[i_match]; |
760 | 0 | start = src; |
761 | 0 | } |
762 | 0 | } |
763 | | |
764 | 0 | 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 | 0 | exsltStrReturnString(ctxt, xmlBufferContent(buf), xmlBufferLength(buf)); |
772 | | |
773 | | /* clean up */ |
774 | |
|
775 | 0 | fail_buffer_add: |
776 | 0 | xmlBufferFree(buf); |
777 | |
|
778 | 0 | fail_buffer: |
779 | 0 | fail_process_args: |
780 | 0 | if (search_set != NULL) { |
781 | 0 | for (i=0; i<n; ++i) |
782 | 0 | xmlFree(search[i]); |
783 | 0 | } |
784 | 0 | if (replace_set != NULL) { |
785 | 0 | for (i=0; i<n; ++i) { |
786 | 0 | if (replace[i] != NULL) |
787 | 0 | xmlFree(replace[i]); |
788 | 0 | } |
789 | 0 | } |
790 | |
|
791 | 0 | if (mem != NULL) |
792 | 0 | xmlFree(mem); |
793 | |
|
794 | 0 | fail_malloc: |
795 | 0 | done_empty_search: |
796 | 0 | xmlFree(string); |
797 | |
|
798 | 0 | fail_string: |
799 | 0 | if (search_set != NULL) |
800 | 0 | xmlXPathFreeNodeSet(search_set); |
801 | 0 | else |
802 | 0 | xmlFree(search_str); |
803 | |
|
804 | 0 | fail_search: |
805 | 0 | if (replace_set != NULL) |
806 | 0 | xmlXPathFreeNodeSet(replace_set); |
807 | 0 | else |
808 | 0 | xmlFree(replace_str); |
809 | |
|
810 | 0 | fail_replace: |
811 | 0 | return; |
812 | 0 | } |
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 | } |