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