Domanda

Consider the following snippet which gets some binary data and writes it to an ostringstream object:

unsigned char* payload;
unsigned long  size;

GetData(&payload, &size);

std::cout << md5(payload, size) << std::endl;

std::ostringstream stream;
stream.write((const char*)payload, size);

std::cout << md5(payload, size) << std::endl;

The problem is that, two printed hash values are different form each other, which means payload has been changed. I tried opening stream in binary mode by using std::ostringstream stream(std::ios::out | std::ios::binary), it did not make a difference, I didn't expect that it would, anyway.

Another fact is, I get a different checksum from the second print statement every time I re-run the program. First hash is always the same.

Now, how can I write my binary data correctly to ostringstream? Can the problem be the cast to const char* (GetData method takes an unsigned char** as the first parameter)?

UPDATE: In the light of comments, here are some more explanations:

  • Comparing the binary diff of the original data and the data written, I saw written data has shifted to the right (24 bytes) in some places. It has also some added bytes in the very beggining. I'm still thinking it has something to do with the cast.
  • There is no more code between GetData and the actual writing.
  • GetData works correctly, since the checksum after calling it is correct (I know what the checksum should be).
  • I cannot post compilable code, because of GetData. And it is not necessary, I have isolated the problem to the line where write is called.
  • System details are: gcc version 4.6.3 on Ubuntu 12.04 64bit
È stato utile?

Soluzione

The mystery of the problem turns out to be the size of the data.

After experimenting with different size values, it was discovered that ostringstream's internal buffer is around 65KB, 65504 bytes to be exact. When the size is bigger, strange shifts and crippled bytes occur.

The workaround is to use:

stream.rdbuf()->pubsetbuf((const char*)payload, payloadSize)

instead of write method. But when this scope terminates, payload will be invalidated and stream cannot be used anywhere else anymore. In my case, it needed to be used somewhere else.

This showed that:

  • I was indeed right that the issue is with ostringstream but not with the hash or anything else.
  • String streams of STL apparently have a default buffer size limit. This is to be remembered in the future.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top