cQBASE64 ;BASE64 encoder and decoder ;cQBASE64; Q ; ;encode BASE64 input string -- see RFC 2045 for specification ;call by reference of variable "outs" to get encoded value ENCODE(ins,outs) ;Base64 Encoder n char64,i,j,triple,bits s char64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" s outs="" f i=1:3:$l(ins) s triple=$e(ins,i,i+2) q:'$l(triple) d . s bits=0 . f j=1:1:$l(triple) s bits=bits*256+$a(triple,j) . if $l(triple)=3 s outs=outs_$e(char64,bits\262144+1)_$e(char64,bits\4096#64+1)_$e(char64,bits\64#64+1)_$e(char64,bits#64+1) . e if $l(triple)=2 s bits=bits*4,outs=outs_$e(char64,bits\4096+1)_$e(char64,bits\64#64+1)_$e(char64,bits#64+1)_"=" . e s bits=bits*16,outs=outs_$e(char64,bits\64+1)_$e(char64,bits#64+1)_"==" ENCODEZ q ; ;decode BASE64 input string -- see RFC 2045 for specification ;call by reference of variable "outs" to get decoded value ;function returns 0 (false) if successful, other wise 1 (true) and error message DECODE(ins,outs) ;Base64 decoder n char64,i,j,bits,quad,f,c s char64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" s outs="" f i=1:4:$l(ins) s quad=$e(ins,i,i+3) q:'$l(quad) d q:f<0 . s bits=0 . f j=1:1:$l(quad) d q:f<0 .. s c=$e(quad,j),f=$f(char64,c)-2 .. if f>-1 s bits=bits*64+f . if f>-1 s outs=outs_$c(bits\65536,bits\256#256,bits#256) if c="=" d . s j=j-1,f=0 . if j=2 s outs=outs_$c(bits\16) . e s outs=outs_$c(bits\1024,bits\4#256) DECODEZ q $s(f<0:"1;invalid input char",1:0) ; ZZ ; 17.01.2012 - 15:58 * Cache-r6.4.9