Why this base64 function stop working when increasing max length?

Posted by flyout on Stack Overflow See other posts from Stack Overflow or by flyout
Published on 2010-05-26T03:32:28Z Indexed on 2010/05/26 3:41 UTC
Read the original article Hit count: 290

Filed under:
|
|
|

I am using this class to encode/decode text to base64. It works fine with MAX_LEN up to 512 but if I increase it to 1024 the decode function returns and empty var.

This is the function:

char* Base64::encode(char *src)
{
    char* ptr = dst+0;
    unsigned triad;
    unsigned int d_len = MAX_LEN;

    memset(dst,'\0', MAX_LEN); 

    unsigned s_len = strlen(src);

    for (triad = 0; triad < s_len; triad += 3)
    {
        unsigned long int sr = 0;
        unsigned byte;

        for (byte = 0; (byte<3)&&(triad+byte<s_len); ++byte)
        {
            sr <<= 8;
            sr |= (*(src+triad+byte) & 0xff);
        }

        sr <<= (6-((8*byte)%6))%6;  // shift left to next 6bit alignment

        if (d_len < 4) return NULL;    // error - dest too short 

        *(ptr+0) = *(ptr+1) = *(ptr+2) = *(ptr+3) = '=';
        switch(byte)
        {
            case 3:
                *(ptr+3) = base64[sr&0x3f];
                sr >>= 6;
            case 2:
                *(ptr+2) = base64[sr&0x3f];
                sr >>= 6;
            case 1:
                *(ptr+1) = base64[sr&0x3f];
                sr >>= 6;
                *(ptr+0) = base64[sr&0x3f];
        }
        ptr += 4; 
        d_len -= 4;
    }

    return dst;
}

Why could be causing this?

© Stack Overflow or respective owner

Related posts about c++

Related posts about visual-c++