OLD | NEW |
1 /* | 1 /* |
2 * string.c : an XML string utilities module | 2 * string.c : an XML string utilities module |
3 * | 3 * |
4 * This module provides various utility functions for manipulating | 4 * This module provides various utility functions for manipulating |
5 * the xmlChar* type. All functions named xmlStr* have been moved here | 5 * the xmlChar* type. All functions named xmlStr* have been moved here |
6 * from the parser.c file (their original home). | 6 * from the parser.c file (their original home). |
7 * | 7 * |
8 * See Copyright for the status of this software. | 8 * See Copyright for the status of this software. |
9 * | 9 * |
10 * UTF8 string routines from: | 10 * UTF8 string routines from: |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 xmlChar *ret; | 450 xmlChar *ret; |
451 | 451 |
452 if ((add == NULL) || (len == 0)) | 452 if ((add == NULL) || (len == 0)) |
453 return(cur); | 453 return(cur); |
454 if (len < 0) | 454 if (len < 0) |
455 return(NULL); | 455 return(NULL); |
456 if (cur == NULL) | 456 if (cur == NULL) |
457 return(xmlStrndup(add, len)); | 457 return(xmlStrndup(add, len)); |
458 | 458 |
459 size = xmlStrlen(cur); | 459 size = xmlStrlen(cur); |
| 460 if (size < 0) |
| 461 return(NULL); |
460 ret = (xmlChar *) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar)); | 462 ret = (xmlChar *) xmlRealloc(cur, (size + len + 1) * sizeof(xmlChar)); |
461 if (ret == NULL) { | 463 if (ret == NULL) { |
462 xmlErrMemory(NULL, NULL); | 464 xmlErrMemory(NULL, NULL); |
463 return(cur); | 465 return(cur); |
464 } | 466 } |
465 memcpy(&ret[size], add, len * sizeof(xmlChar)); | 467 memcpy(&ret[size], add, len * sizeof(xmlChar)); |
466 ret[size + len] = 0; | 468 ret[size + len] = 0; |
467 return(ret); | 469 return(ret); |
468 } | 470 } |
469 | 471 |
470 /** | 472 /** |
471 * xmlStrncatNew: | 473 * xmlStrncatNew: |
472 * @str1: first xmlChar string | 474 * @str1: first xmlChar string |
473 * @str2: second xmlChar string | 475 * @str2: second xmlChar string |
474 * @len: the len of @str2 or < 0 | 476 * @len: the len of @str2 or < 0 |
475 * | 477 * |
476 * same as xmlStrncat, but creates a new string. The original | 478 * same as xmlStrncat, but creates a new string. The original |
477 * two strings are not freed. If @len is < 0 then the length | 479 * two strings are not freed. If @len is < 0 then the length |
478 * will be calculated automatically. | 480 * will be calculated automatically. |
479 * | 481 * |
480 * Returns a new xmlChar * or NULL | 482 * Returns a new xmlChar * or NULL |
481 */ | 483 */ |
482 xmlChar * | 484 xmlChar * |
483 xmlStrncatNew(const xmlChar *str1, const xmlChar *str2, int len) { | 485 xmlStrncatNew(const xmlChar *str1, const xmlChar *str2, int len) { |
484 int size; | 486 int size; |
485 xmlChar *ret; | 487 xmlChar *ret; |
486 | 488 |
487 if (len < 0) | 489 if (len < 0) { |
488 len = xmlStrlen(str2); | 490 len = xmlStrlen(str2); |
| 491 if (len < 0) |
| 492 return(NULL); |
| 493 } |
489 if ((str2 == NULL) || (len == 0)) | 494 if ((str2 == NULL) || (len == 0)) |
490 return(xmlStrdup(str1)); | 495 return(xmlStrdup(str1)); |
491 if (str1 == NULL) | 496 if (str1 == NULL) |
492 return(xmlStrndup(str2, len)); | 497 return(xmlStrndup(str2, len)); |
493 | 498 |
494 size = xmlStrlen(str1); | 499 size = xmlStrlen(str1); |
| 500 if (size < 0) |
| 501 return(NULL); |
495 ret = (xmlChar *) xmlMalloc((size + len + 1) * sizeof(xmlChar)); | 502 ret = (xmlChar *) xmlMalloc((size + len + 1) * sizeof(xmlChar)); |
496 if (ret == NULL) { | 503 if (ret == NULL) { |
497 xmlErrMemory(NULL, NULL); | 504 xmlErrMemory(NULL, NULL); |
498 return(xmlStrndup(str1, size)); | 505 return(xmlStrndup(str1, size)); |
499 } | 506 } |
500 memcpy(ret, str1, size * sizeof(xmlChar)); | 507 memcpy(ret, str1, size * sizeof(xmlChar)); |
501 memcpy(&ret[size], str2, len * sizeof(xmlChar)); | 508 memcpy(&ret[size], str2, len * sizeof(xmlChar)); |
502 ret[size + len] = 0; | 509 ret[size + len] = 0; |
503 return(ret); | 510 return(ret); |
504 } | 511 } |
(...skipping 26 matching lines...) Expand all Loading... |
531 * @buf: the result buffer. | 538 * @buf: the result buffer. |
532 * @len: the result buffer length. | 539 * @len: the result buffer length. |
533 * @msg: the message with printf formatting. | 540 * @msg: the message with printf formatting. |
534 * @...: extra parameters for the message. | 541 * @...: extra parameters for the message. |
535 * | 542 * |
536 * Formats @msg and places result into @buf. | 543 * Formats @msg and places result into @buf. |
537 * | 544 * |
538 * Returns the number of characters written to @buf or -1 if an error occurs. | 545 * Returns the number of characters written to @buf or -1 if an error occurs. |
539 */ | 546 */ |
540 int XMLCDECL | 547 int XMLCDECL |
541 xmlStrPrintf(xmlChar *buf, int len, const xmlChar *msg, ...) { | 548 xmlStrPrintf(xmlChar *buf, int len, const char *msg, ...) { |
542 va_list args; | 549 va_list args; |
543 int ret; | 550 int ret; |
544 | 551 |
545 if((buf == NULL) || (msg == NULL)) { | 552 if((buf == NULL) || (msg == NULL)) { |
546 return(-1); | 553 return(-1); |
547 } | 554 } |
548 | 555 |
549 va_start(args, msg); | 556 va_start(args, msg); |
550 ret = vsnprintf((char *) buf, len, (const char *) msg, args); | 557 ret = vsnprintf((char *) buf, len, (const char *) msg, args); |
551 va_end(args); | 558 va_end(args); |
552 buf[len - 1] = 0; /* be safe ! */ | 559 buf[len - 1] = 0; /* be safe ! */ |
553 | 560 |
554 return(ret); | 561 return(ret); |
555 } | 562 } |
556 | 563 |
557 /** | 564 /** |
558 * xmlStrVPrintf: | 565 * xmlStrVPrintf: |
559 * @buf: the result buffer. | 566 * @buf: the result buffer. |
560 * @len: the result buffer length. | 567 * @len: the result buffer length. |
561 * @msg: the message with printf formatting. | 568 * @msg: the message with printf formatting. |
562 * @ap: extra parameters for the message. | 569 * @ap: extra parameters for the message. |
563 * | 570 * |
564 * Formats @msg and places result into @buf. | 571 * Formats @msg and places result into @buf. |
565 * | 572 * |
566 * Returns the number of characters written to @buf or -1 if an error occurs. | 573 * Returns the number of characters written to @buf or -1 if an error occurs. |
567 */ | 574 */ |
568 int | 575 int |
569 xmlStrVPrintf(xmlChar *buf, int len, const xmlChar *msg, va_list ap) { | 576 xmlStrVPrintf(xmlChar *buf, int len, const char *msg, va_list ap) { |
570 int ret; | 577 int ret; |
571 | 578 |
572 if((buf == NULL) || (msg == NULL)) { | 579 if((buf == NULL) || (msg == NULL)) { |
573 return(-1); | 580 return(-1); |
574 } | 581 } |
575 | 582 |
576 ret = vsnprintf((char *) buf, len, (const char *) msg, ap); | 583 ret = vsnprintf((char *) buf, len, (const char *) msg, ap); |
577 buf[len - 1] = 0; /* be safe ! */ | 584 buf[len - 1] = 0; /* be safe ! */ |
578 | 585 |
579 return(ret); | 586 return(ret); |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 /* then skip over remaining bytes for this char */ | 984 /* then skip over remaining bytes for this char */ |
978 while ( (ch <<= 1) & 0x80 ) | 985 while ( (ch <<= 1) & 0x80 ) |
979 if ( (*utf++ & 0xc0) != 0x80 ) | 986 if ( (*utf++ & 0xc0) != 0x80 ) |
980 return(NULL); | 987 return(NULL); |
981 } | 988 } |
982 } | 989 } |
983 | 990 |
984 return(xmlUTF8Strndup(utf, len)); | 991 return(xmlUTF8Strndup(utf, len)); |
985 } | 992 } |
986 | 993 |
| 994 /** |
| 995 * xmlEscapeFormatString: |
| 996 * @msg: a pointer to the string in which to escape '%' characters. |
| 997 * Must be a heap-allocated buffer created by libxml2 that may be |
| 998 * returned, or that may be freed and replaced. |
| 999 * |
| 1000 * Replaces the string pointed to by 'msg' with an escaped string. |
| 1001 * Returns the same string with all '%' characters escaped. |
| 1002 */ |
| 1003 xmlChar * |
| 1004 xmlEscapeFormatString(xmlChar **msg) |
| 1005 { |
| 1006 xmlChar *msgPtr = NULL; |
| 1007 xmlChar *result = NULL; |
| 1008 xmlChar *resultPtr = NULL; |
| 1009 size_t count = 0; |
| 1010 size_t msgLen = 0; |
| 1011 size_t resultLen = 0; |
| 1012 |
| 1013 if (!msg || !*msg) |
| 1014 return(NULL); |
| 1015 |
| 1016 for (msgPtr = *msg; *msgPtr != '\0'; ++msgPtr) { |
| 1017 ++msgLen; |
| 1018 if (*msgPtr == '%') |
| 1019 ++count; |
| 1020 } |
| 1021 |
| 1022 if (count == 0) |
| 1023 return(*msg); |
| 1024 |
| 1025 resultLen = msgLen + count + 1; |
| 1026 result = (xmlChar *) xmlMallocAtomic(resultLen * sizeof(xmlChar)); |
| 1027 if (result == NULL) { |
| 1028 /* Clear *msg to prevent format string vulnerabilities in |
| 1029 out-of-memory situations. */ |
| 1030 xmlFree(*msg); |
| 1031 *msg = NULL; |
| 1032 xmlErrMemory(NULL, NULL); |
| 1033 return(NULL); |
| 1034 } |
| 1035 |
| 1036 for (msgPtr = *msg, resultPtr = result; *msgPtr != '\0'; ++msgPtr, ++resultP
tr) { |
| 1037 *resultPtr = *msgPtr; |
| 1038 if (*msgPtr == '%') |
| 1039 *(++resultPtr) = '%'; |
| 1040 } |
| 1041 result[resultLen - 1] = '\0'; |
| 1042 |
| 1043 xmlFree(*msg); |
| 1044 *msg = result; |
| 1045 |
| 1046 return *msg; |
| 1047 } |
| 1048 |
987 #define bottom_xmlstring | 1049 #define bottom_xmlstring |
988 #include "elfgcchack.h" | 1050 #include "elfgcchack.h" |
OLD | NEW |