UUDecoding files

[Autolink] Menu

 
/* ------------------------------------------------------------------ */
/* UUDECODE.CMD - uudecode a file                                     */
/*                                                                    */
/* usage: uudecode encodedFile {>decodedFile}                         */
/*                                                                    */
/* where: encodedFile - file to UUDecode                              */
/*        decodedFile - UUDecoded file                                */
/*                                                                    */
/* authors:                                                           */
/*   Based on original by Stefan Haubenthal 1992/94                   */
/*   Hacked for OS/2 by Graham Bingham (grahamb@iacces.za), Dec 1994  */
/*   Hacked for more speed and accuracy near EOF by Xtian, Dec 1994   */
/*                                                                    */

                    /* do not allow uninitialized variables           */
  signal on NOVALUE

                    /* get the parameter                              */
  parse arg input

  if input = "" then
  do
                    /* show usage                                     */
    call LineOut, "Usage: UUDECODE encodedFile {>decodedFile}"
    exit 1
  end /* if input = "" then */

  do forever

                    /* search the start of the UUEncoded data         */
    do until datatype( mode ) = "NUM"
      line = linein( input )
      if stream( input, "S" ) <> "READY" then
        exit
      parse value line with "begin" mode dest .
    end /* do until ... */

    say "uudecoding "dest"..."

    if stream( dest, "C", "QUERY EXISTS" ) <> "" then
      "@ERASE" dest "2>NUL 1>NUL"

    call time( "e" )

    do lines=1
      line=linein( input )
      if line = "end" then
        leave
      len=c2d( decode( 1,left( line,1 ) ) )
      if len > 0 then
        call charout dest,decode( len,substr( line,2,trunc((len+2)/3)*4 ) )
    end /* do lines=1 */

    say trunc( lines/time( "e" ) * 60)" lines per minute"

    call stream dest, "C", "CLOSE"
  end /* do forever */

exit 0

/* ------------------------------------------------------------------ */
/* Function: UUDecode a line                                          */
/*                                                                    */
/* Usage:    Decode p1, p2                                            */
/*                                                                    */
/* where:    p1 = ?                                                   */
/*           p2 = ?                                                   */
/*                                                                    */
/* returns: the UUdecoded line                                        */
/*                                                                    */
/* Notes:                                                             */
/*  xx765432 xx107654 xx321076 xx543210 -> 76543210 76543210 76543210 */
/*                                                                    */
DECODE: PROCEDURE /* sub & del */

 bin=c2b( translate( arg(2),xrange("00"x,"5f"x),xrange("20"x,"7f"x) ) )
 do n=1 to length( bin ) by 6
   bin=delstr( bin,n,2 )
 end /* do n=1 ... */

 if length( bin ) < 8 then
   return b2c( right( bin,8,"0" ) )

return b2c( left( bin,arg(1)*8,"0" ) )

/* ------------------------------------------------------------------ */
/* subroutines from DECODE                                            */

c2b: PROCEDURE
  return x2b( c2x( arg(1) ) )

b2c: PROCEDURE
  return x2c( b2x( arg(1) ) )

/* ------------------------------------------------------------------ */



[Back: Maintain Multi-Value EAs in REXX]
[Next: Copy a file from HPFS to FAT and vice versa]