private static byte[] DecodeBase64(string encodedstring, int maximumBytes)
{
int position = 0,
sourceLength = encodedstring.Length,
outputLength = 0;
if (maximumBytes <= 0)
throw new ArgumentException("Invalid maximum bytes value", "maximumBytes");
// TODO: update to use a List<byte> - it's only ever 16 bytes, so it's not a big deal
StringBuilder rs = new StringBuilder();
while (position < sourceLength - 1 && outputLength < maximumBytes)
{
int c1 = Char64(encodedstring[position++]);
int c2 = Char64(encodedstring[position++]);
if (c1 == -1 || c2 == -1)
break;
rs.Append((char)((c1 << 2) | ((c2 & 0x30) >> 4)));
if (++outputLength >= maximumBytes || position >= sourceLength)
break;
int c3 = Char64(encodedstring[position++]);
if (c3 == -1)
break;
rs.Append((char)(((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2)));
if (++outputLength >= maximumBytes || position >= sourceLength)
break;
int c4 = Char64(encodedstring[position++]);
rs.Append((char)(((c3 & 0x03) << 6) | c4));
++outputLength;
}
byte[] ret = new byte[outputLength];
for (position = 0; position < outputLength; position++)
ret[position] = (byte)rs[position];
return ret;
}