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