Render the cstring provided to an escaped version that can be printed. */
| 947 | |
| 948 | /* Render the cstring provided to an escaped version that can be printed. */ |
| 949 | static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) |
| 950 | { |
| 951 | const unsigned char *input_pointer = NULL; |
| 952 | unsigned char *output = NULL; |
| 953 | unsigned char *output_pointer = NULL; |
| 954 | size_t output_length = 0; |
| 955 | /* numbers of additional characters needed for escaping */ |
| 956 | size_t escape_characters = 0; |
| 957 | |
| 958 | if (output_buffer == NULL) |
| 959 | { |
| 960 | return false; |
| 961 | } |
| 962 | |
| 963 | /* empty string */ |
| 964 | if (input == NULL) |
| 965 | { |
| 966 | output = ensure(output_buffer, sizeof("\"\"")); |
| 967 | if (output == NULL) |
| 968 | { |
| 969 | return false; |
| 970 | } |
| 971 | strcpy((char*)output, "\"\""); |
| 972 | |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | /* set "flag" to 1 if something needs to be escaped */ |
| 977 | for (input_pointer = input; *input_pointer; input_pointer++) |
| 978 | { |
| 979 | switch (*input_pointer) |
| 980 | { |
| 981 | case '\"': |
| 982 | case '\\': |
| 983 | case '\b': |
| 984 | case '\f': |
| 985 | case '\n': |
| 986 | case '\r': |
| 987 | case '\t': |
| 988 | /* one character escape sequence */ |
| 989 | escape_characters++; |
| 990 | break; |
| 991 | default: |
| 992 | if (*input_pointer < 32) |
| 993 | { |
| 994 | /* UTF-16 escape sequence uXXXX */ |
| 995 | escape_characters += 5; |
| 996 | } |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | output_length = (size_t)(input_pointer - input) + escape_characters; |
| 1001 | |
| 1002 | output = ensure(output_buffer, output_length + sizeof("\"\"")); |
| 1003 | if (output == NULL) |
| 1004 | { |
| 1005 | return false; |
| 1006 | } |
no test coverage detected
searching dependent graphs…