YENC Algorithm JPG

P

Phillip Hamlyn

I'm trying to get a Yenc Decoding algorithm working but keep getting the
same problem. I'm using data from the yenc test newsgroup and have tried the
same thing with most of the example source code on sourceforge etc. with
multiple different posts and I keep getting the same problem - the post I'm
trying to decode has an encoded JPG file (a film poster for Kill Bill 2)
which the yEnc tools like yEnc.EXE manage to decode and encode fine, but
when I run my c# decoder I get weird intermittant translation issues.

My code looks like this (and is the same algorithm as almost everyone I've
seen irrespective of the language).

private byte[] YencDecode(byte[] encodedBlock)
{
ArrayList resultBlock = new ArrayList();
bool escape = false;
for(int count = 0; count < encodedBlock.Length ; count++)
{
byte character = encodedBlock[count];
bool conversionComplete = false;
switch(character)
{
case 61: // =
escape = true;
break;
default:
unchecked
{
if (escape)
{
character -= 64;
}
character -= 42;
}
escape = false;
conversionComplete = true;
break;
}
if(conversionComplete )
{
resultBlock.Add(character);
}
}
return (byte[])resultBlock.ToArray(typeof(byte));
}

The problem is that the 26th byte of the particular post I'm playing with is
originally 34 which decodes using this algorithm to 248, however this is
decoded by the yEnc program to 120. In fact every byte which is
mistranslated by my code above in comparisom to the yEnc program is out by
either 128 or -128 which corrupts the JPG. Clearly theres some magic number
at play here which I just don't understand. About 5% of my bytes are
suffering from this problem, (all the rest are identical to those produced
by yEnc.exe) and its unrelated to the yencode "escape" character of "=" or
line breaks, as the first instance is only 26 bytes in and is unescaped. I'm
generating my byte array by cutting out the yencoded block and using
System.Text.ASCIIEncoding.ASCII.GetBytes(string).

I know JPGs are BigEndian but frankly, I've no idea if this is related to my
problem at all, or where I should apply UnicodeBigEndian translation, or
whether thats a complete red herring.

Can anyone help ?

Thanks,
Phillip
 
J

Jon Skeet [C# MVP]

I'm generating my byte array by cutting out the yencoded block and
using System.Text.ASCIIEncoding.ASCII.GetBytes(string).

That's almost certainly the problem - one of the defining features (as
far as I can see) of yEnc is that it *doesn't* just use ASCII.

You'll need to get the raw bytes of the message, as returned by the
server, not the decoded text form.
 
P

Phillip Hamlyn

Jon,

Thanks for the reply. I found I was reading from the NNTP NetworkStream and
returing String by using ASCII Encoding, which I thought was correct (cos
NNTP was an ASCII based protocol), but of course then layered over it the
ability to read yEnc - which as you point out is not strictly ASCII. Of
course my earlier uuEncode worked because it accepts the encoding bulk of
being strictly ASCII compatible but yEnc tries to be clever.

I fixed it by treating all NNTP content as a byte array until I needed to
parse it (for "=ybegin" for instance) whereby I converted it on the fly.

Thanks for the pointing me in the right direction.

Phillip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top