How To Convert Base64 Encoding To Ascii Text?

rajendran

Member
hi all,
i am working on a tool which reads email files from different clients .some emails from yahoo and gmail are getting encoded with base64 content-transfer-encoding.
i am trying to find a solution to decode these files into readable text format .
i have tried to convert the files by the below code.
But no luck

DEFINE VARIABLE decdmptr AS MEMPTR NO-UNDO.
DEFINE VARIABLE decdlngc AS LONGCHAR NO-UNDO.

COPY-LOB FROM FILE "C:\myicons\testencode" TO decdlngc.
decdmptr = BASE64-DECODE(decdlngc).
COPY-LOB FROM decdmptr TO FILE "C:\myicons\test.ico".

Any suggestions would be helpful.
opendge 11.3 in linux environent .
 

RealHeavyDude

Well-Known Member
The issue is that, whatever the end result of the base64-decode is, it is just a bag of bytes on a memory pointer for the ABL. If it does not correspond to an ABL ( or .NET if you use .NET from within the ABL on Windows ) object you have nothing more than just that - some bytes. Since you are on Linux you can't use objects available through the .NET common language runtime.

Other than parse them by byte I don't see how you will succeed working with them unless they are plain text.

Heavy Regards, RealHeavyDude.
 

KrisM

Member
As far as i know all content that is base64 encoded already is ascii text.
That is the reason for using base64 encoding.
 

RealHeavyDude

Well-Known Member
KrisM - you are right. But in this case it is a mail object having been serialized into ascii text in the first place. While you might be able to base64-decode it, the problem is that you can't rebuild the object from its serialized representation in any language when the language either don't supports the object type or does not provide serialization functionality for the object type you need.

The types of Yahoo and gmail mail objects are not known to the ABL. While you could still roll you own serialization support - I doubt that this is feasible.

In the past I had some luck on Windows when .NET provided such support as you can use most .NET objects from the ABL naturally.

Heavy Regards, RealHeavyDude.
 

Cecil

19+ years progress programming and still learning.
When Base64 encoded data is embedded into an email it is usually chunked by 80 characters (need confirmation) followed by a EOL. So you need to strip away the EOL so the Base64 encoded sting is one continuous line. Then you should be able to decode the base64 string into a binary file.

Otherwise use a linux command line (ripmine) tool to extract the attachment(s) from the email then all the hard work is done for you.

Here is my code for using the ripmime tool.

PROCEDURE RipTheMailMessage :
DEFINE INPUT PARAMETER chSourceDirectory AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER opchAttachmentDirectory AS CHARACTER NO-UNDO.

DEFINE VARIABLE chOSCommand AS CHARACTER NO-UNDO.

DEFINE VARIABLE chFilename AS CHARACTER NO-UNDO.
DEFINE VARIABLE chFullFilename AS CHARACTER NO-UNDO.
DEFINE VARIABLE chStandardImport AS CHARACTER NO-UNDO.
DEFINE VARIABLE err-status AS INTEGER NO-UNDO.

ASSIGN
opchAttachmentDirectory = chSourceDirectory + '/mail-attachments':U.

MESSAGE SUBSTITUTE('Create Mail Attachement Directory &1',opchAttachmentDirectory).
OS-CREATE-DIR VALUE(opchAttachmentDirectory).

ASSIGN
err-status = OS-ERROR.

IF err-status NE 0 THEN
CASE err-status:
WHEN 1 THEN
MESSAGE "You are not the owner of this file or directory.".
WHEN 2 THEN
MESSAGE "The file or directory you want to delete does not exist.".
OTHERWISE MESSAGE "OS Error #" + STRING(OS-ERROR,"99").
END CASE.

INPUT STREAM SOSDIR FROM OS-DIR(chSourceDirectory) NO-ATTR-LIST.
FILE-DIR:
REPEAT:

IMPORT STREAM SOSDIR chFilename chFullFilename.

IF chFilename EQ '.' OR
chFilename EQ '..' THEN
NEXT FILE-DIR.

FILE-INFO:FILE-NAME = chFullFilename.
IF FILE-INFO:FILE-TYPE MATCHES '*F*':U THEN
DO:
/* MESSAGE chFullFilename. */

chOSCommand = SUBSTITUTE('ripmime -i "&1" -d "&2/" --no-doublecr --name-by-type -e "mailheader.txt"':U,
chFullFilename,
opchAttachmentDirectory,
chSourceDirectory).

/** RIP THE MAIL MESSAGE **/
INPUT STREAM SOSCOMMAND THROUGH VALUE(chOSCommand) NO-ECHO NO-CONVERT.
REPEAT:
IMPORT STREAM SOSCOMMAND UNFORMATTED chStandardImport.
MESSAGE 'RIPMIME:' chStandardImport.
END.
INPUT STREAM SOSCOMMAND CLOSE.
END.
END.
INPUT STREAM SOSDIR CLOSE.



RETURN.
END PROCEDURE.
 

rajendran

Member
i am able to decode the base64 emails from below program.
http://www.maizels.nu/progress/from64.p

the email files are partly encoded,so i stripped the encoded part to a new file and then decode the data from that file.

could you give any suggestion for extracting email body from html files?
i believe if i can remove all contents within paranthesis then i can get the email body.
<p class=3D"MsoPlainText">Done<o:p></o:p></p>

please let me know if any suggestions.
 
Top