]> The Tcpdump Group git mirrors - tcpdump/blob - print-ascii.c
remove tcpdump's own CVS keywords
[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 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include <tcpdump-stdinc.h>
44 #include <stdio.h>
45
46 #include "netdissect.h"
47 #include "interface.h"
48
49 #define ASCII_LINELENGTH 300
50 #define HEXDUMP_BYTES_PER_LINE 16
51 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
52 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
53 #define HEXDUMP_HEXSTUFF_PER_LINE \
54 (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
55
56 void
57 ascii_print(register const u_char *cp, register u_int length)
58 {
59 register int s;
60
61 putchar('\n');
62 while (length > 0) {
63 s = *cp++;
64 length--;
65 if (!isgraph(s) &&
66 (s != '\t' && s != ' ' && s != '\n' && s != '\r'))
67 putchar('.');
68 else
69 putchar(s);
70 }
71 }
72
73 void
74 hex_and_ascii_print_with_offset(register const char *ident,
75 register const u_char *cp, register u_int length, register u_int oset)
76 {
77 register u_int i;
78 register int s1, s2;
79 register int nshorts;
80 char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
81 char asciistuff[ASCII_LINELENGTH+1], *asp;
82
83 nshorts = length / sizeof(u_short);
84 i = 0;
85 hsp = hexstuff; asp = asciistuff;
86 while (--nshorts >= 0) {
87 s1 = *cp++;
88 s2 = *cp++;
89 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
90 " %02x%02x", s1, s2);
91 hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
92 *(asp++) = (isgraph(s1) ? s1 : '.');
93 *(asp++) = (isgraph(s2) ? s2 : '.');
94 i++;
95 if (i >= HEXDUMP_SHORTS_PER_LINE) {
96 *hsp = *asp = '\0';
97 (void)printf("%s0x%04x: %-*s %s",
98 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
99 hexstuff, asciistuff);
100 i = 0; hsp = hexstuff; asp = asciistuff;
101 oset += HEXDUMP_BYTES_PER_LINE;
102 }
103 }
104 if (length & 1) {
105 s1 = *cp++;
106 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
107 " %02x", s1);
108 hsp += 3;
109 *(asp++) = (isgraph(s1) ? s1 : '.');
110 ++i;
111 }
112 if (i > 0) {
113 *hsp = *asp = '\0';
114 (void)printf("%s0x%04x: %-*s %s",
115 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
116 hexstuff, asciistuff);
117 }
118 }
119
120 void
121 hex_and_ascii_print(register const char *ident, register const u_char *cp,
122 register u_int length)
123 {
124 hex_and_ascii_print_with_offset(ident, cp, length, 0);
125 }
126
127 /*
128 * telnet_print() wants this. It is essentially default_print_unaligned()
129 */
130 void
131 hex_print_with_offset(netdissect_options *ndo,
132 const char *ident, const u_char *cp, u_int length,
133 u_int oset)
134 {
135 register u_int i, s;
136 register int nshorts;
137
138 nshorts = (u_int) length / sizeof(u_short);
139 i = 0;
140 while (--nshorts >= 0) {
141 if ((i++ % 8) == 0) {
142 (void)ND_PRINT((ndo,"%s0x%04x: ", ident, oset));
143 oset += HEXDUMP_BYTES_PER_LINE;
144 }
145 s = *cp++;
146 (void)ND_PRINT((ndo," %02x%02x", s, *cp++));
147 }
148 if (length & 1) {
149 if ((i % 8) == 0)
150 (void)ND_PRINT((ndo,"%s0x%04x: ", ident, oset));
151 (void)ND_PRINT((ndo," %02x", *cp));
152 }
153 }
154
155 /*
156 * just for completeness
157 */
158 void
159 hex_print(netdissect_options *ndo,const char *ident, const u_char *cp, u_int length)
160 {
161 hex_print_with_offset(ndo, ident, cp, length, 0);
162 }
163
164 #ifdef MAIN
165 int
166 main(int argc, char *argv[])
167 {
168 hex_print("\n\t", "Hello, World!\n", 14);
169 printf("\n");
170 hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
171 printf("\n");
172 ascii_print("Hello, World!\n", 14);
173 printf("\n");
174 #define TMSG "Now is the winter of our discontent...\n"
175 hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
176 printf("\n");
177 hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
178 printf("\n");
179 exit(0);
180 }
181 #endif /* MAIN */