Coverage Report

Created: 2026-11-12 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata8/src/util-print.c
Line
Count
Source
1
/* Copyright (C) 2007-2023 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
/**
19
 * \file
20
 *
21
 * \author Victor Julien <victor@inliniac.net>
22
 *
23
 * Print utility functions
24
 */
25
26
#include "suricata-common.h"
27
#include "util-print.h"
28
#include "util-error.h"
29
#include "util-debug.h"
30
#include "util-validate.h"
31
#include "rust.h"
32
33
/**
34
 *  \brief print a buffer as hex on a single line
35
 *
36
 *  Prints in the format "00 AA BB"
37
 *
38
 *  \param nbuf buffer into which the output is written
39
 *  \param offset of where to start writing into the buffer
40
 *  \param max_size the size of the output buffer
41
 *  \param buf buffer to print from
42
 *  \param buflen length of the input buffer
43
 */
44
void PrintBufferRawLineHex(char *nbuf, int *offset, int max_size, const uint8_t *buf, uint32_t buflen)
45
2.74k
{
46
42.3k
    for (uint32_t u = 0; u < buflen; u++) {
47
39.6k
        PrintBufferData(nbuf, offset, max_size, "%02X ", buf[u]);
48
39.6k
    }
49
2.74k
}
50
51
/**
52
 *  \brief print a buffer as hex on a single line into retbuf buffer
53
 *
54
 *  Prints in the format "00 AA BB"
55
 *
56
 *  \param retbuf pointer to the buffer which will have the result
57
 *  \param rebuflen length of the buffer
58
 *  \param buf buffer to print from
59
 *  \param buflen length of the input buffer
60
 */
61
void PrintRawLineHexBuf(char *retbuf, uint32_t retbuflen, const uint8_t *buf, uint32_t buflen)
62
0
{
63
0
    uint32_t offset = 0;
64
0
    for (uint32_t u = 0; u < buflen; u++) {
65
0
        PrintBufferData(retbuf, &offset, retbuflen, "%02X ", buf[u]);
66
0
    }
67
0
}
68
69
void PrintRawUriFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
70
0
{
71
0
#define BUFFER_LENGTH 2048
72
0
    char nbuf[BUFFER_LENGTH] = "";
73
0
    uint32_t offset = 0;
74
75
0
    for (uint32_t u = 0; u < buflen; u++) {
76
0
        if (isprint(buf[u]) && buf[u] != '\"') {
77
0
            if (buf[u] == '\\') {
78
0
                PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
79
0
                                "\\\\");
80
0
            } else {
81
0
                PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
82
0
                                "%c", buf[u]);
83
0
            }
84
0
        } else {
85
0
            PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
86
0
                            "\\x%02X", buf[u]);
87
0
        }
88
0
    }
89
90
0
    fprintf(fp, "%s", nbuf);
91
0
}
92
93
void PrintRawUriBuf(
94
        char *retbuf, uint32_t *offset, uint32_t retbuflen, const uint8_t *buf, size_t buflen)
95
146k
{
96
8.30M
    for (size_t u = 0; u < buflen; u++) {
97
8.15M
        if (isprint(buf[u]) && buf[u] != '\"') {
98
4.37M
            if (buf[u] == '\\') {
99
16.0k
                PrintBufferData(retbuf, offset, retbuflen,
100
16.0k
                                "\\\\");
101
4.35M
            } else {
102
4.35M
                PrintBufferData(retbuf, offset, retbuflen,
103
4.35M
                                "%c", buf[u]);
104
4.35M
            }
105
4.37M
        } else {
106
3.78M
            PrintBufferData(retbuf, offset, retbuflen,
107
3.78M
                            "\\x%02X", buf[u]);
108
3.78M
        }
109
8.15M
    }
110
146k
}
111
112
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
113
0
{
114
0
    int ch = 0;
115
116
0
    if (buf == NULL) {
117
0
        fprintf(fp, " (null)\n");
118
0
        return;
119
0
    }
120
0
    for (uint32_t u = 0; u < buflen; u += 16) {
121
0
        fprintf(fp ," %04X  ", u);
122
0
        for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
123
0
             fprintf(fp, "%02X ", (uint8_t)buf[u+ch]);
124
125
0
             if (ch == 7) fprintf(fp, " ");
126
0
        }
127
0
        if (ch == 16) fprintf(fp, "  ");
128
0
        else if (ch < 8) {
129
0
            int spaces = (16 - ch) * 3 + 2 + 1;
130
0
            int s = 0;
131
0
            for ( ; s < spaces; s++) fprintf(fp, " ");
132
0
        } else if(ch < 16) {
133
0
            int spaces = (16 - ch) * 3 + 2;
134
0
            int s = 0;
135
0
            for ( ; s < spaces; s++) fprintf(fp, " ");
136
0
        }
137
138
0
        for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
139
0
             fprintf(fp, "%c", isprint((uint8_t)buf[u+ch]) ? (uint8_t)buf[u+ch] : '.');
140
141
0
             if (ch == 7)  fprintf(fp, " ");
142
0
             if (ch == 15) fprintf(fp, "\n");
143
0
        }
