yencode

yencode is part of tcllib

Documentation

official reference (alternates 1 ,2 )

Description

This package provides a Tcl-only implementation of the yEnc file encoding. This is a recently introduced method of encoding binary files for transmission through Usenet. This encoding packs binary data into a format that requires an 8-bit clean transmission layer but that escapes characters special to the NNTP posting protocols. See http://www.yenc.org/ for details concerning the algorithm.

See Also

uudecode
base64
related functionality (other encoders)

Pat Thoyts wrote in comp.lang.tcl:

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?


I don't know of any support in tcl; but there is C-code available at http://www.fpx.de/fp/Software/UUDeview/ . It has a tcl interface layer. I wasn't able to get that working myself (did not try very hard) but the standalone application definitely converts multipart yenc files.


There is a bug in the example encode/decode code above that also exists in the tcllib implementation. The yEnc 1.3 spec states that escaped characters should have their ascii value further rotated by 64, not 42, so the escape character rotation value should be by 64, not 42 as in the example code. This bug exists in the current CVS v. 1.11 copy of the base64/yencode.tcl tcllib file.

I discovered this while trying to decode some externally yencoded data with tcllib's yencode.tcl implementation. I was getting corrupt output from ydecode. Making this change to my yencode.tcl file produces correctly decoded output data from ::yencode::decode.

I would have posted to the sourceforge tracker, except that logins to sourceforge are disabled at the moment.

AK 2003-12-10 09:40: Just tried to login and had no trouble.

AK: RE, can you please provide me with an example text which triggers the bug ? A patch showing exactly what to change would be good as well.

AK Ok, should be fixed in Tcllib CVS Head now, yencode 1.1.2.