Last active
June 7, 2017 20:14
-
-
Save alexforster/3b41ea70ac6fc98bc8c1b5afc093d4af to your computer and use it in GitHub Desktop.
C# simple uudecode implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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