0

The data between these two functions is becoming garbled. Inspecting the variables on each side show that the data is definitely different. The message size signal does work. It's the second read/write function that has the problem. Here is my write function and its counter read function:

string Pipe::RecvCompressedMessage()
{
int message_size = 0;
DWORD dwBytesRead = 0;
string buf, data;
byte size_in[sizeof(int)];

if (!ReadFile(_h, &message_size, sizeof(int), &dwBytesRead, NULL))
{
    debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));
    Close();
    return "";
}

if (message_size < 0)
    return "";

buf.resize(message_size);

int total_bytes_read = 0;

while(total_bytes_read < message_size)
{
    if (!ReadFile(_h, (LPVOID) &buf, message_size - total_bytes_read, &dwBytesRead, NULL))
    {
        int err = GetLastError();

        debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));
        Close();
        return "";
    }

    data.append(buf);

    total_bytes_read += dwBytesRead;
}

std::string decompressed;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
out.push(boost::iostreams::bzip2_decompressor());
out.push(boost::iostreams::back_inserter(decompressed));
boost::iostreams::copy(boost::make_iterator_range(buf), out);

return decompressed;
}


void Pipe::SendCompressedMessage(string message)
{
std::string compressed;
boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
out.push(boost::iostreams::bzip2_compressor());
out.push(boost::iostreams::back_inserter(compressed));
boost::iostreams::copy(boost::make_iterator_range(message), out);

DWORD dwBytesWritten;
int message_size = compressed.length();

if (WriteFile(_h, &message_size, sizeof(int), &dwBytesWritten, NULL) == 0)
{
    debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));

    Close();
    return;
}

if (WriteFile(_h, (LPVOID *) &compressed, message_size, &dwBytesWritten, NULL) == 0)
{
    debug->DebugMessage(Error::GetErrorMessageW(GetLastError()));

    Close();
    return;
}
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Bluebaron
  • 2,289
  • 2
  • 27
  • 37

2 Answers2

3
string buf, data;
...
if (!ReadFile(_h, (LPVOID) &buf, message_size - total_bytes_read, &dwBytesRead, NULL))

You pass pointer to string object as lpBuffer to ReadFile. Replace string with vector<char>:

vector<char> buf;
...
buf.resize(message_size - total_bytes_read);
if (!ReadFile(_h, &buf[0], message_size - total_bytes_read, &dwBytesRead, NULL))
Alex F
  • 42,307
  • 41
  • 144
  • 212
1

You are writing a std::string instead a char *:

if (WriteFile(_h, (LPVOID *) (compressed.c_str()), message_size, &dwBytesWritten, NULL) == 0)
Tio Pepe
  • 3,071
  • 1
  • 17
  • 22
  • WTF? I think you accepted the wrong answer mate. @Alex Farber was correct. – trojanfoe Oct 25 '11 at 15:00
  • And the fact that you were corrupting the stack with your `ReadFile` didn't work? – trojanfoe Oct 25 '11 at 15:05
  • I don't see where I corrupted the stack. I'm not sure if you're being sarcastic or you grammar errored, but I think you mean that in my example there was some place where I was corrupting the stack. – Bluebaron Oct 25 '11 at 15:12
  • 1
    OK, well you are reading your data over a `string` object (`buf`) which not only won't store the data correctly but will also write over the stackframe of the method and cause the program to crash. You had errors in both your read and write functions but chose the lesser of the two evils as 'the answer'. I guess you can't choose both, but @Alex Farber answered before @Tio Pepe. – trojanfoe Oct 25 '11 at 15:15