Simple Base64 Decoder
Posted: Fri Oct 20, 2006 8:16 pm
I couldn't use the PB version because I had some special case stuff to add in so if anyone else would like the (stripped down) version that is guaranteed to be slower than PB's command, here you go 
Not a lot of error checking in there so please add your own to make sure nothing bad happens.
Code: Select all
Define.s HoldString
;
Define.l iLoop
;
Global Dim cb64.b(63)
Global Dim cd64.b(122)
;{
HoldString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
For iLoop = 0 To 63 : cb64(iLoop) = Asc(Mid(HoldString, iLoop + 1, 1)) :Next iLoop
For iLoop = 48 To 57 : cd64(iLoop) = iLoop + 4 : Next iLoop
For iLoop = 65 To 90 : cd64(iLoop) = iLoop - 65 : Next iLoop
For iLoop = 97 To 122 : cd64(iLoop) = iLoop - 71 : Next iLoop
cd64(43) = 62
cd64(47) = 63
;}
;-
Procedure.l Decode(HoldString.l, HoldLength.l, OutFile.s)
;
Define.l iFile
;
Define.l i
;
Define.Byte *Position = HoldString
;
Dim in.b(3)
;
Dim out.b(2)
;
Define.b CaughtEqual
;
Define.l lResult
;
iFile = CreateFile(#PB_Any, OutFile)
If iFile
;
While *Position - HoldString < HoldLength
;
If *Position\b = 13 Or *Position\b = 10 : *Position + 1 : Continue : EndIf
;
If i = 4
;
i = 0
;
out(0) = (in(0) << 2) | (in(1) >> 4)
out(1) = ((in(1) & %1111) << 4) | (in(2) >> 2)
out(2) = ((in(2) & %11) << 6) | in(3)
;
WriteByte(iFile, out(0))
WriteByte(iFile, out(1))
WriteByte(iFile, out(2))
;
lResult + 3
;
If CaughtEqual : Break : EndIf
;
in(i) = cd64(*Position\b)
;
i + 1
;
Else
;
If *Position\b = '=' : in(i) = 0 : CaughtEqual = #True : Else : in(i) = cd64(*Position\b) : EndIf
;
i + 1
;
EndIf
;
*Position + 1
;
Wend
;
CloseFile(iFile)
;
EndIf
;
ProcedureReturn lResult
;
EndProcedure