debug: add an empty line
[samba.git] / lib / util / util_str_escape.c
1 /*
2    Samba string escaping routines
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/util/util_str_escape.h"
22
23
24 /*
25  * Calculate the encoded length of a character for log_escape
26  *
27  */
28 static size_t encoded_length(char c)
29 {
30         if (c != '\\' &&  c > 0x1F) {
31                 return 1;
32         } else {
33                 switch (c) {
34                 case '\a':
35                 case '\b':
36                 case '\f':
37                 case '\n':
38                 case '\r':
39                 case '\t':
40                 case '\v':
41                 case '\\':
42                         return 2;  /* C escape sequence */
43                 default:
44                         return 4;  /* hex escape \xhh   */
45                 }
46         }
47 }
48
49 /*
50  * Escape any control characters in the inputs to prevent them from
51  * interfering with the log output.
52  */
53 char *log_escape(TALLOC_CTX *frame, const char *in)
54 {
55         size_t size = 0;        /* Space to allocate for the escaped data */
56         char *encoded = NULL;   /* The encoded string                     */
57         const char *c;
58         char *e;
59
60         if (in == NULL) {
61                 return NULL;
62         }
63
64         /* Calculate the size required for the escaped array */
65         c = in;
66         while (*c) {
67                 size += encoded_length( *c);
68                 c++;
69         }
70         size++;
71
72         encoded = talloc_array( frame, char, size);
73         if (encoded == NULL) {
74                 DBG_ERR( "Out of memory allocating encoded string");
75                 return NULL;
76         }
77
78         c = in;
79         e = encoded;
80         while (*c) {
81                 if (*c != '\\' && *c > 0x1F) {
82                         *e++ = *c++;
83                 } else {
84                         switch (*c) {
85                         case '\a':
86                                 *e++ = '\\';
87                                 *e++ = 'a';
88                                 break;
89                         case '\b':
90                                 *e++ = '\\';
91                                 *e++ = 'b';
92                                 break;
93                         case '\f':
94                                 *e++ = '\\';
95                                 *e++ = 'f';
96                                 break;
97                         case '\n':
98                                 *e++ = '\\';
99                                 *e++ = 'n';
100                                 break;
101                         case '\r':
102                                 *e++ = '\\';
103                                 *e++ = 'r';
104                                 break;
105                         case '\t':
106                                 *e++ = '\\';
107                                 *e++ = 't';
108                                 break;
109                         case '\v':
110                                 *e++ = '\\';
111                                 *e++ = 'v';
112                                 break;
113                         case '\\':
114                                 *e++ = '\\';
115                                 *e++ = '\\';
116                                 break;
117                         default:
118                                 snprintf(e, 5, "\\x%02X", *c);
119                                 e += 4;
120                         }
121                         c++;
122                 }
123         }
124         *e = '\0';
125         return encoded;
126 }