Skip to main content  
        iSeries home   |   Easy400     |   CGIDEV2  
Public source
 
About it
IBMi and Unicode
IBMi and Base64
CRLF commands
 
Download
 
 
IBMi and Base64  

Base64 is a binary-to-text encoding scheme that represent binary data in an ASCII string format by translating it into a radix-64 representation.
The term Base64 originates from a specific MIME content transfer encoding. Each Base64 digit represents exactly 6 bits of data. Three 8-bit bytes (i.e., a total of 24 bits) can therefore be represented by four 6-bit Base64 digits.

Base64 is designed to carry data stored in binary formats across channels that only reliably support text content.
Base64 is particularly prevalent on the World Wide Web where its uses include the ability to embed image files or other binary assets inside textual assets such as HTML and CSS files.
Base64 encoding is widely used to incorporate binary files, such as images, PDFs, Excel files and other in electronic mail messages.

Our CVT101 utility provides the following tools to convert binary data to or from Base64 encoded data:

Procedure EncB64String()-Encode a string to BASE64
Procedure DecB64String()-Decode a BASE64 string
Command BASE64ENC -Encode a stream file to BASE64
Command BASE64DEC -Decode a BASE64 stream file
Command DECB64STOF -Decode a BASE64 string to a stream file


  • 1 - Procedure EncB64String() - Encode a string to BASE64

    • Procedure prototype
      D EncB64String    pi            10u 0       
      D  inptPntr                       *   value 
      D  inptLn                       10u 0 value 
      D  ouptPntr                       *   value 
      D  ouptLn                       10u 0 value
      
      where
      • input variable inptPntr is a pointer to the binary string to be encoded
      • input variable inptPLn is the length of the binary string to be encoded
      • input variable ouptPntr is a pointer to a character string to contain the encoded result
      • input variable outptPLn is the length of the character string to contain the encoded result.
        Note that this length must be at least (4/3) times the length of the binary input string to be encoded.
      • The procedure returns the length of the BASE64 encode string.
    • Coding example (See CVT101/SAMPLERPG member B64ENCSTRG)
       /copy CVT101/qrpglesrc,hspecs                                    
       /copy CVT101/qrpglesrc,hspecsbnd                                 
       /copy CVT101/qrpglesrc,xxxproto                                  
       /copy CVT101/qrpglesrc,prototypeb                                
       /copy CVT101/qrpglesrc,usec                                      
                                                                        
      D outLen          s             10u 0                             
      D inptPntr        s               *                               
      D inptLen         s             10u 0                             
      D ouptPntr        s               *                               
      D ouptLen         s             10u 0                             
                                                                        
      D inptString      s          33797                                
      D ouptString      s          32797                                
       /free                                                            
          inptString=x'3278746F6F6C732E7A';                             
          outLen=EncB64String(%addr(inptString):%len(%trimr(inptString))
                              %addr(ouptString):%size(ouptString));     
          return;
      
      The request is to encode to BASE64 the first 9 bytes of variable inptString.
      As a result, the value of variable outLen is 12, and the first 12 bytes of variable ouptString are 'Mnh0b29scy56'.


  • Procedure DecB64String() - Decode a BASE64 string

    • Procedure prototype
      D DecB64String    pi            10u 0      
      D  inptPntr                       *   value
      D  inptLn                       10u 0 value
      D  ouptPntr                       *   value
      D  ouptLn                       10u 0 value
      
      where
      • input variable inptPntr is a pointer to the BASE64 string to be decoded
      • input variable inptPLn is the length of the BASE64 string to be decoded
      • input variable ouptPntr is a pointer to a string that will contain the decoded result
      • input variable outptPLn is the length of the string that will contain the decoded result.
        Note that this length must be at least (3/4) times the length of the BASE64 string to be decoded.
      • The procedure returns the length of the decoded string.
    • Coding example (See CVT101/SAMPLERPG member B64DECSTRG)
       /copy CVT101/qrpglesrc,hspecs                                    
       /copy CVT101/qrpglesrc,hspecsbnd                                 
       /copy CVT101/qrpglesrc,xxxproto                                  
       /copy CVT101/qrpglesrc,prototypeb                                
       /copy CVT101/qrpglesrc,usec                                      
                                                                        
      D outLen          s             10u 0                             
      D inptPntr        s               *                               
      D inptLen         s             10u 0                             
      D ouptPntr        s               *                               
      D ouptLen         s             10u 0                             
                                                                        
      D inptString      s          33797                                
      D ouptString      s          32797                                
       /free                                                           
          inptString='Mnh0b29scy56';                                      
          outLen=DecB64String(%addr(inptString):%len(%trimr(inptString)): 
                              %addr(ouptString):%size(ouptString));       
          return;
      
      The request is to decode from BASE64 the first 12 bytes of variable inptString.
      As a result, the value of variable outLen is 9, and the first 9 bytes of variable ouptString are x'3278746F6F6C732E7A'.

  • Command BASE64ENC - Encode a stream file to BASE64

                           Encode a stmf to BASE64 (BASE64ENC)                      
    
     Type choices, press Enter.
    
     From stream file . . . . . . . .  FRMSTMF                                           
                       
     To stream file . . . . . . . . .  TOSTMF                                            
                       
     To stream file CCSID . . . . . .  TOCCSID    US_ASCII      1-65535, US_ASCII, *JOB
     E-mail option (74 columns) . . .  EMAILOPT   *NO           *YES, *NO
     Replace TOSTMF . . . . . . . . .  REPLACE *NO           *YES, *NO
    
    
    Use this command to convert a stream file to a BASE64 stream file (The size of a BASE64 stream file is 1.33 times larger than the original one).
    • From stream file (FRMSTMF) - Path and name of the stream file to be encoded to BASE64.
      This stream file must have the CCSID that is required once decoded from BASE64. Examples: if the file is going to be used on a PC, it must have an ASCII CCSID (e.g. 819). If the file is going to be used on an IBM i, it must have an EBCDIC CCSID (e.g. 37).
    • To stream file (TOSTMF) - Path and name of the stream file that will contain the BASE64 version.
    • To stream file CCSID (TOCCSID) - CCSID of the stream file specified in parameter TOSTMF. Special values:
      • US_ASCII - is CCSID 819
      • *JOB - is the job default CCSID
    • E-mail option (74 columns) (EMAILOPT) - Specify *YES to format the BASE64 stream file in 74-columns rows. This format is required when BASE64 data are used in e-mail messages.
    • Replace TOSTMF (REPLACE) - Whether the stream file specified in parameter TOSTMF should be replaced if already existing.


  • Command BASE64DEC - Decode a BASE64 stream file

                            Decode a BASE64 stmf (BASE64DEC)                
    
     Type choices, press Enter.
    
     From stream file . . . . . . . .  FRMSTMF                                           
                       
     To stream file . . . . . . . . .  TOSTMF                                            
                       
     To stream file CCSID . . . . . .  TOCCSID    US_ASCII      Number, US_ASCII, *JOB
     Replace TOSTMF . . . . . . . . .  REPLACE    *NO           *YES, *NO
    
    
    Use this command to convert a BASE64 stream file back to its original binary data. Maximum attention should be paid to parameter TOCCSID. While the binary data of the converted file are restored to their original CCSID, that same original CCSID must be reassigned to the stream file object description.
    • From stream file (FRMSTMF) - Path and name of the BASE64 stream file to be decoded.
    • To stream file (TOSTMF) - Path and name of the stream file that will contain the decoded version of the BASE64 stream file.
    • To stream file CCSID (TOCCSID) - CCSID to be assigned to the object description of the decoded stream file specified in parameter TOSTMF. This CCSID must be the same that the original stream file used to have before conversion to BASE64. Special values:
      • US_ASCII - is CCSID 819
      • *JOB - is the job default CCSID
    • Replace TOSTMF (REPLACE) - Whether the stream file specified in parameter TOSTMF should be replaced if already existing.


  • Command DECB64STOF - Decode a BASE64 string to a stream file

                         Decode BASE64 string to file (DECB64STOF)                
    
     Type choices, press Enter.
    
     From stream file . . . . . . . .  FRMSTMF                                           
                       
     To stream file . . . . . . . . .  TOSTMF                                            
                       
     To stream file CCSID . . . . . .  TOCCSID    US_ASCII      Number, US_ASCII, *JOB
     Replace TOSTMF . . . . . . . . .  REPLACE    *NO           *YES, *NO
     String detection method  . . . .  STGOPT                   1-Position, 2-Delimiters
    
     String starting position . . . .  STGSTRPOS                1-4294967295 
     String length  . . . . . . . . .  STGLEN                   1-4294967295
    
     String left 'delimiter'  . . . .  STGLD                                             
                                                                                         
     String right 'delimiter' . . . .  STGRD                                             
                                                                                         
     String instance number . . . . .  STGINST                  1-999                          
    
    
    Use this command to pick up a BASE64 string from a stream file and to decode it (via command BASE64DEC) into another stream file.
    • From stream file (FRMSTMF) - Path and name of the stream file containing the BASE64 string to be decoded.
    • To stream file (TOSTMF) - Path and name of the stream file contain the decoded BASE64 string.
    • To stream file CCSID (TOCCSID) - CCSID to be assigned to the object description of the decoded stream file specified in parameter TOSTMF. This CCSID must be the same that the original string used to have before conversion to BASE64. Special values:
      • US_ASCII - is CCSID 819
      • *JOB - is the job default CCSID
    • Replace TOSTMF (REPLACE) - Whether the stream file specified in parameter TOSTMF should be replaced if already existing.
    • String detection method (STGOPT) - The method used to detect the BASE64 string within stream file FRMSTMF.
      Select one of the following:
      • 1 - With this method, you have to specify the starting position of the BASE64 string and its length.
        1. String starting position (STGSTRPOS) - The position - within the FRMSTMF stream file - where the BASE64 string starts.
        2. String length (STGLEN) - Length of the BASE64 string to be decoded.
      • 2 - With this method, you have to specify the left delimiter of the BASE64 string, its right delimiter and its instance number.
        1. String left delimiter (STGLD) - Enter - within quotes - the left delimiter identifying the start of the BASE64 string.
          Example: STGLD('<base64pdf>')
        2. String right delimiter (STGRD) - Enter - within quotes - the right delimiter identifying the end of the BASE64 string.
          Example: STGRD('</base64pdf>')
        3. String instance number (STGINST) - The FRMSTMF may contain more than one instance of a string with a given start/end set of delimiters. This is why you have to specify the sequence number of the string instance identified by parameters STGLD and STGRD.
          Example: if there are 3 instances of a given BASE64 string within stream file FRMSTMF and you want to decode the second one, specify STGINST(2).
    Example:
    DECB64STOF FRMSTMF('/tmp/json.txt') TOSTMF('/tmp/json.pdf') TOCCSID(US_ASCII) REPLACE(*YES)
    STGOPT(2) STGLD('<base64pdf>') STGRD('</base64pdf>') STGINST(1)




    Contact