public override byte[] Decode(string data)
{
unchecked
{
string dataWithoutPrefixPostfix = data;
if (PrefixPostfix)
{
if (!dataWithoutPrefixPostfix.StartsWith(Prefix) || !dataWithoutPrefixPostfix.EndsWith(Postfix))
{
throw new Exception("ASCII85 encoded data should begin with '" + Prefix + "' and end with '" + Postfix + "'");
}
dataWithoutPrefixPostfix = dataWithoutPrefixPostfix.Substring(Prefix.Length, dataWithoutPrefixPostfix.Length - Prefix.Length - Postfix.Length);
}
MemoryStream ms = new MemoryStream();
int count = 0;
bool processChar = false;
uint tuple = 0;
int encidedBlockLength = 5;
byte[] decodedBlock = new byte[4];
foreach (char c in dataWithoutPrefixPostfix)
{
switch (c)
{
case 'z':
if (count != 0)
{
throw new Exception("The character 'z' is invalid inside an ASCII85 block.");
}
decodedBlock[0] = 0;
decodedBlock[1] = 0;
decodedBlock[2] = 0;
decodedBlock[3] = 0;
ms.Write(decodedBlock, 0, decodedBlock.Length);
processChar = false;
break;
default:
processChar = true;
break;
}
if (processChar)
{
tuple += (uint)InvAlphabet[c] * Pow85[count];
count++;
if (count == encidedBlockLength)
{
DecodeBlock(decodedBlock.Length, decodedBlock, tuple);
ms.Write(decodedBlock, 0, decodedBlock.Length);
tuple = 0;
count = 0;
}
}
}
if (count != 0)
{
if (count == 1)
{
throw new Exception("The last block of ASCII85 data cannot be a single byte.");
}
count--;
tuple += Pow85[count];
DecodeBlock(count, decodedBlock, tuple);
for (int i = 0; i < count; i++)
{
ms.WriteByte(decodedBlock[i]);
}
}
return ms.ToArray();
}
}