xref: /dflybsd-src/contrib/tcpdump/print-ascii.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
141c99275SPeter Avalos /*	$NetBSD: print-ascii.c,v 1.1 1999/09/30 14:49:12 sjg Exp $ 	*/
241c99275SPeter Avalos 
341c99275SPeter Avalos /*-
441c99275SPeter Avalos  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
541c99275SPeter Avalos  * All rights reserved.
641c99275SPeter Avalos  *
741c99275SPeter Avalos  * This code is derived from software contributed to The NetBSD Foundation
841c99275SPeter Avalos  * by Alan Barrett and Simon J. Gerraty.
941c99275SPeter Avalos  *
1041c99275SPeter Avalos  * Redistribution and use in source and binary forms, with or without
1141c99275SPeter Avalos  * modification, are permitted provided that the following conditions
1241c99275SPeter Avalos  * are met:
1341c99275SPeter Avalos  * 1. Redistributions of source code must retain the above copyright
1441c99275SPeter Avalos  *    notice, this list of conditions and the following disclaimer.
1541c99275SPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1641c99275SPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1741c99275SPeter Avalos  *    documentation and/or other materials provided with the distribution.
1841c99275SPeter Avalos  * 3. All advertising materials mentioning features or use of this software
1941c99275SPeter Avalos  *    must display the following acknowledgement:
2041c99275SPeter Avalos  *        This product includes software developed by the NetBSD
2141c99275SPeter Avalos  *        Foundation, Inc. and its contributors.
2241c99275SPeter Avalos  * 4. Neither the name of The NetBSD Foundation nor the names of its
2341c99275SPeter Avalos  *    contributors may be used to endorse or promote products derived
2441c99275SPeter Avalos  *    from this software without specific prior written permission.
2541c99275SPeter Avalos  *
2641c99275SPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2741c99275SPeter Avalos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2841c99275SPeter Avalos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2941c99275SPeter Avalos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3041c99275SPeter Avalos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3141c99275SPeter Avalos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3241c99275SPeter Avalos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3341c99275SPeter Avalos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3441c99275SPeter Avalos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3541c99275SPeter Avalos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3641c99275SPeter Avalos  * POSSIBILITY OF SUCH DAMAGE.
3741c99275SPeter Avalos  */
3841c99275SPeter Avalos 
39411677aeSAaron LI /* \summary: ASCII packet dump printer */
40411677aeSAaron LI 
4141c99275SPeter Avalos #ifdef HAVE_CONFIG_H
42*ed775ee7SAntonio Huete Jimenez #include <config.h>
4341c99275SPeter Avalos #endif
4441c99275SPeter Avalos 
45*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
46*ed775ee7SAntonio Huete Jimenez 
4741c99275SPeter Avalos #include <stdio.h>
4841c99275SPeter Avalos 
49*ed775ee7SAntonio Huete Jimenez #include "netdissect-ctype.h"
50*ed775ee7SAntonio Huete Jimenez 
51411677aeSAaron LI #include "netdissect.h"
52*ed775ee7SAntonio Huete Jimenez #include "extract.h"
5341c99275SPeter Avalos 
5441c99275SPeter Avalos #define ASCII_LINELENGTH 300
5541c99275SPeter Avalos #define HEXDUMP_BYTES_PER_LINE 16
5641c99275SPeter Avalos #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
5741c99275SPeter Avalos #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
5841c99275SPeter Avalos #define HEXDUMP_HEXSTUFF_PER_LINE \
5941c99275SPeter Avalos 		(HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
6041c99275SPeter Avalos 
6141c99275SPeter Avalos void
ascii_print(netdissect_options * ndo,const u_char * cp,u_int length)62411677aeSAaron LI ascii_print(netdissect_options *ndo,
63411677aeSAaron LI             const u_char *cp, u_int length)
6441c99275SPeter Avalos {
65411677aeSAaron LI 	u_int caplength;
66*ed775ee7SAntonio Huete Jimenez 	u_char s;
67*ed775ee7SAntonio Huete Jimenez 	int truncated = FALSE;
6841c99275SPeter Avalos 
69*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "ascii";
70*ed775ee7SAntonio Huete Jimenez 	caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
71*ed775ee7SAntonio Huete Jimenez 	if (length > caplength) {
72411677aeSAaron LI 		length = caplength;
73*ed775ee7SAntonio Huete Jimenez 		truncated = TRUE;
74*ed775ee7SAntonio Huete Jimenez 	}
75*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("\n");
7641c99275SPeter Avalos 	while (length > 0) {
77*ed775ee7SAntonio Huete Jimenez 		s = GET_U_1(cp);
78*ed775ee7SAntonio Huete Jimenez 		cp++;
7941c99275SPeter Avalos 		length--;
80411677aeSAaron LI 		if (s == '\r') {
81411677aeSAaron LI 			/*
82411677aeSAaron LI 			 * Don't print CRs at the end of the line; they
83411677aeSAaron LI 			 * don't belong at the ends of lines on UN*X,
84411677aeSAaron LI 			 * and the standard I/O library will give us one
85411677aeSAaron LI 			 * on Windows so we don't need to print one
86411677aeSAaron LI 			 * ourselves.
87411677aeSAaron LI 			 *
88411677aeSAaron LI 			 * In the middle of a line, just print a '.'.
89411677aeSAaron LI 			 */
90*ed775ee7SAntonio Huete Jimenez 			if (length > 1 && GET_U_1(cp) != '\n')
91*ed775ee7SAntonio Huete Jimenez 				ND_PRINT(".");
92411677aeSAaron LI 		} else {
93*ed775ee7SAntonio Huete Jimenez 			if (!ND_ASCII_ISGRAPH(s) &&
94411677aeSAaron LI 			    (s != '\t' && s != ' ' && s != '\n'))
95*ed775ee7SAntonio Huete Jimenez 				ND_PRINT(".");
9641c99275SPeter Avalos 			else
97*ed775ee7SAntonio Huete Jimenez 				ND_PRINT("%c", s);
98411677aeSAaron LI 		}
9941c99275SPeter Avalos 	}
100*ed775ee7SAntonio Huete Jimenez 	if (truncated)
101*ed775ee7SAntonio Huete Jimenez 		nd_trunc_longjmp(ndo);
10241c99275SPeter Avalos }
10341c99275SPeter Avalos 
104*ed775ee7SAntonio Huete Jimenez static void
hex_and_ascii_print_with_offset(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length,u_int oset)105*ed775ee7SAntonio Huete Jimenez hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *ident,
106*ed775ee7SAntonio Huete Jimenez     const u_char *cp, u_int length, u_int oset)
10741c99275SPeter Avalos {
108411677aeSAaron LI 	u_int caplength;
109*ed775ee7SAntonio Huete Jimenez 	u_int i;
110*ed775ee7SAntonio Huete Jimenez 	u_int s1, s2;
111*ed775ee7SAntonio Huete Jimenez 	u_int nshorts;
112*ed775ee7SAntonio Huete Jimenez 	int truncated = FALSE;
11341c99275SPeter Avalos 	char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
11441c99275SPeter Avalos 	char asciistuff[ASCII_LINELENGTH+1], *asp;
11541c99275SPeter Avalos 
116*ed775ee7SAntonio Huete Jimenez 	caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
117*ed775ee7SAntonio Huete Jimenez 	if (length > caplength) {
118411677aeSAaron LI 		length = caplength;
119*ed775ee7SAntonio Huete Jimenez 		truncated = TRUE;
120*ed775ee7SAntonio Huete Jimenez 	}
12141c99275SPeter Avalos 	nshorts = length / sizeof(u_short);
12241c99275SPeter Avalos 	i = 0;
12341c99275SPeter Avalos 	hsp = hexstuff; asp = asciistuff;
124*ed775ee7SAntonio Huete Jimenez 	while (nshorts != 0) {
125*ed775ee7SAntonio Huete Jimenez 		s1 = GET_U_1(cp);
126*ed775ee7SAntonio Huete Jimenez 		cp++;
127*ed775ee7SAntonio Huete Jimenez 		s2 = GET_U_1(cp);
128*ed775ee7SAntonio Huete Jimenez 		cp++;
12941c99275SPeter Avalos 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
13041c99275SPeter Avalos 		    " %02x%02x", s1, s2);
13141c99275SPeter Avalos 		hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
132*ed775ee7SAntonio Huete Jimenez 		*(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
133*ed775ee7SAntonio Huete Jimenez 		*(asp++) = (char)(ND_ASCII_ISGRAPH(s2) ? s2 : '.');
13441c99275SPeter Avalos 		i++;
13541c99275SPeter Avalos 		if (i >= HEXDUMP_SHORTS_PER_LINE) {
13641c99275SPeter Avalos 			*hsp = *asp = '\0';
137*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("%s0x%04x: %-*s  %s",
13841c99275SPeter Avalos 			    ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
139*ed775ee7SAntonio Huete Jimenez 			    hexstuff, asciistuff);
14041c99275SPeter Avalos 			i = 0; hsp = hexstuff; asp = asciistuff;
14141c99275SPeter Avalos 			oset += HEXDUMP_BYTES_PER_LINE;
14241c99275SPeter Avalos 		}
143*ed775ee7SAntonio Huete Jimenez 		nshorts--;
14441c99275SPeter Avalos 	}
14541c99275SPeter Avalos 	if (length & 1) {
146*ed775ee7SAntonio Huete Jimenez 		s1 = GET_U_1(cp);
147*ed775ee7SAntonio Huete Jimenez 		cp++;
14841c99275SPeter Avalos 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
14941c99275SPeter Avalos 		    " %02x", s1);
15041c99275SPeter Avalos 		hsp += 3;
151*ed775ee7SAntonio Huete Jimenez 		*(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
15241c99275SPeter Avalos 		++i;
15341c99275SPeter Avalos 	}
15441c99275SPeter Avalos 	if (i > 0) {
15541c99275SPeter Avalos 		*hsp = *asp = '\0';
156*ed775ee7SAntonio Huete Jimenez 		ND_PRINT("%s0x%04x: %-*s  %s",
15741c99275SPeter Avalos 		     ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
158*ed775ee7SAntonio Huete Jimenez 		     hexstuff, asciistuff);
15941c99275SPeter Avalos 	}
160*ed775ee7SAntonio Huete Jimenez 	if (truncated)
161*ed775ee7SAntonio Huete Jimenez 		nd_trunc_longjmp(ndo);
16241c99275SPeter Avalos }
16341c99275SPeter Avalos 
16441c99275SPeter Avalos void
hex_and_ascii_print(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length)165*ed775ee7SAntonio Huete Jimenez hex_and_ascii_print(netdissect_options *ndo, const char *ident,
166*ed775ee7SAntonio Huete Jimenez     const u_char *cp, u_int length)
16741c99275SPeter Avalos {
168411677aeSAaron LI 	hex_and_ascii_print_with_offset(ndo, ident, cp, length, 0);
16941c99275SPeter Avalos }
17041c99275SPeter Avalos 
17141c99275SPeter Avalos /*
17241c99275SPeter Avalos  * telnet_print() wants this.  It is essentially default_print_unaligned()
17341c99275SPeter Avalos  */
17441c99275SPeter Avalos void
hex_print_with_offset(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length,u_int oset)175411677aeSAaron LI hex_print_with_offset(netdissect_options *ndo,
176411677aeSAaron LI                       const char *ident, const u_char *cp, u_int length,
177411677aeSAaron LI 		      u_int oset)
17841c99275SPeter Avalos {
179411677aeSAaron LI 	u_int caplength;
180*ed775ee7SAntonio Huete Jimenez 	u_int i, s;
181*ed775ee7SAntonio Huete Jimenez 	u_int nshorts;
182*ed775ee7SAntonio Huete Jimenez 	int truncated = FALSE;
18341c99275SPeter Avalos 
184*ed775ee7SAntonio Huete Jimenez 	caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
185*ed775ee7SAntonio Huete Jimenez 	if (length > caplength) {
186411677aeSAaron LI 		length = caplength;
187*ed775ee7SAntonio Huete Jimenez 		truncated = TRUE;
188*ed775ee7SAntonio Huete Jimenez 	}
189*ed775ee7SAntonio Huete Jimenez 	nshorts = length / sizeof(u_short);
19041c99275SPeter Avalos 	i = 0;
191*ed775ee7SAntonio Huete Jimenez 	while (nshorts != 0) {
19241c99275SPeter Avalos 		if ((i++ % 8) == 0) {
193*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("%s0x%04x: ", ident, oset);
19441c99275SPeter Avalos 			oset += HEXDUMP_BYTES_PER_LINE;
19541c99275SPeter Avalos 		}
196*ed775ee7SAntonio Huete Jimenez 		s = GET_U_1(cp);
197*ed775ee7SAntonio Huete Jimenez 		cp++;
198*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" %02x%02x", s, GET_U_1(cp));
199*ed775ee7SAntonio Huete Jimenez 		cp++;
200*ed775ee7SAntonio Huete Jimenez 		nshorts--;
20141c99275SPeter Avalos 	}
20241c99275SPeter Avalos 	if (length & 1) {
20341c99275SPeter Avalos 		if ((i % 8) == 0)
204*ed775ee7SAntonio Huete Jimenez 			ND_PRINT("%s0x%04x: ", ident, oset);
205*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(" %02x", GET_U_1(cp));
20641c99275SPeter Avalos 	}
207*ed775ee7SAntonio Huete Jimenez 	if (truncated)
208*ed775ee7SAntonio Huete Jimenez 		nd_trunc_longjmp(ndo);
20941c99275SPeter Avalos }
21041c99275SPeter Avalos 
21141c99275SPeter Avalos void
hex_print(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length)212*ed775ee7SAntonio Huete Jimenez hex_print(netdissect_options *ndo,
213*ed775ee7SAntonio Huete Jimenez 	  const char *ident, const u_char *cp, u_int length)
21441c99275SPeter Avalos {
215411677aeSAaron LI 	hex_print_with_offset(ndo, ident, cp, length, 0);
21641c99275SPeter Avalos }
21741c99275SPeter Avalos 
21841c99275SPeter Avalos #ifdef MAIN
21941c99275SPeter Avalos int
main(int argc,char * argv[])22041c99275SPeter Avalos main(int argc, char *argv[])
22141c99275SPeter Avalos {
22241c99275SPeter Avalos 	hex_print("\n\t", "Hello, World!\n", 14);
22341c99275SPeter Avalos 	printf("\n");
22441c99275SPeter Avalos 	hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
22541c99275SPeter Avalos 	printf("\n");
22641c99275SPeter Avalos 	ascii_print("Hello, World!\n", 14);
22741c99275SPeter Avalos 	printf("\n");
22841c99275SPeter Avalos #define TMSG "Now is the winter of our discontent...\n"
22941c99275SPeter Avalos 	hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
23041c99275SPeter Avalos 	printf("\n");
23141c99275SPeter Avalos 	hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
23241c99275SPeter Avalos 	printf("\n");
23341c99275SPeter Avalos 	exit(0);
23441c99275SPeter Avalos }
23541c99275SPeter Avalos #endif /* MAIN */
236