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

[Ftpapi] Fwd: Re: BASE64 Decode




Hi Ron,

here is a solution for BASE64 decoding and converting from UTF8 to CCSID 37

- create BASUTL with this two CL Statements or put it into a CLLE program
CRTBNDDIR  BNDDIR(MYLIB/BASUTL) TEXT('BASE64 Utilities')        
ADDBNDDIRE BNDDIR(MYLIB/BASUTL) OBJ((QSYSDIR/QAXIS10HT *SRVPGM))

- compile the following program on your system. I hope you're on V7R1 or later
- on this site you can test coding and decoding www.base64encode.org
- please keep in mind that a BASE64 String is often encoded in UTF8 = CCSID 1208
this program first decode the BASE64 String and then convert the string from
CCSID 1208 to CCSID 37


         ctl-opt main(main) dftactgrp(*no) bnddir('BASUTL');                                                           
      //------------------------------------------------------------------//                                           
      //                                                                  //                                           
      // Decode BASE64                                                    //                                           
      //                                                                  //                                           
      //-----------------                                                 //                                           
      // R.Ross 04.2017 *                                                 //                                           
      //------------------------------------------------------------------//                                           
      // iconv_open                                                       //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-pr iconv_open   likeds(dsiconv) extproc('QtqIconvOpen');                                                  
                 ##trgcode   likeds(dstqopen) const;                                                                   
                 ##srccode   likeds(dstqopen) const;                                                                   
         end-pr;                                                                                                       
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // iconv                                                            //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-pr iconv        extproc(*dclcase);                                                                        
                 ic_hconv    like(dsiconv) value;                                                                      
                 p_inbuf     pointer const;                                                                            
                 in_length   uns(10) const;                                                                            
                 p_outbuf    pointer const;                                                                            
                 out_length  uns(10) const;                                                                            
         end-pr;                                                                                                       
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // iconv_close                                                      //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-pr iconv_close  extproc(*dclcase);                                                                        
                 ic_hconv    like(dsiconv) value;                                                                      
         end-pr;                                                                                                       
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // Arrays iconv                                                     //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-ds  dsiconv     template qualified inz;                                                                   
                  ic_rtnval  int(10);                                                                                  
                  ic_cd      int(10) dim(12);                                                                          
         end-ds;                                                                                                       
                                                                                                                       
         dcl-ds  dstqopen    template qualified;                                                                       
                  tq_ccsid   int(10) inz(0);                                                                           
                  tq_convalt int(10) inz(0);                                                                           
                  tq_subsalt int(10) inz(0);                                                                           
                  tq_shftalt int(10) inz(1);                                                                           
                  tq_inplen  int(10) inz(0);                                                                           
                  tq_erropt  int(10) inz(0);                                                                           
                  tq_reserv  char(8) inz(*allx'00');                                                                   
         end-ds;                                                                                                       
                                                                                                                       
         dcl-ds  dsconv      likeds(dsiconv)  inz(*likeds);                                                            
         dcl-ds  dssrcccsid  likeds(dstqopen) inz(*likeds);                                                            
         dcl-ds  dstrgccsid  likeds(dstqopen) inz(*likeds);                                                            
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // BASE64 - Encode String - SRVPGM(QSYSDIR/QAXIS10HT)               //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-pr encbase64bin int(10) extproc('apr_base64_encode_binary');                                              
                 ##enc_p     like(d#enc_p)  value;    // Encoded                                                       
                 ##src_p     like(d#src_p)  value;    // Source                                                        
                 ##srclen    like(d#srclen) value;    // SourceLength                                                  
         end-pr;                                                                                                       
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // BASE64 - Decode String - SRVPGM(QSYSDIR/QAXIS10HT)               //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-pr decbase64bin int(10) extproc('apr_base64_decode_binary');                                              
                 ##dec_p     like(d#dec_p)  value;    // Decoded                                                       
                 ##enc_p     like(d#enc_p)  value;    // Encoded                                                       
         end-pr;                                                                                                       
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // BASE64 - Variables                                               //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-s   d#enc_p     pointer;                 // EncodedPointer                                                
         dcl-s   d#dec_p     pointer;                 // DecodedPointer                                                
         dcl-s   d#src_p     pointer;                 // SourcePointer                                                 
         dcl-s   d#srclen    int(10);                 // SourceLength                                                  
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // Variables                                                        //                                           
      //------------------------------------------------------------------//                                           
                                                                                                                       
         dcl-s   GblString   char(20000);             // String                                                        
                                                                                                                       
      //------------------------------------------------------------------//                                           
      // Main                                                             //                                           
      //------------------------------------------------------------------//                                           
         dcl-proc Main;                                                                                                
                                                                                                                       
         dcl-s   LocString   like(GblString);                                                                          
         dcl-s   LocBase64   like(GblString);                                                                          
                                                                                                                       
      // 'Test' encoded ->
