Вопрос

I'm trying to get some data to be sent correctly to a external program. The data is base64 encoded into a string and correctly processed if I pass over the entire encoded string in one go. However, as this data can grow to be quite large, I'm attempting to break up the data to send in chunks.

char* encodedBMPData = EncodeImage(hBitmap, info, m_currentImageLength);

int numOfChunks = (*m_currentImageLength / fiveMBByteSize);
long trailingLength = *m_currentImageLength - (numOfChunks * fiveMBByteSize);
if(trailingLength > 0)
    numOfChunks++; //One more chunk for trailing length

char **segmentedImageData = (char**)malloc(numOfChunks*sizeof(char*));

for(int i = 0; i < numOfChunks; i++)
{
    if((i+1) != numOfChunks)
    {
        segmentedImageData[i] = (char*)malloc(fiveMBByteSize * sizeof(char));
        long memorySpace = (i*fiveMBByteSize);
        memcpy(segmentedImageData[i], encodedBMPData + memorySpace, fiveMBByteSize);
    }
    else
    {
        segmentedImageData[i] = (char*)malloc(trailingLength * sizeof(char));
        long memorySpace = (i*fiveMBByteSize);
        memcpy(segmentedImageData[i], encodedBMPData + memorySpace, trailingLength);
    }
}

status = PostMessage(this->m_hMasterMessageWnd, WM_IMAGE_SEGMENTED_READY, (WPARAM)&numOfChunks, (LPARAM)segmentedImageData);
free(encodedBMPData);

As far as why I'm using a string, for those of you curious, is that this is in a ActiveX control and passes data to Javascript. I have tested this for small image sizes to transmit everything in one go (so I'd pass just the encodedBMPData away) and the javascript it gets handed off to, it works correctly. If I have a small image hit this specific code and get transmitted in one chunk, it still breaks on server side when it attempts to decode it.

Appreciate any help y'all can give.

Edit Oh and since I named it horribly, I'll say here that m_currentImageLength is actually the length of the encoded data, not the raw image byte length (by the time it reaches this function).

Это было полезно?

Решение

UPDATE

I got it to work after discovering something odd, there was a extra 4 bytes getting added when our server got a hold of the data. These 4 bytes disappear if you add a null character to the end of each chunk. My best theory for this is javascript does something with it if no null character is added.

For anyone working with ActiveX controls, particularly TWAIN interface....beware of this depending on what you do with image data.

Original Post So I haven't been successful yet, whenever I have more than one chunk of data to send, it fails. I believe this is a different issue though, at this point I don't believe it is a C++ issue. Sorry if I didn't specify C or C++, I didn't really care one way or the other.

This did lead to me cleaning up the code to read a little bit and realizing if a null was added to the end of the only/last chunk, it worked (all credit to my coworker). Hopefully I'll figure out why the null suddenly makes it work. Thanks everyone.

        m_currentImageLength = (int*)malloc(sizeof(int));
        encodedBMPData = EncodeImage(hBitmap, info, m_currentImageLength);

        //Image will need to be broken up into segments
        numOfChunks = (*m_currentImageLength / fiveMBByteSize);
        if ( *m_currentImageLength % fiveMBByteSize != 0)
            numOfChunks++; //One more chunk for trailing length

        encodedCurrentPosition = encodedBMPData;
        bytesRemaining = *m_currentImageLength;
        segmentedImageData = (char**)malloc(numOfChunks*sizeof(char*));

        for(int i = 0; i < numOfChunks; i++)
        {
            if(bytesRemaining > fiveMBByteSize)
            {
                segmentedImageData[i] = (char*)malloc(fiveMBByteSize * sizeof(char));
                memcpy(segmentedImageData[i], encodedCurrentPosition, fiveMBByteSize);
                encodedCurrentPosition += fiveMBByteSize;
                bytesRemaining -= fiveMBByteSize;
            }
            else
            {
                // Add an extra byte to add null character at end of string for last segment
                segmentedImageData[i] = (char*)malloc((bytesRemaining*sizeof(char)) + 1 );
                memcpy(segmentedImageData[i], encodedCurrentPosition, bytesRemaining);
                segmentedImageData[i][bytesRemaining] = '\0';
                break;
            }
        }

        status = PostMessage(this->m_hMasterMessageWnd, WM_IMAGE_SEGMENTED_READY, (WPARAM)&numOfChunks, (LPARAM)segmentedImageData);
        free(encodedBMPData);
        free(m_currentImageLength);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top