Coverage Report

Created: 2026-10-10 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/xmlstring.c
Line
Count
Source
1
/*
2
 * string.c : an XML string utilities module
3
 *
4
 * This module provides various utility functions for manipulating
5
 * the xmlChar* type. All functions named xmlStr* have been moved here
6
 * from the parser.c file (their original home).
7
 *
8
 * See Copyright for the status of this software.
9
 *
10
 * UTF8 string routines from:
11
 * William Brack <wbrack@mmm.com.hk>
12
 *
13
 * daniel@veillard.com
14
 */
15
16
#define IN_LIBXML
17
#include "libxml.h"
18
19
#include <stdlib.h>
20
#include <string.h>
21
#include <limits.h>
22
#include <libxml/xmlmemory.h>
23
#include <libxml/parserInternals.h>
24
#include <libxml/xmlstring.h>
25
26
#include "private/parser.h"
27
#include "private/string.h"
28
29
#ifndef va_copy
30
  #ifdef __va_copy
31
    #define va_copy(dest, src) __va_copy(dest, src)
32
  #else
33
    #define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
34
  #endif
35
#endif
36
37
/************************************************************************
38
 *                                                                      *
39
 *                Commodity functions to handle xmlChars                *
40
 *                                                                      *
41
 ************************************************************************/
42
43
/**
44
 * xmlStrndup:
45
 * @cur:  the input xmlChar *
46
 * @len:  the len of @cur
47
 *
48
 * a strndup for array of xmlChar's
49
 *
50
 * Returns a new xmlChar * or NULL
51
 */
52
xmlChar *
53
43.2M
xmlStrndup(const xmlChar *cur, int len) {
54
43.2M
    xmlChar *ret;
55
56
43.2M
    if ((cur == NULL) || (len < 0)) return(NULL);
57
43.2M
    ret = xmlMalloc((size_t) len + 1);
58
43.2M
    if (ret == NULL) {
59
34.1k
        return(NULL);
60
34.1k
    }
61
43.1M
    memcpy(ret, cur, len);
62
43.1M
    ret[len] = 0;
63
43.1M
    return(ret);
64
43.2M
}
65
66
/**
67
 * xmlStrdup:
68
 * @cur:  the input xmlChar *
69
 *
70
 * a strdup for array of xmlChar's. Since they are supposed to be
71
 * encoded in UTF-8 or an encoding with 8bit based chars, we assume
72
 * a termination mark of '0'.
73
 *
74
 * Returns a new xmlChar * or NULL
75
 */
76
xmlChar *
77
33.7M
xmlStrdup(const xmlChar *cur) {
78
33.7M
    const xmlChar *p = cur;
79
80
33.7M
    if (cur == NULL) return(NULL);
81
2.24G
    while (*p != 0) p++; /* non input consuming */
82
33.7M
    return(xmlStrndup(cur, p - cur));
83
33.7M
}
84
85
/**
86
 * xmlCharStrndup:
87
 * @cur:  the input char *
88
 * @len:  the len of @cur
89
 *
90
 * a strndup for char's to xmlChar's
91
 *
92
 * Returns a new xmlChar * or NULL
93
 */
94
95
xmlChar *
96
804k
xmlCharStrndup(const char *cur, int len) {
97
804k
    int i;
98
804k
    xmlChar *ret;
99
100
804k
    if ((cur == NULL) || (len < 0)) return(NULL);
101
804k
    ret = xmlMalloc((size_t) len + 1);
102
804k
    if (ret == NULL) {
103
96
        return(NULL);
104
96
    }
105
185M
    for (i = 0;i < len;i++) {
106
        /* Explicit sign change */
107
184M
        ret[i] = (xmlChar) cur[i];
108
184M
        if (ret[i] == 0) return(ret);
109
184M
    }
110
804k
    ret[len] = 0;
111
804k
    return(ret);
112
804k
}
113
114
/**
115
 * xmlCharStrdup:
116
 * @cur:  the input char *
117
 *
118
 * a strdup for char's to xmlChar's
119
 *
120
 * Returns a new xmlChar * or NULL
121
 */
122
123
xmlChar *
124
804k
xmlCharStrdup(const char *cur) {
125
804k
    const char *p = cur;
126
127
804k
    if (cur == NULL) return(NULL);
128
185M
    while (*p != '\0') p++; /* non input consuming */
129
804k
    return(xmlCharStrndup(cur, p - cur));
130
804k
}
131
132
/**
133
 * xmlStrcmp:
134
 * @str1:  the first xmlChar *
135
 * @str2:  the second xmlChar *
136
 *
137
 * a strcmp for xmlChar's
138
 *
139
 * Returns the integer result of the comparison
140
 */
141
142
int
143
473k
xmlStrcmp(const xmlChar *str1, const xmlChar *str2) {
144
473k
    if (str1 == str2) return(0);
145
473k
    if (str1 == NULL) return(-1);
146
473k
    if (str2 == NULL) return(1);
147
473k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
148
473k
    return(strcmp((const char *)str1, (const char *)str2));
149
#else
150
    do {
151
        int tmp = *str1++ - *str2;
152
        if (tmp != 0) return(tmp);
153
    } while (*str2++ != 0);
154
    return 0;
155
#endif
156
473k
}
157
158
/**
159
 * xmlStrEqual:
160
 * @str1:  the first xmlChar *
161
 * @str2:  the second xmlChar *
162
 *
163
 * Check if both strings are equal of have same content.
164
 * Should be a bit more readable and faster than xmlStrcmp()
165
 *
166
 * Returns 1 if they are equal, 0 if they are different
167
 */
