make_printable.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #define _XOPEN_SOURCE 600 /* Make sure strdup() is in <string.h> */
  2. #include <stdarg.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include "xmlrpc_config.h"
  8. #include "xmlrpc-c/string_int.h"
  9. const char *
  10. xmlrpc_makePrintable_lp(const char * const input,
  11. size_t const inputLength) {
  12. /*----------------------------------------------------------------------------
  13. Convert an arbitrary string of characters in length-pointer form to
  14. printable ASCII. E.g. convert newlines to "\n".
  15. Return the result in newly malloc'ed storage. Return NULL if we can't
  16. get the storage.
  17. -----------------------------------------------------------------------------*/
  18. char * output;
  19. output = malloc(inputLength*4+1);
  20. /* Worst case, we render a character like \x01 -- 4 characters */
  21. if (output != NULL) {
  22. unsigned int inputCursor, outputCursor;
  23. for (inputCursor = 0, outputCursor = 0;
  24. inputCursor < inputLength;
  25. ++inputCursor) {
  26. if (0) {
  27. } else if (input[inputCursor] == '\\') {
  28. output[outputCursor++] = '\\';
  29. output[outputCursor++] = '\\';
  30. } else if (input[inputCursor] == '\n') {
  31. output[outputCursor++] = '\\';
  32. output[outputCursor++] = 'n';
  33. } else if (input[inputCursor] == '\t') {
  34. output[outputCursor++] = '\\';
  35. output[outputCursor++] = 't';
  36. } else if (input[inputCursor] == '\a') {
  37. output[outputCursor++] = '\\';
  38. output[outputCursor++] = 'a';
  39. } else if (input[inputCursor] == '\r') {
  40. output[outputCursor++] = '\\';
  41. output[outputCursor++] = 'r';
  42. } else if (isprint(input[inputCursor])) {
  43. output[outputCursor++] = input[inputCursor];
  44. } else {
  45. snprintf(&output[outputCursor], 5, "\\x%02x",
  46. input[inputCursor]);
  47. outputCursor += 4;
  48. }
  49. }
  50. output[outputCursor++] = '\0';
  51. }
  52. return output;
  53. }
  54. const char *
  55. xmlrpc_makePrintable(const char * const input) {
  56. /*----------------------------------------------------------------------------
  57. Convert an arbitrary string of characters (NUL-terminated, though) to
  58. printable ASCII. E.g. convert newlines to "\n".
  59. Return the result in newly malloc'ed storage. Return NULL if we can't
  60. get the storage.
  61. -----------------------------------------------------------------------------*/
  62. return xmlrpc_makePrintable_lp(input, strlen(input));
  63. }
  64. const char *
  65. xmlrpc_makePrintableChar(char const input) {
  66. /*----------------------------------------------------------------------------
  67. Return an ASCIIZ string consisting of the character 'input',
  68. properly escaped so as to be printable. E.g., in C notation, '\n'
  69. turns into "\\n"
  70. -----------------------------------------------------------------------------*/
  71. const char * retval;
  72. if (input == '\0')
  73. retval = strdup("\\0");
  74. else {
  75. char buffer[2];
  76. buffer[0] = input;
  77. buffer[1] = '\0';
  78. retval = xmlrpc_makePrintable(buffer);
  79. }
  80. return retval;
  81. }