1 # $MirOS: src/bin/mksh/dot.mkshrc,v 1.68 2011/11/25 23:58:04 tg Exp $
3 # Copyright (c) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011
4 # Thorsten Glaser <tg@mirbsd.org>
6 # Provided that these terms and disclaimer and all copyright notices
7 # are retained or reproduced in an accompanying document, permission
8 # is granted to deal in this work without restriction, including un-
9 # limited rights to use, publicly perform, distribute, sell, modify,
10 # merge, give away, or sublicence.
12 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
13 # the utmost extent permitted by applicable law, neither express nor
14 # implied; without malicious intent or gross negligence. In no event
15 # may a licensor, author or contributor be held liable for indirect,
16 # direct, other damage, loss, or other issues arising in any way out
17 # of dealing in the work, even if advised of the possibility of such
18 # damage or existence of a defect, except proven that it results out
19 # of said person's immediate fault when using the work as intended.
21 # RFC compliant base64 encoder and decoder
23 # NUL safe base64 decoder
25 [[ -o utf8-mode ]]; local u=$?
28 [[ -n $s ]] || { s=$(cat;print x); s=${s%x}; }
29 local -i i=0 j=0 n=${#s} p=0 v x
36 ([A-Z]) (( v = 1#$c - 65 )) ;;
37 ([a-z]) (( v = 1#$c - 71 )) ;;
38 ([0-9]) (( v = 1#$c + 4 )) ;;
43 (( x = (x << 6) | v ))
46 (1) (( o = (x >> 4) & 255 )) ;;
47 (2) (( o = (x >> 2) & 255 )) ;;
53 (( ++j & 4095 )) && continue
61 set -A Lb64encode_code -- A B C D E F G H I J K L M N O P Q R S T U V W X Y Z \
62 a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 + /
64 # not NUL safe base64 encoder
66 [[ -o utf8-mode ]]; typeset u=$?
69 [[ -n $s ]] || { s=$(cat;print x); s=${s%x}; }
70 typeset -i i=0 n=${#s} j v
76 (( j = ${#c} ? 1#$c : 0 ))
79 (( j = ${#c} ? 1#$c : 0 ))
81 t=$t${Lb64encode_code[v >> 18]}${Lb64encode_code[v >> 12 & 63]}
82 c=${Lb64encode_code[v >> 6 & 63]}
84 t=$t$c${Lb64encode_code[v & 63]}
85 elif (( i == n + 1 )); then
90 if (( ${#t} == 76 || i >= n )); then
98 # NUL safe base64 encoder, needs mksh R40
100 [[ -o utf8-mode ]]; local u=$?
104 read -raN-1 s <<<"$*"
109 local -i i=0 n=${#s[*]} j v
111 while (( i < n )); do
112 (( v = s[i++] << 16 ))
113 (( j = i < n ? s[i++] : 0 ))
115 (( j = i < n ? s[i++] : 0 ))
117 t+=${Lb64encode_code[v >> 18]}${Lb64encode_code[v >> 12 & 63]}
118 c=${Lb64encode_code[v >> 6 & 63]}
119 if (( i <= n )); then
120 t+=$c${Lb64encode_code[v & 63]}
121 elif (( i == n + 1 )); then
126 if (( ${#t} == 76 || i >= n )); then