At last ! A euphoria solution to those irritating attachments.

new topic     » topic index » view thread      » older message » newer message

Here is a special message, for those of you who, like me, are unable to decode
Base64 files. Below is a fully-functional programme which does the nuts and
bolts of Base64 decoding. All you have to do is remove the header lines.

If you find it useful, please vote for it, and I will be encouraged to get on
with other projects, like my much publicised 3d toolkit.

NB, if the listserv is having an =3D day ie messing up all our programmes with
lots of =FA and =this and =that then you will also have to correct this
manually. This may even be the case with my prog, in which case you better get
it off Irv or Rob.

I have been up all night working out the Base64 lookup tables so I hope it is
useful.

The Lord bless and keep you,

Daniel

----------------------Begin unbase63.ex--------------------------------
--Prog by D Johnson to decode Base64. You should manually strip out the
--headers so that the file starts with the first character.

constant lookup=
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,


function check_valid(sequence data)
    integer ret
    ret=1
    for a=1 to length(data) do
        if not find(data[a], lookup) and not data[a]='=' then ret=0 end if
    end for
    return ret and remainder(length(data),4)=0
end function

function read_file(integer fn)
--Reads in everythin from an open file, ignoring CR/LFs
    object this
    sequence ret
    ret={}
    this=gets(fn)
    while sequence(this) do
        if this[length(this)]='\n' then
            ret = ret & this[1..length(this)-1]
        else
            ret = ret & this
        end if
        this=gets(fn)
    end while
    return ret
end function

function unbase64(sequence data)
--Supposedly decodes Base64 data. Data should be checked first using
check_valid()
    sequence undecoded, temp, tempdata
    integer const
    undecoded={}
    data = data + 1
    if find('=',data[length(data)-3..length(data)]) then const= -4 else
const=0 end if
    for a=1 to length(data)+const by 4 do
        tempdata={}
        for b=a to a+3 do
            tempdata = tempdata & lookup[data[b]]
        end for
        temp={0,0,0}
        temp[1] = tempdata[1]*4 + and_bits(tempdata[2],48)/16
        temp[2] = and_bits(tempdata[2],15)*16 + and_bits(tempdata[3],60)/4
        temp[3] = and_bits(tempdata[3],3)*64 + tempdata[4]
        undecoded = undecoded & temp
    end for
    --Special code for last four bytes
    if const= -4 then
        tempdata = lookup[data[length(data)-3]] & lookup[data[length(data)-2]]
        temp={}
        temp = temp & tempdata[1]*4 + and_bits(tempdata[2],48)/16
        const = find('=',data[length(data)-3..length(data)]) --find the '='
        if const = 4 then --ie if there are two bytes to extract
            tempdata = tempdata & lookup[data[length(data)-1]]
            temp = temp & and_bits(tempdata[2],15)*16 +
and_bits(tempdata[3],60)/4
        end if
        undecoded = undecoded & temp
    end if
    return undecoded
end function

procedure main()
    sequence filename, data
    integer fn
    puts(1,"\nEnter the file to decode : ")
    filename=gets(0)
    filename = filename[1..length(filename)-1]
    fn = open(filename,"r")
    if fn > 1 then
        data=read_file(fn)
        close(fn)
        if check_valid(data) then
            puts(1,"\nDecoding...\n")
            fn = open(filename[1..find('.',filename)]&"lmt","wb")
            if fn > 2 then
                puts(fn,unbase64(data))
                puts(1,"File saved, with .lmt extension. Success !\n")
            else
                puts(1,"Could not open destination file\n")
            end if
        else
            puts(1,"\nNot a valid Base64 file\n")
            ? length(data)
        end if
    else
        puts(1,"\nCould not open "&filename&"\n")
    end if
end procedure

main()

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu