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