Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame^] | 1 | /*****************************************************************************\ |
| 2 | * hexdump.h |
| 3 | * $Id: hexdump.h,v 1.2 2005/12/16 22:45:49 dsp_llnl Exp $ |
| 4 | \*****************************************************************************/ |
| 5 | |
| 6 | #ifndef _HEXDUMP_H |
| 7 | #define _HEXDUMP_H |
| 8 | |
| 9 | /* hexdump.h |
| 10 | * |
| 11 | * Copyright (C) 2002 |
| 12 | * David S. Peterson. All rights reserved. |
| 13 | * |
| 14 | * Author: David S. Peterson <dave_peterson@pobox.com> |
| 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions are |
| 18 | * met: |
| 19 | * 1. Redistributions of source code must retain the above copyright notice, |
| 20 | * this list of conditions, and the entire permission notice, including |
| 21 | * the following disclaimer of warranties. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions, and the entire permission notice, |
| 24 | * including the following disclaimer in the documentation and/or other |
| 25 | * materials provided with the distribution. |
| 26 | * 3. The name(s) of the author(s) may not be used to endorse or promote |
| 27 | * products derived from this software without specific prior written |
| 28 | * permission. |
| 29 | * |
| 30 | * ALTERNATIVELY, this product may be distributed under the terms of the GNU |
| 31 | * General Public License, in which case the provisions of the GPL are |
| 32 | * required INSTEAD OF the above restrictions. (This clause is necessary due |
| 33 | * to a potential bad interaction between the GPL and the restrictions |
| 34 | * contained in a BSD-style copyright.) |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR |
| 37 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 38 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. |
| 39 | * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 40 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 45 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 46 | */ |
| 47 | |
| 48 | #include <stdint.h> |
| 49 | #include <sys/types.h> |
| 50 | #include <stdio.h> |
| 51 | |
| 52 | typedef int (*is_printable_fn_t) (unsigned char c); |
| 53 | |
| 54 | /*-------------------------------------------------------------------------- |
| 55 | * hexdump_format_t |
| 56 | * |
| 57 | * This specifies how the output of the 'hexdump' function should look. |
| 58 | * |
| 59 | * fields: |
| 60 | * bytes_per_line: the number of data bytes to display per line of |
| 61 | * output |
| 62 | * addrprint_width: Each line of output begins with the address of the |
| 63 | * first data byte displayed on that line. This |
| 64 | * specifies the number of bytes wide the address |
| 65 | * should be displayed as. This value must be from 1 |
| 66 | * to 8. |
| 67 | * indent: This is a string to display at the start of each |
| 68 | * output line. Its purpose is to indent the output. |
| 69 | * sep1: This is a string to display between the address and |
| 70 | * the bytes of data displayed in hex. It serves as a |
| 71 | * separator. |
| 72 | * sep2: This is a string to display between individual hex |
| 73 | * values. It serves as a separator. |
| 74 | * sep3: This is a string to display between the bytes of |
| 75 | * data in hex and the bytes of data displayed as |
| 76 | * characters. It serves as a separator. |
| 77 | * nonprintable: This is a substitute character to display in place |
| 78 | * of nonprintable characters. |
| 79 | * is_printable_fn: This is a user-supplied function that takes a byte |
| 80 | * value as input and returns a boolean value |
| 81 | * indicating whether the corresponding character is |
| 82 | * printable. A value of NULL will cause |
| 83 | * default_is_printable_fn to be used. |
| 84 | *--------------------------------------------------------------------------*/ |
| 85 | typedef struct |
| 86 | { int bytes_per_line; |
| 87 | int addrprint_width; |
| 88 | const char *indent; |
| 89 | const char *sep1; |
| 90 | const char *sep2; |
| 91 | const char *sep3; |
| 92 | unsigned char nonprintable; |
| 93 | is_printable_fn_t is_printable_fn; |
| 94 | } |
| 95 | hexdump_format_t; |
| 96 | |
| 97 | /*-------------------------------------------------------------------------- |
| 98 | * hexdump |
| 99 | * |
| 100 | * Write a hex dump of 'mem' to 'outfile'. |
| 101 | * |
| 102 | * parameters: |
| 103 | * mem: a pointer to the memory to display |
| 104 | * bytes: the number of bytes of data to display |
| 105 | * addrprint_start: The address to associate with the first byte of |
| 106 | * data. For instance, a value of 0 indicates that the |
| 107 | * first byte displayed should be labeled as byte 0. |
| 108 | * outfile: The place where the hex dump should be written. |
| 109 | * For instance, stdout or stderr may be passed here. |
| 110 | * format: A structure specifying how the hex dump should be |
| 111 | * formatted. |
| 112 | *--------------------------------------------------------------------------*/ |
| 113 | void hexdump (const void *mem, int bytes, uint64_t addrprint_start, |
| 114 | FILE *outfile, const hexdump_format_t *format); |
| 115 | |
| 116 | /*-------------------------------------------------------------------------- |
| 117 | * default_is_printable_fn |
| 118 | * |
| 119 | * Determine whether the input character is printable. The proper behavior |
| 120 | * for this type of function may be system-dependent. This function appears |
| 121 | * to work well on a Linux system. However, if it is not adequate for your |
| 122 | * purposes, you can write your own. |
| 123 | * |
| 124 | * parameters: |
| 125 | * c: the input character |
| 126 | * |
| 127 | * return value: |
| 128 | * Return 1 if the input character is printable. Otherwise return 0. |
| 129 | *--------------------------------------------------------------------------*/ |
| 130 | int default_is_printable_fn (unsigned char c); |
| 131 | |
| 132 | #endif /* _HEXDUMP_H */ |