Line data Source code
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 326925 : static size_t encoded_length(char c)
29 : {
30 359109 : if (c != '\\' && c > 0x1F) {
31 326925 : return 1;
32 : } else {
33 11 : switch (c) {
34 0 : case '\a':
35 : case '\b':
36 : case '\f':
37 : case '\n':
38 : case '\r':
39 : case '\t':
40 : case '\v':
41 : case '\\':
42 0 : return 2; /* C escape sequence */
43 3 : default:
44 0 : 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 31901 : char *log_escape(TALLOC_CTX *frame, const char *in)
54 : {
55 31901 : size_t size = 0; /* Space to allocate for the escaped data */
56 31901 : char *encoded = NULL; /* The encoded string */
57 : const char *c;
58 : char *e;
59 :
60 31901 : if (in == NULL) {
61 0 : return NULL;
62 : }
63 :
64 : /* Calculate the size required for the escaped array */
65 28685 : c = in;
66 416123 : while (*c) {
67 359109 : size += encoded_length( *c);
68 359109 : c++;
69 : }
70 31900 : size++;
71 :
72 31900 : encoded = talloc_array( frame, char, size);
73 31900 : if (encoded == NULL) {
74 0 : DBG_ERR( "Out of memory allocating encoded string");
75 0 : return NULL;
76 : }
77 :
78 28685 : c = in;
79 28685 : e = encoded;
80 416123 : while (*c) {
81 359109 : if (*c != '\\' && *c > 0x1F) {
82 359098 : *e++ = *c++;
83 : } else {
84 11 : switch (*c) {
85 1 : case '\a':
86 1 : *e++ = '\\';
87 1 : *e++ = 'a';
88 1 : break;
89 1 : case '\b':
90 1 : *e++ = '\\';
91 1 : *e++ = 'b';
92 1 : break;
93 1 : case '\f':
94 1 : *e++ = '\\';
95 1 : *e++ = 'f';
96 1 : break;
97 1 : case '\n':
98 1 : *e++ = '\\';
99 1 : *e++ = 'n';
100 1 : break;
101 1 : case '\r':
102 1 : *e++ = '\\';
103 1 : *e++ = 'r';
104 1 : break;
105 1 : case '\t':
106 1 : *e++ = '\\';
107 1 : *e++ = 't';
108 1 : break;
109 1 : case '\v':
110 1 : *e++ = '\\';
111 1 : *e++ = 'v';
112 1 : break;
113 1 : case '\\':
114 1 : *e++ = '\\';
115 1 : *e++ = '\\';
116 1 : break;
117 3 : default:
118 6 : snprintf(e, 5, "\\x%02X", *c);
119 3 : e += 4;
120 : }
121 11 : c++;
122 : }
123 : }
124 31900 : *e = '\0';
125 31900 : return encoded;
126 : }
|