[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Ftpapi] Need to understand how decode from Base64 to Ascii



Scott thanks for your time and attention,
my application is only the partial update of a much larger suite of programs that was once feeded with floppy disk, then with cd / dvd and now with a web service.

This applications starts retrieving an xml file from a web service of our bank and saving it in the ifs.

Within this xml file there is tag who has the base64 encoded data string.

The lenght of this base64 string is not given, it may change, since the users who starts the application, asking data to the bank, may ask data from one day (given a small string of data) to thirty days of work (givin a very big string of data), but there is no way to know before the lenght of the string. Anyway doing the tests I saw that the largest file that is downloaded is about 5 megabytes.

This string represents an ascii file with fixed format data that should be processed inside the as400 from a rpgle program, and outside the as400 from a windows based program that waits to be feed with ascii data. 

So the program of which I have posted a part of the code, takes the xml file received from the bank and saved in the IFS, identifies the tag that contains the file, with an xml-into it puts the contents in the variable named File and then process it to decode the data and write it in the ifs, in a folder that is shared with the windows server.

My problem is that when i open the file resulting from decoding with EDTF inside my client access session i see human readable data, and the following rpgle program works it without any issue (since it starts making a CPYFRMIMPF command), when i open the file with notepad from the windows server i see a txt file full of garbage and so does the windows program that should work on it.

Rereading the code i have posted i see this

http_xlatep(declen:%addr(decbuf):TO_ASCII);

This is my attempt, obviously failed, to produce something in ascii that can be fed to the windows program, previously my code did

http_xlatep(declen:%addr(decbuf):TO_EBCDIC);

I hope I was able to give you all the correct information and I apologize for my English as it is not my mother tongue.

----- Messaggio originale -----
> Da: "Scott Klement" <sk@xxxxxxxxxxxxxxxx>
> A: "FTPAPI/HTTPAPI mailing list" <ftpapi@xxxxxxxxxxxxxxxxxxxxxx>
> Inviato: Venerdì, 15 giugno 2018 23:29:44
> Oggetto: Re: [Ftpapi] Need to understand how decode from Base64 to Ascii
> 
> Luca,
> 
> Always remember that the purpose of base64 encoding is to preserve the
> binary value of data.   When you decode it, it will have the exact same
> values it had when it was encoded.   Now, I'm not familiar with your
> data, but since most computers don't use EBCDIC, I don't think you want
> to translate it to ASCII again.
> 
> I think you have ASCII data already.  (Or maybe Unicode) and you are
> telling the computer "this is EBCDIC, translate it to ASCII" (even
> though it is not).   Then, you're doing that a second time by telling
> the open() API that "this is text data" and "my data is in the job
> CCSID, aka CCSID 0".  So its already ASCII, but then you're running it
> through an EBCDIC->ASCII translation table again twice.
> 
> I also don't understand why you're doing this 4 bytes at once. Assuming
> you are using my BASE64R4 service program, the base64_decode() routine
> is not limited to 4 bytes.
> 
> You also have a fixed-length variable named 'File' that is based on a
> pointer...   but, then you are calling %TRIM() on that.   Again, I'm not
> familiar with your data, but in my experience, it's very unusual to use
> a pointer on something that's actually fixed length. (There'd be no
> reason to use a pointer if it was actually fixed-length, you'd just use
> a variable) so this is potentially going to result in lots of "garbage"
> being picked up from memory. Or, in unusual circumstances, it could
> cause your program to crash.
> 
> So, I really think you're approaching this incorrectly.   Please tell us
> more about your data and where it's coming from so that we can suggest a
> better solution.
> 
> -SK
> 
> 
> On 6/15/2018 11:08 AM, Luca Giammattei wrote:
> > Hi list,
> > i'm using BASE64 service program to decode a base64 string who
> > contains ascii
> > data. My goal is to write the data to a txt file on the ifs, a file
> > who should
> > be ASCII and not EBCDIC since it will serve other two applications
> > windows based.
> >
> > What am i missing?
> >
> > This is a snippet from my code:
> >
> > d FilePtr         s               *   Inz(*Null)
> > D File            s          65535A   Based(FilePtr)
> >  *
> > D bytesToDec      s              4A   Inz
> > D decbuf          s              3a   Inz
> > d declen          s             10i 0 Inz
> > D fd              s             10i 0 Inz
> > D rc              s             10i 0 Inz
> > d pathToFile      s             80a   Inz
> >  *
> > d C_pathToXML     c                   '/BNL'
> > d Path2           c                   '/Req/XML/BNL'
> > d MAV             c                   'MAV'
> > d RH              c                   'RH'
> >
> > /free
> >  *INLR=*On;
> >  pathToFile=C_PathToXml + '/' + MAV + '/' +
> >             MAV  + '_' + %Trim(%Char(TBNL.BFIbnl)) + '_' +
> >             %Char(%dec(TBNL.BFDTA1)) + '_' + %Char(%dec(TBNL.BFDTA2)) +
> >              '.txt';
> >
> >  Count = %Size(File) * 100;
> >  FilePtr = %Alloc(Count);
> >
> >  //do stuff here that put base64 string into File variable
> >
> >  fd = openf((%trim(pathToFile))
> >              : O_CREAT + O_EXCL + O_WRONLY
> >              + O_TEXTDATA + O_TEXT_CREAT + O_CCSID
> >              : S_IRUSR + S_IWUSR
> >              : 819
> >              : 0);
> >  If fd < 0;
> >
> >    //write error to log file
> >   return;
> >
> >  Else;
> >
> >   //Take File variable 4 bytes per time and decode them
> >    DoW %Len(%Trim(File)) > 0;
> >      BytesToDec = %subst(File:1:4);
> >      If %len(%Trim(File)) > 4;
> >        File = %subst(File:5);
> >      Else;
> >
> >        File = '';
> >      EndIf;
> >      declen = base64_decode(%addr(BytesToDec):4
> >                            :%addr(decbuf):3);
> >      http_xlatep(declen:%addr(decbuf):TO_ASCII);
> >      writef(fd:%addr(decbuf):declen);
> >    ENDDO;
> >
> >    closef(fd);
> >
> >    //Write to log
> >
> >  EndIf;
> >
> >  Dealloc(en) Fileptr;
> >
> >  Return;
> > /end-free
> >
> >
> > --
> > Luca Giammattei
> >
> >
> > ---
> > Questa e-mail è stata controllata per individuare virus con Avast
> > antivirus.
> > https://www.avast.com/antivirus
> >
> >
> >
> > --
> > This message has been scanned by E.F.A. Project and is believed to be
> > clean.
> >
> >
> 
> --
> _______________________________________________
> Ftpapi mailing list
> Ftpapi@xxxxxxxxxxxxxxxxxxxxxx
> http://scottklement.com/mailman/listinfo/ftpapi
> 

-- 
Luca Giammattei
F.I.G.C. - Lega Nazionale Dilettanti
Ufficio C.E.D. - IT
Piazzale Flaminio, 9
I - 00196 Roma
Tel. dir.: +39 0632822372
Fax: +39 0632822711
-- 
_______________________________________________
Ftpapi mailing list
Ftpapi@xxxxxxxxxxxxxxxxxxxxxx
http://scottklement.com/mailman/listinfo/ftpapi