xref: /minix/external/bsd/tcpdump/dist/print-ascii.c (revision b636d99d)
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 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: print-ascii.c,v 1.5 2015/03/31 21:59:35 christos Exp $");
35 #endif
36 
37 #define NETDISSECT_REWORKED
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41 
42 #include <tcpdump-stdinc.h>
43 #include <stdio.h>
44 
45 #include "interface.h"
46 
47 #define ASCII_LINELENGTH 300
48 #define HEXDUMP_BYTES_PER_LINE 16
49 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
50 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
51 #define HEXDUMP_HEXSTUFF_PER_LINE \
52 		(HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
53 
54 void
ascii_print(netdissect_options * ndo,const u_char * cp,u_int length)55 ascii_print(netdissect_options *ndo,
56             const u_char *cp, u_int length)
57 {
58 	u_int caplength;
59 	register u_char s;
60 
61 	caplength = (ndo->ndo_snapend >= cp) ? ndo->ndo_snapend - cp : 0;
62 	if (length > caplength)
63 		length = caplength;
64 	ND_PRINT((ndo, "\n"));
65 	while (length > 0) {
66 		s = *cp++;
67 		length--;
68 		if (s == '\r') {
69 			/*
70 			 * Don't print CRs at the end of the line; they
71 			 * don't belong at the ends of lines on UN*X,
72 			 * and the standard I/O library will give us one
73 			 * on Windows so we don't need to print one
74 			 * ourselves.
75 			 *
76 			 * In the middle of a line, just print a '.'.
77 			 */
78 			if (length > 1 && *cp != '\n')
79 				ND_PRINT((ndo, "."));
80 		} else {
81 			if (!ND_ISGRAPH(s) &&
82 			    (s != '\t' && s != ' ' && s != '\n'))
83 				ND_PRINT((ndo, "."));
84 			else
85 				ND_PRINT((ndo, "%c", s));
86 		}
87 	}
88 }
89 
90 void
hex_and_ascii_print_with_offset(netdissect_options * ndo,register const char * ident,register const u_char * cp,register u_int length,register u_int oset)91 hex_and_ascii_print_with_offset(netdissect_options *ndo, register const char *ident,
92     register const u_char *cp, register u_int length, register u_int oset)
93 {
94 	u_int caplength;
95 	register u_int i;
96 	register int s1, s2;
97 	register int nshorts;
98 	char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
99 	char asciistuff[ASCII_LINELENGTH+1], *asp;
100 
101 	caplength = (ndo->ndo_snapend >= cp) ? ndo->ndo_snapend - cp : 0;
102 	if (length > caplength)
103 		length = caplength;
104 	nshorts = length / sizeof(u_short);
105 	i = 0;
106 	hsp = hexstuff; asp = asciistuff;
107 	while (--nshorts >= 0) {
108 		s1 = *cp++;
109 		s2 = *cp++;
110 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
111 		    " %02x%02x", s1, s2);
112 		hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
113 		*(asp++) = (ND_ISGRAPH(s1) ? s1 : '.');
114 		*(asp++) = (ND_ISGRAPH(s2) ? s2 : '.');
115 		i++;
116 		if (i >= HEXDUMP_SHORTS_PER_LINE) {
117 			*hsp = *asp = '\0';
118 			ND_PRINT((ndo, "%s0x%04x: %-*s  %s",
119 			    ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
120 			    hexstuff, asciistuff));
121 			i = 0; hsp = hexstuff; asp = asciistuff;
122 			oset += HEXDUMP_BYTES_PER_LINE;
123 		}
124 	}
125 	if (length & 1) {
126 		s1 = *cp++;
127 		(void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
128 		    " %02x", s1);
129 		hsp += 3;
130 		*(asp++) = (ND_ISGRAPH(s1) ? s1 : '.');
131 		++i;
132 	}
133 	if (i > 0) {
134 		*hsp = *asp = '\0';
135 		ND_PRINT((ndo, "%s0x%04x: %-*s  %s",
136 		     ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
137 		     hexstuff, asciistuff));
138 	}
139 }
140 
141 void
hex_and_ascii_print(netdissect_options * ndo,register const char * ident,register const u_char * cp,register u_int length)142 hex_and_ascii_print(netdissect_options *ndo, register const char *ident,
143     register const u_char *cp, register u_int length)
144 {
145 	hex_and_ascii_print_with_offset(ndo, ident, cp, length, 0);
146 }
147 
148 /*
149  * telnet_print() wants this.  It is essentially default_print_unaligned()
150  */
151 void
hex_print_with_offset(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length,u_int oset)152 hex_print_with_offset(netdissect_options *ndo,
153                       const char *ident, const u_char *cp, u_int length,
154 		      u_int oset)
155 {
156 	u_int caplength;
157 	register u_int i, s;
158 	register int nshorts;
159 
160 	caplength = (ndo->ndo_snapend >= cp) ? ndo->ndo_snapend - cp : 0;
161 	if (length > caplength)
162 		length = caplength;
163 	nshorts = (u_int) length / sizeof(u_short);
164 	i = 0;
165 	while (--nshorts >= 0) {
166 		if ((i++ % 8) == 0) {
167 			ND_PRINT((ndo,"%s0x%04x: ", ident, oset));
168 			oset += HEXDUMP_BYTES_PER_LINE;
169 		}
170 		s = *cp++;
171 		ND_PRINT((ndo," %02x%02x", s, *cp++));
172 	}
173 	if (length & 1) {
174 		if ((i % 8) == 0)
175 			ND_PRINT((ndo,"%s0x%04x: ", ident, oset));
176 		ND_PRINT((ndo," %02x", *cp));
177 	}
178 }
179 
180 /*
181  * just for completeness
182  */
183 void
hex_print(netdissect_options * ndo,const char * ident,const u_char * cp,u_int length)184 hex_print(netdissect_options *ndo,const char *ident, const u_char *cp, u_int length)
185 {
186   hex_print_with_offset(ndo, ident, cp, length, 0);
187 }
188 
189 #ifdef MAIN
190 int
main(int argc,char * argv[])191 main(int argc, char *argv[])
192 {
193 	hex_print("\n\t", "Hello, World!\n", 14);
194 	printf("\n");
195 	hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
196 	printf("\n");
197 	ascii_print("Hello, World!\n", 14);
198 	printf("\n");
199 #define TMSG "Now is the winter of our discontent...\n"
200 	hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
201 	printf("\n");
202 	hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
203 	printf("\n");
204 	exit(0);
205 }
206 #endif /* MAIN */
207