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