bvstone

Base64 Encoding a File with RPG

Posted:

Base64 Encoding a File with RPG

This is some sample source of how I convert a file in the IFS to Base 64 using the APR Base 64 APIs.  This isn't complete as there were some small additional functions used in this, but they should be pretty self explanatory and easily substituted with functions of your own.

It also is pretty old and doesn't include any /copy source for the IFS functions, but they are also readily available.

      /COPY QCOPYSRC,APR_B64_H
      *//////////////////////////////////////////////////////////////*
      * (#b64encFile)                                                *
      *//////////////////////////////////////////////////////////////*
     P #b64encFile     B                   export
     D #b64encFile     PI            10i 0
     D   InFile                     256    value
     D   OutFile                    256    value
     D   OutCCSID                    10i 0 value options(*NOPASS)
     D   Replace                       N   value options(*NOPASS)
     D   DoCRLF                        N   value options(*NOPASS)
     D   Out_errMsg                 256    options(*NOPASS)

     D l_replace       S               N
     D l_doCRLF        S               N
     D l_inFile        S            256
     D l_outFile       S            256
     D l_ccsid         S             10i 0
     D inFDesc         S             10i 0
     D outFDesc        S             10i 0
     D rc              S             10i 0
     D b64rc           S             10i 0
     D totalBytesOut   S             10i 0
     D readSize        S             10i 0 INZ(57)
     D bytesRead       S             10i 0 
     D bytesOut        S             10i 0
     D dataRead        S            256
     D dataOut         S           2048
     D CRLF            S              2    INZ(X'0D25')
      *--------------------------------------------------------------*
      /free

       if (%parms > 3);
         l_replace = Replace;
       else;
         l_replace = *OFF;
       endif;

       if (%parms > 4);
         l_doCRLF = DoCRLF;
       else;
         l_doCRLF = *OFF;
       endif;

       // Do error checking here like if InFile = Outfile, InFile exists,
       //  outfile exists and replace = *NO, etc.

       l_inFile = %trim(InFile) + X'00';
       l_outFile = %trim(OutFile) + X'00';

       OFlag = O_RDONLY;
       inFDesc = openstmf(l_inFile:OFlag);

       if (inFDesc < 0);
         // Error opening InFile
       endif;

       if (%parms > 2);

         if (OutCCSID > 0);
           l_ccsid = OutCCSID;
         else;
           l_ccsid = #getStmfCCSID(InFile);
         endif;

       else;
         l_ccsid = #getStmfCCSID(InFile);
       endif;

       Mode = S_IRWXU + S_IRWXG + S_IRWXO;
       Oflag = O_CREATE + O_WRONLY + O_TRUNC + O_TEXTDATA +
               O_CCSID + O_TEXT_CREAT;
       outFDesc = openstmf(l_outFile:Oflag:Mode:l_ccsid:0);

       if (outFDesc < 0);
         // Error opening OutFile
       endif;

       bytesRead = readstmf(inFDesc:%addr(dataRead):readSize);

       dow (bytesRead > 0);
         dataRead = %Subst(dataRead:1:bytesRead);
         dataOut = ' ';
         b64rc = apr_base64_encode_binary(dataOut:dataRead:BytesRead);

         if (b64rc <= 0);
           // Error converting to Base 64 
         else;
           exsr $writeOut;
         endif;

         bytesRead = readstmf(inFDesc:%addr(dataRead):readSize);
       enddo;

       closestmf(InFDesc);
       closestmf(OutFDesc);

       return totalBytesOut;
       //***************************************************************
       //* Write to Output Stream File
       //***************************************************************
       begsr $writeOut;

         bytesOut = writestmf(outFDesc:%addr(dataOut):b64rc-1);

         if (bytesOut <= 0);
           // Error writing to OutFile
         else;
           totalBytesOut += bytesOut;
         endif;

         if (l_doCRLF);
           bytesOut = writestmf(outFDesc:%addr(CRLF):2);

           if (bytesOut <= 0);
             // Error writing CRLF to OutFile
           else;
             totalBytesOut += bytesOut;
           endif;

         endif;

       endsr;

      /end-free
      *--------------------------------------------------------------*
     P #b64encFile     E

 

 

 


Last edited 09/06/2018 at 14:41:23




Reply




© Copyright 1983-2024 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, TinyMCE and running on the IBM i (AKA AS/400, iSeries, System i).