xref: /freebsd/contrib/tcpdump/print-ascii.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1b0453382SBill Fenner /*	$NetBSD: print-ascii.c,v 1.1 1999/09/30 14:49:12 sjg Exp $	*/
2b0453382SBill Fenner 
3b0453382SBill Fenner /*-
4b0453382SBill Fenner  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5b0453382SBill Fenner  * All rights reserved.
6b0453382SBill Fenner  *
7b0453382SBill Fenner  * This code is derived from software contributed to The NetBSD Foundation
8b0453382SBill Fenner  * by Alan Barrett and Simon J. Gerraty.
9b0453382SBill Fenner  *
10b0453382SBill Fenner  * Redistribution and use in source and binary forms, with or without
11b0453382SBill Fenner  * modification, are permitted provided that the following conditions
12b0453382SBill Fenner  * are met:
13b0453382SBill Fenner  * 1. Redistributions of source code must retain the above copyright
14b0453382SBill Fenner  *    notice, this list of conditions and the following disclaimer.
15b0453382SBill Fenner  * 2. Redistributions in binary form must reproduce the above copyright
16b0453382SBill Fenner  *    notice, this list of conditions and the following disclaimer in the
17b0453382SBill Fenner  *    documentation and/or other materials provided with the distribution.
18b0453382SBill Fenner  * 3. All advertising materials mentioning features or use of this software
19b0453382SBill Fenner  *    must display the following acknowledgement:
20b0453382SBill Fenner  *        This product includes software developed by the NetBSD
21b0453382SBill Fenner  *        Foundation, Inc. and its contributors.
22b0453382SBill Fenner  * 4. Neither the name of The NetBSD Foundation nor the names of its
23b0453382SBill Fenner  *    contributors may be used to endorse or promote products derived
24b0453382SBill Fenner  *    from this software without specific prior written permission.
25b0453382SBill Fenner  *
26b0453382SBill Fenner  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27b0453382SBill Fenner  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28b0453382SBill Fenner  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29b0453382SBill Fenner  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30b0453382SBill Fenner  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31b0453382SBill Fenner  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32b0453382SBill Fenner  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33b0453382SBill Fenner  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34b0453382SBill Fenner  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35b0453382SBill Fenner  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36b0453382SBill Fenner  * POSSIBILITY OF SUCH DAMAGE.
37b0453382SBill Fenner  */
38b0453382SBill Fenner 
393340d773SGleb Smirnoff /* \summary: ASCII packet dump printer */
403340d773SGleb Smirnoff 
41ee67461eSJoseph Mingrone #include <config.h>
42b0453382SBill Fenner 
43ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
44ee67461eSJoseph Mingrone 
45b0453382SBill Fenner #include <stdio.h>
46b0453382SBill Fenner 
47ee67461eSJoseph Mingrone #include "netdissect-ctype.h"
48ee67461eSJoseph Mingrone 
493340d773SGleb Smirnoff #include "netdissect.h"
50ee67461eSJoseph Mingrone #include "extract.h"
51b0453382SBill Fenner 
525b0fe478SBruce M Simpson #define ASCII_LINELENGTH 300
53b0453382SBill Fenner #define HEXDUMP_BYTES_PER_LINE 16
54b0453382SBill Fenner #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
55b0453382SBill Fenner #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
56b0453382SBill Fenner #define HEXDUMP_HEXSTUFF_PER_LINE \
57b0453382SBill Fenner 		(HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
58b0453382SBill Fenner 
59b0453382SBill Fenner void
ascii_print(netdissect_options * ndo,const u_char * cp,u_int length)603c602fabSXin LI ascii_print(netdissect_options *ndo,
613c602fabSXin LI             const u_char *cp, u_int length)
622ebc47dbSSam Leffler {
638bdc5a62SPatrick Kelsey 	u_int caplength;
64ee67461eSJoseph Mingrone 	u_char s;
65ee67461eSJoseph Mingrone 	int truncated = FALSE;
662ebc47dbSSam Leffler 
67ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "ascii";
68*0a7e5f1fSJoseph Mingrone 	caplength = ND_BYTES_AVAILABLE_AFTER(cp);
69ee67461eSJoseph Mingrone 	if (length > caplength) {
708bdc5a62SPatrick Kelsey 		length = caplength;
71ee67461eSJoseph Mingrone 		truncated = TRUE;
72ee67461eSJoseph Mingrone 	}
73ee67461eSJoseph Mingrone 	ND_PRINT("\n");
742ebc47dbSSam Leffler 	while (length > 0) {
75ee67461eSJoseph Mingrone 		s = GET_U_1(cp);
76ee67461eSJoseph Mingrone 		cp++;
772ebc47dbSSam Leffler 		length--;
783c602fabSXin LI 		if (s == '\r') {
793c602fabSXin LI 			/*
803c602fabSXin LI 			 * Don't print CRs at the end of the line; they
813c602fabSXin LI 			 * don't belong at the ends of lines on UN*X,
823c602fabSXin LI 			 * and the standard I/O library will give us one
833c602fabSXin LI 			 * on Windows so we don't need to print one
843c602fabSXin LI 			 * ourselves.
853c602fabSXin LI 			 *
863c602fabSXin LI 			 * In the middle of a line, just print a '.'.
873c602fabSXin LI 			 */
88ee67461eSJoseph Mingrone 			if (length > 1 && GET_U_1(cp) != '\n')
89ee67461eSJoseph Mingrone 				ND_PRINT(".");
903c602fabSXin LI 		} else {
91ee67461eSJoseph Mingrone 			if (!ND_ASCII_ISGRAPH(s) &&
923c602fabSXin LI 			    (s != '\t' && s != ' ' && s != '\n'))
93ee67461eSJoseph Mingrone 				ND_PRINT(".");
942ebc47dbSSam Leffler 			else
95ee67461eSJoseph Mingrone 				ND_PRINT("%c", s);
963c602fabSXin LI 		}
972ebc47dbSSam Leffler 	}
98ee67461eSJoseph Mingrone 	if (truncated)
99ee67461eSJoseph Mingrone 		nd_trunc_longjmp(ndo);
1002ebc47dbSSam Leffler }
1012ebc47dbSSam Leffler 
102ee67461eSJoseph Mingrone static void
hex_and_ascii_print_with_offset(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length,u_int oset)103ee67461eSJoseph Mingrone hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *ident,
104ee67461eSJoseph Mingrone     const u_char *cp, u_int length, u_int oset)
105b0453382SBill Fenner {
1068bdc5a62SPatrick Kelsey 	u_int caplength;
107ee67461eSJoseph Mingrone 	u_int i;
108ee67461eSJoseph Mingrone 	u_int s1, s2;
109ee67461eSJoseph Mingrone 	u_int nshorts;
110ee67461eSJoseph Mingrone 	int truncated = FALSE;
111b0453382SBill Fenner 	char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
1125b0fe478SBruce M Simpson 	char asciistuff[ASCII_LINELENGTH+1], *asp;
113b0453382SBill Fenner 
114*0a7e5f1fSJoseph Mingrone 	caplength = ND_BYTES_AVAILABLE_AFTER(cp);
115ee67461eSJoseph Mingrone 	if (length > caplength) {
1168bdc5a62SPatrick Kelsey 		length = caplength;
117ee67461eSJoseph Mingrone 		truncated = TRUE;
118ee67461eSJoseph Mingrone 	}
119b0453382SBill Fenner 	nshorts = length / sizeof(u_short);
120b0453382SBill Fenner 	i = 0;
121b0453382SBill Fenner 	hsp = hexstuff; asp = asciistuff;
122ee67461eSJoseph Mingrone 	while (nshorts != 0) {
123ee67461eSJoseph Mingrone 		s1 = GET_U_1(cp);
124ee67461eSJoseph Mingrone 		cp++;
125ee67461eSJoseph Mingrone 		s2 = GET_U_1(cp);
126ee67461eSJoseph Mingrone 		cp++;
127685295f4SBill Fenner 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
128685295f4SBill Fenner 		    " %02x%02x", s1, s2);
129b0453382SBill Fenner 		hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
130ee67461eSJoseph Mingrone 		*(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
131ee67461eSJoseph Mingrone 		*(asp++) = (char)(ND_ASCII_ISGRAPH(s2) ? s2 : '.');
1325b0fe478SBruce M Simpson 		i++;
1332ebc47dbSSam Leffler 		if (i >= HEXDUMP_SHORTS_PER_LINE) {
134b0453382SBill Fenner 			*hsp = *asp = '\0';
135ee67461eSJoseph Mingrone 			ND_PRINT("%s0x%04x: %-*s  %s",
1365b0fe478SBruce M Simpson 			    ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
137ee67461eSJoseph Mingrone 			    hexstuff, asciistuff);
138b0453382SBill Fenner 			i = 0; hsp = hexstuff; asp = asciistuff;
139b0453382SBill Fenner 			oset += HEXDUMP_BYTES_PER_LINE;
140b0453382SBill Fenner 		}
141ee67461eSJoseph Mingrone 		nshorts--;
142b0453382SBill Fenner 	}
143b0453382SBill Fenner 	if (length & 1) {
144ee67461eSJoseph Mingrone 		s1 = GET_U_1(cp);
145ee67461eSJoseph Mingrone 		cp++;
146685295f4SBill Fenner 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
147685295f4SBill Fenner 		    " %02x", s1);
148b0453382SBill Fenner 		hsp += 3;
149ee67461eSJoseph Mingrone 		*(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
150b0453382SBill Fenner 		++i;
151b0453382SBill Fenner 	}
152b0453382SBill Fenner 	if (i > 0) {
153b0453382SBill Fenner 		*hsp = *asp = '\0';
154ee67461eSJoseph Mingrone 		ND_PRINT("%s0x%04x: %-*s  %s",
1555b0fe478SBruce M Simpson 		     ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
156ee67461eSJoseph Mingrone 		     hexstuff, asciistuff);
157b0453382SBill Fenner 	}
158ee67461eSJoseph Mingrone 	if (truncated)
159ee67461eSJoseph Mingrone 		nd_trunc_longjmp(ndo);
160b0453382SBill Fenner }
161b0453382SBill Fenner 
162b0453382SBill Fenner void
hex_and_ascii_print(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length)163ee67461eSJoseph Mingrone hex_and_ascii_print(netdissect_options *ndo, const char *ident,
164ee67461eSJoseph Mingrone     const u_char *cp, u_int length)
165b0453382SBill Fenner {
1663c602fabSXin LI 	hex_and_ascii_print_with_offset(ndo, ident, cp, length, 0);
167b0453382SBill Fenner }
168b0453382SBill Fenner 
169b0453382SBill Fenner /*
170b0453382SBill Fenner  * telnet_print() wants this.  It is essentially default_print_unaligned()
171b0453382SBill Fenner  */
172b0453382SBill Fenner void
hex_print_with_offset(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length,u_int oset)1733c602fabSXin LI hex_print_with_offset(netdissect_options *ndo,
1743c602fabSXin LI                       const char *ident, const u_char *cp, u_int length,
1753c602fabSXin LI 		      u_int oset)
176b0453382SBill Fenner {
1778bdc5a62SPatrick Kelsey 	u_int caplength;
178ee67461eSJoseph Mingrone 	u_int i, s;
179ee67461eSJoseph Mingrone 	u_int nshorts;
180ee67461eSJoseph Mingrone 	int truncated = FALSE;
181b0453382SBill Fenner 
182*0a7e5f1fSJoseph Mingrone 	caplength = ND_BYTES_AVAILABLE_AFTER(cp);
183ee67461eSJoseph Mingrone 	if (length > caplength) {
1848bdc5a62SPatrick Kelsey 		length = caplength;
185ee67461eSJoseph Mingrone 		truncated = TRUE;
186ee67461eSJoseph Mingrone 	}
187ee67461eSJoseph Mingrone 	nshorts = length / sizeof(u_short);
188b0453382SBill Fenner 	i = 0;
189ee67461eSJoseph Mingrone 	while (nshorts != 0) {
190b0453382SBill Fenner 		if ((i++ % 8) == 0) {
191ee67461eSJoseph Mingrone 			ND_PRINT("%s0x%04x: ", ident, oset);
192b0453382SBill Fenner 			oset += HEXDUMP_BYTES_PER_LINE;
193b0453382SBill Fenner 		}
194ee67461eSJoseph Mingrone 		s = GET_U_1(cp);
195ee67461eSJoseph Mingrone 		cp++;
196ee67461eSJoseph Mingrone 		ND_PRINT(" %02x%02x", s, GET_U_1(cp));
197ee67461eSJoseph Mingrone 		cp++;
198ee67461eSJoseph Mingrone 		nshorts--;
199b0453382SBill Fenner 	}
200b0453382SBill Fenner 	if (length & 1) {
201b0453382SBill Fenner 		if ((i % 8) == 0)
202ee67461eSJoseph Mingrone 			ND_PRINT("%s0x%04x: ", ident, oset);
203ee67461eSJoseph Mingrone 		ND_PRINT(" %02x", GET_U_1(cp));
204b0453382SBill Fenner 	}
205ee67461eSJoseph Mingrone 	if (truncated)
206ee67461eSJoseph Mingrone 		nd_trunc_longjmp(ndo);
207b0453382SBill Fenner }
208b0453382SBill Fenner 
209b0453382SBill Fenner void
hex_print(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length)210ee67461eSJoseph Mingrone hex_print(netdissect_options *ndo,
211ee67461eSJoseph Mingrone 	  const char *ident, const u_char *cp, u_int length)
212b0453382SBill Fenner {
2133c602fabSXin LI 	hex_print_with_offset(ndo, ident, cp, length, 0);
214b0453382SBill Fenner }
215b0453382SBill Fenner 
216b0453382SBill Fenner #ifdef MAIN
217b0453382SBill Fenner int
main(int argc,char * argv[])218b0453382SBill Fenner main(int argc, char *argv[])
219b0453382SBill Fenner {
2202ebc47dbSSam Leffler 	hex_print("\n\t", "Hello, World!\n", 14);
2212ebc47dbSSam Leffler 	printf("\n");
2222ebc47dbSSam Leffler 	hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
223b0453382SBill Fenner 	printf("\n");
224b0453382SBill Fenner 	ascii_print("Hello, World!\n", 14);
225b0453382SBill Fenner 	printf("\n");
226b0453382SBill Fenner #define TMSG "Now is the winter of our discontent...\n"
2272ebc47dbSSam Leffler 	hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
2282ebc47dbSSam Leffler 	printf("\n");
2292ebc47dbSSam Leffler 	hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
230b0453382SBill Fenner 	printf("\n");
231b0453382SBill Fenner 	exit(0);
232b0453382SBill Fenner }
233b0453382SBill Fenner #endif /* MAIN */
234