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