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/util_str_escape.h"
25 * Calculate the encoded length of a character for log_escape
28 static size_t encoded_length(char c)
30 if (c != '\\' && c > 0x1F) {
42 return 2; /* C escape sequence */
44 return 4; /* hex escape \xhh */
50 * Escape any control characters in the inputs to prevent them from
51 * interfering with the log output.
53 char *log_escape(TALLOC_CTX *frame, const char *in)
55 size_t size = 0; /* Space to allocate for the escaped data */
56 char *encoded = NULL; /* The encoded string */
64 /* Calculate the size required for the escaped array */
67 size += encoded_length( *c);
72 encoded = talloc_array( frame, char, size);
73 if (encoded == NULL) {
74 DBG_ERR( "Out of memory allocating encoded string");
81 if (*c != '\\' && *c > 0x1F) {
118 snprintf(e, 5, "\\x%02X", *c);