Search code examples
c++base64bit-manipulation

How to read N bytes from a file continuously untill the EOF


I am trying a wave to base 64 converter program.

I am trying this following code snippet:

    vector<char> in(3);
    std::string out = "abcd";         //four letter garbage value as initializer
    ifstream file_ptr(filename.c_str(), ios::in | ios::binary);

    unsigned int threebytes = 0;
    //Apply the Base 64 encoding algorithm
    do {
        threebytes = (unsigned int) file_ptr.rdbuf()->sgetn(&in[0], 3);
        if (threebytes > 0) {
            EncodeBlock(in, out, (int)threebytes);  //Apply conversion algorithm to convert 3 bytes into 4
            outbuff = outbuff + out;                //Append the 4 bytes got from above step to the output
        }
    } while (threebytes == in.size());

    file_ptr.close();

In encode block where the Base64 encoding algorithm is written

void EncodeBlock(const std::vector<char>& in, std::string& out, int len) {
    using namespace std;
    cb64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    out[0] = cb64[(int) (in[0] >> 2)];
    out[1] = cb64[(int) (((in[0] << 6) >> 2) | (in[1] >> 4))];
    out[2] = (len > 1) ?
             cb64[(int) (((in[1] << 4) >> 2) | (in[2] >> 6))] :
             '=';
    out[3] = (len > 2) ?
             cb64[(int) ((in[2] << 2) >> 2)] :
             '=';

}

The cb64 is a 64 length long string but the index generated by bit manipulation sometimes fall out of range (0 to 63).

Why!!!


Solution

  • The resolution to this was to handle the bit manipulation correctly.

    the char 8 bits are operated and then casted to unsigned int introduces 24 bits extra into it which needed to be set to 0.

    So, out[0] = cb64[(unsigned int) ((in[0] >> 2) & 0x003f)];
    out[1] = cb64[(unsigned int) ((((in[0] << 6) >> 2) | (in[1] >> 4))) & 0x003f)]; .. and so on handles the masking