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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __RCSID("$NetBSD: print-ascii.c,v 1.7 2017/02/05 04:05:05 spz Exp $"); 35 #endif 36 37 /* \summary: ASCII packet dump printer */ 38 39 #ifdef HAVE_CONFIG_H 40 #include "config.h" 41 #endif 42 43 #include <netdissect-stdinc.h> 44 #include <stdio.h> 45 46 #include "netdissect.h" 47 48 #define ASCII_LINELENGTH 300 49 #define HEXDUMP_BYTES_PER_LINE 16 50 #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2) 51 #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */ 52 #define HEXDUMP_HEXSTUFF_PER_LINE \ 53 (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE) 54 55 void 56 ascii_print(netdissect_options *ndo, 57 const u_char *cp, u_int length) 58 { 59 u_int caplength; 60 register u_char s; 61 62 caplength = (ndo->ndo_snapend >= cp) ? ndo->ndo_snapend - cp : 0; 63 if (length > caplength) 64 length = caplength; 65 ND_PRINT((ndo, "\n")); 66 while (length > 0) { 67 s = *cp++; 68 length--; 69 if (s == '\r') { 70 /* 71 * Don't print CRs at the end of the line; they 72 * don't belong at the ends of lines on UN*X, 73 * and the standard I/O library will give us one 74 * on Windows so we don't need to print one 75 * ourselves. 76 * 77 * In the middle of a line, just print a '.'. 78 */ 79 if (length > 1 && *cp != '\n') 80 ND_PRINT((ndo, ".")); 81 } else { 82 if (!ND_ISGRAPH(s) && 83 (s != '\t' && s != ' ' && s != '\n')) 84 ND_PRINT((ndo, ".")); 85 else 86 ND_PRINT((ndo, "%c", s)); 87 } 88 } 89 } 90 91 void 92 hex_and_ascii_print_with_offset(netdissect_options *ndo, register const char *ident, 93 register const u_char *cp, register u_int length, register u_int oset) 94 { 95 u_int caplength; 96 register u_int i; 97 register int s1, s2; 98 register int nshorts; 99 char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp; 100 char asciistuff[ASCII_LINELENGTH+1], *asp; 101 102 caplength = (ndo->ndo_snapend >= cp) ? ndo->ndo_snapend - cp : 0; 103 if (length > caplength) 104 length = caplength; 105 nshorts = length / sizeof(u_short); 106 i = 0; 107 hsp = hexstuff; asp = asciistuff; 108 while (--nshorts >= 0) { 109 s1 = *cp++; 110 s2 = *cp++; 111 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), 112 " %02x%02x", s1, s2); 113 hsp += HEXDUMP_HEXSTUFF_PER_SHORT; 114 *(asp++) = (ND_ISGRAPH(s1) ? s1 : '.'); 115 *(asp++) = (ND_ISGRAPH(s2) ? s2 : '.'); 116 i++; 117 if (i >= HEXDUMP_SHORTS_PER_LINE) { 118 *hsp = *asp = '\0'; 119 ND_PRINT((ndo, "%s0x%04x: %-*s %s", 120 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, 121 hexstuff, asciistuff)); 122 i = 0; hsp = hexstuff; asp = asciistuff; 123 oset += HEXDUMP_BYTES_PER_LINE; 124 } 125 } 126 if (length & 1) { 127 s1 = *cp++; 128 (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), 129 " %02x", s1); 130 hsp += 3; 131 *(asp++) = (ND_ISGRAPH(s1) ? s1 : '.'); 132 ++i; 133 } 134 if (i > 0) { 135 *hsp = *asp = '\0'; 136 ND_PRINT((ndo, "%s0x%04x: %-*s %s", 137 ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, 138 hexstuff, asciistuff)); 139 } 140 } 141 142 void 143 hex_and_ascii_print(netdissect_options *ndo, register const char *ident, 144 register const u_char *cp, register u_int length) 145 { 146 hex_and_ascii_print_with_offset(ndo, ident, cp, length, 0); 147 } 148 149 /* 150 * telnet_print() wants this. It is essentially default_print_unaligned() 151 */ 152 void 153 hex_print_with_offset(netdissect_options *ndo, 154 const char *ident, const u_char *cp, u_int length, 155 u_int oset) 156 { 157 u_int caplength; 158 register u_int i, s; 159 register int nshorts; 160 161 caplength = (ndo->ndo_snapend >= cp) ? ndo->ndo_snapend - cp : 0; 162 if (length > caplength) 163 length = caplength; 164 nshorts = (u_int) length / sizeof(u_short); 165 i = 0; 166 while (--nshorts >= 0) { 167 if ((i++ % 8) == 0) { 168 ND_PRINT((ndo,"%s0x%04x: ", ident, oset)); 169 oset += HEXDUMP_BYTES_PER_LINE; 170 } 171 s = *cp++; 172 ND_PRINT((ndo," %02x%02x", s, *cp++)); 173 } 174 if (length & 1) { 175 if ((i % 8) == 0) 176 ND_PRINT((ndo,"%s0x%04x: ", ident, oset)); 177 ND_PRINT((ndo," %02x", *cp)); 178 } 179 } 180 181 /* 182 * just for completeness 183 */ 184 void 185 hex_print(netdissect_options *ndo,const char *ident, const u_char *cp, u_int length) 186 { 187 hex_print_with_offset(ndo, ident, cp, length, 0); 188 } 189 190 #ifdef MAIN 191 int 192 main(int argc, char *argv[]) 193 { 194 hex_print("\n\t", "Hello, World!\n", 14); 195 printf("\n"); 196 hex_and_ascii_print("\n\t", "Hello, World!\n", 14); 197 printf("\n"); 198 ascii_print("Hello, World!\n", 14); 199 printf("\n"); 200 #define TMSG "Now is the winter of our discontent...\n" 201 hex_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100); 202 printf("\n"); 203 hex_and_ascii_print_with_offset("\n\t", TMSG, sizeof(TMSG) - 1, 0x100); 204 printf("\n"); 205 exit(0); 206 } 207 #endif /* MAIN */ 208