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