168
169
int
170
105M
xmlStrEqual(const xmlChar *str1, const xmlChar *str2) {
171
105M
    if (str1 == str2) return(1);
172
102M
    if (str1 == NULL) return(0);
173
78.5M
    if (str2 == NULL) return(0);
174
77.3M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
175
77.3M
    return(strcmp((const char *)str1, (const char *)str2) == 0);
176
#else
177
    do {
178
        if (*str1++ != *str2) return(0);
179
    } while (*str2++);
180
    return(1);
181
#endif
182
78.5M
}
183
184
/**
185
 * xmlStrQEqual:
186
 * @pref:  the prefix of the QName
187
 * @name:  the localname of the QName
188
 * @str:  the second xmlChar *
189
 *
190
 * Check if a QName is Equal to a given string
191
 *
192
 * Returns 1 if they are equal, 0 if they are different
193
 */
194
195
int
196
171k
xmlStrQEqual(const xmlChar *pref, const xmlChar *name, const xmlChar *str) {
197
171k
    if (pref == NULL) return(xmlStrEqual(name, str));
198
130k
    if (name == NULL) return(0);
199
130k
    if (str == NULL) return(0);
200
201
1.03M
    do {
202
1.03M
        if (*pref++ != *str) return(0);
203
1.03M
    } while ((*str++) && (*pref));
204
130k
    if (*str++ != ':') return(0);
205
1.25M
    do {
206
1.25M
        if (*name++ != *str) return(0);
207
1.25M
    } while (*str++);
208
130k
    return(1);
209
130k
}
210
211
/**
212
 * xmlStrncmp:
213
 * @str1:  the first xmlChar *
214
 * @str2:  the second xmlChar *
215
 * @len:  the max comparison length
216
 *
217
 * a strncmp for xmlChar's
218
 *
219
 * Returns the integer result of the comparison
220
 */
221
222
int
223
36.6M
xmlStrncmp(const xmlChar *str1, const xmlChar *str2, int len) {
224
36.6M
    if (len <= 0) return(0);
225
36.6M
    if (str1 == str2) return(0);
226
36.6M
    if (str1 == NULL) return(-1);
227
36.6M
    if (str2 == NULL) return(1);
228
36.6M
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
229
36.6M
    return(strncmp((const char *)str1, (const char *)str2, len));
230
#else
231
    do {
232
        int tmp = *str1++ - *str2;
233
        if (tmp != 0 || --len == 0) return(tmp);
234
    } while (*str2++ != 0);
235
    return 0;
236
#endif
237
36.6M
}
238
239
static const xmlChar casemap[256] = {
240
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
241
    0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
242
    0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
243
    0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
244
    0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,
245
    0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
246
    0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
247
    0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,
248
    0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
249
    0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
250
    0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
251
    0x78,0x79,0x7A,0x7B,0x5C,0x5D,0x5E,0x5F,
252
    0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,
253
    0x68,0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,
254
    0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,
255
    0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
256
    0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
257
    0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,
258
    0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
259
    0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F,
260
    0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,
261
    0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
262
    0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,
263
    0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF,
264
    0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,
265
    0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,
266
    0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,
267
    0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF,
268
    0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,
269
    0xE8,0xE9,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,
270
    0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
271
    0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF
272
};
273
274
/**
275
 * xmlStrcasecmp:
276
 * @str1:  the first xmlChar *
277
 * @str2:  the second xmlChar *
278
 *
279
 * a strcasecmp for xmlChar's
280
 *
281
 * Returns the integer result of the comparison
282
 */
283
284
int
285
1.32M
xmlStrcasecmp(const xmlChar *str1, const xmlChar *str2) {
286
1.32M
    register int tmp;
287
288
1.32M
    if (str1 == str2) return(0);
289
1.32M
    if (str1 == NULL) return(-1);
290
1.31M
    if (str2 == NULL) return(1);
291
3.90M
    do {
292
3.90M
        tmp = casemap[*str1++] - casemap[*str2];
293
3.90M
        if (tmp != 0) return(tmp);
294
3.90M
    } while (*str2++ != 0);
295
123k
    return 0;
296
1.31M
}
297
298
/**
299
 * xmlStrncasecmp:
300
 * @str1:  the first xmlChar *
301
 * @str2:  the second xmlChar *
302
 * @len:  the max comparison length
303
 *
304
 * a strncasecmp for xmlChar's
305
 *
306
 * Returns the integer result of the comparison
307
 */
308
309
int
310
4.17k
xmlStrncasecmp(const xmlChar *str1, const xmlChar *str2, int len) {
311
4.17k
    register int tmp;
312
313
4.17k
    if (len <= 0) return(0);
314
4.17k
    if (str1 == str2) return(0);
315
4.17k
    if (str1 == NULL) return(-1);
316
4.17k
    if (str2 == NULL) return(1);
317
5.05k
    do {
318
5.05k
        tmp = casemap[*str1++] - casemap[*str2];
319
5.05k
        if (tmp != 0 || --len == 0) return(tmp);
320
5.05k
    } while (*str2++ != 0);
321
0
    return 0;
322
4.17k
}
323
324
/**
325
 * xmlStrchr:
326
 * @str:  the xmlChar * array
327
 * @val:  the xmlChar to search
328
 *
329
 * a strchr for xmlChar's
330
 *
331
 * Returns the xmlChar * for the first occurrence or NULL.
332
 */
