Skip to content

Instantly share code, notes, and snippets.

@alexforster
Last active June 7, 2017 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexforster/3b41ea70ac6fc98bc8c1b5afc093d4af to your computer and use it in GitHub Desktop.
Save alexforster/3b41ea70ac6fc98bc8c1b5afc093d4af to your computer and use it in GitHub Desktop.
C# simple uudecode implementation
public static Byte[] UUDecode(StreamReader input)
{
var result = new MemoryStream();
for(String ascii; (ascii = input.ReadLine()) != null;)
{
var encodedBuffer = Encoding.ASCII.GetBytes(ascii);
if(encodedBuffer[0] == 0x60) break;
var nrDecoded = encodedBuffer[0] - 32;
var decoded = new Byte[nrDecoded];
for(Int32 i = 1, j = 0; ; i += 4)
{
var tmp = encodedBuffer.Skip(i).Take(4).Select(b => (Byte)((b - 0x20) & 0x3F)).ToArray();
decoded[j++] = (Byte)(tmp[0] << 2 | tmp[1] >> 4);
if(j == nrDecoded) break;
decoded[j++] = (Byte)(tmp[1] << 4 | tmp[2] >> 2);
if(j == nrDecoded) break;
decoded[j++] = (Byte)(tmp[2] << 6 | tmp[3]);
if(j == nrDecoded) break;
}
result.Write(decoded, 0, nrDecoded);
}
return result.ToArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment