-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Darren Mulholland
committed
Jun 9, 2017
0 parents
commit 99d2d62
Showing
3 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
#!/usr/bin/env python3 | ||
# -------------------------------------------------------------------------- | ||
# Hexdump command line utility. Requires Python 3. | ||
# | ||
# Author: Darren Mulholland <darren@mulholland.xyz> | ||
# License: Public Domain | ||
# -------------------------------------------------------------------------- | ||
|
||
import argparse | ||
import os | ||
import signal | ||
import sys | ||
import shutil | ||
|
||
|
||
# Application version number. | ||
__version__ = '2.0.0' | ||
|
||
|
||
# Command line help text. | ||
helptext = """ | ||
Usage: %s [FLAGS] [OPTIONS] ARGUMENTS | ||
Hexdump utility. | ||
Arguments: | ||
[file] File to dump. Defaults to stdin. | ||
Options: | ||
-l, --line <int> Bytes per line in output (defaults to 16). | ||
-n, --number <int> Number of bytes to read. | ||
-o, --offset <int> Byte offset at which to begin reading. | ||
Flags: | ||
--help Display this help text and exit. | ||
--version Display version number and exit. | ||
""" % os.path.basename(sys.argv[0]) | ||
|
||
|
||
# Custom argparse 'action' to print our own help text instead of the default. | ||
class HelpAction(argparse.Action): | ||
def __call__(self, parser, namespace, values, option_string=None): | ||
print(helptext.strip()) | ||
sys.exit() | ||
|
||
|
||
# Suppress 'broken pipe' warnings when piping output through pagers. | ||
# This resolves the issue when running under Cygwin on Windows. | ||
if hasattr(signal, 'SIGPIPE'): | ||
signal.signal(signal.SIGPIPE, signal.SIG_DFL) | ||
|
||
|
||
# Wrapper for the sys.stdout.write() function. | ||
def write(s): | ||
# Suppress 'broken pipe' warnings when piping output through pagers. | ||
# This resolves the issue when running natively on Windows. | ||
try: | ||
sys.stdout.write(s) | ||
except (IOError, OSError): | ||
sys.exit() | ||
|
||
|
||
# Print a single line of output to stdout. | ||
def writeln(offset, buffer, bytes_per_line): | ||
|
||
# Write the line number. | ||
write('% 6X │' % offset) | ||
|
||
for i in range(bytes_per_line): | ||
|
||
# Write an extra space in front of every fourth byte except the first. | ||
if i > 0 and i % 4 == 0: | ||
write(' ') | ||
|
||
# Write the byte in hex form, or a spacer if we're out of bytes. | ||
write(' %02X' % buffer[i] if i < len(buffer) else ' ') | ||
|
||
write(' │ ') | ||
|
||
# Write a character for each byte in the printable ascii range. | ||
for i in range(len(buffer)): | ||
write('%c' % buffer[i] if 32 <= buffer[i] <= 126 else '·') | ||
|
||
write('\n') | ||
|
||
|
||
# Dump the specified file to stdout. | ||
def dump(file, offset, bytes_to_read, bytes_per_line): | ||
|
||
# If an offset has been specified, attempt to seek to it. | ||
if offset: | ||
if file.seekable(): | ||
file.seek(offset) | ||
else: | ||
sys.exit('Error: %s is not seekable.' % file.name) | ||
|
||
# Print a line. | ||
cols, _ = shutil.get_terminal_size() | ||
print("─" * cols) | ||
|
||
# Read and dump one line per iteration. | ||
while True: | ||
|
||
# If bytes_to_read < 0 (read all), try to read one full line. | ||
if bytes_to_read < 0: | ||
max_bytes = bytes_per_line | ||
|
||
# Else if line length < bytes_to_read, try to read one full line. | ||
elif bytes_per_line < bytes_to_read: | ||
max_bytes = bytes_per_line | ||
|
||
# Otherwise, try to read all the remaining bytes in one go. | ||
else: | ||
max_bytes = bytes_to_read | ||
|
||
# Attempt to read up to max_bytes from the file. | ||
buffer = file.read(max_bytes) | ||
|
||
# A buffer length of zero means we're done. | ||
if len(buffer): | ||
writeln(offset, buffer, bytes_per_line) | ||
offset += len(buffer) | ||
bytes_to_read -= len(buffer) | ||
else: | ||
break | ||
|
||
print("─" * cols) | ||
|
||
|
||
def main(): | ||
|
||
# Setting add_help to false prevents the parser from automatically | ||
# generating a -h flag. | ||
parser = argparse.ArgumentParser(add_help=False) | ||
|
||
# The filename argument is optional. We default to reading from | ||
# stdin if it's omitted. | ||
parser.add_argument('file', | ||
nargs='?', | ||
help='file to dump (default: stdin)', | ||
type=argparse.FileType('rb'), | ||
default=sys.stdin.buffer, | ||
) | ||
|
||
# Flags. | ||
parser.add_argument('--help', | ||
action = HelpAction, | ||
nargs=0, | ||
help = 'print this help message and exit', | ||
) | ||
parser.add_argument('--version', | ||
action='version', | ||
version=__version__, | ||
) | ||
|
||
# Options. | ||
parser.add_argument('-l', '--line', | ||
help='bytes per line in output (default: 16)', | ||
default=16, | ||
type=int, | ||
dest='bpl', | ||
) | ||
parser.add_argument('-n', '--number', | ||
nargs='?', | ||
help='number of bytes to read (default: 256)', | ||
type=int, | ||
default=-1, | ||
const=256, | ||
dest='btr', | ||
) | ||
parser.add_argument('-o', '--offset', | ||
help='offset at which to begin reading (default: 0)', | ||
type=int, | ||
default=0, | ||
dest='offset', | ||
) | ||
|
||
args = parser.parse_args() | ||
dump(args.file, args.offset, args.btr, args.bpl) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <http://unlicense.org/>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
# Hex | ||
|
||
A hexdump command line utility implemented in Python. | ||
|
||
Sample output: | ||
|
||
$ hex < hex.py -n 128 | ||
──────────────────────────────────────────────────────────────────────────────── | ||
0 │ 23 21 2F 75 73 72 2F 62 69 6E 2F 65 6E 76 20 70 │ #!/usr/bin/env p | ||
10 │ 79 74 68 6F 6E 33 0A 23 20 2D 2D 2D 2D 2D 2D 2D │ ython3·# ------- | ||
20 │ 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D │ ---------------- | ||
30 │ 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D │ ---------------- | ||
40 │ 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D │ ---------------- | ||
50 │ 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D 2D │ ---------------- | ||
60 │ 2D 2D 2D 0A 23 20 48 65 78 64 75 6D 70 20 63 6F │ ---·# Hexdump co | ||
70 │ 6D 6D 61 6E 64 20 6C 69 6E 65 20 75 74 69 6C 69 │ mmand line utili | ||
──────────────────────────────────────────────────────────────────────────────── | ||
|
||
Interface: | ||
|
||
$ hex --help | ||
|
||
Usage: hex [FLAGS] [OPTIONS] ARGUMENTS | ||
|
||
Hexdump utility. | ||
|
||
Arguments: | ||
[file] File to dump. Defaults to stdin. | ||
|
||
Options: | ||
-l, --line <int> Bytes per line in output (defaults to 16). | ||
-n, --number <int> Number of bytes to read. | ||
-o, --offset <int> Byte offset at which to begin reading. | ||
|
||
Flags: | ||
--help Display this help text and exit. | ||
--version Display version number and exit. |