Version 3 of yencode

Updated 2006-03-07 20:07:06

Pat Thoyts wrote in the comp.lang.tcl newsgroup: I found out about yenc encoding ... and it's not the same as uuencoding. Hopefully you are not trying to decode yenc data using the uuencode/uudecode code :) If you want a tcl yenc decoder I'm going to add one to tcllib's base64 module shortly.

LV and in fact Pat has added (to the latest base64 module of tcllib) a yencode set of functions.

In the meantime here is the core of this method:

 namespace eval yencode {} ;# added by RS 

 proc yencode::yEncode {s} {
    set r {}
    binary scan $s c* d
    foreach {c} $d {
        set v [expr {($c + 42) % 256}]
        if {$v == 0x00 || $v == 0x09 || $v == 0x0A 
            || $v == 0x0D || $v == 0x3D} {
            append r "="
            set v [expr {($v + 42) % 256}]
        }
        append r [format %c $v]
    }
    return $r
 }

 proc yencode::yDecode {s} {
    set r {}
    set esc 0
    binary scan $s c* d
    foreach c $d {
        if {$c == 61 && $esc == 0} {
            set esc 1
            continue
        }
        set v [expr {($c - 42) % 256}]
        if {$esc} {
            set v [expr {($v - 42) % 256}]
            set esc 0
        }
        append r [format %c $v]
    }
    return $r
 }

Someone recently posted a question on comp.lang.tcl asking about yenc multi-part support in Tcl; anyone know of such code?


Category Package, subset Tcllib see also uudecode, base64, Arts and crafts of Tcl-Tk programming