-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode
More file actions
executable file
·118 lines (108 loc) · 3.42 KB
/
decode
File metadata and controls
executable file
·118 lines (108 loc) · 3.42 KB
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
#!/usr/bin/env bash
#########################region CONSTANTS#########################
readonly NAME="decode"
readonly ERROR_BAD_METHOD=4
readonly ERROR_NO_INPUT=2
readonly DECODE_METHOD_ESCAPE="escape"
readonly DECODE_METHOD_URL="url"
readonly DECODE_METHOD_UNDERSCORE="underscore"
readonly TRUE="[ a = a ]"
readonly FALSE="[ a = b ]"
readonly DEFAULT_DECODE_METHOD=$DECODE_METHOD_ESCAPE
#########################endregion CONSTANTS#########################
#########################region HELP#########################
show_help() {
# -d | --decode | --decode_method METHOD: The decoding method to use for values and labels.
cat << __EOF__
$NAME VALUE [--escape|url|underscore] | $NAME VALUE [-e|u|_] | $NAME -h | $NAME --help
Decodes the given value with the selected method. Defaults to $DEFAULT_DECODE_METHOD
Parameters:
-u | --url: use url decoding
-e | --escape: use escape decoding (i.e. use "\\\\" to encode escape sequences)
-_ | --underscore: use underscore decoding (i.e. replaces all underscores with spaces)
Defaults to "$DEFAULT_DECODE_METHOD"
-h | --help: Display this help text.
EXIT STATUS:
Error Level of $ERROR_BAD_METHOD if selection is invalid.
__EOF__
# Anything else will result in no decoding being used. If this is used, ensure there are no spaces in your values nor labels.
# -e | --error_on_fail: Set the error level on failure.
}
show_version() {
cat << __EOF__
$0 1.0.0
Copyright (C) 2024 Justin Morris
__EOF__
}
#########################endregion HELP#########################
#########################region DECODE METHODS#########################
# https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html
url_decode() {
echo -e $(echo "$1" | sed -z -E "s/\\+/ /g" | sed -z -E "s/%([0-9a-fA-F][0-9a-fA-F])/\\\x\1/g")
}
escape_decode() {
echo -e $(echo "$1" | sed -z -E "s/\\\\/\\\/g")
}
# TODO: TEST
underscore_decode() {
echo -e $(echo "$1" | sed -z -E "s/_/ /g")
}
#########################endregion DECODE METHODS#########################
#########################region GET OPTIONS#########################
decode_method=$DEFAULT_DECODE_METHOD
encoded=""
not_assigned=$TRUE
while [ "$1" != "" ]; do
case "$1" in
-h | --help )
show_help
exit
;;
-v | --version )
show_version
exit
;;
# #region Flags
-u | --url )
decode_method=$DECODE_METHOD_URL
;;
-e | --escape )
decode_method=$DECODE_METHOD_ESCAPE
;;
-_ | --underscore )
decode_method=$DECODE_METHOD_UNDERSCORE
;;
# #endregion Flags
* )
# if [[ "$decode_method" == "" && $1 =~ ^--(.*)$ ]]; then
# decode_method=${BASH_REMATCH[1]}
# elif [ "$encoded" = "" ]; then
if $($not_assigned); then
encoded=$1
not_assigned="$FALSE"
else
encoded="$encoded $1"
# else
# show_help
# exit 1
fi
;;
esac
shift
done
if $($not_assigned); then
show_help
exit 1
fi
#########################endregion GET OPTIONS#########################
if [[ "$decode_method" = $DECODE_METHOD_URL ]]; then
echo $(url_decode "$encoded")
elif [[ "$decode_method" = $DECODE_METHOD_UNDERSCORE ]]; then
echo $(underscore_decode "$encoded")
elif [[ "$decode_method" = $DECODE_METHOD_ESCAPE ]]; then
echo $(escape_decode "$encoded")
else
echo "$encoded"
exit $ERROR_BAD_METHOD
fi
exit # $?