Render the cstring provided to an escaped version that can be printed. */
| 900 | |
| 901 | /* Render the cstring provided to an escaped version that can be printed. */ |
| 902 | static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) |
| 903 | { |
| 904 | const unsigned char *input_pointer = NULL; |
| 905 | unsigned char *output = NULL; |
| 906 | unsigned char *output_pointer = NULL; |
| 907 | size_t output_length = 0; |
| 908 | /* numbers of additional characters needed for escaping */ |
| 909 | size_t escape_characters = 0; |
| 910 | |
| 911 | if (output_buffer == NULL) |
| 912 | { |
| 913 | return false; |
| 914 | } |
| 915 | |
| 916 | /* empty string */ |
| 917 | if (input == NULL) |
| 918 | { |
| 919 | output = ensure(output_buffer, sizeof("\"\"")); |
| 920 | if (output == NULL) |
| 921 | { |
| 922 | return false; |
| 923 | } |
| 924 | strcpy((char*)output, "\"\""); |
| 925 | |
| 926 | return true; |
| 927 | } |
| 928 | |
| 929 | /* set "flag" to 1 if something needs to be escaped */ |
| 930 | for (input_pointer = input; *input_pointer; input_pointer++) |
| 931 | { |
| 932 | switch (*input_pointer) |
| 933 | { |
| 934 | case '\"': |
| 935 | case '\\': |
| 936 | case '\b': |
| 937 | case '\f': |
| 938 | case '\n': |
| 939 | case '\r': |
| 940 | case '\t': |
| 941 | /* one character escape sequence */ |
| 942 | escape_characters++; |
| 943 | break; |
| 944 | default: |
| 945 | if (*input_pointer < 32) |
| 946 | { |
| 947 | /* UTF-16 escape sequence uXXXX */ |
| 948 | escape_characters += 5; |
| 949 | } |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | output_length = (size_t)(input_pointer - input) + escape_characters; |
| 954 | |
| 955 | output = ensure(output_buffer, output_length + sizeof("\"\"")); |
| 956 | if (output == NULL) |
| 957 | { |
| 958 | return false; |
| 959 | } |
no test coverage detected