333
334
const xmlChar *
335
129M
xmlStrchr(const xmlChar *str, xmlChar val) {
336
129M
    if (str == NULL) return(NULL);
337
3.04G
    while (*str != 0) { /* non input consuming */
338
2.92G
        if (*str == val) return((xmlChar *) str);
339
2.92G
        str++;
340
2.92G
    }
341
122M
    return(NULL);
342
129M
}
343
344
/**
345
 * xmlStrstr:
346
 * @str:  the xmlChar * array (haystack)
347
 * @val:  the xmlChar to search (needle)
348
 *
349
 * a strstr for xmlChar's
350
 *
351
 * Returns the xmlChar * for the first occurrence or NULL.
352
 */
353
354
const xmlChar *
355
2.89M
xmlStrstr(const xmlChar *str, const xmlChar *val) {
356
2.89M
    int n;
357
358
2.89M
    if (str == NULL) return(NULL);
359
2.89M
    if (val == NULL) return(NULL);
360
2.89M
    n = xmlStrlen(val);
361
362
2.89M
    if (n == 0) return(str);
363
91.2M
    while (*str != 0) { /* non input consuming */
364
88.6M
        if (*str == *val) {
365
932k
            if (!xmlStrncmp(str, val, n)) return((const xmlChar *) str);
366
932k
        }
367
88.3M
        str++;
368
88.3M
    }
369
2.59M
    return(NULL);
370
2.88M
}
371
372
/**
373
 * xmlStrcasestr:
374
 * @str:  the xmlChar * array (haystack)
375
 * @val:  the xmlChar to search (needle)
376
 *
377
 * a case-ignoring strstr for xmlChar's
378
 *
379
 * Returns the xmlChar * for the first occurrence or NULL.
380
 */
381
382
const xmlChar *
383
0
xmlStrcasestr(const xmlChar *str, const xmlChar *val) {
384
0
    int n;
385
386
0
    if (str == NULL) return(NULL);
387
0
    if (val == NULL) return(NULL);
388
0
    n = xmlStrlen(val);
389
390
0
    if (n == 0) return(str);
391
0
    while (*str != 0) { /* non input consuming */
392
0
        if (casemap[*str] == casemap[*val])
393
0
            if (!xmlStrncasecmp(str, val, n)) return(str);
394
0
        str++;
395
0
    }
396
0
    return(NULL);
397
0
}
398
399
/**
400
 * xmlStrsub:
401
 * @str:  the xmlChar * array (haystack)
402
 * @start:  the index of the first char (zero based)
403
 * @len:  the length of the substring
404
 *
405
 * Extract a substring of a given string
406
 *
407
 * Returns the xmlChar * for the first occurrence or NULL.
408
 */
409
410
xmlChar *
411
0
xmlStrsub(const xmlChar *str, int start, int len) {
412
0
    int i;
413
414
0
    if (str == NULL) return(NULL);
415
0
    if (start < 0) return(NULL);
416
0
    if (len < 0) return(NULL);
417
418
0
    for (i = 0;i < start;i++) {
419
0
        if (*str == 0) return(NULL);
420
0
        str++;
421
0
    }
422
0
    if (*str == 0) return(NULL);
423
0
    return(xmlStrndup(str, len));
424
0
}
425
426
/**
427
 * xmlStrlen:
428
 * @str:  the xmlChar * array
429
 *
430
 * length of a xmlChar's string
431
 *
432
 * Returns the number of xmlChar contained in the ARRAY.
433
 */
434
435
int
436
12.6M
xmlStrlen(const xmlChar *str) {
437
12.6M
    size_t len = str ? strlen((const char *)str) : 0;
438
12.6M
    return(len > INT_MAX ? 0 : len);
439
12.6M
}
440
441
/**
442
 * xmlStrncat:
443
 * @cur:  the original xmlChar * array
444
 * @add:  the xmlChar * array added
445
 * @len:  the length of @add
446
 *
447
 * a strncat for array of xmlChar's, it will extend @cur with the len
448
 * first bytes of @add. Note that if @len < 0 then this is an API error
449
 * and NULL will be returned.
450
 *
451
 * Returns a new xmlChar *, the original @cur is reallocated and should
452
 * not be freed.
453
 */
454
455
xmlChar *
456
238k
xmlStrncat(xmlChar *cur, const xmlChar *add, int len) {
457
238k
    int size;
458
238k
    xmlChar *ret;
459
460
238k
    if ((add == NULL) || (len == 0))
461
26.4k
        return(cur);
462
212k
    if (len < 0)
463
0
  return(NULL);
464
212k
    if (cur == NULL)
465
46.3k
        return(xmlStrndup(add, len));
466
467
165k
    size = xmlStrlen(cur);
468
165k
    if ((size < 0) || (size > INT_MAX - len))
469
0
        return(NULL);
470
165k
    ret = (xmlChar *) xmlRealloc(cur, (size_t) size + len + 1);
471
165k
    if (ret == NULL) {
472
159
        xmlFree(cur);
473
159
        return(NULL);
474
159
    }
475
165k
    memcpy(&ret[size], add, len);
476
165k
    ret[size + len] = 0;
477
165k
    return(ret);
478
165k
}
479
480
/**
481
 * xmlStrncatNew:
482
 * @str1:  first xmlChar string
483
 * @str2:  second xmlChar string
484
 * @len:  the len of @str2 or < 0
485
 *
486
 * same as xmlStrncat, but creates a new string.  The original
487
 * two strings are not freed. If @len is < 0 then the length
488
 * will be calculated automatically.
489
 *
490
 * Returns a new xmlChar * or NULL
491
 */
