forked from mateusza/shellscripthttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.sh
More file actions
244 lines (211 loc) · 3.74 KB
/
base64.sh
File metadata and controls
244 lines (211 loc) · 3.74 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
##inspired from https://gist.github.com/markusfisch/2648733
##modded by https://github.com/benchonaut 2019
# Fallback base64 en-/decoder for systems that lack a native implementation
#
# @param ... - flags
which base64 &>/dev/null || {
# if even od is missing
which od &>/dev/null || od()
{
local C O=0 W=16
while IFS= read -r -d '' -n 1 C
do
(( O%W )) || printf '%07o' $O
printf ' %02x' "'$C"
(( ++O%W )) || echo
done
echo
}
if which awk &>/dev/null
then
base64()
{
# written by Danny Chouinard
# https://sites.google.com/site/dannychouinard/Home/unix-linux-trinkets/little-utilities/base64-and-base85-encoding-awk-scripts
awk \
'function encode64()
{
while( "od -v -t x1" | getline )
{
l = length( $0 );
for( c = 9; c <= l; ++c )
{
d = index( "0123456789abcdef", substr( $0, c, 1 ) );
if( d-- )
{
for( b = 1; b <= 4; ++b )
{
o = o*2+int( d/8 );
d = (d*2)%16;
if( ++obc == 6 )
{
printf substr( b64, ++o, 1 );
if( ++rc > 75 )
{
printf( "\n" );
rc = 0;
}
obc = 0;
o = 0;
}
}
}
}
}
if( obc )
{
while( obc++ < 6 )
{
o = o*2;
}
printf "%c", substr( b64, ++o, 1 );
}
print "==";
}
function decode64()
{
while( getline < "/dev/stdin" )
{
l = length( $0 );
for( i = 1; i <= l; ++i )
{
c = index( b64, substr( $0, i, 1 ) );
if( c-- )
{
for( b = 0; b < 6; ++b )
{
o = o*2+int( c/32 );
c = (c*2)%64;
if( ++obc == 8 )
{
printf "%c", o;
obc = 0;
o = 0;
}
}
}
}
}
}
BEGIN {
b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
if( ARGV[1] == "-d" )
decode64();
else
encode64();
}' "$@"
}
else
cat <<EOF
WARNING: your system is missing base64 AND awk!
base64 encoding/decoding will be painfully slow!
EOF
base64()
{
local SET='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
[ "$1" == '-d' ] && {
local N=0 V=0 C S IFS=
while read -r -d '' -r -n1 C
do
[ "$C" == $'\n' ] && continue
if [ "$C" == '=' ]
then
V=$(( V << 6 ))
else
C=${SET#*$C}
C=$(( ${#SET}-${#C} ))
(( C )) || continue
V=$(( V << 6 | --C ))
fi
(( ++N == 4 )) && {
## replace for #for ( sms=16; sms > -1; sms -= 8 )
sms=16
while [ "$sms" -gt -1 ]
##
do
C=$(( V >> sms & 255 ))
# shellcheck disable=SC2059
printf "\\$(( C*100/64+C%64*10/8+C%8 ))"
## replace for
sms=$(( sms - 8 ))
## replace for
done
V=0
N=0
}
done
return
}
od -v -t x1 | {
local V=0 W=0 SH=16 A S N L
while read -r -a A
do
## replace for #for (( N=1, L=${#A[@]}; N < L; ++N ))
N=1;L=${#A[@]}
while [ "$N" -lt "$L" ]
do
V=$(( 16#${A[$N]} << SH | V ))
(( (SH -= 8) < 0 )) || continue
## replace for #for (( S=18; S > -1; S -= 6 ))
S=18
while [ "$S" -gt -1 ]
## replace for
do
echo -n ${SET:$(( V >> S & 63 )):1}
(( ++W > 75 )) && {
echo
W=0
}
## replace for
S=$(( S - 6 ))
## replace for
done
SH=16
V=0
## replace for
N=$(( N + 1 ))
## replace for
done
done
if (( SH == 8 ))
then
N=11
elif (( SH == 0 ))
then
N=5
else
N=0
fi
(( N )) && {
## replace for #for (( S=18; S > N; S -= 6 ))
S=18
while [ "$S" -gt "$N" ]
## replace for
do
echo -n ${SET:$(( V >> S & 63 )):1}
(( ++W > 75 )) && {
echo
W=0
}
## replace for
S=$(( S - 6 ))
## replace for
done
## replace for #for (( S=N/5; S--; ))
S=$( expr "$N" "/" 5 )
while [ "$S" -gt -1 ]
## replace for
do
echo -n '='
## replace for
S=$(( S - 1 ))
## replace for
done
}
echo
}
}
fi
}
base64 "$@"