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