OBJECT Codeunit 99000 Base64 Management { OBJECT-PROPERTIES { Date=12/04/05; Time=15:04:40; Modified=Yes; Version List=; } PROPERTIES { OnRun=BEGIN gtxt_Plaintext := 'Base64 encoding is great for storing binary files as printable text!!!'; gtxt_Encoded := Fct_Base64Encode(gtxt_Plaintext); gtxt_Decoded := Fct_Base64Decode(gtxt_Encoded); MESSAGE('Plain: %1\Encoded: %2\Decoded: %3',gtxt_Plaintext,gtxt_Encoded,gtxt_Decoded); END; } CODE { VAR gtxt_Plaintext@1100074002 : Text[1024]; gtxt_Encoded@1100074001 : Text[1024]; gtxt_Decoded@1100074000 : Text[1024]; PROCEDURE Fct_Base64Encode@1100074001(ptxt_Source@1100074000 : Text[1024]) rtxt_Encoded : Text[1024]; VAR ltxt_Base64@1100074005 : Text[64]; ltxt_SourceBits@1100074001 : Text[24]; ltxt_PartBits@1100074003 : Text[8]; lint_Index@1100074006 : Integer; c@1100074002 : Integer; i@1100074004 : Integer; BEGIN ltxt_Base64 := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; c := 1; REPEAT ltxt_SourceBits := Fct_IntToBin(ptxt_Source[c]); ltxt_SourceBits += Fct_IntToBin(ptxt_Source[c + 1]); ltxt_SourceBits += Fct_IntToBin(ptxt_Source[c + 2]); FOR i := 1 TO 4 DO BEGIN ltxt_PartBits := '00' + COPYSTR(ltxt_SourceBits, (i*6)-5,6); lint_Index := Fct_BinToInt(ltxt_PartBits); IF lint_Index <> 0 THEN rtxt_Encoded += FORMAT(ltxt_Base64[lint_Index+1]) ELSE rtxt_Encoded += '='; END; c += 3; UNTIL c > STRLEN(ptxt_Source); END; PROCEDURE Fct_Base64Decode@1100074003(ptxt_Source@1100074000 : Text[1024]) rtxt_Decoded : Text[1024]; VAR ltxt_Base64@1100074006 : Text[64]; ltxt_SourceBits@1100074005 : Text[24]; ltxt_PartBits@1100074004 : Text[8]; ltxt_Char@1100074007 : Text[1]; lint_Index@1100074003 : Integer; c@1100074002 : Integer; i@1100074001 : Integer; iPos@1100074008 : Integer; BEGIN ltxt_Base64 := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; c := 1; REPEAT ltxt_SourceBits := COPYSTR(Fct_IntToBin(STRPOS(ltxt_Base64,FORMAT(ptxt_Source[c]))-1),3,6); ltxt_SourceBits += COPYSTR(Fct_IntToBin(STRPOS(ltxt_Base64,FORMAT(ptxt_Source[c+1]))-1),3,6); ltxt_SourceBits += COPYSTR(Fct_IntToBin(STRPOS(ltxt_Base64,FORMAT(ptxt_Source[c+2]))-1),3,6); ltxt_SourceBits += COPYSTR(Fct_IntToBin(STRPOS(ltxt_Base64,FORMAT(ptxt_Source[c+3]))-1),3,6); FOR i := 1 TO 3 DO BEGIN ltxt_PartBits := COPYSTR(ltxt_SourceBits, (i*8)-7,8); lint_Index := Fct_BinToInt(ltxt_PartBits); ltxt_Char[1] := lint_Index; rtxt_Decoded += ltxt_Char; END; c += 4; UNTIL c > STRLEN(ptxt_Source); END; PROCEDURE Fct_IntToBin@1100074000(pint_Number@1100074000 : Integer) rtxt_Binary : Text[8]; BEGIN WHILE pint_Number > 0 DO BEGIN rtxt_Binary := FORMAT(pint_Number MOD 2) + rtxt_Binary; pint_Number := pint_Number DIV 2; END; WHILE STRLEN(rtxt_Binary) < 8 DO rtxt_Binary := '0' + rtxt_Binary; END; PROCEDURE Fct_BinToInt@1100074002(ptxt_binary@1100074000 : Text[8]) rint_Number : Integer; VAR i@1100074001 : Integer; c@1100074002 : Integer; f@1100074003 : Integer; BEGIN FOR i := 1 TO 8 DO BEGIN IF ptxt_binary[i] = '1' THEN BEGIN FOR c := 1 TO 9-i DO BEGIN IF c = 1 THEN f := 1 ELSE f := f * 2; END; rint_Number += f; END; END; END; BEGIN END. } }
Comments
Anyway, here's the decode...:
It is really fast and uses automation from Commerce Gateway, as used in codeunit "BizTalk XML DOM Management"
Did some small test with a PDFfile, and this one was about 1000 times faster (10 sec compared with 0,01 sec)