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