xref: /freebsd/contrib/tcpdump/print-ascii.c (revision b0453382235492c8e30b09659b52d784128ca7d0)
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 
39b0453382SBill Fenner #ifdef HAVE_CONFIG_H
40b0453382SBill Fenner #include "config.h"
41b0453382SBill Fenner #endif
42b0453382SBill Fenner 
43b0453382SBill Fenner #ifndef lint
44b0453382SBill Fenner static const char rcsid[] =
45b0453382SBill Fenner      "@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.2.2.2 2000/01/11 06:58:23 fenner Exp $";
46b0453382SBill Fenner #endif
47b0453382SBill Fenner #include <stdio.h>
48b0453382SBill Fenner #include <sys/types.h>
49b0453382SBill Fenner #include <ctype.h>
50b0453382SBill Fenner 
51b0453382SBill Fenner #include "interface.h"
52b0453382SBill Fenner 
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
60b0453382SBill Fenner ascii_print_with_offset(register const u_char *cp, register u_int length,
61b0453382SBill Fenner 			register u_int oset)
62b0453382SBill Fenner {
63b0453382SBill Fenner 	register u_int i;
64b0453382SBill Fenner 	register int s1, s2;
65b0453382SBill Fenner 	register int nshorts;
66b0453382SBill Fenner 	char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
67b0453382SBill Fenner 	char asciistuff[HEXDUMP_BYTES_PER_LINE+1], *asp;
68b0453382SBill Fenner 
69b0453382SBill Fenner 	nshorts = length / sizeof(u_short);
70b0453382SBill Fenner 	i = 0;
71b0453382SBill Fenner 	hsp = hexstuff; asp = asciistuff;
72b0453382SBill Fenner 	while (--nshorts >= 0) {
73b0453382SBill Fenner 		s1 = *cp++;
74b0453382SBill Fenner 		s2 = *cp++;
75b0453382SBill Fenner 		(void)sprintf(hsp, " %02x%02x", s1, s2);
76b0453382SBill Fenner 		hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
77b0453382SBill Fenner 		*(asp++) = (isgraph(s1) ? s1 : '.');
78b0453382SBill Fenner 		*(asp++) = (isgraph(s2) ? s2 : '.');
79b0453382SBill Fenner 		if (++i >= HEXDUMP_SHORTS_PER_LINE) {
80b0453382SBill Fenner 		    *hsp = *asp = '\0';
81b0453382SBill Fenner 		    (void)printf("\n0x%04x\t%-*s\t%s",
82b0453382SBill Fenner 				 oset, HEXDUMP_HEXSTUFF_PER_LINE,
83b0453382SBill Fenner 				 hexstuff, asciistuff);
84b0453382SBill Fenner 		    i = 0; hsp = hexstuff; asp = asciistuff;
85b0453382SBill Fenner 		    oset += HEXDUMP_BYTES_PER_LINE;
86b0453382SBill Fenner 		}
87b0453382SBill Fenner 	}
88b0453382SBill Fenner 	if (length & 1) {
89b0453382SBill Fenner 		s1 = *cp++;
90b0453382SBill Fenner 		(void)sprintf(hsp, " %02x", s1);
91b0453382SBill Fenner 		hsp += 3;
92b0453382SBill Fenner 		*(asp++) = (isgraph(s1) ? s1 : '.');
93b0453382SBill Fenner 		++i;
94b0453382SBill Fenner 	}
95b0453382SBill Fenner 	if (i > 0) {
96b0453382SBill Fenner 		*hsp = *asp = '\0';
97b0453382SBill Fenner 		(void)printf("\n0x%04x\t%-*s\t%s",
98b0453382SBill Fenner 			     oset, HEXDUMP_HEXSTUFF_PER_LINE,
99b0453382SBill Fenner 			     hexstuff, asciistuff);
100b0453382SBill Fenner 	}
101b0453382SBill Fenner }
102b0453382SBill Fenner 
103b0453382SBill Fenner void
104b0453382SBill Fenner ascii_print(register const u_char *cp, register u_int length)
105b0453382SBill Fenner {
106b0453382SBill Fenner 	ascii_print_with_offset(cp, length, 0);
107b0453382SBill Fenner }
108b0453382SBill Fenner 
109b0453382SBill Fenner /*
110b0453382SBill Fenner  * telnet_print() wants this.  It is essentially default_print_unaligned()
111b0453382SBill Fenner  */
112b0453382SBill Fenner void
113b0453382SBill Fenner hex_print_with_offset(register const u_char *cp, register u_int length,
114b0453382SBill Fenner 		      register u_int oset)
115b0453382SBill Fenner {
116b0453382SBill Fenner 	register u_int i, s;
117b0453382SBill Fenner 	register int nshorts;
118b0453382SBill Fenner 
119b0453382SBill Fenner 	nshorts = (u_int) length / sizeof(u_short);
120b0453382SBill Fenner 	i = 0;
121b0453382SBill Fenner 	while (--nshorts >= 0) {
122b0453382SBill Fenner 		if ((i++ % 8) == 0) {
123b0453382SBill Fenner 			(void)printf("\n0x%04x\t", oset);
124b0453382SBill Fenner 			oset += HEXDUMP_BYTES_PER_LINE;
125b0453382SBill Fenner 		}
126b0453382SBill Fenner 		s = *cp++;
127b0453382SBill Fenner 		(void)printf(" %02x%02x", s, *cp++);
128b0453382SBill Fenner 	}
129b0453382SBill Fenner 	if (length & 1) {
130b0453382SBill Fenner 		if ((i % 8) == 0)
131b0453382SBill Fenner 			(void)printf("\n0x%04x\t", oset);
132b0453382SBill Fenner 		(void)printf(" %02x", *cp);
133b0453382SBill Fenner 	}
134b0453382SBill Fenner }
135b0453382SBill Fenner 
136b0453382SBill Fenner /*
137b0453382SBill Fenner  * just for completeness
138b0453382SBill Fenner  */
139b0453382SBill Fenner void
140b0453382SBill Fenner hex_print(register const u_char *cp, register u_int length)
141b0453382SBill Fenner {
142b0453382SBill Fenner 	hex_print_with_offset(cp, length, 0);
143b0453382SBill Fenner }
144b0453382SBill Fenner 
145b0453382SBill Fenner #ifdef MAIN
146b0453382SBill Fenner int
147b0453382SBill Fenner main(int argc, char *argv[])
148b0453382SBill Fenner {
149b0453382SBill Fenner 	hex_print("Hello, World!\n", 14);
150b0453382SBill Fenner 	printf("\n");
151b0453382SBill Fenner 	ascii_print("Hello, World!\n", 14);
152b0453382SBill Fenner 	printf("\n");
153b0453382SBill Fenner #define TMSG "Now is the winter of our discontent...\n"
154b0453382SBill Fenner 	ascii_print_with_offset(TMSG, sizeof(TMSG) - 1, 0x100);
155b0453382SBill Fenner 	printf("\n");
156b0453382SBill Fenner 	exit(0);
157b0453382SBill Fenner }
158b0453382SBill Fenner #endif /* MAIN */
159