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