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