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