-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.sh
140 lines (116 loc) · 2.85 KB
/
encode.sh
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
#
# Copyright (c) 2015 Tristan Le Guern <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
LIBNAME="libencode.sh"
LIBVERSION="2.0"
#
# enum is a portable glue for enumerating between two numbers $_first and $_last
#
enum() {
if command -v jot > /dev/null 2>&1; then
unset -f enum
enum() { jot $2 $1 $2 1; }
elif command -v seq > /dev/null 2>&1; then
unset -f enum
enum() { seq $1 1 $(( $2 - 1 )); }
fi
enum $1 $2
}
#
# leftpad will add $_num time the char $_pad on the left of the string $_value
#
leftpad() {
local _value="$1"
local _num="$2"
local _pad="${3:-0}"
local _i=0
local _res="$_value"
if [ $_num -eq 0 ]; then
echo "$_res"
return 0
fi
for _i in $(enum 0 $_num); do
_res="${_pad}${_res}"
done
echo "$_res"
}
#
# rightpad will add $_num time the char $_pad on the right of the string $_value
#
rightpad() {
local _value="$1"
local _num="$2"
local _pad="${3:-0}"
local _i=0
local _res="$_value"
for _i in $(enum 0 $_num); do
_res="${_res}${_pad}"
done
echo "$_res"
}
#
# ord() converts the given ASCII char to a decimal value
#
ord() {
local _value="$1"
printf "%d" "'$_value"
}
#
# chr() converts the given decimal $_value to an ASCII char
#
chr() {
local _value="$1"
printf "\\$(printf %o $_value)\n"
}
#
# dectobin() converts the given decimal $_value to binary
#
dectobin() {
local _value="$1"
local _bc="ibase=10; obase=2; print $_value;"
local _res="$(echo "$_bc" | bc)"
local _len="${#_res}"
local _des=$(( _len / 8 + (_len % 8 != 0) ))
local _num=$(( _des * 8 - _len ))
_res="$(leftpad $_res $_num)"
echo "$_res"
}
#
# bintodec() converts the given binary $_value to decimal
#
bintodec() {
local _value="$1"
local _bc="obase=10; ibase=2; print $_value;"
echo "$_bc" | bc
}
#
# dectohex() converts the given decimal $_value to hexadecimal
#
dectohex() {
local _value="$1"
printf %x "$_value"
}
#
# swap32() reverses the byte ordering of the given 32-bits binary value
#
swap32() {
local _value="$1"
_a="$(printf "%s" "$_value" | cut -b 1-8 -)"
_b="$(printf "%s" "$_value" | cut -b 9-16 -)"
_c="$(printf "%s" "$_value" | cut -b 17-24 -)"
_d="$(printf "%s" "$_value" | cut -b 25-32 -)"
echo "$_d$_c$_b$_a"
unset _a _b _c _d
}