Replace
Eric
elw000 at verizonz.net
Sat May 6 19:35:56 EDT 2006
On 2006-05-06, Dennis Lee Bieber wrote:
> On Sat, 06 May 2006 19:55:35 GMT, Dennis Lee Bieber
><wlfraed at ix.netcom.com> declaimed the following in comp.lang.python:
>
>
>> splits = encData.split("#", 1)
>
> Whoops.... # => =
>
> Since there are only what, five, escaped characters, the use of a
> translate table seems somewhat overkill... A simple dictionary can be
> used...
>
> # pseudo-yENC decoder
> # ignores all of the protocol except for the
> # data portion decode
>
> d42 = "".join([chr((x - 42) % 256) for x in range(256)])
> d64 = { "@" : "\0",
> "I" : "\t",
> "J" : "\n",
> "M" : "\r",
> "}" : "=" }
> e42 = "".join([chr((x + 42) % 256) for x in range(256)])
> e64 = { "\0" : "=@",
> "\t" : "=I",
> "\n" : "=J",
> "\r" : "=M",
> "=" : "=}" }
>
> def decode(encData):
> segments = []
> while True:
> splits = encData.split("=", 1)
> segments.append(splits[0])
> if len(splits) == 1: break
> segments.append(d64[splits[1][0]])
> encData = splits[1][1:]
>
> decData = "".join(segments).translate(d42)
> return decData
>
> def encode(decData):
> segments = []
> encData = decData.translate(e42)
> while encData:
> pos = len(encData)
>
> nl = encData.find("\0")
> tb = encData.find("\t")
> lf = encData.find("\n")
> cr = encData.find("\r")
> eq = encData.find("=")
>
> if nl != -1:
> pos = min(pos, nl)
> if tb != -1:
> pos = min(pos, tb)
> if lf != -1:
> pos = min(pos, lf)
> if cr != -1:
> pos = min(pos, cr)
> if eq != -1:
> pos = min(pos, eq)
>
> segments.append(encData[:pos])
>
> if pos < len(encData):
> segments.append(e64[encData[pos]])
> encData = encData[pos+1:]
>
> return "".join(segments)
>
>
> if __name__ == "__main__":
> junk = """This is not
> the end of the silliness
> being inflicted upon the world of 183743ljf8734jflu807ur32"""
>
> morejunk = encode(junk)
> print morejunk
>
> print decode(morejunk)
> print
>
> tbl = "".join([chr(x) for x in range(256)])
>
> print repr(tbl)
> print
> yetmore = encode(tbl)
> print repr(yetmore)
> print
> print repr(decode(yetmore))
I think you right about the d64 translation thing, it does seem like overkill.
I've been playin with sting.split(), came up with...
def decode(encData):
segments = []
splits = encData.split("=")
for i in range(len(splits)):
if i == 0:
segments.append(splits[0].translate(d42))
else:
segments.append(splits[i][0].translate(d64) + splits[i][1:].translate(d42
))
decoded = ''.join(segments)
return decoded
string.split might be the way to go, this function almost decodes the test file
from the ydec site correctly.
More information about the Python-list
mailing list