xref: /netbsd/external/bsd/tcpdump/dist/print-ascii.c (revision 6550d01e)
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static const char rcsid[] _U_ =
40      "@(#) Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.17 2005-07-06 20:53:32 guy Exp";
41 #else
42 __RCSID("$NetBSD: print-ascii.c,v 1.2 2010/12/05 05:11:30 christos Exp $");
43 #endif
44 #endif
45 #include <tcpdump-stdinc.h>
46 #include <stdio.h>
47 
48 #include "interface.h"
49 
50 #define ASCII_LINELENGTH 300
51 #define HEXDUMP_BYTES_PER_LINE 16
52 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
53 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
54 #define HEXDUMP_HEXSTUFF_PER_LINE \
55 		(HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
56 
57 void
58 ascii_print(register const u_char *cp, register u_int length)
59 {
60 	register int s;
61 
62 	putchar('\n');
63 	while (length > 0) {
64 		s = *cp++;
65 		length--;
66 		if (!isgraph(s) &&
67 		    (s != '\t' && s != ' ' && s != '\n' && s != '\r'))
68 			putchar('.');
69 		else
70 			putchar(s);
71 	}
72 }
73 
74 void
75 hex_and_ascii_print_with_offset(register const char *ident,
76     register const u_char *cp, register u_int length, register u_int oset)
77 {
78 	register u_int i;
79 	register int s1, s2;
80 	register int nshorts;
81 	char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
82 	char asciistuff[ASCII_LINELENGTH+1], *asp;
83 
84 	nshorts = length / sizeof(u_short);
85 	i = 0;
86 	hsp = hexstuff; asp = asciistuff;
87 	while (--nshorts >= 0) {
88 		s1 = *cp++;
89 		s2 = *cp++;
90 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
91 		    " %02x%02x", s1, s2);
92 		hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
93 		*(asp++) = (isgraph(s1) ? s1 : '.');
94 		*(asp++) = (isgraph(s2) ? s2 : '.');
95 		i++;
96 		if (i >= HEXDUMP_SHORTS_PER_LINE) {
97 			*hsp = *asp = '\0';
98 			(void)printf("%s0x%04x: %-*s  %s",
99 			    ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
100 			    hexstuff, asciistuff);
101 			i = 0; hsp = hexstuff; asp = asciistuff;
102 			oset += HEXDUMP_BYTES_PER_LINE;
103 		}
104 	}
105 	if (length & 1) {
106 		s1 = *cp++;
107 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
108 		    " %02x", s1);
109 		hsp += 3;
110 		*(asp++) = (isgraph(s1) ? s1 : '.');
111 		++i;
112 	}
113 	if (i > 0) {
114 		*hsp = *asp = '\0';
115 		(void)printf("%s0x%04x: %-*s  %s",
116 		     ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
117 		     hexstuff, asciistuff);
118 	}
119 }
120 
121 void
122 hex_and_ascii_print(register const char *ident, register const u_char *cp,
123     register u_int length)
124 {
125 	hex_and_ascii_print_with_offset(ident, cp, length, 0);
126 }
127 
128 /*
129  * telnet_print() wants this.  It is essentially default_print_unaligned()
130  */
131 void
132 hex_print_with_offset(register const char *ident, register const u_char *cp, register u_int length,
133 		      register u_int oset)
134 {
135 	register u_int i, s;
136 	register int nshorts;
137 
138 	nshorts = (u_int) length / sizeof(u_short);
139 	i = 0;
140 	while (--nshorts >= 0) {
141 		if ((i++ % 8) == 0) {
142 			(void)printf("%s0x%04x: ", ident, oset);
143 			oset += HEXDUMP_BYTES_PER_LINE;
144 		}
145 		s = *cp++;
146 		(void)printf(" %02x%02x", s, *cp++);
147 	}
148 	if (length & 1) {
149 		if ((i % 8) == 0)
150 			(void)printf("%s0x%04x: ", ident, oset);
151 		(void)printf(" %02x", *cp);
152 	}
153 }
154 
155 /*
156  * just for completeness
157  */
158 void
159 hex_print(register const char *ident, register const u_char *cp, register u_int length)
160 {
161 	hex_print_with_offset(ident, cp, length, 0);
162 }
163 
164 #ifdef MAIN
165 int
166 main(int argc, char *argv[])
167 {
168 	hex_print("\n\t", "Hello, World!\n", 14);
169 	printf("\n");
170 	hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
171 	printf("\n");
172 	ascii_print("Hello, World!\n", 14);
173 	printf("\n");
174 #define TMSG "Now is the winter of our discontent...\n"
175 	hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
176 	printf("\n");
177 	hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
178 	printf("\n");
179 	exit(0);
180 }
181 #endif /* MAIN */
182