I've picked up some old code that I need to work with and I have the following function in C
int pem64_decode_bytes(const char *intext,
int nchars,
unsigned char *outbytes)
{
unsigned char *outbcopy = outbytes;
while (nchars >= 4) {
char
c1 = intext[0],
c2 = intext[1],
c3 = intext[2],
c4 = intext[3];
int
b1 = pem64_dict_offset(c1),
b2 = pem64_dict_offset(c2),
b3 = pem64_dict_offset(c3),
b4 = pem64_dict_offset(c4);
if ((b1 == -1) || (b2 == -1) || (b3 == -1) || (b4 == -1))
return outbytes - outbcopy;
*(outbytes++) = (b1 << 2) | (b2 >> 4);
if (c3 != FILLERCHAR)
*(outbytes++) = ((b2 & 0xf) << 4) | (b3 >> 2);
if (c4 != FILLERCHAR)
*(outbytes++) = ((b3 & 0x3) << 6) | b4;
nchars -= 4;
intext += 4;
}
return outbytes - outbcopy;
}
It should decode a packet of data that has been encoded. Does anyone know if this is a standard function? I need to convert this to C#, I'm not a C coder does anyone know of any samples that do this?
Edit
=======
public static List<byte> pem64_decode_bytes(string InText, int NumberOfBytes)
{
var RetData = new List<byte>();
while (NumberOfBytes >= 4)
{
char c1 = InText[0];
char c2 = InText[1];
char c3 = InText[2];
char c4 = InText[3];
int b1 = pem64_dict_offset(c1);
int b2 = pem64_dict_offset(c2);
int b3 = pem64_dict_offset(c3);
int b4 = pem64_dict_offset(c4);
if (b1 == -1 || b2 == -1 || b3 == -1 || b4 == -1)
{
return RetData;
}
(outbytes)++.Deref = b1 << 2 | b2 >> 4;
if (c3 != FILLERCHAR)
{
(outbytes)++.Deref = (b2 & 0xf) << 4 | b3 >> 2;
}
if (c4 != FILLERCHAR)
{
(outbytes)++.Deref = (b3 & 0x3) << 6 | b4;
}
NumberOfBytes -= 4;
}
return RetData;
}