midrange.com code scratchpad
Name:
Simple Base64 example
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
12/12/2013 09:17:00 pm
IP:
Logged
Description:
Decoding Base64 from an XML element.
Code:
  1.       /copy qrpgsrc,umedheader                                    
  2.       /copy qrpgsrc,httpapi_h                                     
  3.       /copy qrpgsrc,ifsio_h                                       
  4.       /copy qrpgsrc,base64_h                                      
  5.      D Response        s            256a                          
  6.      D DecodedResp     s            256a                          
  7.      D Incoming        PR                                         
  8.      D   userdata                      *   value                  
  9.      D   depth                       10I 0 value                  
  10.      D   name                      1024A   varying const          
  11.      D   path                     24576A   varying const          
  12.      D   value                    65535A   varying const          
  13.      D   Attrs                         *   dim(32767)             
  14.      D                                     const options(*varsize)
  15.                                                                   
  16.       /free                                                       
  17.          Response = '/kevin/testb64.xml';                         
  18.          DecodedResp = '/kevin/testb64.txt';                      
  19.          http_parse_xml_stmf( Response                            
  20.                            : HTTP_XML_CALC         
  21.                            : *null                 
  22.                            : %paddr(Incoming)      
  23.                            : *null );              
  24.        *Inlr = *on;                                
  25.        Return;                                     
  26.      /end-free                                     
  27.                                                    
  28.       P Incoming        B                                          
  29.       D Incoming        PI                                         
  30.       D   userdata                      *   value                  
  31.       D   depth                       10I 0 value                  
  32.       D   name                      1024A   varying const          
  33.       D   path                     24576A   varying const          
  34.       D   value                    65535A   varying const          
  35.       D   attrs                         *   dim(32767)             
  36.       D                                     const options(*varsize)
  37.                                                                    
  38.       D count           s             10I 0                        
  39.       D attrname        s           1024A   varying                
  40.       D attrval         s          65535A   varying                
  41.       D incval          s          32766A   varying                
  42.       D fourbytes       s              4A                          
  43.       D decbuf          s              3a                          
  44.      D declen          s             10i 0                     
  45.      D outhandle       s             10i 0                     
  46.       /free                                                    
  47.         If name = 'Response';                                  
  48.           outhandle = open(%trim(DecodedResp)                  
  49.                           : O_CREAT + O_EXCL + O_WRONLY        
  50.                           + O_TEXTDATA + O_TEXT_CREAT + O_CCSID
  51.                           : S_IRUSR + S_IWUSR                  
  52.                           : 1208                               
  53.                           : 0);                                
  54.           If outhandle = -1;                                   
  55.             dsply 'Open out failed';                           
  56.           EndIf;                                               
  57.                                                                
  58.           incval = value;                                      
  59.           dow %len(incval) > 0;                                
  60.             fourbytes = %subst(incval:1:4);                    
  61.             If %len(incval) > 4;                               
  62.               incval = %subst(incval:5);                       
  63.             Else;                                              
  64.              incval = '';                                
  65.            EndIf;                                        
  66.            declen = base64_decode(%addr(fourbytes):4     
  67.                                  :%addr(decbuf):3);      
  68.            http_xlatep(declen:%addr(decbuf):TO_EBCDIC);  
  69.            callp write(outhandle:%addr(decbuf):declen);  
  70.          EndDo;                                          
  71.          callp close(outhandle);                         
  72.        EndIf;                                            
  73.                                                          
  74.      /end-free                                           
  75.     P                 E                                  
  76.  
  77.  
  78.  
  79. *************  XML file from /kevin/testb64.xml
  80. <?xml version="1.0" encoding="iso-8859-1" ?>
  81. <Body>
  82. <Response>U2NvdHQgS2xlbWVudCB3cml0ZXMgc29tZSBncmVhdCBzb2Z0d2FyZSE=</Response>
  83. </Body>
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css