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



Latest Posts:

G4MSMAIL Now Allows Wildcard Attachments G4MSMAIL Now Allows Wildcard Attachments
Posted by June 10, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
G4G Update for Uploading Large Files G4G Update for Uploading Large Files
Posted by May 28, 2021
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Can We Purchase License Keys for More Than One Year at a Time? Can We Purchase License Keys for More Than One Year at a Time?
Posted by May 21, 2021
BVSTools >> BVSTools Announcements
JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages JOBWATCH 5.10 Released with New Feature to Monitor CPU Percentages
Posted by May 10, 2021
BVSTools >> BVSTools Announcements >> Job Watch (JOBWATCH) Specific Announcements
Iseries Access data transfer Iseries Access data transfer
Posted by April 29, 2021
IBM Power Systems >> (QGPL) IBM i
YAJL - Parsing YAJL - Parsing
Posted by April 4, 2021
Programming >> RPG Programming
Create LPAR partition and install OS Create LPAR partition and install OS
Posted by March 24, 2021
IBM Power Systems >> (QGPL) IBM i
G4MS Now Allows User/Password Authentication Option G4MS Now Allows User/Password Authentication Option
Posted by March 16, 2021
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
IPDS printer file using CHRSIZ IPDS printer file using CHRSIZ
Posted by February 25, 2021
Programming >> RPG Programming
How to Correctly Send the Display Machine Information (DSPMCHINF) Data to BVSTools for Key Generation How to Correctly Send the Display Machine Information (DSPMCHINF) Data to BVSTools for Key Generation
Posted by February 9, 2021
BVSTools >> BVSTools Software Discussion
G4MS Updated to v7.10 - Required Update for All G4MS Users G4MS Updated to v7.10 - Required Update for All G4MS Users
Posted by November 12, 2020
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
MAILTOOL Errors with Microsoft Office 365 (2020) MAILTOOL Errors with Microsoft Office 365 (2020)
Posted by September 3, 2020
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Using MAILTOOL With Office 365 and Two Factor Authentication (2FA or MFA) Using MAILTOOL With Office 365 and Two Factor Authentication (2FA or MFA)
Posted by August 17, 2020
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
Updating DNS With Dynamic IP Addresses After a Router Reboot/Power Outage Updating DNS With Dynamic IP Addresses After a Router Reboot/Power Outage
Posted by August 16, 2020
Programming >> Proof of Concept (POC)
GETURI v10.10 Released with New ILE Functions GETURI v10.10 Released with New ILE Functions
Posted by August 12, 2020
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements

Reply




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