-
Notifications
You must be signed in to change notification settings - Fork 8
/
hex
executable file
·112 lines (88 loc) · 2.91 KB
/
hex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/python
# vim: set fileencoding=utf-8 tabstop=4 shiftwidth=4 :
# hex (Python script) -- Print decoded hex string
#
# Version: 1.0
# Copyright: (c)2018 Alastair Irvine <[email protected]>
# Keywords: hex decode
# Notice:
# Licence: This file is released under the GNU General Public License
#
'''Description: Decodes the command-line parameter, or stdin
Usage: hex [ -n | -s <sep> ] [ <string> ]
Options:
'''
# Licence details:
# 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.
#
# See http://www.gnu.org/licenses/gpl-2.0.html for more information.
#
# You can find the complete text of the GPLv2 in the file
# /usr/share/common-licenses/GPL-2 on Debian systems.
# Or see the file COPYING in the same directory as this program.
#
#
# TO-DO:
# + special last-line handling for sep
from __future__ import print_function
import sys
import getopt
import sys, binascii
self="hex"
allowed_options='hdns:'
allowed_long_options=['help', 'nosep', 'sep=']
# *** CLASSES ***
# *** FUNCTIONS ***
def show_help(dest=sys.stdout):
print(__doc__.rstrip(), file=dest)
def report_error(msg):
print(self + ": Error: " + msg, file=sys.stderr)
def report_warning(msg):
print(self + ": Warning: " + msg, file=sys.stderr)
def report_notice(msg):
print(self + ": Notice: " + msg, file=sys.stderr)
def process(source, sep = None):
for line in source:
if sep is False or sep is None:
print(binascii.unhexlify(line))
else:
print(binascii.unhexlify(line), end=sep)
# *** MAINLINE ***
if __name__ == '__main__':
# == Command-line parsing ==
# -- defaults --
debug = 0
# -- option handling --
try:
optlist, args = getopt.getopt(sys.argv[1:], allowed_options, allowed_long_options)
except getopt.GetoptError as e:
report_error(e)
sys.exit(1)
# Create a special dict object that defaults to False for unspecified options
from collections import defaultdict
params = defaultdict(bool)
for option, opt_arg in optlist:
if option in ("--nosep", "-n"):
params["sep"] = ""
elif option in ("--sep", "-s"):
params["sep"] = opt_arg
elif option == "-d":
debug += 1
elif option == "-h" or option == "--help":
show_help()
sys.exit(0)
# -- argument checking --
## if len(args) not in (2, 3):
## report_error("Invalid command-line parameters.")
## print("", file=sys.stderr)
## show_help(sys.stderr)
## sys.exit(1)
# -- argument handling --
if len(args) == 0:
source = (l.rstrip() for l in sys.stdin)
else:
source = iter(args)
process(source, params["sep"])