492
xmlChar *
493
196k
xmlStrncatNew(const xmlChar *str1, const xmlChar *str2, int len) {
494
196k
    int size;
495
196k
    xmlChar *ret;
496
497
196k
    if (len < 0) {
498
0
        len = xmlStrlen(str2);
499
0
        if (len < 0)
500
0
            return(NULL);
501
0
    }
502
196k
    if (str1 == NULL)
503
0
        return(xmlStrndup(str2, len));
504
196k
    if ((str2 == NULL) || (len == 0))
505
0
        return(xmlStrdup(str1));
506
507
196k
    size = xmlStrlen(str1);
508
196k
    if ((size < 0) || (size > INT_MAX - len))
509
0
        return(NULL);
510
196k
    ret = (xmlChar *) xmlMalloc((size_t) size + len + 1);
511
196k
    if (ret == NULL)
512
680
        return(NULL);
513
196k
    memcpy(ret, str1, size);
514
196k
    memcpy(&ret[size], str2, len);
515
196k
    ret[size + len] = 0;
516
196k
    return(ret);
517
196k
}
518
519
/**
520
 * xmlStrcat:
521
 * @cur:  the original xmlChar * array
522
 * @add:  the xmlChar * array added
523
 *
524
 * a strcat for array of xmlChar's. Since they are supposed to be
525
 * encoded in UTF-8 or an encoding with 8bit based chars, we assume
526
 * a termination mark of '0'.
527
 *
528
 * Returns a new xmlChar * containing the concatenated string. The original
529
 * @cur is reallocated and should not be freed.
530
 */
531
xmlChar *
532
120k
xmlStrcat(xmlChar *cur, const xmlChar *add) {
533
120k
    const xmlChar *p = add;
534
535
120k
    if (add == NULL) return(cur);
536
117k
    if (cur == NULL)
537
11.3k
        return(xmlStrdup(add));
538
539
186M
    while (*p != 0) p++; /* non input consuming */
540
106k
    return(xmlStrncat(cur, add, p - add));
541
117k
}
542
543
/**
544
 * xmlStrPrintf:
545
 * @buf:   the result buffer.
546
 * @len:   the result buffer length.
547
 * @msg:   the message with printf formatting.
548
 * @...:   extra parameters for the message.
549
 *
550
 * Formats @msg and places result into @buf.
551
 *
552
 * Returns the number of characters written to @buf or -1 if an error occurs.
553
 */
554
int
555
0
xmlStrPrintf(xmlChar *buf, int len, const char *msg, ...) {
556
0
    va_list args;
557
0
    int ret;
558
559
0
    if((buf == NULL) || (msg == NULL)) {
560
0
        return(-1);
561
0
    }
562
563
0
    va_start(args, msg);
564
0
    ret = vsnprintf((char *) buf, len, (const char *) msg, args);
565
0
    va_end(args);
566
0
    buf[len - 1] = 0; /* be safe ! */
567
568
0
    return(ret);
569
0
}
570
571
/**
572
 * xmlStrVPrintf:
573
 * @buf:   the result buffer.
574
 * @len:   the result buffer length.
575
 * @msg:   the message with printf formatting.
576
 * @ap:    extra parameters for the message.
577
 *
578
 * Formats @msg and places result into @buf.
579
 *
580
 * Returns the number of characters written to @buf or -1 if an error occurs.
581
 */
582
int
583
0
xmlStrVPrintf(xmlChar *buf, int len, const char *msg, va_list ap) {
584
0
    int ret;
585
586
0
    if((buf == NULL) || (msg == NULL)) {
587
0
        return(-1);
588
0
    }
589
590
0
    ret = vsnprintf((char *) buf, len, (const char *) msg, ap);
591
0
    buf[len - 1] = 0; /* be safe ! */
592
593
0
    return(ret);
594
0
}
595
596
/**
597
 * xmlStrVASPrintf:
598
 * @out:  pointer to the resulting string
599
 * @maxSize:  maximum size of the output buffer
600
 * @msg:  printf format string
601
 * @ap:  arguments for format string
602
 *
603
 * Creates a newly allocated string according to format.
604
 *
605
 * Returns 0 on success, 1 if the result was truncated or on other
606
 * errors, -1 if a memory allocation failed.
607
 */