VGVzdA==                                                                                             
                                                                                                                       
           LocBase64 = 'VGVzdA==';                                                                                     
                                                                                                                       
           LocString = decBase64(LocBase64:37:1208); // CCSID 1208 -> 37
                                                                                                                       
         end-proc;                                                                                                     
      //------------------------------------------------------------------//                                           
      // Decode BASE64 Data                                               //                                           
      // First decode from BASE64 to UTF8 then convert to Job CCSID       //                                           
      //------------------------------------------------------------------//                                           
         dcl-proc decBase64;                                                                                           
         dcl-pi *n           like(GblString) rtnparm;      // ResultString                                             
                 ##encstr    like(GblString) options(*varsize);                                                        
                 ##trgccsid  uns(10)         const;        // Target-CCSID                                             
                 ##srcccsid  uns(10)         const;        // Source-CCSID                                             
         end-pi;                                                                                                       
                                                                                                                       
         dcl-s   p#result    like(GblString);              // ResultString                                             
         dcl-s   p#reslen    uns(10) inz(%size(p#result)); // ResultLength                                             
         dcl-s   p#decstr    like(GblString);              // DecodedString                                            
         dcl-s   p#declen    int(10);                      // DecodedLength                                            
                                                                                                                       
           p#declen = decbase64bin(%addr(p#decstr):%addr(##encstr));                                                   
                                                                                                                       
           if p#declen = *zero;                            // DecodedLength                                            
              return *blanks;                                                                                          
           endif;                                                                                                      
                                                                                                                       
      // Convert from CCSID to CCSID with ICONV                                                                        
                                                                                                                       
           dssrcccsid.tq_ccsid = ##srcccsid;               // Source-CCSID                                             
           dstrgccsid.tq_ccsid = ##trgccsid;               // Target-CCSID                                             
           dsconv              = *allx'00';                // Convert                                                  
                                                                                                                       
           dsconv = iconv_open(dstrgccsid:dssrcccsid);     // iconv Open                                               
           iconv(dsconv:%addr(p#decstr):p#declen:          // iconv Convert                                            
                        %addr(p#result):p#reslen);                                                                     
           iconv_close(dsconv);                            // iconv Close                                              
                                                                                                                       
           return %subst(p#result:1:%size(p#result) - p#reslen);                                                       
                                                                                                                       
         end-proc;                                                                                                     
      //------------------------------------------------------------------//      
                                      

 
  


Am 07.06.2017 um 16:10 schrieb Ron Koontz:

I was reading this the other day and was going to try it.  However the link is broken at the bottom to see examples.  Anyone have that link or know where I can get it ?

 

Thanks

Ron Koontz

 

From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx [mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Bryan Dietz
Sent: Tuesday, June 06, 2017 9:36 AM
To: FTPAPI/HTTPAPI mailing list
Subject: Re: [Ftpapi] BASE64 Decode

 

Just wanted to pass along the other way to do it using built in en/decoder:

 

 

bryan

 

 

On Jun 5, 2017, at 10:39 AM, Ron Koontz <rkoontz@xxxxxxxxx> wrote:

 

Is there an easy way to decode a field from BASE64 ? ( IBM API maybe that I cannot find )

I have tried Scott’s example but get a “Definition not found for symbol 'BASE64_ENCODE'. “ after I compile my program.

I did all the  compile options in the BASE64R4 documentation.

 

I tried an example online as well and got the same error just to make sure it was not my main pgm..

 

Any Ideas?  Its probably something easy I’m not doing  I would guess.

 

Example off the web :

/copy BASE64_H                                  

D Input           S              6a              

D Output          S              8A              

                                                 

 /free                                           

                                                 

     input = x'61626b3735';                      

     Output = *blanks;                           

                                                 

    base64_encode( %addr(Input)                  

     : %len(%trimr(Input))                       

     : %addr(Output)                             

     : %size(Output) );                          

                                                 

     dsply Output;                               

     *inlr = *on;                                

                                                 

 /end-free                                       

 

Thanks 

Ron

 



The information contained in this e-mail is intended only for the individual or entity to whom it is addressed. Its contents (including any attachments) may contain confidential and/or privileged information. If you are not an intended recipient you shall not use, disclose, disseminate, copy or print its contents. If you receive this e-mail in error, please notify the sender by reply e-mail and delete and destroy the message. Continental Express, Inc. and its subsidiaries and affiliates will not be held liable for the unintended or unauthorized use of any information contained in this email or as a result of any additions or deletions of information originally contained in this email.
-- 
_______________________________________________
Ftpapi mailing list
Ftpapi@xxxxxxxxxxxxxxxxxxxxxx
http://scottklement.com/mailman/listinfo/ftpapi

 




The information contained in this e-mail is intended only for the individual or entity to whom it is addressed. Its contents (including any attachments) may contain confidential and/or privileged information. If you are not an intended recipient you shall not use, disclose, disseminate, copy or print its contents. If you receive this e-mail in error, please notify the sender by reply e-mail and delete and destroy the message. Continental Express, Inc. and its subsidiaries and affiliates will not be held liable for the unintended or unauthorized use of any information contained in this email or as a result of any additions or deletions of information originally contained in this email.



-- 
Rainer Ross
IT-Beratung
www.myhofi.com
Hotels finden - leicht gemacht
Sitz:   Schmalholzstr. 27, 86916 Kaufering
Tel:    0171/9637923
Ust-ID: DE154580259
-- 
_______________________________________________
Ftpapi mailing list
Ftpapi@xxxxxxxxxxxxxxxxxxxxxx
http://scottklement.com/mailman/listinfo/ftpapi