shamsh said:
Is it possible for you to provide me with a standard .cpp
"to64frombits(unsigned char *out, const unsigned char *in, int inlen)" and a
corresponding .net decode.
Since nothing looks wrong except '$' to '+' within current to64frombits.
Within calling function there is a statement after to64frombits() called :
*pbstrResultText = CComBSTR(pszBase64).Detach(); where pszBase64 is the
output from to64frombits.
I'm not sure, but I don't think it will disturb the output from to64frombits.
I wrote some C code back in the 90's.
They should be compatible with the standard .NET methods.
You may want to skip the linlen functionality though.
See code below.
Arne
===================================
struct sixbits { unsigned sb1:6;
unsigned sb2:6;
unsigned sb3:6;
unsigned sb4:6; };
union overlay { struct sixbits sixs;
char eigths[3]; };
char enc_b64vals[64] = { 'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/' };
int enc_deb64(char c)
{
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return c-'0'+52;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return c-'A';
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
return c-'a'+26;
case '+':
return 62;
case '/':
return 63;
case '=':
return 0;
default:
return -1;
}
return -1;
}
void NormalToB64(char *s1,int l1,char *s2,int *l2)
{
union overlay Cvt;
char *p = s1;
int ltmp = (l1*8+5)/6;
int extra = 3-(ltmp+3)%4;
int linlen = 0;
(*l2) = 0;
// loop through string
for(int i=0;i<ltmp;i=i+4) {
// convert 8->6 bit
Cvt.eigths[2]=(((p-s1)<l1)?(*p):0);
p++;
Cvt.eigths[1]=(((p-s1)<l1)?(*p):0);
p++;
Cvt.eigths[0]=(((p-s1)<l1)?(*p):0);
p++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb4];
(*l2)++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb3];
(*l2)++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb2];
(*l2)++;
s2[*l2]=enc_b64vals[Cvt.sixs.sb1];
(*l2)++;
// append newline if line too long
linlen=linlen+4;
if(linlen>75) {
s2[*l2]='\n';
(*l2)++;
linlen=0;
}
}
// append 0,1 or 2 "="
(*l2)=(*l2)-extra;
for(int j=0;j<extra;j++) {
s2[*l2]='=';
(*l2)++;
}
// append newline
s2[*l2]='\n';
(*l2)++;
return;
}
void B64ToNormal(char *s1,int l1,char *s2,int *l2)
{
union overlay Cvt;
char *p = s1;
int skip = 0;
(*l2) = 0;
// loop through string
while(p<(s1+l1)) {
// convert 6->8 bit
Cvt.sixs.sb4=enc_deb64(*p);
p++;
if((*p)=='=') skip++;
Cvt.sixs.sb3=enc_deb64(*p);
p++;
if((*p)=='=') skip++;
Cvt.sixs.sb2=enc_deb64(*p);
p++;
if((*p)=='=') skip++;
Cvt.sixs.sb1=enc_deb64(*p);
p++;
s2[*l2]=Cvt.eigths[2];
(*l2)++;
s2[*l2]=Cvt.eigths[1];
(*l2)++;
s2[*l2]=Cvt.eigths[0];
(*l2)++;
(*l2)=(*l2)-skip;
// skip newline
if((*p)=='\n') p++;
}
return;
}