608
int
609
7.96M
xmlStrVASPrintf(xmlChar **out, int maxSize, const char *msg, va_list ap) {
610
7.96M
    char empty[1];
611
7.96M
    va_list copy;
612
7.96M
    xmlChar *buf;
613
7.96M
    int res, size;
614
7.96M
    int truncated = 0;
615
616
7.96M
    if (out == NULL)
617
0
        return(1);
618
7.96M
    *out = NULL;
619
7.96M
    if (msg == NULL)
620
0
        return(1);
621
7.96M
    if (maxSize < 32)
622
0
        maxSize = 32;
623
624
7.96M
    va_copy(copy, ap);
625
7.96M
    res = vsnprintf(empty, 1, msg, copy);
626
7.96M
    va_end(copy);
627
628
7.96M
    if (res > 0) {
629
        /* snprintf seems to work according to C99. */
630
631
7.96M
        if (res < maxSize) {
632
7.96M
            size = res + 1;
633
7.96M
        } else {
634
1.09k
            size = maxSize;
635
1.09k
            truncated = 1;
636
1.09k
        }
637
7.96M
        buf = xmlMalloc(size);
638
7.96M
        if (buf == NULL)
639
5.85k
            return(-1);
640
7.96M
        if (vsnprintf((char *) buf, size, msg, ap) < 0) {
641
0
            xmlFree(buf);
642
0
            return(1);
643
0
        }
644
7.96M
    } else {
645
        /*
646
         * Unfortunately, older snprintf implementations don't follow the
647
         * C99 spec. If the output exceeds the size of the buffer, they can
648
         * return -1, 0 or the number of characters written instead of the
649
         * needed size. Older MSCVRT also won't write a terminating null
650
         * byte if the buffer is too small.
651
         *
652
         * If the value returned is non-negative and strictly less than
653
         * the buffer size (without terminating null), the result should
654
         * have been written completely, so we double the buffer size
655
         * until this condition is true. This assumes that snprintf will
656
         * eventually return a non-negative value. Otherwise, we will
657
         * allocate more and more memory until we run out.
658
         *
659
         * Note that this code path is also executed on conforming
660
         * platforms if the output is the empty string.
661
         */
662
663
0
        buf = NULL;
664
0
        size = 32;
665
0
        while (1) {
666
0
            buf = xmlMalloc(size);
667
0
            if (buf == NULL)
668
0
                return(-1);
669
670
0
            va_copy(copy, ap);
671
0
            res = vsnprintf((char *) buf, size, msg, copy);
672
0
            va_end(copy);
673
0
            if ((res >= 0) && (res < size - 1))
674
0
                break;
675
676
0
            if (size >= maxSize) {
677
0
                truncated = 1;
678
0
                break;
679
0
            }
680
681
0
            xmlFree(buf);
682
683
0
            if (size > maxSize / 2)
684
0
                size = maxSize;
685
0
            else
686
0
                size *= 2;
687
0
        }
688
0
    }
689
690
    /*
691
     * If the output was truncated, make sure that the buffer doesn't
692
     * end with a truncated UTF-8 sequence.
693
     */
694
7.96M
    if (truncated != 0) {
695
1.09k
        int i = size - 1;
696
697
2.09k
        while (i > 0) {
698
            /* Break after ASCII */
699
2.09k
            if (buf[i-1] < 0x80)
700
354
                break;
701
1.74k
            i -= 1;
702
            /* Break before non-ASCII */
703
1.74k
            if (buf[i] >= 0xc0)
704
740
                break;
705
1.74k
        }
706
707
1.09k
        buf[i] = 0;
708
1.09k
    }
709
710
7.96M
    *out = (xmlChar *) buf;
711
7.96M
    return(truncated);
712
7.96M
}
713
714
/**
715
 * xmlStrASPrintf:
716
 * @out:  pointer to the resulting string
717
 * @maxSize:  maximum size of the output buffer
718
 * @msg:  printf format string
719
 * @...:  arguments for format string
720
 *
721
 * See xmlStrVASPrintf.
722
 *
723
 * Returns 0 on success, 1 if the result was truncated or on other
724
 * errors, -1 if a memory allocation failed.
725
 */
726
int
727
0
xmlStrASPrintf(xmlChar **out, int maxSize, const char *msg, ...) {
728
0
    va_list ap;
729
0
    int ret;
730
731
0
    va_start(ap, msg);
732
0
    ret = xmlStrVASPrintf(out, maxSize, msg, ap);
733
0
    va_end(ap);
734
735
0
    return(ret);
736
0
}
737
738
/************************************************************************
739
 *                                                                      *
740
 *              Generic UTF8 handling routines                          *
741
 *                                                                      *
742
 * From rfc2044: encoding of the Unicode values on UTF-8:               *
743
 *                                                                      *
744
 * UCS-4 range (hex.)           UTF-8 octet sequence (binary)           *
745
 * 0000 0000-0000 007F   0xxxxxxx                                       *
746
 * 0000 0080-0000 07FF   110xxxxx 10xxxxxx                              *
747
 * 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx                     *
748
 *                                                                      *
749
 * I hope we won't use values > 0xFFFF anytime soon !                   *
750
 *                                                                      *
751
 ************************************************************************/
752
753
754
/**
755
 * xmlUTF8Size:
756
 * @utf: pointer to the UTF8 character
757
 *
758
 * calculates the internal size of a UTF8 character
759
 *
760
 * returns the numbers of bytes in the character, -1 on format error
761
 */
762
int
763
121k
xmlUTF8Size(const xmlChar *utf) {
764
121k
    xmlChar mask;
765
121k
    int len;
766
767
121k
    if (utf == NULL)
768
0
        return -1;
769
121k
    if (*utf < 0x80)
770
117k
        return 1;
771
    /* check valid UTF8 character */
772
3.57k
    if (!(*utf & 0x40))
773
0
        return -1;
774
    /* determine number of bytes in char */
775
3.57k
    len = 2;
776
8.51k
    for (mask=0x20; mask != 0; mask>>=1) {
777
8.51k
        if (!(*utf & mask))
778
3.57k
            return len;
779
4.94k
        len++;
780
4.94k
    }
781
0
    return -1;
782
3.57k
}
783
784
/**
785
 * xmlUTF8Charcmp:
786
 * @utf1: pointer to first UTF8 char
787
 * @utf2: pointer to second UTF8 char
788
 *
789
 * compares the two UCS4 values
790
 *
791
 * returns result of the compare as with xmlStrncmp
792
 */
