blob: f09aa98b43733739d922df7bc9da87d6fdd8280f [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/* $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
44static const char rcsid[] _U_ =
JP Abgrall53f17a92014-02-12 14:02:41 -080045 "@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.17 2005-07-06 20:53:32 guy Exp $";
The Android Open Source Project2949f582009-03-03 19:30:46 -080046#endif
47#include <tcpdump-stdinc.h>
48#include <stdio.h>
49
50#include "interface.h"
51
52#define ASCII_LINELENGTH 300
53#define HEXDUMP_BYTES_PER_LINE 16
54#define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2)
55#define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */
56#define HEXDUMP_HEXSTUFF_PER_LINE \
57 (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE)
58
59void
60ascii_print(register const u_char *cp, register u_int length)
61{
62 register int s;
63
64 putchar('\n');
65 while (length > 0) {
66 s = *cp++;
67 length--;
JP Abgrall53f17a92014-02-12 14:02:41 -080068 if (s == '\r') {
69 /*
70 * Don't print CRs at the end of the line; they
71 * don't belong at the ends of lines on UN*X,
72 * and the standard I/O library will give us one
73 * on Windows so we don't need to print one
74 * ourselves.
75 *
76 * In the middle of a line, just print a '.'.
77 */
78 if (length > 1 && *cp != '\n')
79 putchar('.');
80 } else {
81 if (!ND_ISGRAPH(s) &&
82 (s != '\t' && s != ' ' && s != '\n'))
83 putchar('.');
84 else
85 putchar(s);
86 }
The Android Open Source Project2949f582009-03-03 19:30:46 -080087 }
88}
89
90void
91hex_and_ascii_print_with_offset(register const char *ident,
92 register const u_char *cp, register u_int length, register u_int oset)
93{
94 register u_int i;
95 register int s1, s2;
96 register int nshorts;
97 char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
98 char asciistuff[ASCII_LINELENGTH+1], *asp;
99
100 nshorts = length / sizeof(u_short);
101 i = 0;
102 hsp = hexstuff; asp = asciistuff;
103 while (--nshorts >= 0) {
104 s1 = *cp++;
105 s2 = *cp++;
106 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
107 " %02x%02x", s1, s2);
108 hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
JP Abgrall53f17a92014-02-12 14:02:41 -0800109 *(asp++) = (ND_ISGRAPH(s1) ? s1 : '.');
110 *(asp++) = (ND_ISGRAPH(s2) ? s2 : '.');
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111 i++;
112 if (i >= HEXDUMP_SHORTS_PER_LINE) {
113 *hsp = *asp = '\0';
114 (void)printf("%s0x%04x: %-*s %s",
115 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
116 hexstuff, asciistuff);
117 i = 0; hsp = hexstuff; asp = asciistuff;
118 oset += HEXDUMP_BYTES_PER_LINE;
119 }
120 }
121 if (length & 1) {
122 s1 = *cp++;
123 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
124 " %02x", s1);
125 hsp += 3;
JP Abgrall53f17a92014-02-12 14:02:41 -0800126 *(asp++) = (ND_ISGRAPH(s1) ? s1 : '.');
The Android Open Source Project2949f582009-03-03 19:30:46 -0800127 ++i;
128 }
129 if (i > 0) {
130 *hsp = *asp = '\0';
131 (void)printf("%s0x%04x: %-*s %s",
132 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
133 hexstuff, asciistuff);
134 }
135}
136
137void
138hex_and_ascii_print(register const char *ident, register const u_char *cp,
139 register u_int length)
140{
141 hex_and_ascii_print_with_offset(ident, cp, length, 0);
142}
143
144/*
145 * telnet_print() wants this. It is essentially default_print_unaligned()
146 */
147void
148hex_print_with_offset(register const char *ident, register const u_char *cp, register u_int length,
149 register u_int oset)
150{
151 register u_int i, s;
152 register int nshorts;
153
154 nshorts = (u_int) length / sizeof(u_short);
155 i = 0;
156 while (--nshorts >= 0) {
157 if ((i++ % 8) == 0) {
158 (void)printf("%s0x%04x: ", ident, oset);
159 oset += HEXDUMP_BYTES_PER_LINE;
160 }
161 s = *cp++;
162 (void)printf(" %02x%02x", s, *cp++);
163 }
164 if (length & 1) {
165 if ((i % 8) == 0)
166 (void)printf("%s0x%04x: ", ident, oset);
167 (void)printf(" %02x", *cp);
168 }
169}
170
171/*
172 * just for completeness
173 */
174void
175hex_print(register const char *ident, register const u_char *cp, register u_int length)
176{
177 hex_print_with_offset(ident, cp, length, 0);
178}
179
180#ifdef MAIN
181int
182main(int argc, char *argv[])
183{
184 hex_print("\n\t", "Hello, World!\n", 14);
185 printf("\n");
186 hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
187 printf("\n");
188 ascii_print("Hello, World!\n", 14);
189 printf("\n");
190#define TMSG "Now is the winter of our discontent...\n"
191 hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
192 printf("\n");
193 hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
194 printf("\n");
195 exit(0);
196}
197#endif /* MAIN */