Render the cstring provided to an escaped version that can be printed. */
| 841 | |
| 842 | /* Render the cstring provided to an escaped version that can be printed. */ |
| 843 | static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) |
| 844 | { |
| 845 | const unsigned char *input_pointer = NULL; |
| 846 | unsigned char *output = NULL; |
| 847 | unsigned char *output_pointer = NULL; |
| 848 | size_t output_length = 0; |
| 849 | /* numbers of additional characters needed for escaping */ |
| 850 | size_t escape_characters = 0; |
| 851 | |
| 852 | if (output_buffer == NULL) |
| 853 | { |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | /* empty string */ |
| 858 | if (input == NULL) |
| 859 | { |
| 860 | output = ensure(output_buffer, sizeof("\"\"")); |
| 861 | if (output == NULL) |
| 862 | { |
| 863 | return false; |
| 864 | } |
| 865 | strcpy((char*)output, "\"\""); |
| 866 | |
| 867 | return true; |
| 868 | } |
| 869 | |
| 870 | /* set "flag" to 1 if something needs to be escaped */ |
| 871 | for (input_pointer = input; *input_pointer; input_pointer++) |
| 872 | { |
| 873 | switch (*input_pointer) |
| 874 | { |
| 875 | case '\"': |
| 876 | case '\\': |
| 877 | case '\b': |
| 878 | case '\f': |
| 879 | case '\n': |
| 880 | case '\r': |
| 881 | case '\t': |
| 882 | /* one character escape sequence */ |
| 883 | escape_characters++; |
| 884 | break; |
| 885 | default: |
| 886 | if (*input_pointer < 32) |
| 887 | { |
| 888 | /* UTF-16 escape sequence uXXXX */ |
| 889 | escape_characters += 5; |
| 890 | } |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | output_length = (size_t)(input_pointer - input) + escape_characters; |
| 895 | |
| 896 | output = ensure(output_buffer, output_length + sizeof("\"\"")); |
| 897 | if (output == NULL) |
| 898 | { |
| 899 | return false; |
| 900 | } |
no test coverage detected