A Simple YENC Decoder Module

April 2, 2002 | Fredrik Lundh

Note: The code below is a yEnc decoder module for the Python programming language. If you’re looking for something that you can use right away, chances are that you will found what you’re looking for here or here, or via one of the links to the left.

 
Decode a YENC-encoded message (Python 1.5.2 and later)
import string

yenc42 = string.join(map(lambda x: chr((x-42) & 255), range(256)), "")
yenc64 = string.join(map(lambda x: chr((x-64) & 255), range(256)), "")

##
# Decode a YENC-encoded message into a list of string fragments.
# Returns None if no YENC body was found.

def yenc_decode(file):
    # find body
    while 1:
        line = file.readline()
        if not line:
            return None
        if line[:7] == "=ybegin":
            break
    # extract data
    buffer = []
    while 1:
        line = file.readline()
        if not line or line[:5] == "=yend":
            break
        if line[-2:] == "\r\n":
            line = line[:-2]
        elif line[-1:] in "\r\n":
            line = line[:-1]
        data = string.split(line, "=")
        buffer.append(string.translate(data[0], yenc42))
        for data in data[1:]:
            data = string.translate(data, yenc42)
            buffer.append(string.translate(data[0], yenc64))
            buffer.append(data[1:])
    return buffer

A Django site. rendered by a django application. hosted by webfaction.