void OCT_tobase64()

in src/oct.c [237:278]


void OCT_tobase64(char *b,const octet *w)
{
    int i;
    int j;
    int k;
    int rem;
    int last;
    int c;
    int ch[4];
    unsigned char ptr[3];
    rem=w->len%3;
    j=k=0;
    last=4;
    while (j<w->len)
    {
        for (i=0; i<3; i++)
        {
            if (j<w->len) ptr[i]=w->val[j++];
            else
            {
                ptr[i]=0;
                last--;
            }
        }
        ch[0]=(ptr[0]>>2)&0x3f;
        ch[1]=((ptr[0]<<4)|(ptr[1]>>4))&0x3f;
        ch[2]=((ptr[1]<<2)|(ptr[2]>>6))&0x3f;
        ch[3]=ptr[2]&0x3f;
        for (i=0; i<last; i++)
        {
            c=ch[i];
            if (c<26) c+=65;
            if (c>=26 && c<52) c+=71;
            if (c>=52 && c<62) c-=4;
            if (c==62) c='+';
            if (c==63) c='/';
            b[k++]=(char)c;
        }
    }
    if (rem>0) for (i=rem; i<3; i++) b[k++]='=';
    b[k]='\0';  /* dangerous! */
}