144
0
    }
145
0
    if (ch != 16)
146
0
        fprintf(fp, "\n");
147
0
}
148
149
void PrintRawDataToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
150
                          const uint8_t *src_buf, uint32_t src_buf_len)
151
0
{
152
0
    int ch = 0;
153
154
0
    for (uint32_t u = 0; u < src_buf_len; u += 16) {
155
0
        PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
156
0
                        " %04X  ", u);
157
0
        for (ch = 0; (u + ch) < src_buf_len && ch < 16; ch++) {
158
0
            PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
159
0
                            "%02X ", (uint8_t)src_buf[u + ch]);
160
161
0
            if (ch == 7) {
162
0
                PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
163
0
                                " ");
164
0
            }
165
0
        }
166
0
        if (ch == 16) {
167
0
            PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "  ");
168
0
        } else if (ch < 8) {
169
0
            int spaces = (16 - ch) * 3 + 2 + 1;
170
0
            int s = 0;
171
0
            for ( ; s < spaces; s++)
172
0
                PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
173
0
        } else if(ch < 16) {
174
0
            int spaces = (16 - ch) * 3 + 2;
175
0
            int s = 0;
176
0
            for ( ; s < spaces; s++)
177
0
                PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
178
0
        }
179
180
0
        for (ch = 0; (u+ch) < src_buf_len && ch < 16; ch++) {
181
0
            PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
182
0
                            "%c",
183
0
                            isprint((uint8_t)src_buf[u + ch]) ? (uint8_t)src_buf[u + ch] : '.');
184
185
0
             if (ch == 7)
186
0
                 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
187
0
             if (ch == 15)
188
0
                 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
189
0
        }
190
0
    }
191
0
    if (ch != 16)
192
0
        PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
193
0
}
194
195
void PrintStringsToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
196
                          const uint8_t *src_buf, const uint32_t src_buf_len)
197
0
{
198
0
    for (uint32_t ch = 0; ch < src_buf_len && *dst_buf_offset_ptr < dst_buf_size;
199
0
            ch++, (*dst_buf_offset_ptr)++) {
200
0
        if (isprint((uint8_t)src_buf[ch]) || src_buf[ch] == '\n' || src_buf[ch] == '\r') {
201
0
            dst_buf[*dst_buf_offset_ptr] = src_buf[ch];
202
0
        } else {
203
0
            dst_buf[*dst_buf_offset_ptr] = '.';
204
0
        }
205
0
    }
206
0
    dst_buf[dst_buf_size - 1] = 0;
207
0
}
208
209
static const char *PrintInetIPv6(const void *src, char *dst, socklen_t size)
210
10.2M
{
211
10.2M
    char s_part[6];
212
10.2M
    uint16_t x[8];
213
10.2M
    memcpy(&x, src, 16);
214
215
    /* current IPv6 format is fixed size */
216
10.2M
    if (size < 8 * 5) {
217
0
        SCLogWarning("Too small buffer to write IPv6 address");
218
0
        return NULL;
219
0
    }
220
10.2M
    memset(dst, 0, size);
221
91.8M
    for (int i = 0; i < 8; i++) {
222
81.6M
        snprintf(s_part, sizeof(s_part), "%04x:", htons(x[i]));
223
81.6M
        strlcat(dst, s_part, size);
224
81.6M
    }
225
    /* suppress last ':' */
226
10.2M
    dst[strlen(dst) - 1] = 0;
227
228
10.2M
    return dst;
229
10.2M
}
230
231
const char *PrintInet(int af, const void *src, char *dst, socklen_t size)
232
28.4M
{
233
28.4M
    switch (af) {
234
18.2M
        case AF_INET:
235
#if defined(OS_WIN32) && NTDDI_VERSION >= NTDDI_VISTA
236
{
237
            // because Windows has to provide a non-conformant inet_ntop, of
238
            // course!
239
            struct in_addr _src;
240
            memcpy(&_src, src, sizeof(struct in_addr));
241
            return inet_ntop(af, &_src, dst, size);
242
}
243
#else
244
18.2M
            return inet_ntop(af, src, dst, size);
245
0
#endif
246
10.2M
        case AF_INET6:
247
            /* Format IPv6 without deleting zeroes */
248
10.2M
            return PrintInetIPv6(src, dst, size);
249
0
        default:
250
0
            SCLogError("Unsupported protocol: %d", af);
251
28.4M
    }
252
0
    return NULL;
253
28.4M
}
254
255
void PrintHexString(char *str, size_t size, uint8_t *buf, size_t buf_len)
256
143k
{
257
143k
    DEBUG_VALIDATE_BUG_ON(size < 2 * buf_len);
258
143k
    SCToHex((uint8_t *)str, size, buf, buf_len);
259
143k
}