2 Samba string escaping routines
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
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.
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.
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/>.
21 #include "lib/util/debug.h"
22 #include "lib/util/util_str_escape.h"
26 * Calculate the encoded length of a character for log_escape
29 static size_t encoded_length(unsigned char c
)
31 if (c
!= '\\' && c
> 0x1F) {
43 return 2; /* C escape sequence */
45 return 4; /* hex escape \xhh */
51 * Escape any control characters in the inputs to prevent them from
52 * interfering with the log output.
54 char *log_escape(TALLOC_CTX
*frame
, const char *in
)
56 size_t size
= 0; /* Space to allocate for the escaped data */
57 char *encoded
= NULL
; /* The encoded string */
65 /* Calculate the size required for the escaped array */
68 size
+= encoded_length( *c
);
73 encoded
= talloc_array( frame
, char, size
);
74 if (encoded
== NULL
) {
75 DBG_ERR( "Out of memory allocating encoded string\n");
82 if (*c
!= '\\' && (unsigned char)(*c
) > 0x1F) {
119 snprintf(e
, 5, "\\x%02hhX", (unsigned char)(*c
));