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