kyluck
kyluck

Reputation: 13

Access violation when calling IMG_Load_RW() over a binary stringstream?

I have created some encrypted jpg images by reading them byte by byte, adding a value 3 to each byte and sending the manipulated bytes back to a filestream. The program can also decrypt them to their original file by doing the reverse operation (subtracting the number 3) and it all works fine.

Now I want to read an encrypted jpg image into a binary stringstream, decrypt it and then create an SDL Surface directly from that jpg stringstream memory data.

This is the function I wrote:

bool b_get_SDLSurface_from_stream(string str_path, SDL_Surface** sdls_image)
{
    ifstream ifstr_i;
    stringstream ss_image_data(stringstream::out | stringstream::binary);
    unsigned char  uint8_b;

    ifstr_i.open(str_path.c_str(), std::ios_base::binary);
    if (!ifstr_i.is_open())
    {
        return false;
    }

    while (ifstr_i >> noskipws >> uint8_b)
    {
        uint8_b = uint8_b - 3;                                                                
        ss_image_data << uint8_b;
    }

    if (ss_do.rdbuf()->in_avail() == 0)
    {
        return false;
    }

    int n_size = ss_image_data.str().size();
    SDL_RWops* p_sdlrwops_image = SDL_RWFromMem(&ss_image_data, n_size);

    if (p_sdlrwops_i == NULL)
    {
        return false;
    }

    *sdls_image = IMG_Load_RW(p_sdlrwops_image, 0);
    if (*sdls_image == NULL)
    {
        return false;
    }

    ifstr_i.close();
    return true;
}

...but I get an error saying there is an access violation at the point where the following line is called:

*sdls_image = IMG_Load_RW(p_sdlrwops_image, 0);

Upvotes: 0

Views: 61

Answers (0)

Related Questions