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:

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 2 days ago
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
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion

Reply




Copyright 1983-2019 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).