blob: 2f5fe8547f6dc10da6e803c3de0e8ec7d8a4e676 [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
Elliott Hughese2e3bd12017-05-15 10:59:29 -070039/* \summary: ASCII packet dump printer */
40
The Android Open Source Project2949f582009-03-03 19:30:46 -080041#ifdef HAVE_CONFIG_H
Elliott Hughes820eced2021-08-20 18:00:50 -070042#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080043#endif
44
Elliott Hughes820eced2021-08-20 18:00:50 -070045#include "netdissect-stdinc.h"
46
The Android Open Source Project2949f582009-03-03 19:30:46 -080047#include <stdio.h>
48
Elliott Hughes820eced2021-08-20 18:00:50 -070049#include "netdissect-ctype.h"
50
Elliott Hughese2e3bd12017-05-15 10:59:29 -070051#include "netdissect.h"
Elliott Hughes820eced2021-08-20 18:00:50 -070052#include "extract.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080053
54#define ASCII_LINELENGTH 300
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
61void
Elliott Hughes892a68b2015-10-19 14:43:53 -070062ascii_print(netdissect_options *ndo,
63 const u_char *cp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -080064{
Elliott Hughes892a68b2015-10-19 14:43:53 -070065 u_int caplength;
Elliott Hughes820eced2021-08-20 18:00:50 -070066 u_char s;
67 int truncated = FALSE;
The Android Open Source Project2949f582009-03-03 19:30:46 -080068
Elliott Hughes820eced2021-08-20 18:00:50 -070069 ndo->ndo_protocol = "ascii";
70 caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
71 if (length > caplength) {
Elliott Hughes892a68b2015-10-19 14:43:53 -070072 length = caplength;
Elliott Hughes820eced2021-08-20 18:00:50 -070073 truncated = TRUE;
74 }
75 ND_PRINT("\n");
The Android Open Source Project2949f582009-03-03 19:30:46 -080076 while (length > 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -070077 s = GET_U_1(cp);
78 cp++;
The Android Open Source Project2949f582009-03-03 19:30:46 -080079 length--;
JP Abgrall53f17a92014-02-12 14:02:41 -080080 if (s == '\r') {
81 /*
82 * Don't print CRs at the end of the line; they
83 * don't belong at the ends of lines on UN*X,
84 * and the standard I/O library will give us one
85 * on Windows so we don't need to print one
86 * ourselves.
87 *
88 * In the middle of a line, just print a '.'.
89 */
Elliott Hughes820eced2021-08-20 18:00:50 -070090 if (length > 1 && GET_U_1(cp) != '\n')
91 ND_PRINT(".");
JP Abgrall53f17a92014-02-12 14:02:41 -080092 } else {
Elliott Hughes820eced2021-08-20 18:00:50 -070093 if (!ND_ASCII_ISGRAPH(s) &&
JP Abgrall53f17a92014-02-12 14:02:41 -080094 (s != '\t' && s != ' ' && s != '\n'))
Elliott Hughes820eced2021-08-20 18:00:50 -070095 ND_PRINT(".");
JP Abgrall53f17a92014-02-12 14:02:41 -080096 else
Elliott Hughes820eced2021-08-20 18:00:50 -070097 ND_PRINT("%c", s);
JP Abgrall53f17a92014-02-12 14:02:41 -080098 }
The Android Open Source Project2949f582009-03-03 19:30:46 -080099 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700100 if (truncated)
101 nd_trunc_longjmp(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800102}
103
Elliott Hughes820eced2021-08-20 18:00:50 -0700104static void
105hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *ident,
106 const u_char *cp, u_int length, u_int oset)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800107{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700108 u_int caplength;
Elliott Hughes820eced2021-08-20 18:00:50 -0700109 u_int i;
110 u_int s1, s2;
111 u_int nshorts;
112 int truncated = FALSE;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800113 char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
114 char asciistuff[ASCII_LINELENGTH+1], *asp;
115
Elliott Hughes820eced2021-08-20 18:00:50 -0700116 caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
117 if (length > caplength) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700118 length = caplength;
Elliott Hughes820eced2021-08-20 18:00:50 -0700119 truncated = TRUE;
120 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800121 nshorts = length / sizeof(u_short);
122 i = 0;
123 hsp = hexstuff; asp = asciistuff;
Elliott Hughes820eced2021-08-20 18:00:50 -0700124 while (nshorts != 0) {
125 s1 = GET_U_1(cp);
126 cp++;
127 s2 = GET_U_1(cp);
128 cp++;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800129 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
130 " %02x%02x", s1, s2);
131 hsp += HEXDUMP_HEXSTUFF_PER_SHORT;
Elliott Hughes820eced2021-08-20 18:00:50 -0700132 *(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
133 *(asp++) = (char)(ND_ASCII_ISGRAPH(s2) ? s2 : '.');
The Android Open Source Project2949f582009-03-03 19:30:46 -0800134 i++;
135 if (i >= HEXDUMP_SHORTS_PER_LINE) {
136 *hsp = *asp = '\0';
Elliott Hughes820eced2021-08-20 18:00:50 -0700137 ND_PRINT("%s0x%04x: %-*s %s",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800138 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
Elliott Hughes820eced2021-08-20 18:00:50 -0700139 hexstuff, asciistuff);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800140 i = 0; hsp = hexstuff; asp = asciistuff;
141 oset += HEXDUMP_BYTES_PER_LINE;
142 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700143 nshorts--;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800144 }
145 if (length & 1) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700146 s1 = GET_U_1(cp);
147 cp++;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800148 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff),
149 " %02x", s1);
150 hsp += 3;
Elliott Hughes820eced2021-08-20 18:00:50 -0700151 *(asp++) = (char)(ND_ASCII_ISGRAPH(s1) ? s1 : '.');
The Android Open Source Project2949f582009-03-03 19:30:46 -0800152 ++i;
153 }
154 if (i > 0) {
155 *hsp = *asp = '\0';
Elliott Hughes820eced2021-08-20 18:00:50 -0700156 ND_PRINT("%s0x%04x: %-*s %s",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800157 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE,
Elliott Hughes820eced2021-08-20 18:00:50 -0700158 hexstuff, asciistuff);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800159 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700160 if (truncated)
161 nd_trunc_longjmp(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800162}
163
164void
Elliott Hughes820eced2021-08-20 18:00:50 -0700165hex_and_ascii_print(netdissect_options *ndo, const char *ident,
166 const u_char *cp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800167{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700168 hex_and_ascii_print_with_offset(ndo, ident, cp, length, 0);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800169}
170
171/*
172 * telnet_print() wants this. It is essentially default_print_unaligned()
173 */
174void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700175hex_print_with_offset(netdissect_options *ndo,
176 const char *ident, const u_char *cp, u_int length,
177 u_int oset)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800178{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700179 u_int caplength;
Elliott Hughes820eced2021-08-20 18:00:50 -0700180 u_int i, s;
181 u_int nshorts;
182 int truncated = FALSE;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800183
Elliott Hughes820eced2021-08-20 18:00:50 -0700184 caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
185 if (length > caplength) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700186 length = caplength;
Elliott Hughes820eced2021-08-20 18:00:50 -0700187 truncated = TRUE;
188 }
189 nshorts = length / sizeof(u_short);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800190 i = 0;
Elliott Hughes820eced2021-08-20 18:00:50 -0700191 while (nshorts != 0) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800192 if ((i++ % 8) == 0) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700193 ND_PRINT("%s0x%04x: ", ident, oset);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800194 oset += HEXDUMP_BYTES_PER_LINE;
195 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700196 s = GET_U_1(cp);
197 cp++;
198 ND_PRINT(" %02x%02x", s, GET_U_1(cp));
199 cp++;
200 nshorts--;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800201 }
202 if (length & 1) {
203 if ((i % 8) == 0)
Elliott Hughes820eced2021-08-20 18:00:50 -0700204 ND_PRINT("%s0x%04x: ", ident, oset);
205 ND_PRINT(" %02x", GET_U_1(cp));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800206 }
Elliott Hughes820eced2021-08-20 18:00:50 -0700207 if (truncated)
208 nd_trunc_longjmp(ndo);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800209}
210
The Android Open Source Project2949f582009-03-03 19:30:46 -0800211void
Elliott Hughes820eced2021-08-20 18:00:50 -0700212hex_print(netdissect_options *ndo,
213 const char *ident, const u_char *cp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800214{
Elliott Hughes820eced2021-08-20 18:00:50 -0700215 hex_print_with_offset(ndo, ident, cp, length, 0);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800216}
217
218#ifdef MAIN
219int
220main(int argc, char *argv[])
221{
222 hex_print("\n\t", "Hello, World!\n", 14);
223 printf("\n");
224 hex_and_ascii_print("\n\t", "Hello, World!\n", 14);
225 printf("\n");
226 ascii_print("Hello, World!\n", 14);
227 printf("\n");
228#define TMSG "Now is the winter of our discontent...\n"
229 hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
230 printf("\n");
231 hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100);
232 printf("\n");
233 exit(0);
234}
235#endif /* MAIN */