Data-Hexdumper-3.0001/0000755000076500000240000000000011761233730013362 5ustar davidstaffData-Hexdumper-3.0001/ARTISTIC.txt0000644000076500000240000001373711612550577015366 0ustar davidstaff The "Artistic License" Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder as specified below. "Copyright Holder" is whoever is named in the copyright or copyrights for the package. "You" is you, if you're thinking about copying or distributing this Package. "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as uunet.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) give non-standard executables non-standard names, and clearly document the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. You may embed this Package's interpreter within an executable of yours (by linking); this shall be construed as a mere form of aggregation, provided that the complete Standard Version of the interpreter is so embedded. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whoever generated them, and may be sold commercially, and may be aggregated with this Package. If such scripts or library files are aggregated with this Package via the so-called "undump" or "unexec" methods of producing a binary executable image, then distribution of such an image shall neither be construed as a distribution of this Package nor shall it fall under the restrictions of Paragraphs 3 and 4, provided that you do not represent such an executable image as a Standard Version of this Package. 7. C subroutines (or comparably compiled subroutines in other languages) supplied by you and linked into this Package in order to emulate subroutines and variables of the language defined by this Package shall not be considered part of this Package, but are the equivalent of input as in Paragraph 6, provided these subroutines do not change the language in any way that would cause it to fail the regression tests for the language. 8. Aggregation of this Package with a commercial distribution is always permitted provided that the use of this Package is embedded; that is, when no overt attempt is made to make this Package's interfaces visible to the end user of the commercial distribution. Such use shall not be construed as a distribution of this Package. 9. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Data-Hexdumper-3.0001/CHANGELOG0000644000076500000240000000233411761233521014574 0ustar davidstaff3.0001 2012-05-29 Trivial spelling fix, RT 70221 3.00 2011-07-28 Add output_format. Minor incompatible change for number_format. 2.01 2009-03-03 Bugfix: Q< and Q> now work on pre- perl 5.10; 64-bit support now works on 32-bit machines, and < and > work on pre- perl 5.10.0 (this is because to implement the Q>/Q< fix, I needed to emulate pack() so am no longer bound by its limitations) 2.0 2009-03-02 Add hexdump($string) and hexdump($string, { opts }) Add 64-bit support 1.4 2008-05-18 Fix bug where data is ASCII 0 (thanks to Stefan Siegl) 1.3 2007-08-20 Fix bug when data length is one character 1.2 2004-02-20 1.1 2003-08-10 Emit fewer warnings 1.0.1 2002-09-20 Lack of bug reports indicated that it was ready for a full 1.0 version, which swiftly got updated to 1.0.1 as I fixed a bug where the data wasn't an integer multiple of word length 0.01 2001-02-09 Initial release Data-Hexdumper-3.0001/GPL2.txt0000644000076500000240000004310311612550577014636 0ustar davidstaff GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. Data-Hexdumper-3.0001/lib/0000755000076500000240000000000011761233730014130 5ustar davidstaffData-Hexdumper-3.0001/lib/Data/0000755000076500000240000000000011761233730015001 5ustar davidstaffData-Hexdumper-3.0001/lib/Data/Hexdumper.pm0000644000076500000240000003320411761233375017307 0ustar davidstaffpackage Data::Hexdumper; use strict; use warnings; use vars qw($VERSION @ISA @EXPORT); $VERSION = "3.0001"; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(hexdump); use constant BIGENDIAN => (unpack("h*", pack("s", 1)) =~ /01/); use constant LITTLEENDIAN => (unpack("h*", pack("s", 1)) =~ /^1/); # static data, tells us the length of each type of word my %num_bytes=( '%C' => 1, # unsigned char '%S' => 2, # unsigned 16-bit '%L' => 4, # unsigned 32-bit '%L<' => 4, # unsigned 32-bit, little-endian '%L>' => 4, # unsigned 32-bit, big-endian '%V' => 4, # unsigned 32-bit, little-endian '%N' => 4, # unsigned 32-bit, big-endian '%S<' => 2, # unsigned 16-bit, little-endian '%S>' => 2, # unsigned 16-bit, big-endian '%v' => 2, # unsigned 16-bit, little-endian '%n' => 2, # unsigned 16-bit, big-endian '%Q' => 8, # unsigned 64-bit '%Q<' => 8, # unsigned 64-bit, little-endian '%Q>' => 8, # unsigned 64-bit, big-endian ); my %number_format_to_new_format = ( 'C' => ' %4a : %16C : %d', 'S' => ' %4a : %8S : %d', 'S<' => ' %4a : %8S< : %d', 'S>' => ' %4a : %8S> : %d', 'L' => ' %4a : %4L : %d', 'L<' => ' %4a : %4L< : %d', 'L>' => ' %4a : %4L> : %d', 'Q' => ' %4a : %2Q : %d', 'Q<' => ' %4a : %2Q< : %d', 'Q>' => ' %4a : %2Q> : %d', ); =head1 NAME Data::Hexdumper - Make binary data human-readable =head1 SYNOPSIS use Data::Hexdumper qw(hexdump); print hexdump( data => $data, # what to dump # NB number_format is deprecated number_format => 'S', # display as unsigned 'shorts' start_position => 100, # start at this offset ... end_position => 148 # ... and end at this offset ); print hexdump( "abcdefg", { output_format => '%4a : %C %S< %L> : %d' } ); =head1 DESCRIPTION C provides a simple way to format arbitrary binary data into a nice human-readable format, somewhat similar to the Unix 'hexdump' utility. It gives the programmer a considerable degree of flexibility in how the data is formatted, with sensible defaults. It is envisaged that it will primarily be of use for those wrestling alligators in the swamp of binary file formats, which is why it was written in the first place. =head1 SUBROUTINES The following subroutines are exported by default, although this is deprecated and will be removed in some future version. Please pretend that you need to ask the module to export them to you. If you do assume that the module will always export them, then you may also assume that your code will break at some point after 1 Aug 2012. =head2 hexdump Does everything. Takes a hash of parameters, one of which is mandatory, the rest having sensible defaults if not specified. Available parameters are: =over =item data A scalar containing the binary data we're interested in. This is mandatory. =item start_position An integer telling us where in C to start dumping. Defaults to the beginning of C. =item end_position An integer telling us where in C to stop dumping. Defaults to the end of C. =item number_format This is deprecated. See 'INCOMPATIBLE CHANGES' below. If you use this your data will be padded with NULLs to be an integer multiple of 16 bytes. You can expect number_format to be removed at some point in 2014 or later. A string specifying how to format the data. It can be any of the following, which you will notice have the same meanings as they do to perl's C function: =over =item C - unsigned char =item S - unsigned 16-bit, native endianness =item v or SE - unsigned 16-bit, little-endian =item n or SE - unsigned 16-bit, big-endian =item L - unsigned 32-bit, native endianness =item V or LE - unsigned 32-bit, little-endian =item N or LE - unsigned 32-bit, big-endian =item Q - unsigned 64-bit, native endianness =item QE - unsigned 64-bit, little-endian =item QE - unsigned 64-bit, big-endian =back Note that 64-bit formats are *always* available, even if your perl is only 32-bit. Similarly, using E and E on the S and L formats always works, even if you're using a pre 5.10.0 perl. That's because this code doesn't use C. =item output_format This is an alternative and much more flexible (but more complex) method of specifying the output format. Instead of specifying a single format for all your output, you can specify formats like: %4a : %C %S %L> %Q : %d which will, on each line, display first the address (consisting of '0x' and 4 hexadecimal digits, zero-padded if necessary), then a space, then a colon, then a single byte of data, then a space, then an unsigned 16-bit value in native endianness, then a space, then an unsigned 32-bit big-endian value, ... then a colon, a space, then the characters representing your 15 byte record. You can use exactly the same characters and character sequences as are specified above for number_format, plus 'a' for the address, and 'd' for the data. To output a literal % character, use %% as is normal with formats - see sprintf for details. To output a literal E or E character where it may be confused with any of the {S,L,Q}{E,E} sequences, use %E or %E. So, for example, to output a 16-bit value in native endianness followed by <, use %S%<. %a takes an optional base-ten number between the % and the a signifying the number of hexadecimal digits. This defaults to 4. %{C,S,L,Q} also take an optional base-ten number between the % and the letter, signifying the number of repeats. These will be separated by spaces in the output. So '%4C' is equivalent to '%C %C %C %C'. Anything else will get printed literally. This format will be repeated for as many lines as necessary. If the amount of data isn't enough to completely fill the last line, it will be padded with NULL bytes. To specify both number_format and output_format is a fatal error. If neither are given, output_format defaults to: ' %4a : %16C : %d' which is equivalent to the old-style: number_format => 'C' =item suppress_warnings Make this true if you want to suppress any warnings - such as that your data may have been padded with NULLs if it didn't exactly fit into an integer number of words, or if you do something that is deprecated. =item space_as_space Make this true if you want spaces (ASCII character 0x20) to be printed as spaces Otherwise, spaces will be printed as full stops / periods (ASCII 0x2E). =back Alternatively, you can supply the parameters as a scalar chunk of data followed by an optional hashref of the other options: $results = hexdump($string); $results = hexdump( $string, { start_position => 100, end_position => 148 } ); =cut sub hexdump { my @params = @_; # first let's see if we need to massage the data into canonical form ... if($#params == 0) { # one param: hexdump($string) @params = (data => $params[0]); } elsif($#params == 1 && ref($params[1])) { # two: hexdump($foo, {...}) @params = ( data => $params[0], %{$params[1]} ) } my %params=@params; my($data, $number_format, $output_format, $start_position, $end_position)= @params{qw(data number_format output_format start_position end_position)}; die("can't have both number_format and output_format\n") if($output_format && $number_format); my $addr = $start_position ||= 0; $end_position ||= length($data)-1; if(!$output_format) { # $output_format = ' %a : %C %C %C %C %C %C %C %C %C %C %C %C %C %C %C %C : %d'; warn("Data::Hexdumper: number_format is deprecated\n") if($number_format && !$params{suppress_warnings}); $number_format ||= 'C'; if($number_format eq 'V') { $number_format = 'L<'; } if($number_format eq 'N') { $number_format = 'L>'; } if($number_format eq 'v') { $number_format = 'S<'; } if($number_format eq 'n') { $number_format = 'S>'; } $output_format = $number_format_to_new_format{$number_format} || die("number_format not recognised\n"); } my @format_elements_raw = split(//, $output_format); my @format_elements; while(@format_elements_raw) { push @format_elements, shift(@format_elements_raw); if($format_elements[-1] eq '%') { while(exists($format_elements_raw[0]) && $format_elements_raw[0] =~ /\d/) { $format_elements[-1] .= shift(@format_elements_raw); } if(exists($format_elements_raw[0]) && $format_elements_raw[0] =~ /[adCSLQ%<>]/) { $format_elements[-1] .= shift(@format_elements_raw); } if($format_elements[-1] =~ /%([%<>])/) { $format_elements[-1] = $1 } elsif($format_elements[-1] =~ /%\d*[QSL]/ && exists($format_elements_raw[0]) && $format_elements_raw[0] =~ /[<>]/ ) { $format_elements[-1] .= shift(@format_elements_raw); } } } @format_elements = map { my $format = $_; my @r; if($format =~ /^([^%]|%\d*a|%\D|%$)/) { push @r, $format; } else { $format =~ /^%(\d+)(.*)/; push @r, ('%'.$2, ' ') x $1; pop @r; # get rid of the last space } @r; } @format_elements; my $chunk_length = 0; foreach my $format (grep { /^%[CSLQ]/ } @format_elements) { $chunk_length += $num_bytes{$format}; } # sanity-check the parameters die("No data given to hexdump.") unless length($data); die("start_position must be numeric.") if($start_position=~/\D/); die("end_position must be numeric.") if($end_position=~/\D/); die("end_position must not be before start_position.") if($end_position < $start_position); # extract the required range and pad end with NULLs if necessary $data=substr($data, $start_position, 1+$end_position-$start_position); if(length($data) / $chunk_length != int(length($data) / $chunk_length)) { warn "Data::Hexdumper: data length isn't an integer multiple of lines\n". "so has been padded with NULLs at the end.\n" unless($params{suppress_warnings}); $data .= pack('C', 0) x ($chunk_length - length($data) + int(length($data)/$chunk_length)*$chunk_length); } my $output=''; # where we put the formatted results while(length($data)) { # Get a chunk my $chunk = substr($data, 0, $chunk_length); $data = ($chunk eq $data) ? '' : substr($data, $chunk_length); my $characters = $chunk; # replace any non-printable character with . if($params{space_as_space}) { $characters =~ s/[^a-z0-9\\|,.<>;:'\@[{\]}#`!"\$%^&*()_+=~?\/ -]/./gi; } else { $characters =~ s/[^a-z0-9\\|,.<>;:'\@[{\]}#`!"\$%^&*()_+=~?\/-]/./gi; } foreach my $format (@format_elements) { if(length($format) == 1) { # pass straight through $output .= $format; } elsif($format =~ /%(\d*)a/) { # address my $nibbles = $1 || 4; $output .= sprintf("0x%0${nibbles}X", $addr); } elsif($format eq '%d') { # data $output .= $characters; } else { my $word = substr($chunk, 0, $num_bytes{$format}); if(length($chunk) > $num_bytes{$format}) { $chunk = substr($chunk, $num_bytes{$format}); } else { $chunk = ''; } $output .= _format_word($format, $word); } } $output .= "\n"; $addr += $chunk_length; } $output; } sub _format_word { my($format, $data) = @_; # big endian my @bytes = map { ord($_) } split(//, $data); # make little endian if necessary @bytes = reverse(@bytes) if($format =~ // && LITTLEENDIAN)); return join('', map { sprintf('%02X', $_) } @bytes); } =head1 SEE ALSO L L if your needs are simple perldoc -f unpack perldoc -f pack =head1 INCOMPATIBLE CHANGES 'number_format' is now implemented in terms of 'output_format'. Your data will be padded to a multiple of 16 bytes. Previously-silent code may now emit warnings. The mappings are: 'C' => ' %4a : %C %C %C %C %C %C %C %C %C %C %C %C %C %C %C %C : %d' 'S' => ' %4a : %S %S %S %S %S %S %S %S : %d' 'S<' => ' %4a : %S< %S< %S< %S< %S< %S< %S< %S< : %d' 'S>' => ' %4a : %S> %S> %S> %S> %S> %S> %S> %S> : %d' 'L' => ' %4a : %L %L %L %L : %d' 'L<' => ' %4a : %L< %L< %L< %L< : %d' 'L>' => ' %4a : %L> %L> %L> %L> : %d' 'Q' => ' %4a : %Q %Q : %d' 'Q<' => ' %4a : %Q< %Q< : %d' 'Q>' => ' %4a : %Q> %Q> : %d' and of course: 'V' => 'L<' 'N' => 'L>' 'v' => 'S<' 'n' => 'S>' =head1 BUGS/LIMITATIONS Behaviour of %a is not defined if your file is too big. Behaviour of %NNa is not defined if NN is too big for your sprintf implementation to handle 0x%0${NN}X. =head1 FEEDBACK I welcome constructive criticism and bug reports. Please report bugs either by email or via RT: L The best bug reports contain a test file that fails with the current code, and will pass once it has been fixed. The code repository is on Github: L =head1 AUTHOR, COPYRIGHT and LICENCE Copyright 2001 - 2012 David Cantrell EFE This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. =head1 CONSPIRACY This module is also free-as-in-mason software. =head1 THANKS TO ... MHX, for reporting a bug when dumping a single byte of data Stefan Siegl, for reporting a bug when dumping an ASCII 0 Steffen Winkler, for inspiring me to use proper output formats =cut 1; Data-Hexdumper-3.0001/Makefile.PL0000755000076500000240000000071611761233636015350 0ustar davidstaffuse ExtUtils::MakeMaker; WriteMakefile( NAME => 'Data::Hexdumper', META_MERGE => { license => 'other', resources => { repository => { web => 'https://github.com/DrHyde/perl-modules-Data-Hexdumper' }, bugtracker => { web => 'https://github.com/DrHyde/perl-modules-Data-Hexdumper/issues/new' }, }, }, VERSION_FROM => 'lib/Data/Hexdumper.pm', PREREQ_PM => { 'Test::More' => 0.65, } ); Data-Hexdumper-3.0001/MANIFEST0000644000076500000240000000051111761233730014510 0ustar davidstaffCHANGELOG MANIFEST Makefile.PL README lib/Data/Hexdumper.pm t/regression.t t/pod-coverage.t t/pod.t ARTISTIC.txt GPL2.txt t/endianspecifiers.t t/output_format.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Data-Hexdumper-3.0001/META.json0000644000076500000240000000152711761233730015010 0ustar davidstaff{ "abstract" : "unknown", "author" : [ "unknown" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.112150", "license" : [ "unknown" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Data-Hexdumper", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : 0 } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : 0 } }, "runtime" : { "requires" : { "Test::More" : "0.65" } } }, "release_status" : "stable", "resources" : {}, "version" : "3.0001" } Data-Hexdumper-3.0001/META.yml0000644000076500000240000000071611761233730014637 0ustar davidstaff--- abstract: unknown author: - unknown build_requires: ExtUtils::MakeMaker: 0 configure_requires: ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.112150' license: unknown meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Data-Hexdumper no_index: directory: - t - inc requires: Test::More: 0.65 resources: {} version: 3.0001 Data-Hexdumper-3.0001/README0000644000076500000240000000026611612550577014254 0ustar davidstaffThis module dumps binary data in a variety of formats, allowing control over endianness and word length. To install, do the usual: perl Makefile.PL make make test make install Data-Hexdumper-3.0001/t/0000755000076500000240000000000011761233730013625 5ustar davidstaffData-Hexdumper-3.0001/t/endianspecifiers.t0000755000076500000240000000235211614106265017331 0ustar davidstaff#!perl -w # $Id: endianspecifiers.t,v 1.1 2009/03/03 20:18:06 drhyde Exp $ use strict; use Test::More tests => 4; use Data::Hexdumper qw(hexdump); is_deeply(hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'n', suppress_warnings => 1, ) , hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'S>', suppress_warnings => 1, ), "n == S>"); is_deeply(hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'v', suppress_warnings => 1, ) , hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'S<', suppress_warnings => 1, ), "v == S<"); is_deeply(hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'N', suppress_warnings => 1, ) , hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'L>', suppress_warnings => 1, ), "N == L>"); is_deeply(hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'V', suppress_warnings => 1, ) , hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'L<', suppress_warnings => 1, ), "V == L<"); Data-Hexdumper-3.0001/t/output_format.t0000755000076500000240000000300411614104605016715 0ustar davidstaff#!perl -w use strict; use warnings; use Test::More tests => 7; use Data::Hexdumper qw(hexdump); eval { hexdump(data => '0123456789ABCDEF', number_format => 'C', output_format => '%C'); }; ok($@, "number_format with output_format is fatal"); is( hexdump(data => 'abcdefghijklmno', output_format => '%4a %C %S %L< %Q> %d'), Data::Hexdumper::LITTLEENDIAN ? "0x0000 61 6362 67666564 68696A6B6C6D6E6F abcdefghijklmno\n" : "0x0000 61 6263 67666564 68696A6B6C6D6E6F abcdefghijklmno\n", "mixed formats work" ); is( hexdump(data => 'abcdefghijklmno', output_format => '%4a %%C % < > %C %S%> %L%< %Q%% %d'), Data::Hexdumper::LITTLEENDIAN ? "0x0000 %C % < > 61 6362> 67666564< 6F6E6D6C6B6A6968% abcdefghijklmno\n" : "0x0000 %C % < > 61 6263> 64656667< 68696A6B6C6D6E6F% abcdefghijklmno\n", "%{%,<,>} work" ); is( hexdump(data => 'abcdefgh', output_format => '%4a %L< %L<'), hexdump(data => 'abcdefgh', output_format => '%a %L< %L<'), '%4a == %a' ); is( hexdump(data => 'abcdefgh', output_format => '%8a %L< %L<'), "0x00000000 64636261 68676665\n", '%8a works' ); is( hexdump(data => 'abcdefghabcdefgh', output_format => '%11a %L< %L<'), "0x00000000000 64636261 68676665\n0x00000000008 64636261 68676665\n", '%11a works' ); is( hexdump(data => 'abcdefgh', suppress_warnings => 1, output_format => '%a %2Q %3C %4S< %1L'), hexdump(data => 'abcdefgh', suppress_warnings => 1, output_format => '%a %Q %Q %C %C %C %S< %S< %S< %S< %L'), '%2Q %3C %4S< %1L == %Q %Q %C %C %C %S< %S< %S< %S< %L' ); Data-Hexdumper-3.0001/t/pod-coverage.t0000644000076500000240000000036311612550577016375 0ustar davidstaff# $Id: pod-coverage.t,v 1.1 2007/07/30 12:49:38 drhyde Exp $ use strict; $^W=1; use Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok(); Data-Hexdumper-3.0001/t/pod.t0000644000076500000240000000031211612550577014576 0ustar davidstaff# $Id: pod.t,v 1.1 2007/07/30 12:49:38 drhyde Exp $ use strict; $^W=1; use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); Data-Hexdumper-3.0001/t/regression.t0000755000076500000240000001515111614106555016201 0ustar davidstaff#!perl -w # $Id: regression.t,v 1.1 2009/03/02 22:00:39 drhyde Exp $ use strict; use Test::More tests => 16; use Data::Hexdumper qw(hexdump); eval { hexdump('foo', {number_format => 'R', suppress_warnings => 1}) }; ok($@, "invalid format is fatal: $@"); is("\n".hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'N', start_position => 0, suppress_warnings => 1, end_position => 0x1F ), q{ 0x0000 : 20212223 24252627 28292A2B 2C2D2E2F : .!"#$%&'()*+,-./ 0x0010 : 30313233 34353637 38393A3B 3C3D3E3F : 0123456789:;<=>? }, "big-endian 32-bit words, no padding"); ok("\n".hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), number_format => 'N', start_position => 0, end_position => 0x1F, suppress_warnings => 1, space_as_space => 1 ) eq q{ 0x0000 : 20212223 24252627 28292A2B 2C2D2E2F : !"#$%&'()*+,-./ 0x0010 : 30313233 34353637 38393A3B 3C3D3E3F : 0123456789:;<=>? }, "space_as_space"); ok("\n".hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), suppress_warnings => 1, number_format => 'N' ) eq q{ 0x0000 : 20212223 24252627 28292A2B 2C2D2E2F : .!"#$%&'()*+,-./ 0x0010 : 30313233 34353637 38393A3B 3C3D3E3F : 0123456789:;<=>? }, "start_position and end_position defaults"); my $results = ''; foreach my $format(qw (C n v V)) { # same trivial test for other formats $results .= hexdump( data => join('', map { pack('C', $_) } (0x20 .. 0x3F)), suppress_warnings => 1, number_format => $format ); } ok("\n".$results eq q{ 0x0000 : 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F : .!"#$%&'()*+,-./ 0x0010 : 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F : 0123456789:;<=>? 0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F : .!"#$%&'()*+,-./ 0x0010 : 3031 3233 3435 3637 3839 3A3B 3C3D 3E3F : 0123456789:;<=>? 0x0000 : 2120 2322 2524 2726 2928 2B2A 2D2C 2F2E : .!"#$%&'()*+,-./ 0x0010 : 3130 3332 3534 3736 3938 3B3A 3D3C 3F3E : 0123456789:;<=>? 0x0000 : 23222120 27262524 2B2A2928 2F2E2D2C : .!"#$%&'()*+,-./ 0x0010 : 33323130 37363534 3B3A3938 3F3E3D3C : 0123456789:;<=>? }, "other data formats"); ok("\n".hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'N' ) eq q{ 0x0000 : 10111213 14151617 18191A1B 1C1D1E1F : ................ 0x0010 : 20212223 24252627 28292A2B 2C2D2E2F : .!"#$%&'()*+,-./ }, "unprintable characters"); ok(hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'S' ) eq hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'n' ) || hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'S' ) eq hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'v' ), "S eq n or v"); ok(hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'L' ) eq hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'N' ) || hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'L' ) eq hexdump( data => join('', map { pack('C', $_) } (0x10 .. 0x2F)), suppress_warnings => 1, number_format => 'V' ), "L eq N or V"); $results = ''; foreach my $format (qw(N n)) { foreach my $max (0x3C, 0x3D, 0x3E) { $results .= hexdump( data => join('', map { pack('C', $_) } (0x20 .. $max)), suppress_warnings => 1, number_format => $format, suppress_warnings => 1 ); } } is("\n".$results, q{ 0x0000 : 20212223 24252627 28292A2B 2C2D2E2F : .!"#$%&'()*+,-./ 0x0010 : 30313233 34353637 38393A3B 3C000000 : 0123456789:;<... 0x0000 : 20212223 24252627 28292A2B 2C2D2E2F : .!"#$%&'()*+,-./ 0x0010 : 30313233 34353637 38393A3B 3C3D0000 : 0123456789:;<=.. 0x0000 : 20212223 24252627 28292A2B 2C2D2E2F : .!"#$%&'()*+,-./ 0x0010 : 30313233 34353637 38393A3B 3C3D3E00 : 0123456789:;<=>. 0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F : .!"#$%&'()*+,-./ 0x0010 : 3031 3233 3435 3637 3839 3A3B 3C00 0000 : 0123456789:;<... 0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F : .!"#$%&'()*+,-./ 0x0010 : 3031 3233 3435 3637 3839 3A3B 3C3D 0000 : 0123456789:;<=.. 0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F : .!"#$%&'()*+,-./ 0x0010 : 3031 3233 3435 3637 3839 3A3B 3C3D 3E00 : 0123456789:;<=>. }, "NULL-padding"); is("\n".hexdump(data => '!', suppress_warnings => 1), q{ 0x0000 : 21 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : !............... }, "Single byte data"); is("\n".hexdump(data => '0', suppress_warnings => 1), q{ 0x0000 : 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : 0............... }, "Can dump a zero (0, not NULL!) byte"); ok(hexdump(data => 'abcdefghijklmnop') eq hexdump('abcdefghijklmnop'), 'hexdump($string) works'); ok(hexdump( data => join('', map { pack('C', $_) } (0x00 .. 0x3F)), number_format => 'N', start_position => 0, suppress_warnings => 1, end_position => 0x1F, space_as_space => 1 ) eq hexdump( join('', map { pack('C', $_) } (0x00 .. 0x3F)), { number_format => 'N', start_position => 0, suppress_warnings => 1, end_position => 0x1F, space_as_space => 1 } ), 'hexdump($string, {... opts ...}) works'); ok((Data::Hexdumper::LITTLEENDIAN && hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q' }) eq hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q<' }) ) || ( Data::Hexdumper::BIGENDIAN && hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q' }) eq hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q>' }) ), "64 bit native byte order works"); is_deeply("\n".hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q<' }) , q{ 0x0000 : 6867666564636261 706F6E6D6C6B6A69 : abcdefghijklmnop }, "64 bit little-endian works"); ok("\n".hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q>' }) eq q{ 0x0000 : 6162636465666768 696A6B6C6D6E6F70 : abcdefghijklmnop }, "64 bit big-endian works");