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

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