Search code examples
base64ibm-midrangerpgle

Issue while using SYSTOOLS.BASE64DECODE in RPGLE program


I am trying to decode a base64 string in SQLRPGLE using SYSTOOLS.BASE64DECODE function. The result is a blank string. The base64 string has CRLF characters as well. When I ommit the CRLF from the base64 string, decode works fine. looks like something to do with the character set. Any help is appreciated

sample base64 string: XlhBCl5GWCoqKioqKipBUEMgTG9nbyoqKioqKioqXkZTCl5GTzU4MCw

SQLRPGLE

Dcl-S VarcharField1 Varchar(4096) ;                          
Dcl-S VarcharField2 varchar(13096) inz(' ') ccsid(819);  
    
VarcharField1 = 'XlhBCl5GWCoqKioqKipBU';                     
                                                             
EXEC SQL SET :VarcharField2 =                                
              SYSTOOLS.BASE64DECODE(:VarcharField1);         
                             
*InLr = *On;       

                                      

Solution

  • It's always a good idea to check SQLSTATE or SQLCODE after running an SQL statement in RPG.

    Had you done so you would have seen,
    SQLSTATE==38000-->A Java™ routine has exited with an exception.

    Looking in the joblog, there's more info

    Java stored procedure or user-defined function SYSTOOLS.BASE64DECODE,   
      specific name BASE600002 aborted with an exception "incomplete base64 
      string: XlhBCl5GWCoqKioqKipBU".                                       
    

    As I recall, a base64 encoded string is supposed to be a multiple of 4, and must be padded with '=' if necessary.

    However, the string you've given is 21 characters long, which would mean 3 padding characters which isn't valid, you shouldn't ever need more than 2.

    If you pad the full string, instead of the substring, it should work.

    VarcharField1 = 'XlhBCl5GWCoqKioqKipBUEMgTG9nbyoqKioqKioqXkZTCl5GTzU4MCw=';  
    

    Edit
    well it works, in that no exception is thrown. Think there's still a CCSID issue, I don't usually use the SYSTOOLS.BASE64DECODE.