1b0453382SBill Fenner /* $NetBSD: print-ascii.c,v 1.1 1999/09/30 14:49:12 sjg Exp $ */ 2b0453382SBill Fenner 3b0453382SBill Fenner /*- 4b0453382SBill Fenner * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5b0453382SBill Fenner * All rights reserved. 6b0453382SBill Fenner * 7b0453382SBill Fenner * This code is derived from software contributed to The NetBSD Foundation 8b0453382SBill Fenner * by Alan Barrett and Simon J. Gerraty. 9b0453382SBill Fenner * 10b0453382SBill Fenner * Redistribution and use in source and binary forms, with or without 11b0453382SBill Fenner * modification, are permitted provided that the following conditions 12b0453382SBill Fenner * are met: 13b0453382SBill Fenner * 1. Redistributions of source code must retain the above copyright 14b0453382SBill Fenner * notice, this list of conditions and the following disclaimer. 15b0453382SBill Fenner * 2. Redistributions in binary form must reproduce the above copyright 16b0453382SBill Fenner * notice, this list of conditions and the following disclaimer in the 17b0453382SBill Fenner * documentation and/or other materials provided with the distribution. 18b0453382SBill Fenner * 3. All advertising materials mentioning features or use of this software 19b0453382SBill Fenner * must display the following acknowledgement: 20b0453382SBill Fenner * This product includes software developed by the NetBSD 21b0453382SBill Fenner * Foundation, Inc. and its contributors. 22b0453382SBill Fenner * 4. Neither the name of The NetBSD Foundation nor the names of its 23b0453382SBill Fenner * contributors may be used to endorse or promote products derived 24b0453382SBill Fenner * from this software without specific prior written permission. 25b0453382SBill Fenner * 26b0453382SBill Fenner * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27b0453382SBill Fenner * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28b0453382SBill Fenner * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29b0453382SBill Fenner * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30b0453382SBill Fenner * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31b0453382SBill Fenner * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32b0453382SBill Fenner * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33b0453382SBill Fenner * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34b0453382SBill Fenner * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35b0453382SBill Fenner * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36b0453382SBill Fenner * POSSIBILITY OF SUCH DAMAGE. 37b0453382SBill Fenner */ 38b0453382SBill Fenner 39b0453382SBill Fenner #ifdef HAVE_CONFIG_H 40b0453382SBill Fenner #include "config.h" 41b0453382SBill Fenner #endif 42b0453382SBill Fenner 43b0453382SBill Fenner #ifndef lint 445b0fe478SBruce M Simpson static const char rcsid[] _U_ = 455b0fe478SBruce M Simpson "@(#) $Header: /tcpdump/master/tcpdump/print-ascii.c,v 1.10.2.3 2003/12/29 22:42:20 hannes Exp $"; 46b0453382SBill Fenner #endif 475b0fe478SBruce M Simpson #include <tcpdump-stdinc.h> 48b0453382SBill Fenner #include <stdio.h> 49b0453382SBill Fenner 50b0453382SBill Fenner #include "interface.h" 51b0453382SBill Fenner 525b0fe478SBruce M Simpson #define ASCII_LINELENGTH 300 53b0453382SBill Fenner #define HEXDUMP_BYTES_PER_LINE 16 54b0453382SBill Fenner #define HEXDUMP_SHORTS_PER_LINE (HEXDUMP_BYTES_PER_LINE / 2) 55b0453382SBill Fenner #define HEXDUMP_HEXSTUFF_PER_SHORT 5 /* 4 hex digits and a space */ 56b0453382SBill Fenner #define HEXDUMP_HEXSTUFF_PER_LINE \ 57b0453382SBill Fenner (HEXDUMP_HEXSTUFF_PER_SHORT * HEXDUMP_SHORTS_PER_LINE) 58b0453382SBill Fenner 59b0453382SBill Fenner void 605b0fe478SBruce M Simpson ascii_print_with_offset(register const u_char *ident, register const u_char *cp, register u_int length, 61b0453382SBill Fenner register u_int oset) 62b0453382SBill Fenner { 63b0453382SBill Fenner register u_int i; 64b0453382SBill Fenner register int s1, s2; 65b0453382SBill Fenner register int nshorts; 66b0453382SBill Fenner char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp; 675b0fe478SBruce M Simpson char asciistuff[ASCII_LINELENGTH+1], *asp; 685b0fe478SBruce M Simpson u_int maxlength = (Aflag ? ASCII_LINELENGTH : HEXDUMP_SHORTS_PER_LINE); 69b0453382SBill Fenner 70b0453382SBill Fenner nshorts = length / sizeof(u_short); 71b0453382SBill Fenner i = 0; 72b0453382SBill Fenner hsp = hexstuff; asp = asciistuff; 735b0fe478SBruce M Simpson if (Aflag) *(asp++) = '\n'; 74b0453382SBill Fenner while (--nshorts >= 0) { 75b0453382SBill Fenner s1 = *cp++; 76b0453382SBill Fenner s2 = *cp++; 775b0fe478SBruce M Simpson if (Aflag) { 785b0fe478SBruce M Simpson i += 2; 795b0fe478SBruce M Simpson *(asp++) = (isgraph(s1) ? s1 : (s1 != '\t' && s1 != ' ' && s1 != '\n' && s1 != '\r' ? '.' : s1) ); 805b0fe478SBruce M Simpson *(asp++) = (isgraph(s2) ? s2 : (s2 != '\t' && s2 != ' ' && s2 != '\n' && s2 != '\r' ? '.' : s2) ); 815b0fe478SBruce M Simpson if (s1 == '\n' || s2 == '\n') i = maxlength; 825b0fe478SBruce M Simpson 835b0fe478SBruce M Simpson } else { 84685295f4SBill Fenner (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), 85685295f4SBill Fenner " %02x%02x", s1, s2); 86b0453382SBill Fenner hsp += HEXDUMP_HEXSTUFF_PER_SHORT; 87b0453382SBill Fenner *(asp++) = (isgraph(s1) ? s1 : '.'); 88b0453382SBill Fenner *(asp++) = (isgraph(s2) ? s2 : '.'); 895b0fe478SBruce M Simpson i++; 905b0fe478SBruce M Simpson } 915b0fe478SBruce M Simpson if (i >= maxlength) { 92b0453382SBill Fenner *hsp = *asp = '\0'; 935b0fe478SBruce M Simpson if (Aflag) { 945b0fe478SBruce M Simpson (void)printf("%s", asciistuff); 955b0fe478SBruce M Simpson } else { 965b0fe478SBruce M Simpson (void)printf("%s0x%04x: %-*s %s", 975b0fe478SBruce M Simpson ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, 98b0453382SBill Fenner hexstuff, asciistuff); 995b0fe478SBruce M Simpson } 100b0453382SBill Fenner i = 0; hsp = hexstuff; asp = asciistuff; 101b0453382SBill Fenner oset += HEXDUMP_BYTES_PER_LINE; 102b0453382SBill Fenner } 103b0453382SBill Fenner } 104b0453382SBill Fenner if (length & 1) { 105b0453382SBill Fenner s1 = *cp++; 1065b0fe478SBruce M Simpson if (Aflag) { 1075b0fe478SBruce M Simpson *(asp++) = (isgraph(s1) ? s1 : (s1 != '\t' && s1 != ' ' && s1 != '\n' && s1 != '\r' ? '.' : s1) ); 1085b0fe478SBruce M Simpson } else { 109685295f4SBill Fenner (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), 110685295f4SBill Fenner " %02x", s1); 111b0453382SBill Fenner hsp += 3; 112b0453382SBill Fenner *(asp++) = (isgraph(s1) ? s1 : '.'); 1135b0fe478SBruce M Simpson } 114b0453382SBill Fenner ++i; 115b0453382SBill Fenner } 116b0453382SBill Fenner if (i > 0) { 117b0453382SBill Fenner *hsp = *asp = '\0'; 1185b0fe478SBruce M Simpson if (Aflag) { 1195b0fe478SBruce M Simpson (void)printf("%s%s", ident, asciistuff); 1205b0fe478SBruce M Simpson } else { 1215b0fe478SBruce M Simpson (void)printf("%s0x%04x: %-*s %s", 1225b0fe478SBruce M Simpson ident, oset, HEXDUMP_HEXSTUFF_PER_LINE, 123b0453382SBill Fenner hexstuff, asciistuff); 124b0453382SBill Fenner } 125b0453382SBill Fenner } 1265b0fe478SBruce M Simpson } 127b0453382SBill Fenner 128b0453382SBill Fenner void 1295b0fe478SBruce M Simpson ascii_print(register const u_char *ident, register const u_char *cp, register u_int length) 130b0453382SBill Fenner { 1315b0fe478SBruce M Simpson ascii_print_with_offset(ident, cp, length, 0); 132b0453382SBill Fenner } 133b0453382SBill Fenner 134b0453382SBill Fenner /* 135b0453382SBill Fenner * telnet_print() wants this. It is essentially default_print_unaligned() 136b0453382SBill Fenner */ 137b0453382SBill Fenner void 1385b0fe478SBruce M Simpson hex_print_with_offset(register const u_char *ident, register const u_char *cp, register u_int length, 139b0453382SBill Fenner register u_int oset) 140b0453382SBill Fenner { 141b0453382SBill Fenner register u_int i, s; 142b0453382SBill Fenner register int nshorts; 143b0453382SBill Fenner 144b0453382SBill Fenner nshorts = (u_int) length / sizeof(u_short); 145b0453382SBill Fenner i = 0; 146b0453382SBill Fenner while (--nshorts >= 0) { 147b0453382SBill Fenner if ((i++ % 8) == 0) { 1485b0fe478SBruce M Simpson (void)printf("%s0x%04x: ", ident, oset); 149b0453382SBill Fenner oset += HEXDUMP_BYTES_PER_LINE; 150b0453382SBill Fenner } 151b0453382SBill Fenner s = *cp++; 152b0453382SBill Fenner (void)printf(" %02x%02x", s, *cp++); 153b0453382SBill Fenner } 154b0453382SBill Fenner if (length & 1) { 155b0453382SBill Fenner if ((i % 8) == 0) 1565b0fe478SBruce M Simpson (void)printf("%s0x%04x: ", ident, oset); 157b0453382SBill Fenner (void)printf(" %02x", *cp); 158b0453382SBill Fenner } 159b0453382SBill Fenner } 160b0453382SBill Fenner 161b0453382SBill Fenner /* 162b0453382SBill Fenner * just for completeness 163b0453382SBill Fenner */ 164b0453382SBill Fenner void 1655b0fe478SBruce M Simpson hex_print(register const u_char *ident, register const u_char *cp, register u_int length) 166b0453382SBill Fenner { 1675b0fe478SBruce M Simpson hex_print_with_offset(ident, cp, length, 0); 168b0453382SBill Fenner } 169b0453382SBill Fenner 170b0453382SBill Fenner #ifdef MAIN 171b0453382SBill Fenner int 172b0453382SBill Fenner main(int argc, char *argv[]) 173b0453382SBill Fenner { 174b0453382SBill Fenner hex_print("Hello, World!\n", 14); 175b0453382SBill Fenner printf("\n"); 176b0453382SBill Fenner ascii_print("Hello, World!\n", 14); 177b0453382SBill Fenner printf("\n"); 178b0453382SBill Fenner #define TMSG "Now is the winter of our discontent...\n" 179b0453382SBill Fenner ascii_print_with_offset(TMSG, sizeof(TMSG) - 1, 0x100); 180b0453382SBill Fenner printf("\n"); 181b0453382SBill Fenner exit(0); 182b0453382SBill Fenner } 183b0453382SBill Fenner #endif /* MAIN */ 1845b0fe478SBruce M Simpson 1855b0fe478SBruce M Simpson 186