Render the cstring provided to an escaped version that can be printed. */
| 713 | |
| 714 | /* Render the cstring provided to an escaped version that can be printed. */ |
| 715 | static unsigned char *print_string_ptr(const unsigned char *str, printbuffer *p) |
| 716 | { |
| 717 | const unsigned char *ptr = NULL; |
| 718 | unsigned char *ptr2 = NULL; |
| 719 | unsigned char *out = NULL; |
| 720 | size_t len = 0; |
| 721 | cjbool flag = false; |
| 722 | unsigned char token = '\0'; |
| 723 | |
| 724 | /* empty string */ |
| 725 | if (!str) |
| 726 | { |
| 727 | if (p) |
| 728 | { |
| 729 | out = ensure(p, 3); |
| 730 | } |
| 731 | else |
| 732 | { |
| 733 | out = (unsigned char*)cJSON_malloc(3); |
| 734 | } |
| 735 | if (!out) |
| 736 | { |
| 737 | return NULL; |
| 738 | } |
| 739 | strcpy((char*)out, "\"\""); |
| 740 | |
| 741 | return out; |
| 742 | } |
| 743 | |
| 744 | /* set "flag" to 1 if something needs to be escaped */ |
| 745 | for (ptr = str; *ptr; ptr++) |
| 746 | { |
| 747 | flag |= (((*ptr > 0) && (*ptr < 32)) /* unprintable characters */ |
| 748 | || (*ptr == '\"') /* double quote */ |
| 749 | || (*ptr == '\\')) /* backslash */ |
| 750 | ? 1 |
| 751 | : 0; |
| 752 | } |
| 753 | /* no characters have to be escaped */ |
| 754 | if (!flag) |
| 755 | { |
| 756 | len = (size_t)(ptr - str); |
| 757 | if (p) |
| 758 | { |
| 759 | out = ensure(p, len + 3); |
| 760 | } |
| 761 | else |
| 762 | { |
| 763 | out = (unsigned char*)cJSON_malloc(len + 3); |
| 764 | } |
| 765 | if (!out) |
| 766 | { |
| 767 | return NULL; |
| 768 | } |
| 769 | |
| 770 | ptr2 = out; |
| 771 | *ptr2++ = '\"'; |
| 772 | strcpy((char*)ptr2, (const char*)str); |
no test coverage detected