]> git.ipfire.org Git - people/ms/suricata.git/blob - src/util-print.c
core: Remove unneeded consts
[people/ms/suricata.git] / src / util-print.c
1 /* Copyright (C) 2007-2010 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
31 /**
32 * \brief print a buffer as hex on a single line
33 *
34 * Prints in the format "00 AA BB"
35 *
36 * \param nbuf buffer into which the output is written
37 * \param offset of where to start writting into the buffer
38 * \param max_size the size of the output buffer
39 * \param buf buffer to print from
40 * \param buflen length of the input buffer
41 */
42 void PrintBufferRawLineHex(char *nbuf, int *offset, int max_size, const uint8_t *buf, uint32_t buflen)
43 {
44 uint32_t u = 0;
45
46 for (u = 0; u < buflen; u++) {
47 PrintBufferData(nbuf, offset, max_size, "%02X ", buf[u]);
48 }
49 }
50
51 /**
52 * \brief print a buffer as hex on a single line in to 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 lenght 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 {
63 uint32_t offset = 0;
64 uint32_t u = 0;
65
66 for (u = 0; u < buflen; u++) {
67 PrintBufferData(retbuf, &offset, retbuflen, "%02X ", buf[u]);
68 }
69 }
70
71 void PrintRawJsonFp(FILE *fp, uint8_t *buf, uint32_t buflen)
72 {
73 #define BUFFER_LENGTH 2048
74 char nbuf[BUFFER_LENGTH] = "";
75 uint32_t offset = 0;
76 uint32_t u = 0;
77
78 for (u = 0; u < buflen; u++) {
79 if (buf[u] == '\\' || buf[u] == '/' || buf[u] == '\"') {
80 PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
81 "\\%c", buf[u]);
82 } else if (isprint(buf[u])) {
83 PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
84 "%c", buf[u]);
85 } else {
86 PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
87 "\\\\x%02X", buf[u]);
88 }
89 }
90 fprintf(fp, "%s", nbuf);
91 }
92
93 void PrintRawUriFp(FILE *fp, uint8_t *buf, uint32_t buflen)
94 {
95 #define BUFFER_LENGTH 2048
96 char nbuf[BUFFER_LENGTH] = "";
97 uint32_t offset = 0;
98 uint32_t u = 0;
99
100 for (u = 0; u < buflen; u++) {
101 if (isprint(buf[u]) && buf[u] != '\"') {
102 if (buf[u] == '\\') {
103 PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
104 "\\\\");
105 } else {
106 PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
107 "%c", buf[u]);
108 }
109 } else {
110 PrintBufferData(nbuf, &offset, BUFFER_LENGTH,
111 "\\x%02X", buf[u]);
112 }
113 }
114
115 fprintf(fp, "%s", nbuf);
116 }
117
118 void PrintRawUriBuf(char *retbuf, uint32_t *offset, uint32_t retbuflen,
119 uint8_t *buf, uint32_t buflen)
120 {
121 uint32_t u = 0;
122
123 for (u = 0; u < buflen; u++) {
124 if (isprint(buf[u]) && buf[u] != '\"') {
125 if (buf[u] == '\\') {
126 PrintBufferData(retbuf, offset, retbuflen,
127 "\\\\");
128 } else {
129 PrintBufferData(retbuf, offset, retbuflen,
130 "%c", buf[u]);
131 }
132 } else {
133 PrintBufferData(retbuf, offset, retbuflen,
134 "\\x%02X", buf[u]);
135 }
136 }
137
138 return;
139 }
140
141 void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
142 {
143 int ch = 0;
144 uint32_t u = 0;
145
146 if (buf == NULL) {
147 fprintf(fp, " (null)\n");
148 return;
149 }
150 for (u = 0; u < buflen; u+=16) {
151 fprintf(fp ," %04X ", u);
152 for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
153 fprintf(fp, "%02X ", (uint8_t)buf[u+ch]);
154
155 if (ch == 7) fprintf(fp, " ");
156 }
157 if (ch == 16) fprintf(fp, " ");
158 else if (ch < 8) {
159 int spaces = (16 - ch) * 3 + 2 + 1;
160 int s = 0;
161 for ( ; s < spaces; s++) fprintf(fp, " ");
162 } else if(ch < 16) {
163 int spaces = (16 - ch) * 3 + 2;
164 int s = 0;
165 for ( ; s < spaces; s++) fprintf(fp, " ");
166 }
167
168 for (ch = 0; (u+ch) < buflen && ch < 16; ch++) {
169 fprintf(fp, "%c", isprint((uint8_t)buf[u+ch]) ? (uint8_t)buf[u+ch] : '.');
170
171 if (ch == 7) fprintf(fp, " ");
172 if (ch == 15) fprintf(fp, "\n");
173 }
174 }
175 if (ch != 16)
176 fprintf(fp, "\n");
177 }
178
179 void PrintRawDataToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
180 const uint8_t *src_buf, uint32_t src_buf_len)
181 {
182 int ch = 0;
183 uint32_t u = 0;
184
185 for (u = 0; u < src_buf_len; u+=16) {
186 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
187 " %04X ", u);
188 for (ch = 0; (u + ch) < src_buf_len && ch < 16; ch++) {
189 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
190 "%02X ", (uint8_t)src_buf[u + ch]);
191
192 if (ch == 7) {
193 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
194 " ");
195 }
196 }
197 if (ch == 16) {
198 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
199 } else if (ch < 8) {
200 int spaces = (16 - ch) * 3 + 2 + 1;
201 int s = 0;
202 for ( ; s < spaces; s++)
203 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
204 } else if(ch < 16) {
205 int spaces = (16 - ch) * 3 + 2;
206 int s = 0;
207 for ( ; s < spaces; s++)
208 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
209 }
210
211 for (ch = 0; (u+ch) < src_buf_len && ch < 16; ch++) {
212 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size,
213 "%c",
214 isprint((uint8_t)src_buf[u + ch]) ? (uint8_t)src_buf[u + ch] : '.');
215
216 if (ch == 7)
217 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, " ");
218 if (ch == 15)
219 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
220 }
221 }
222 if (ch != 16)
223 PrintBufferData((char *)dst_buf, dst_buf_offset_ptr, dst_buf_size, "\n");
224
225 return;
226 }
227
228 void PrintStringsToBuffer(uint8_t *dst_buf, uint32_t *dst_buf_offset_ptr, uint32_t dst_buf_size,
229 const uint8_t *src_buf, const uint32_t src_buf_len)
230 {
231 uint32_t ch = 0;
232 for (ch = 0; ch < src_buf_len && *dst_buf_offset_ptr < dst_buf_size;
233 ch++, (*dst_buf_offset_ptr)++) {
234 if (isprint((uint8_t)src_buf[ch]) || src_buf[ch] == '\n' || src_buf[ch] == '\r') {
235 dst_buf[*dst_buf_offset_ptr] = src_buf[ch];
236 } else {
237 dst_buf[*dst_buf_offset_ptr] = '.';
238 }
239 }
240 dst_buf[dst_buf_size - 1] = 0;
241
242 return;
243 }
244
245 #ifndef s6_addr16
246 # define s6_addr16 __u6_addr.__u6_addr16
247 #endif
248
249 static const char *PrintInetIPv6(const void *src, char *dst, socklen_t size)
250 {
251 int i;
252 char s_part[6];
253 uint16_t x[8];
254 memcpy(&x, src, 16);
255
256 /* current IPv6 format is fixed size */
257 if (size < 8 * 5) {
258 SCLogWarning(SC_ERR_ARG_LEN_LONG, "Too small buffer to write IPv6 address");
259 return NULL;
260 }
261 memset(dst, 0, size);
262 for(i = 0; i < 8; i++) {
263 snprintf(s_part, sizeof(s_part), "%04x:", htons(x[i]));
264 strlcat(dst, s_part, size);
265 }
266 /* suppress last ':' */
267 dst[strlen(dst) - 1] = 0;
268
269 return dst;
270 }
271
272 const char *PrintInet(int af, const void *src, char *dst, socklen_t size)
273 {
274 switch (af) {
275 case AF_INET:
276 #if defined(OS_WIN32) && NTDDI_VERSION >= NTDDI_VISTA
277 {
278 // because Windows has to provide a non-conformant inet_ntop, of
279 // course!
280 struct in_addr _src;
281 memcpy(&_src, src, sizeof(struct in_addr));
282 return inet_ntop(af, &_src, dst, size);
283 }
284 #else
285 return inet_ntop(af, src, dst, size);
286 #endif
287 case AF_INET6:
288 /* Format IPv6 without deleting zeroes */
289 return PrintInetIPv6(src, dst, size);
290 default:
291 SCLogError(SC_ERR_INVALID_VALUE, "Unsupported protocol: %d", af);
292 }
293 return NULL;
294 }