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:

GreenTools for Slack (G4SLK) v3.00 Released GreenTools for Slack (G4SLK) v3.00 Released
Posted by January 17, 2020
BVSTools >> BVSTools Announcements >> GreenTools for Slack (G4SLK) Specific Announcements
Calling a QSH Command from RPG Calling a QSH Command from RPG
Posted by December 26, 2019
Programming >> RPG Programming
SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages SPLTOOL Print Range (PRTRNG) Function Updated to Handle Spooled Files up to 999,999,999 Pages
Posted by December 14, 2019
BVSTools >> BVSTools Announcements >> Spooled File Tools (SPLTOOL) Specific Announcements
GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs GreenTools for Microsoft Apps (G4MS) Updated to v6.00 - Now Uses Microsoft Graph APIs
Posted by November 24, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix V7R4 Changes CCSID of Compressed File Using PASE JAR Command - Here's The Fix
Posted by November 21, 2019
IBM Power Systems >> (QGPL) IBM i
Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens Using GETURI to Make OAuth 2.0 Requests - Custom Headers for Access Tokens
Posted by November 11, 2019
BVSTools >> BVSTools Software Discussion >> Get URI (GETURI) Specific Discussion
GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality GreenTools for Microsoft Apps (G4MS) v5.00 Released with Updated OneDrive Support and 3rd Party Functionality
Posted by October 20, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
BVSTools is Now Running V7R4M0 BVSTools is Now Running V7R4M0
Posted by September 28, 2019
BVSTools >> BVSTools Announcements
GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon) GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon)
Posted by August 27, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality
Posted by July 28, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
BVSTools Small Price Increase in 2020 BVSTools Small Price Increase in 2020
Posted by July 26, 2019
BVSTools >> BVSTools Announcements
GreenTools for Vertex Cloud (VTXCLOUD) Now Available GreenTools for Vertex Cloud (VTXCLOUD) Now Available
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Vertex Cloud (VTXCLOUD) Specific Announcements
GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by July 17, 2019
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) 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).