Coverage Report

Created: 2026-10-28 06:30

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