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