xref: /freebsd/contrib/tcpdump/print-ascii.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
1 /*	$NetBSD: print-ascii.c,v 1.1 1999/09/30 14:49:12 sjg Exp $ 	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Alan Barrett and Simon J. Gerraty.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
25  *
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.
37  */
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42 
43 #ifndef lint
44 static const char rcsid[] _U_ =
45      "@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.16 2004/07/21 22:00:10 guy Exp $";
46 #endif
47 #include <tcpdump-stdinc.h>
48 #include <stdio.h>
49 
50 #include "interface.h"
51 
52 #define ASCII_LINELENGTH 300
53 #define HEXDUMP_BYTES_PER_LINE 16
54 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
55 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
56 #define HEXDUMP_HEXSTUFF_PER_LINE \
57 		(HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
58 
59 void
60 ascii_print_with_offset(register const char *ident, register const u_char *cp, register u_int length,
61 			register u_int oset)
62 {
63 	register u_int i;
64 	register int s1, s2;
65 	register int nshorts;
66 	char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
67 	char asciistuff[ASCII_LINELENGTH+1], *asp;
68 	u_int maxlength = (Aflag ? ASCII_LINELENGTH : HEXDUMP_SHORTS_PER_LINE);
69 
70 	nshorts = length / sizeof(u_short);
71 	i = 0;
72 	hsp = hexstuff; asp = asciistuff;
73 	if (Aflag) *(asp++) = '\n';
74 	while (--nshorts >= 0) {
75 		s1 = *cp++;
76 		s2 = *cp++;
77 		if (Aflag) {
78 			i += 2;
79 			*(asp++) = (isgraph(s1) ? s1 : (s1 != '\t' && s1 != ' ' && s1 != '\n' && s1 != '\r' ? '.' : s1) );
80 			*(asp++) = (isgraph(s2) ? s2 : (s2 != '\t' && s2 != ' ' && s2 != '\n' && s2 != '\r' ? '.' : s2) );
81 			if (s1 == '\n' || s2 == '\n') i = maxlength;
82 
83 		} else {
84 			(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
85 			    " %02x%02x", s1, s2);
86 			hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
87 			*(asp++) = (isgraph(s1) ? s1 : '.');
88 			*(asp++) = (isgraph(s2) ? s2 : '.');
89 			i++;
90 		}
91 		if (i >= maxlength) {
92 			*hsp = *asp = '\0';
93 			if (Aflag) {
94 				(void)printf("%s", asciistuff);
95 			} else {
96 				(void)printf("%s0x%04x: %-*s  %s",
97 				    ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
98 				    hexstuff, asciistuff);
99 			}
100 			i = 0; hsp = hexstuff; asp = asciistuff;
101 			oset += HEXDUMP_BYTES_PER_LINE;
102 		}
103 	}
104 	if (length & 1) {
105 		s1 = *cp++;
106 		if (Aflag) {
107 			*(asp++) = (isgraph(s1) ? s1 : (s1 != '\t' && s1 != ' ' && s1 != '\n' && s1 != '\r' ? '.' : s1) );
108 		} else {
109 			(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
110 			    " %02x", s1);
111 			hsp += 3;
112 			*(asp++) = (isgraph(s1) ? s1 : '.');
113 		}
114 		++i;
115 	}
116 	if (i > 0) {
117 		*hsp = *asp = '\0';
118 		if (Aflag) {
119 			(void)printf("%s%s", ident, asciistuff);
120 		} else {
121 			(void)printf("%s0x%04x: %-*s  %s",
122 			     ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
123 			     hexstuff, asciistuff);
124 		}
125 	}
126 }
127 
128 void
129 ascii_print(register const char *ident, register const u_char *cp, register u_int length)
130 {
131 	ascii_print_with_offset(ident, cp, length, 0);
132 }
133 
134 /*
135  * telnet_print() wants this.  It is essentially default_print_unaligned()
136  */
137 void
138 hex_print_with_offset(register const char *ident, register const u_char *cp, register u_int length,
139 		      register u_int oset)
140 {
141 	register u_int i, s;
142 	register int nshorts;
143 
144 	nshorts = (u_int) length / sizeof(u_short);
145 	i = 0;
146 	while (--nshorts >= 0) {
147 		if ((i++ % 8) == 0) {
148 			(void)printf("%s0x%04x: ", ident, oset);
149 			oset += HEXDUMP_BYTES_PER_LINE;
150 		}
151 		s = *cp++;
152 		(void)printf(" %02x%02x", s, *cp++);
153 	}
154 	if (length & 1) {
155 		if ((i % 8) == 0)
156 			(void)printf("%s0x%04x: ", ident, oset);
157 		(void)printf(" %02x", *cp);
158 	}
159 }
160 
161 /*
162  * just for completeness
163  */
164 void
165 hex_print(register const char *ident, register const u_char *cp, register u_int length)
166 {
167 	hex_print_with_offset(ident, cp, length, 0);
168 }
169 
170 #ifdef MAIN
171 int
172 main(int argc, char *argv[])
173 {
174 	hex_print("Hello, World!\n", 14);
175 	printf("\n");
176 	ascii_print("Hello, World!\n", 14);
177 	printf("\n");
178 #define TMSG "Now is the winter of our discontent...\n"
179 	ascii_print_with_offset(TMSG, sizeof(TMSG) - 1, 0x100);
180 	printf("\n");
181 	exit(0);
182 }
183 #endif /* MAIN */
184 
185 
186