c++ - ostringstream::write method modifies input parameter -
consider following snippet gets binary data , writes 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 that, 2 printed hash values different form each other, means payload
has been changed. tried opening stream
in binary mode using std::ostringstream stream(std::ios::out | std::ios::binary)
, did not make difference, didn't expect would, anyway.
another fact is, different checksum second print statement every time re-run program. first hash same.
now, how can write binary data correctly ostringstream? can problem cast const char*
(getdata
method takes unsigned char**
first parameter)?
update: in light of comments, here more explanations:
- comparing binary diff of original data , data written, saw written data has shifted right (24 bytes) in places. has added bytes in beggining. i'm still thinking has cast.
- there no more code between
getdata
, actual writing. - getdata works correctly, since checksum after calling correct (i know checksum should be).
- i cannot post compilable code, because of
getdata
. , not necessary, have isolated problem linewrite
called. - system details are: gcc version 4.6.3 on ubuntu 12.04 64bit
the mystery of problem turns out size of data.
after experimenting different size values, discovered ostringstream
's internal buffer around 65kb, 65504 bytes exact. when size bigger, strange shifts , crippled bytes occur.
the workaround use:
stream.rdbuf()->pubsetbuf((const char*)payload, payloadsize)
instead of write
method. when scope terminates, payload invalidated , stream
cannot used anywhere else anymore. in case, needed used somewhere else.
this showed that:
- i indeed right issue
ostringstream
not hash or else. - string streams of stl apparently have default buffer size limit. remembered in future.
Comments
Post a Comment