Debian Codegraph @

src/trace.c

raw
1 /*
2 * Author Jerry Lundström <[email protected]>
3 * Copyright (c) 2019-2026 OARC, Inc.
4 * All rights reserved.
5 *
6 * This file is part of the dnswire library.
7 *
8 * dnswire library is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * dnswire library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23
24 #include "dnswire/trace.h"
25
26 #if DNSWIRE_TRACE
27 const char* __printable_string(const uint8_t* data, size_t len)
28 {
29 static char buf[512];
30 size_t r = 0, w = 0;
31
32 while (r < len && w < sizeof(buf) - 1) {
33 if (isprint(data[r])) {
34 buf[w++] = data[r++];
35 } else {
36 if (w + 4 >= sizeof(buf) - 1) {
37 break;
38 }
39
40 sprintf(&buf[w], "\\x%02x", data[r++]);
41 w += 4;
42 }
43 }
44 if (w >= sizeof(buf)) {
45 buf[sizeof(buf) - 1] = 0;
46 } else {
47 buf[w] = 0;
48 }
49
50 return buf;
51 }
52 #endif