Strange result using base64_decode

Other open source tools published on ScottKlement.com
Post Reply
pgurov
Posts: 2
Joined: Thu Jul 25, 2024 5:06 pm

Strange result using base64_decode

Post by pgurov »

Hello,

I came across a strange behaviour of the base64_decode function. This is the code that I have:

Code: Select all

H DFTACTGRP(*NO) actgrp(*caller) decedit('.')                            
H debug option(*srcstmt:*nodebugio:*noshowcpy)                           
H bnddir('BASE64')                                                       
                                                                         
 /copy BASE64_H                                                          
                                                                         
d message         s          80000a   ccsid(*utf8) varying inz(*all' ')  
d msg             s          80000a   varying                            
                                                                         
                                                                         
                                                                         
  //  message = '';                                                      
      msg = 'SGkgVGhlcmUu';                                              
      %len(message)=base64_decode(                                       
                      %addr(msg:*data):%len(msg):                        
                      %addr(message:*data):%len(message:*max));          
                                                                         
    *inlr = *on;                                                         
If I uncomment the "message = '';" line the decode doesn't return the correct value. The length of the variable is set correctly but the string is not decoded.
When the line is commented as it is it works fine.

It seems that base64_decode takes into account the length of the variable before the call when it should not. Or is this rather a compiler issue (we are not quite current on PTFs)?

Regards,
Peter
pgurov
Posts: 2
Joined: Thu Jul 25, 2024 5:06 pm

Re: Strange result using base64_decode

Post by pgurov »

Found the issue. It is in the way how setting the length works. from the ILE RPG Language reference:
If the set length is greater than the current length, the characters in the field between the old
length and the new length are set to blanks.
The problem is that the "blanks" are always x40 no matter that the variable is defined with CCSID(*utf8).

Solution is to set the length to %len(<var>:*max) before doing the base64_decode.
Post Reply