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:

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 7 hours ago
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
Resending Emails that have Errored Out with Updated Router or Authentication Information Resending Emails that have Errored Out with Updated Router or Authentication Information
Posted by March 1, 2019
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i
Posted by January 27, 2019
BVSTools >> BVSTools Announcements
G4MSDRV Currently Not Supported G4MSDRV Currently Not Supported
Posted by January 17, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Microsoft Apps (G4MS) Specific Announcements
Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG Removing Trailing Carriage Returns and/or Line Feeds from a String with RPG
Posted by December 26, 2018
Programming >> RPG Programming
Create QRCODE in DDS Create QRCODE in DDS
Posted by September 21, 2018
Programming >> RPG Programming
Base64 Encoding a File with RPG Base64 Encoding a File with RPG
Posted by September 6, 2018
Programming >> RPG Programming
Building JSON with RPG and YAJL and Writing to Standard Output Building JSON with RPG and YAJL and Writing to Standard Output
Posted by August 31, 2018
Programming >> Proof of Concept (POC)
How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE How to Delete Files or Empty Trash From Your Google Drive with your IBM i and RPG/ILE
Posted by July 24, 2018
BVSTools >> BVSTools Software Discussion >> GreenTools for G Suite (Google Apps) (G4G) 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).