793
int
794
121k
xmlUTF8Charcmp(const xmlChar *utf1, const xmlChar *utf2) {
795
796
121k
    if (utf1 == NULL ) {
797
0
        if (utf2 == NULL)
798
0
            return 0;
799
0
        return -1;
800
0
    }
801
121k
    return xmlStrncmp(utf1, utf2, xmlUTF8Size(utf1));
802
121k
}
803
804
/**
805
 * xmlUTF8Strlen:
806
 * @utf:  a sequence of UTF-8 encoded bytes
807
 *
808
 * compute the length of an UTF8 string, it doesn't do a full UTF8
809
 * checking of the content of the string.
810
 *
811
 * Returns the number of characters in the string or -1 in case of error
812
 */
813
int
814
58.8k
xmlUTF8Strlen(const xmlChar *utf) {
815
58.8k
    size_t ret = 0;
816
817
58.8k
    if (utf == NULL)
818
95
        return(-1);
819
820
7.69M
    while (*utf != 0) {
821
7.63M
        if (utf[0] & 0x80) {
822
100k
            if ((utf[1] & 0xc0) != 0x80)
823
0
                return(-1);
824
100k
            if ((utf[0] & 0xe0) == 0xe0) {
825
40.4k
                if ((utf[2] & 0xc0) != 0x80)
826
0
                    return(-1);
827
40.4k
                if ((utf[0] & 0xf0) == 0xf0) {
828
3.35k
                    if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
829
0
                        return(-1);
830
3.35k
                    utf += 4;
831
37.0k
                } else {
832
37.0k
                    utf += 3;
833
37.0k
                }
834
60.1k
            } else {
835
60.1k
                utf += 2;
836
60.1k
            }
837
7.53M
        } else {
838
7.53M
            utf++;
839
7.53M
        }
840
7.63M
        ret++;
841
7.63M
    }
842
58.7k
    return(ret > INT_MAX ? 0 : ret);
843
58.7k
}
844
845
/**
846
 * xmlGetUTF8Char:
847
 * @utf:  a sequence of UTF-8 encoded bytes
848
 * @len:  a pointer to the minimum number of bytes present in
849
 *        the sequence.  This is used to assure the next character
850
 *        is completely contained within the sequence.
851
 *
852
 * Read the first UTF8 character from @utf
853
 *
854
 * Returns the char value or -1 in case of error, and sets *len to
855
 *        the actual number of bytes consumed (0 in case of error)
856
 */
857
int
858
219M
xmlGetUTF8Char(const unsigned char *utf, int *len) {
859
219M
    unsigned int c;
860
861
219M
    if (utf == NULL)
862
0
        goto error;
863
219M
    if (len == NULL)
864
0
        goto error;
865
866
219M
    c = utf[0];
867
219M
    if (c < 0x80) {
868
122M
        if (*len < 1)
869
0
            goto error;
870
        /* 1-byte code */
871
122M
        *len = 1;
872
122M
    } else {
873
97.2M
        if ((*len < 2) || ((utf[1] & 0xc0) != 0x80))
874
1.04M
            goto error;
875
96.2M
        if (c < 0xe0) {
876
61.0M
            if (c < 0xc2)
877
166k
                goto error;
878
            /* 2-byte code */
879
60.8M
            *len = 2;
880
60.8M
            c = (c & 0x1f) << 6;
881
60.8M
            c |= utf[1] & 0x3f;
882
60.8M
        } else {
883
35.2M
            if ((*len < 3) || ((utf[2] & 0xc0) != 0x80))
884
42.6k
                goto error;
885
35.1M
            if (c < 0xf0) {
886
                /* 3-byte code */
887
35.0M
                *len = 3;
888
35.0M
                c = (c & 0xf) << 12;
889
35.0M
                c |= (utf[1] & 0x3f) << 6;
890
35.0M
                c |= utf[2] & 0x3f;
891
35.0M
                if ((c < 0x800) || ((c >= 0xd800) && (c < 0xe000)))
892
4.28k
                    goto error;
893
35.0M
            } else {
894
128k
                if ((*len < 4) || ((utf[3] & 0xc0) != 0x80))
895
3.68k
                    goto error;
896
124k
                *len = 4;
897
                /* 4-byte code */
898
124k
                c = (c & 0x7) << 18;
899
124k
                c |= (utf[1] & 0x3f) << 12;
900
124k
                c |= (utf[2] & 0x3f) << 6;
901
124k
                c |= utf[3] & 0x3f;
902
124k
                if ((c < 0x10000) || (c >= 0x110000))
903
3.91k
                    goto error;
904
124k
            }
905
35.1M
        }
906
96.2M
    }
907
218M
    return(c);
908
909
1.26M
error:
910
1.26M
    if (len != NULL)
911
1.26M
  *len = 0;
912
1.26M
    return(-1);
913
219M
}
914
915
/**
916
 * xmlCheckUTF8:
917
 * @utf: Pointer to putative UTF-8 encoded string.
918
 *
919
 * Checks @utf for being valid UTF-8. @utf is assumed to be
920
 * null-terminated. This function is not super-strict, as it will
921
 * allow longer UTF-8 sequences than necessary. Note that Java is
922
 * capable of producing these sequences if provoked. Also note, this
923
 * routine checks for the 4-byte maximum size, but does not check for
924
 * 0x10ffff maximum value.
925
 *
926
 * Return value: true if @utf is valid.
927
 **/
