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