/* Encodes a file in Base64. REXX */ /* */ /* Written by James L. Dean */ /* */ /* Modification history: */ /* @18-FEB-1998...David Alcock DaveA@ticnet.com */ /* - Added cross platform code for MVS & VM support; */ /* - char_set change to fit in 72 columns */ /* - Added command line ability: ENBASE64 i.zip o.b64*/ char_set = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' char_set = char_set||'abcdefghijklmnopqrstuvwxyz0123456789+/' mtype = address() /* Get the environment */ execio = 0 /* We haven't done EXECIO yet */ /*--------------------------------------------------------------------- | Read in the input file based on the environment ------------------------------------------------------------------- */ select /*-------------------------------------------------------------- | PC: OS/2, Windows 95, Windows NT, etc. | | For Rexx implementations that have the LINEIN/CHARIN & | LINEOUT/CHAROUT functions, we don't read in the whole file | here, we just get ready... ------------------------------------------------------------ */ when mtype == "CMD" | mtype == "COMMAND" then do arg input_file_name output_file_name if input_file_name == "" then do CALL CHAROUT,'Input? ' input_file_name=LINEIN() end if output_file_name == "" then do CALL CHAROUT,'Output? ' output_file_name=LINEIN() end SAY 'Writing "'output_file_name'".' CALL RxFuncAdd 'SysFileDelete','RexxUtil','SysFileDelete' i=SysFileDelete(output_file_name) end /*-------------------------------------------------------------- | MVS (batch execution) - defaults to SYSUT1 for input ------------------------------------------------------------ */ when mtype == "MVS" then do execio = 1 arg options "EXECIO * DISKR SYSUT1 (FINIS STEM filein." erc = rc if erc <> 0 then do say "Error reading SYSUT1" exit end if filein.0 == 0 then do say "SYSUT1 is empty" exit 12 end fileout_dd = "SYSUT2" end /*-------------------------------------------------------------- | TSO (but not batch "MVS") ------------------------------------------------------------ */ when mtype == "TSO" then do execio = 1 arg filein_dsn fileout_dsn if fileout_dsn == "" then do say "%ENBASE64 - Missing output dataset name, terminating" exit end fileout_dd = b64||random() x = LISTDSI(fileout_dsn) fileout_pdsn = sysdsname parse value fileout_dsn with . "(" member ")" . if member <> "" then do fileout_pdsn = fileout_pdsn"("strip(member)")" end if x <> 0 then do say "%ENBASE64 - Creating new output dataset: "fileout_dsn "ALLOCATE FILE("fileout_dd") DA('"fileout_pdsn"') NEW" , "UNIT(SYSDA) TRACKS PRI(5 30)" , "LRECL(80) BLKSIZE(8800) RECFM(FB)" end else "ALLOCATE FILE("fileout_dd") DA('"fileout_pdsn"') SHR REUSE" if filein_dsn == "" then do say "%ENBASE64 - Missing input dataset name, terminating" exit end x = LISTDSI(filein_dsn) if x <> 0 then do say "Error accessing DSN:" filein_dsn say "> "sysmsglvl1 say "> "sysmsglvl2 say "> SYSREASON: "sysreason exit end filein_pdsn = sysdsname parse value filein_dsn with . "(" member ")" . if member <> "" then do filein_pdsn = filein_pdsn"("strip(member)")" end say "Processing input file "filein_pdsn filein_dd = "SYU1"random() address TSO "ALLOCATE FILE("filein_dd")" , "DA('"filein_pdsn"') SHR REUSE" "EXECIO * DISKR "filein_dd , "(FINIS STEM filein." erc = rc address TSO "FREE FILE("filein_dd")" if erc <> 0 then do say "Error reading input file: "filein_pdsn exit end if filein.0 == 0 then do say "Input file is empty: "filein_pdsn exit end if member == "" then name = "UNKNOWN" else name = member end /*-------------------------------------------------------------- | VM files ------------------------------------------------------------ */ when mtype == "CMS" then do execio = 1 parse arg fn ft fm on ot om "(" options fn = translate(strip(fn)) ft = translate(strip(ft)) fm = translate(strip(fm)) if fm == "" then fm = "A" else fm = translate(fm) address command "STATE" fn ft fm if rc <> 0 then do upper fn ft fm "STATE" fn ft fm if rc <> 0 then do say "File" fn ft fm "not found" exit end end vmfclear "FINIS" fn ft fm "EXECIO * DISKR "fn ft fm" (STEM FILEIN. FINIS" erc = rc if erc <> 0 then do say "Error reading input file: "fn ft fm exit end if filein.0 == 0 then do say "Input file is empty: "fn ft fm exit end on = translate(strip(on)) ot = translate(strip(ot)) om = translate(strip(om)) if om == "" then om = "A" end /*-------------------------------------------------------------- | Other systems that we don't support ------------------------------------------------------------ */ otherwise say "Not written to support system type: "mtype say "We do support: CMS, CMD, COMMAND, MVS and TSO" exit end /* of "select" */ /*--------------------------------------------------------------------- | Perform functions for Mainframe environments ------------------------------------------------------------------- */ if execio == 1 then do filesize = 0 file = "" do i = 1 to filein.0 file = file||filein.i filesize = filesize + length(filein.i) drop filein.i end drop filein.0 /* After here, filein.x vars are not needed and aren't valid */ lineout = "" lineoff = 1 fileoff = 1 if mtype == "MVS" | mtype = "TSO" then "newstack" if mtype == "CMS" then do "MAKEBUF" makebufn = rc end end /*--------------------------------------------------------------------- | Read the input file and write out a BASE64 encoded version of it ------------------------------------------------------------------- */ input_eof=0 col_num=1 DO WHILE (input_eof = 0) num_octets=0 triple=0 DO octet_num=1 TO 3 IF input_eof = 0 THEN DO select /* -------------------------------------------------- */ when mtype == "CMD" | mtype == "COMMAND" then do octet=CHARIN(input_file_name) end when execio == 1 then do if fileoff > filesize then octet = "" else do octet = substr(file,fileoff,1) fileoff = fileoff + 1 /* increment file offset */ end end otherwise nop end /* of select ---------------------------------------- */ IF LENGTH(octet) = 0 THEN input_eof=-1 END IF input_eof = 0 THEN DO triple=256*triple+C2D(octet) num_octets=num_octets+1 END ELSE triple=256*triple END num_sextets=(8*num_octets)%6 IF 6*num_sextets < 8*num_octets THEN num_sextets=num_sextets+1 IF num_sextets > 0 THEN DO sextet_num=1 DO WHILE(sextet_num <= 4) quotient=triple%64 stack.sextet_num=triple-64*quotient sextet_num=sextet_num+1 triple=quotient END DO WHILE(num_sextets >= 1) sextet_num=sextet_num-1 select /* -------------------------------------------------- */ when mtype == "CMD" | mtype == "COMMAND" then do rc=CHAROUT(output_file_name,SUBSTR(char_set,1+stack.sextet_num,1)) end when execio == 1 then do lineout = lineout""SUBSTR(char_set,1+stack.sextet_num,1) end otherwise nop end /* of select --------------------------------------- */ col_num=col_num+1 IF col_num > 76 THEN DO select /* ------------------------------------------------ */ when mtype == "CMD" | mtype == "COMMAND" then do rc=CHAROUT(output_file_name,D2C(13)) rc=CHAROUT(output_file_name,D2C(10)) end when execio == 1 then do queue lineout lineout = "" end otherwise nop end /* of select ------------------------------------- */ col_num=1 END num_sextets=num_sextets-1 END DO WHILE(sextet_num > 1) select /* ------------------------------------------------ */ when mtype == "CMD" | mtype == "COMMAND" then do rc=CHAROUT(output_file_name,'=') end when execio == 1 then do lineout = lineout||"=" end otherwise nop end /* of select ------------------------------------- */ col_num=col_num+1 IF col_num > 76 THEN DO select /* ------------------------------------------------ */ when mtype == "CMD" | mtype == "COMMAND" then do rc=CHAROUT(output_file_name,D2C(13)) rc=CHAROUT(output_file_name,D2C(10)) end when execio == 1 then do queue lineout lineout = "" end otherwise nop end /* of select ------------------------------------- */ col_num=1 END sextet_num=sextet_num-1 END END END IF col_num > 1 THEN DO select /* ------------------------------------------------ */ when mtype == "CMD" | mtype == "COMMAND" then do rc=CHAROUT(output_file_name,D2C(13)) rc=CHAROUT(output_file_name,D2C(10)) end when execio == 1 then do queue lineout lineout = "" end otherwise nop end /* of select ------------------------------------- */ END /*--------------------------------------------------------------------- | Write the file out for EXECIO environments ------------------------------------------------------------------- */ select when mtype == "TSO" | mtype == "MVS" then do queue "" /* end of file */ "EXECIO * DISKW "fileout_dd" (FINIS" "DELSTACK" if fileout_dd <> "SYSUT2" then "FREE FILE("fileout_dd")" end when mtype == CMS then do queue "" /* end of file */ "EXECIO * DISKW "on ot om" (FINIS" "DROPBUF " end otherwise nop end /*--------------------------------------------------------------------- | END END END END END - End of ENBASE64 exec - END END END END END ------------------------------------------------------------------- */