-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbase16
executable file
·147 lines (130 loc) · 2.58 KB
/
base16
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
#
# base16
#
# Base16 encode/decode data.
#
# Author: Emanuel Duss
#
set -o errexit
set -o nounset
set -o pipefail
print_usage(){
cat << EOI
Usage: base16 [OPTION]... [FILE]
Base16 (Hex) encode or decode FILE, or standard input, to standard output.
With no FILE, or when FILE is -, read standard input.
Options:
-d decode data
-i when decoding, ignore non-alphabet characters
-w COLS wrap encoded lines after COLS character (default 76).
Use 0 to disable line wrapping
-h display this help and exit
The data are encoded as described for the base16 alphabet in RFC 4648.
When decoding, the input may contain newlines in addition to the bytes of
the formal base64 alphabet. Use -i to attempt to recover from any other
non-alphabet bytes in the encoded stream.
EOI
}
check_dependencies(){
local FAIL=0
local MISSING=""
for tool in $@
do
if ! hash "$tool" &> /dev/null
then
MISSING="$MISSING, $tool"
FAIL=1
fi
done
if [[ "$FAIL" == 1 ]]
then
echo "Missing dependency: ${MISSING/, }."
exit 1
fi
}
parse_arguments(){
WRAP_COLS="76"
while getopts diw:h name
do
case $name
in
d)
DECODE="1"
;;
i)
IGNORE_GARBAGE="1"
;;
w)
WRAP="1"
WRAP_COLS="$OPTARG"
;;
h)
print_usage
exit
;;
?)
print_usage >&2
exit 1
;;
esac
done
}
hex_to_binary(){
perl -e '
local $/;
$i = <>;
$i =~ s/[\r\n]//g;
if ($i =~ /[^0-9a-fA-F]/){
print STDERR "base16: invalid input\n";
exit 1;
}
print pack "H*", $i;'
}
hex_to_binary_ignore_garbage(){
perl -e '
local $/;
$i = <>;
$i =~ s/[^0-9a-fA-F\n\r]//g;
print pack "H*", $i'
}
binary_to_hex(){
# hexdump -ve '1/1 "%.2x"'
perl -e '
local $/;
print unpack "H*", <>'
}
main(){
check_dependencies perl
parse_arguments "$@"
shift $(($OPTIND - 1))
# Only one file is allowed
if [[ "$#" > 1 ]]
then
echo -e "base16: extra operand '$1'"
echo "Try 'base16 -h for more information."
exit 1
fi
# Use STDIN (-) if $1 is not set
FILE="${1:--}"
# Decode
if [[ -n "${DECODE:-}" ]]
then
if [[ -n "${IGNORE_GARBAGE:-}" ]]
then
cat "$FILE" | hex_to_binary_ignore_garbage
else
cat "$FILE" | hex_to_binary
fi
# Encode
else
if [[ "$WRAP_COLS" == "0" ]]
then
cat "$FILE" | binary_to_hex | tr -d '\n'
else
cat "$FILE" | binary_to_hex | fold -w "$WRAP_COLS"
echo # Print newline at the end
fi
fi
}
main "$@"