Version 1 of uuencode

Updated 2002-01-15 21:41:55

Here is a preliminary version of a uuencode module for tcllib inclusion. There are two entry points. uuencode::encode and uuencode::decode just encode or decode the input data while uuencode::uuencode and uuencode::uudecode will generate a properly formatted message (lines limited to 67 chars, length encoded into the first character).

Once I've finished (soon) I just need to decide where in tcllib we place this. I figure it can accompany base64. PT


 # uuencode - Copyright (C) 2002 Pat Thoyts <[email protected]>
 #
 # -------------------------------------------------------------------------
 # See the file "license.terms" for information on usage and redistribution
 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 # -------------------------------------------------------------------------
 # @(#)$Id: 2865,v 1.2 2002-06-21 04:28:36 jcw Exp $

 namespace eval uuencode {
     namespace export encode decode
 }

 proc uuencode::Enc {c} {
     return [format %c [expr {($c != 0) ? (($c & 0x3f) + 0x20) : 0x60}]]
 }

 proc uuencode::encode {s} {
     set r {}
     binary scan $s c* d
     foreach {c1 c2 c3} $d {
         if {$c1 == {}} {set c1 0}
         if {$c2 == {}} {set c2 0}
         if {$c3 == {}} {set c3 0}
         append r [Enc [expr {$c1 >> 2}]]
         append r [Enc [expr {(($c1 << 4) & 060) | (($c2 >> 4) & 017)}]]
         append r [Enc [expr {(($c2 << 2) & 074) | (($c3 >> 6) & 003)}]]
         append r [Enc [expr {($c3 & 077)}]]
     }
     return $r
 }

 proc uuencode::decode {s} {
     set r {}
     binary scan $s c* d
     foreach {c0 c1 c2 c3} $d {
         append r [format %c [expr {((($c0-0x20)&0x3F) << 2) & 0xFF
                                    | ((($c1-0x20)&0x3F) >> 4) & 0xFF}]]
         append r [format %c [expr {((($c1-0x20)&0x3F) << 4) & 0xFF
                                    | ((($c2-0x20)&0x3F) >> 2) & 0xFF}]]
         append r [format %c [expr {((($c2-0x20)&0x3F) << 6) & 0xFF
                                    | (($c3-0x20)&0x3F) & 0xFF}]]
     }
     return $r
 }

 proc uuencode::uuencode {str} {

     set mode 0644
     set filename Filename

     set r {}
     append r [format "begin %o %s" $mode $filename] "\n"
     for {set n 0} {$n < [string length $str]} {incr n 45} {
         set s [string range $str $n [expr {$n + 44}]]
         append r [Enc [string length $s]]
         append r [encode $s] "\n"
     }
     append r "`\nend"
     return $r
 }


 package provide uuencode 1.0

 # Local variables:
 #   mode: tcl
 #   indent-tabs-mode: nil
 # End:

Category Package, subset Tcllib see also uudecode