REM ***********************************************
REM Title: Base64 Encoder-Decoder
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
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 *
REM * Edited: Feb 26, 2014
REM *  - Added command decode64ToByteArray() to write
REM *    decoded bytes to an array called B64[]. 
REM *    Returns array size
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 decode64ToByteArray(decode as string)
 
    if isBase64(decode) = 0 then exitfunction
 
    // Determine size of byte array
    for i = len(decode) to 1 step -1
        if mid(decode, i, 1) = "=" then inc e else exit
    next i
    size = (len(decode) / 4) * 3 - e
 
    Global dim B64[size]
 
    length = len(decode)
    index = 0
    for i = 1 to length step 4
        inc index
        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$)
 
        B64[index] = b1<<2 || b2>>4
 
        if c3$ <> "="
            inc index
            B64[index] = ((b2&&0xf)<<4) || (b3>>2)
        endif
        if c4$ <> "="
            inc index
            B64[index] = ((b3&&0x3)<<6) || b4
        endif
 
    next i
endfunction size
 
 
 
function decode64$(decode as string)
    value$ = ""
 
    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