Coverage Report

Created: 2023-09-24 16:02

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