1 /* $NetBSD: print-ascii.c,v 1.1 1999/09/30 14:49:12 sjg Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Alan Barrett and Simon J. Gerraty.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 /* \summary: ASCII packet dump printer */
45 #include "netdissect-stdinc.h"
49 #include "netdissect-ctype.h"
51 #include "netdissect.h"
54 #define ASCII_LINELENGTH 300
55 #define HEXDUMP_BYTES_PER_LINE 16
56 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
57 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
58 #define HEXDUMP_HEXSTUFF_PER_LINE \
59 (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
62 ascii_print(netdissect_options
*ndo
,
63 const u_char
*cp
, u_int length
)
67 int truncated
= FALSE
;
69 ndo
->ndo_protocol
= "ascii";
70 caplength
= ND_BYTES_AVAILABLE_AFTER(cp
);
71 if (length
> caplength
) {
82 * Don't print CRs at the end of the line; they
83 * don't belong at the ends of lines on UN*X,
84 * and the standard I/O library will give us one
85 * on Windows so we don't need to print one
88 * In the middle of a line, just print a '.'.
90 if (length
> 1 && GET_U_1(cp
) != '\n')
93 if (!ND_ASCII_ISGRAPH(s
) &&
94 (s
!= '\t' && s
!= ' ' && s
!= '\n'))
101 nd_trunc_longjmp(ndo
);
105 hex_and_ascii_print_with_offset(netdissect_options
*ndo
, const char *indent
,
106 const u_char
*cp
, u_int length
, u_int offset
)
112 int truncated
= FALSE
;
113 char hexstuff
[HEXDUMP_SHORTS_PER_LINE
*HEXDUMP_HEXSTUFF_PER_SHORT
+1], *hsp
;
114 char asciistuff
[ASCII_LINELENGTH
+1], *asp
;
116 caplength
= ND_BYTES_AVAILABLE_AFTER(cp
);
117 if (length
> caplength
) {
121 nshorts
= length
/ sizeof(u_short
);
123 hsp
= hexstuff
; asp
= asciistuff
;
124 while (nshorts
!= 0) {
129 (void)snprintf(hsp
, sizeof(hexstuff
) - (hsp
- hexstuff
),
130 " %02x%02x", s1
, s2
);
131 hsp
+= HEXDUMP_HEXSTUFF_PER_SHORT
;
132 *(asp
++) = (char)(ND_ASCII_ISGRAPH(s1
) ? s1
: '.');
133 *(asp
++) = (char)(ND_ASCII_ISGRAPH(s2
) ? s2
: '.');
135 if (i
>= HEXDUMP_SHORTS_PER_LINE
) {
137 ND_PRINT("%s0x%04x: %-*s %s",
138 indent
, offset
, HEXDUMP_HEXSTUFF_PER_LINE
,
139 hexstuff
, asciistuff
);
140 i
= 0; hsp
= hexstuff
; asp
= asciistuff
;
141 offset
+= HEXDUMP_BYTES_PER_LINE
;
148 (void)snprintf(hsp
, sizeof(hexstuff
) - (hsp
- hexstuff
),
151 *(asp
++) = (char)(ND_ASCII_ISGRAPH(s1
) ? s1
: '.');
156 ND_PRINT("%s0x%04x: %-*s %s",
157 indent
, offset
, HEXDUMP_HEXSTUFF_PER_LINE
,
158 hexstuff
, asciistuff
);
161 nd_trunc_longjmp(ndo
);
165 hex_and_ascii_print(netdissect_options
*ndo
, const char *indent
,
166 const u_char
*cp
, u_int length
)
168 hex_and_ascii_print_with_offset(ndo
, indent
, cp
, length
, 0);
172 * telnet_print() wants this. It is essentially default_print_unaligned()
175 hex_print_with_offset(netdissect_options
*ndo
,
176 const char *indent
, const u_char
*cp
, u_int length
,
182 int truncated
= FALSE
;
184 caplength
= ND_BYTES_AVAILABLE_AFTER(cp
);
185 if (length
> caplength
) {
189 nshorts
= length
/ sizeof(u_short
);
191 while (nshorts
!= 0) {
192 if ((i
++ % 8) == 0) {
193 ND_PRINT("%s0x%04x: ", indent
, offset
);
194 offset
+= HEXDUMP_BYTES_PER_LINE
;
198 ND_PRINT(" %02x%02x", s
, GET_U_1(cp
));
204 ND_PRINT("%s0x%04x: ", indent
, offset
);
205 ND_PRINT(" %02x", GET_U_1(cp
));
208 nd_trunc_longjmp(ndo
);
212 hex_print(netdissect_options
*ndo
,
213 const char *indent
, const u_char
*cp
, u_int length
)
215 hex_print_with_offset(ndo
, indent
, cp
, length
, 0);