Base64 encoder/decoder by Phaelax2nd Jul 2013 8:00
|
---|
Summary Encodes and decodes base64 strings Description Code ` This code was downloaded from The Game Creators ` It is reproduced here with full permission ` http://www.thegamecreators.com REM ************************************************* REM * Original Author: Randy Charles Morin REM * Author's Website: http://www.kbcafe.com (2006) REM * REM * Ported from C++ to AGK REM * REM * Converted By: Phaelax REM * Website: http://zimnox.com REM ************************************************* function encode64$(encode as string) value$ = "" length = len(encode) for i = 1 to length step 3 b1 = asc(mid(encode, i, 1)) b2 = 0 b3 = 0 if (i+1 <= length) then b2 = asc(mid(encode, i+1, 1)) if (i+2 <= length) then b3 = asc(mid(encode, i+2, 1)) b4 = b1 >> 2 b5 = ((b1&&0x3)<<4) || (b2>>4) b6 = ((b2&&0xf)<<2) || (b3>>6) b7 = b3 && 0x3f value$ = value$ + encodeByte$(b4) value$ = value$ + encodeByte$(b5) if i+1 <= length value$ = value$ + encodeByte$(b6) else value$ = value$ + "=" endif if i+2 <= length value$ = value$ + encodeByte$(b7) else value$ = value$ + "=" endif if mod(i, 76) = 0 then value$ = value$ + chr(13)+chr(10) next i endfunction value$ function decode64$(decode as string) value$ = "" temp$ = "" if isBase64(decode) = 0 then exitfunction "" length = len(decode) for i = 1 to length step 4 c1$ = mid(decode, i, 1) c2$ = "A" c3$ = "A" c4$ = "A" if i+1 <= length then c2$ = mid(decode, i+1, 1) if i+2 <= length then c3$ = mid(decode, i+2, 1) if i+3 <= length then c4$ = mid(decode, i+3, 1) b1 = decodeChar(c1$) b2 = decodeChar(c2$) b3 = decodeChar(c3$) b4 = decodeChar(c4$) value$ = value$ + chr(b1<<2 || b2>>4) if c3$ <> "=" then value$ = value$ + chr(((b2&&0xf)<<4) || (b3>>2)) if c4$ <> "=" then value$ = value$ + chr(((b3&&0x3)<<6) || b4) next i endfunction value$ function isBase64(check as string) c as string for i = 1 to len(check) thing = 0 c = mid(check, i, 1) if c >= "A" and c <= "Z" then thing = 1 if c >= "a" and c <= "z" then thing = 1 if c >= "0" and c <= "9" then thing = 1 if c = "+" then thing = 1 if c = "/" then thing = 1 if c = "=" then thing = 1 if thing = 0 then exitfunction 0 next i endfunction 1 function encodeByte$(byte) if byte < 26 then exitfunction chr(asc("A")+ byte) if byte < 52 then exitfunction chr(asc("a")+(byte-26)) if byte < 62 then exitfunction chr(asc("0")+(byte-52)) if byte = 62 then exitfunction "+" endfunction "/" function decodeChar(c as string) if c >= "A" and c <= "Z" v = asc(c)-65 exitfunction v endif if c >= "a" and c <= "z" v = asc(c)-97+26 exitfunction v endif if c >= "0" and c <= "9" v = asc(c)-48+52 exitfunction v endif if c = "+" then exitfunction 62 endfunction 63 |