928
int
929
xmlCheckUTF8(const unsigned char *utf)
930
3.17k
{
931
3.17k
    int ix;
932
3.17k
    unsigned char c;
933
934
3.17k
    if (utf == NULL)
935
2
        return(0);
936
    /*
937
     * utf is a string of 1, 2, 3 or 4 bytes.  The valid strings
938
     * are as follows (in "bit format"):
939
     *    0xxxxxxx                                      valid 1-byte
940
     *    110xxxxx 10xxxxxx                             valid 2-byte
941
     *    1110xxxx 10xxxxxx 10xxxxxx                    valid 3-byte
942
     *    11110xxx 10xxxxxx 10xxxxxx 10xxxxxx           valid 4-byte
943
     */
944
2.82M
    while ((c = utf[0])) {      /* string is 0-terminated */
945
2.82M
        ix = 0;
946
2.82M
        if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */
947
154k
            ix = 1;
948
2.66M
  } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */
949
193k
      if ((utf[1] & 0xc0 ) != 0x80)
950
262
          return 0;
951
193k
      ix = 2;
952
2.47M
  } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */
953
2.47M
      if (((utf[1] & 0xc0) != 0x80) ||
954
2.47M
          ((utf[2] & 0xc0) != 0x80))
955
484
        return 0;
956
2.47M
      ix = 3;
957
2.47M
  } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */
958
1.10k
      if (((utf[1] & 0xc0) != 0x80) ||
959
888
          ((utf[2] & 0xc0) != 0x80) ||
960
396
    ((utf[3] & 0xc0) != 0x80))
961
925
        return 0;
962
175
      ix = 4;
963
175
  } else       /* unknown encoding */
964
768
      return 0;
965
2.82M
        utf += ix;
966
2.82M
      }
967
736
      return(1);
968
3.17k
}
969
970
/**
971
 * xmlUTF8Strsize:
972
 * @utf:  a sequence of UTF-8 encoded bytes
973
 * @len:  the number of characters in the array
974
 *
975
 * storage size of an UTF8 string
976
 * the behaviour is not guaranteed if the input string is not UTF-8
977
 *
978
 * Returns the storage size of
979
 * the first 'len' characters of ARRAY
980
 */
981
982
int
983
2.39M
xmlUTF8Strsize(const xmlChar *utf, int len) {
984
2.39M
    const xmlChar *ptr=utf;
985
2.39M
    int ch;
986
2.39M
    size_t ret;
987
988
2.39M
    if (utf == NULL)
989
0
        return(0);
990
991
2.39M
    if (len <= 0)
992
1.57k
        return(0);
993
994
12.0M
    while ( len-- > 0) {
995
9.65M
        if ( !*ptr )
996
28.2k
            break;
997
9.62M
        ch = *ptr++;
998
9.62M
        if ((ch & 0x80))
999
317k
            while ((ch<<=1) & 0x80 ) {
1000
180k
    if (*ptr == 0) break;
1001
180k
                ptr++;
1002
180k
      }
1003
9.62M
    }
1004
2.38M
    ret = ptr - utf;
1005
2.38M
    return (ret > INT_MAX ? 0 : ret);
1006
2.39M
}
1007
1008
1009
/**
1010
 * xmlUTF8Strndup:
1011
 * @utf:  the input UTF8 *
1012
 * @len:  the len of @utf (in chars)
1013
 *
1014
 * a strndup for array of UTF8's
1015
 *
1016
 * Returns a new UTF8 * or NULL
1017
 */
1018
xmlChar *
1019
30.4k
xmlUTF8Strndup(const xmlChar *utf, int len) {
1020
30.4k
    xmlChar *ret;
1021
30.4k
    int i;
1022
1023
30.4k
    if ((utf == NULL) || (len < 0)) return(NULL);
1024
30.4k
    i = xmlUTF8Strsize(utf, len);
1025
30.4k
    ret = xmlMalloc((size_t) i + 1);
1026
30.4k
    if (ret == NULL) {
1027
3
        return(NULL);
1028
3
    }
1029
30.3k
    memcpy(ret, utf, i);
1030
30.3k
    ret[i] = 0;
1031
30.3k
    return(ret);
1032
30.4k
}
1033
1034
/**
1035
 * xmlUTF8Strpos:
1036
 * @utf:  the input UTF8 *
1037
 * @pos:  the position of the desired UTF8 char (in chars)
1038
 *
1039
 * a function to provide the equivalent of fetching a
1040
 * character from a string array
1041
 *
1042
 * Returns a pointer to the UTF8 character or NULL
1043
 */
1044
const xmlChar *
1045
15.4k
xmlUTF8Strpos(const xmlChar *utf, int pos) {
1046
15.4k
    int ch;
1047
1048
15.4k
    if (utf == NULL) return(NULL);
1049
15.4k
    if (pos < 0)
1050
0
        return(NULL);
1051
68.5k
    while (pos--) {
1052
53.0k
        ch = *utf++;
1053
53.0k
        if (ch == 0)
1054
0
            return(NULL);
1055
53.0k
        if ( ch & 0x80 ) {
1056
            /* if not simple ascii, verify proper format */
1057
2.07k
            if ( (ch & 0xc0) != 0xc0 )
1058
0
                return(NULL);
1059
            /* then skip over remaining bytes for this char */
1060
5.81k
            while ( (ch <<= 1) & 0x80 )
1061
3.73k
                if ( (*utf++ & 0xc0) != 0x80 )
1062
0
                    return(NULL);
1063
2.07k
        }
1064
53.0k
    }
1065
15.4k
    return((xmlChar *)utf);
1066
15.4k
}
1067
1068
/**
1069
 * xmlUTF8Strloc:
1070
 * @utf:  the input UTF8 *
1071
 * @utfchar:  the UTF8 character to be found
1072
 *
1073
 * a function to provide the relative location of a UTF8 char
1074
 *
1075
 * Returns the relative character position of the desired char
1076
 * or -1 if not found
1077
 */
