@@ -5,51 +5,40 @@ algo.base64encode() {
5
5
local input=" $1 "
6
6
7
7
local char_str=" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
8
- local has_{second,third}_byte=
9
- local {first,second,third}_{byte,char}=
8
+ local input_byte_{one,two,three}=
9
+ local bits_{one,two,three,four}=
10
+ local output_byte_{one,two,three_four}
10
11
for (( i= 0 ; i< ${# input} ; i= i+ 3 )) ; do
11
- has_second_byte=
12
- has_third_byte=
13
-
14
- # These branches execute if the string's length isn't divisible by 3. i.e., it
15
- # is divisible by 2 or only 1 (has lengths of 4, or 5). The i+1 is skipped because
16
- # it will always be true due to the condition in the for loop
17
- printf -v first_byte ' %d' " '${input: $i : 1} "
18
-
19
- # TODO: can simplify if empty string results in 0
20
- if (( i+ 2 > ${# input} )) ; then
21
- second_byte=$(( 2#00000000 ))
22
- else
23
- has_second_byte=yes
24
- printf -v second_byte ' %d' " '${input: $i +1: 1} "
25
- fi
26
-
27
- if (( i+ 3 > ${# input} )) ; then
28
- third_byte=$(( 2#00000000 ))
12
+ # If there are only two bytes left, the value of third_byte and
13
+ # fourth_byte will both be 0
14
+ printf -v input_byte_one ' %d' " '${input: $i : 1} "
15
+ printf -v input_byte_two ' %d' " '${input: $i +1: 1} "
16
+ printf -v input_byte_three ' %d' " '${input: $i +2: 1} "
17
+
18
+ # Output byte one
19
+ bits_one=$(( (input_byte_one >> 2 ) & 2#00111111 ))
20
+ output_byte_one=" ${char_str: $bits_one : 1} "
21
+
22
+ # Output byte two
23
+ bits_two=$(( ((input_byte_one & 2#00000011 ) << 4 ) | ((input_byte_two & 2#11110000 ) >> 4 & 2#00001111 ) ))
24
+ output_byte_two=" ${char_str: $bits_two : 1} "
25
+
26
+ # Output byte three
27
+ if (( input_byte_two == 0 )) ; then
28
+ output_byte_three=' ='
29
29
else
30
- has_third_byte=yes
31
- printf -v third_byte ' %d ' " ' ${input : $i +2 : 1}"
30
+ bits_three= $(( (input_byte_two & 2#00001111 ) << 2 | input_byte_three >> 6 & 2#00000011 ))
31
+ output_byte_three= " ${char_str : $bits_three : 1}"
32
32
fi
33
33
34
- new_first_bits=$(( (first_byte >> 2 ) & 2#00111111 ))
35
- new_second_bits=$(( ((first_byte & 2#00000011 ) << 4 ) | ((second_byte & 2#11110000 ) >> 4 & 2#00001111 ) ))
36
- new_third_bits=$(( (second_byte & 2#00001111 ) << 2 | third_byte >> 6 & 2#00000011 ))
37
- new_fourth_bits=$(( third_byte & 2#00111111 ))
38
-
39
- first_char=" ${char_str: $new_first_bits : 1} "
40
- second_char=" ${char_str: $new_second_bits : 1} "
41
-
42
- if [ " $has_second_byte " = yes ]; then
43
- third_char=" ${char_str: $new_third_bits : 1} "
44
- else
45
- third_char=' ='
46
- fi
47
- if [ " $has_third_byte " = yes ]; then
48
- fourth_char=" ${char_str: $new_fourth_bits : 1} "
34
+ # Output byte four
35
+ if (( input_byte_three == 0 )) ; then
36
+ output_byte_four=' ='
49
37
else
50
- fourth_char=' ='
38
+ bits_four=$(( input_byte_three & 2#00111111 ))
39
+ output_byte_four=" ${char_str: $bits_four : 1} "
51
40
fi
52
41
53
- REPLY+=" $first_char$second_char$third_char$fourth_char "
42
+ REPLY+=" ${output_byte_one}${output_byte_two}${output_byte_three}${output_byte_four} "
54
43
done
55
44
}
0 commit comments