|
| 1 | +# shellcheck shell=bash |
| 2 | + |
| 3 | +algo.base64encode() { |
| 4 | + unset REPLY; REPLY= |
| 5 | + local input="$1" |
| 6 | + |
| 7 | + local char_str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" |
| 8 | + local has_{second,third}_byte= |
| 9 | + local {first,second,third}_{byte,char}= |
| 10 | + 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)) |
| 29 | + else |
| 30 | + has_third_byte=yes |
| 31 | + printf -v third_byte '%d' "'${input:$i+2:1}" |
| 32 | + fi |
| 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}" |
| 49 | + else |
| 50 | + fourth_char='=' |
| 51 | + fi |
| 52 | + |
| 53 | + REPLY+="$first_char$second_char$third_char$fourth_char" |
| 54 | + done |
| 55 | +} |
0 commit comments