1078
int
1079
1.03M
xmlUTF8Strloc(const xmlChar *utf, const xmlChar *utfchar) {
1080
1.03M
    size_t i;
1081
1.03M
    int size;
1082
1.03M
    int ch;
1083
1084
1.03M
    if (utf==NULL || utfchar==NULL) return -1;
1085
1.03M
    size = xmlUTF8Strsize(utfchar, 1);
1086
35.9M
        for(i=0; (ch=*utf) != 0; i++) {
1087
35.2M
            if (xmlStrncmp(utf, utfchar, size)==0)
1088
267k
                return(i > INT_MAX ? 0 : i);
1089
34.9M
            utf++;
1090
34.9M
            if ( ch & 0x80 ) {
1091
                /* if not simple ascii, verify proper format */
1092
374k
                if ( (ch & 0xc0) != 0xc0 )
1093
0
                    return(-1);
1094
                /* then skip over remaining bytes for this char */
1095
970k
                while ( (ch <<= 1) & 0x80 )
1096
595k
                    if ( (*utf++ & 0xc0) != 0x80 )
1097
0
                        return(-1);
1098
374k
            }
1099
34.9M
        }
1100
1101
763k
    return(-1);
1102
1.03M
}
1103
/**
1104
 * xmlUTF8Strsub:
1105
 * @utf:  a sequence of UTF-8 encoded bytes
1106
 * @start: relative pos of first char
1107
 * @len:   total number to copy
1108
 *
1109
 * Create a substring from a given UTF-8 string
1110
 * Note:  positions are given in units of UTF-8 chars
1111
 *
1112
 * Returns a pointer to a newly created string or NULL if the
1113
 * start index is out of bounds or a memory allocation failed.
1114
 * If len is too large, the result is truncated.
1115
 */
1116
1117
xmlChar *
1118
28.5k
xmlUTF8Strsub(const xmlChar *utf, int start, int len) {
1119
28.5k
    int i;
1120
28.5k
    int ch;
1121
1122
28.5k
    if (utf == NULL) return(NULL);
1123
28.5k
    if (start < 0) return(NULL);
1124
28.5k
    if (len < 0) return(NULL);
1125
1126
    /*
1127
     * Skip over any leading chars
1128
     */
1129
243k
    for (i = 0; i < start; i++) {
1130
215k
        ch = *utf++;
1131
215k
        if (ch == 0)
1132
0
            return(NULL);
1133
        /* skip over remaining bytes for this char */
1134
215k
        if (ch & 0x80) {
1135
6.84k
            ch <<= 1;
1136
17.6k
            while (ch & 0x80) {
1137
10.8k
                if (*utf++ == 0)
1138
0
                    return(NULL);
1139
10.8k
                ch <<= 1;
1140
10.8k
            }
1141
6.84k
        }
1142
215k
    }
1143
1144
28.5k
    return(xmlUTF8Strndup(utf, len));
1145
28.5k
}
1146
1147
/**
1148
 * xmlEscapeFormatString:
1149
 * @msg:  a pointer to the string in which to escape '%' characters.
1150
 * Must be a heap-allocated buffer created by libxml2 that may be
1151
 * returned, or that may be freed and replaced.
1152
 *
1153
 * Replaces the string pointed to by 'msg' with an escaped string.
1154
 * Returns the same string with all '%' characters escaped.
1155
 */
1156
xmlChar *
1157
xmlEscapeFormatString(xmlChar **msg)
1158
0
{
1159
0
    xmlChar *msgPtr = NULL;
1160
0
    xmlChar *result = NULL;
1161
0
    xmlChar *resultPtr = NULL;
1162
0
    size_t count = 0;
1163
0
    size_t msgLen = 0;
1164
0
    size_t resultLen = 0;
1165
1166
0
    if (!msg || !*msg)
1167
0
        return(NULL);
1168
1169
0
    for (msgPtr = *msg; *msgPtr != '\0'; ++msgPtr) {
1170
0
        ++msgLen;
1171
0
        if (*msgPtr == '%')
1172
0
            ++count;
1173
0
    }
1174
1175
0
    if (count == 0)
1176
0
        return(*msg);
1177
1178
0
    if ((count > INT_MAX) || (msgLen > INT_MAX - count))
1179
0
        return(NULL);
1180
0
    resultLen = msgLen + count + 1;
1181
0
    result = xmlMalloc(resultLen);
1182
0
    if (result == NULL) {
1183
        /* Clear *msg to prevent format string vulnerabilities in
1184
           out-of-memory situations. */
1185
0
        xmlFree(*msg);
1186
0
        *msg = NULL;
1187
0
        return(NULL);
1188
0
    }
1189
1190
0
    for (msgPtr = *msg, resultPtr = result; *msgPtr != '\0'; ++msgPtr, ++resultPtr) {
1191
0
        *resultPtr = *msgPtr;
1192
0
        if (*msgPtr == '%')
1193
0
            *(++resultPtr) = '%';
1194
0
    }
1195
0
    result[resultLen - 1] = '\0';
1196
1197
0
    xmlFree(*msg);
1198
0
    *msg = result;
1199
1200
0
    return *msg;
1201
0
}
1202