2

I'm trying to read a tcp packet containing a http zipped message, but it fails with 'Exception during zlib decompression: ( -3 ) incorrect header check'. What's wrong with my code, or is there a library that does that for me ?

std::string decompress_string(const std::string& str) {
    z_stream zs;                        // z_stream is zlib's control structure
    memset(&zs, 0, sizeof(zs));

    if (inflateInit(&zs) != Z_OK)
        throw(std::runtime_error("inflateInit failed while decompressing."));

    zs.next_in = (Bytef*)str.data();
    zs.avail_in = str.size();

    int ret;
    char outbuffer[32768];
    std::string outstring;

    // get the decompressed bytes blockwise using repeated calls to inflate
    do {
        zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
        zs.avail_out = sizeof(outbuffer);

        ret = inflate(&zs, 0);

        if (outstring.size() < zs.total_out) {
            outstring.append(outbuffer,
                             zs.total_out - outstring.size());
        }

    } while (ret == Z_OK);

    inflateEnd(&zs);

    if (ret != Z_STREAM_END) {          // an error occurred that was not EOF
        qDebug()  << "Exception during zlib decompression: (" << ret << ") " << zs.msg;
        return "";
    }

    return outstring;
}

std::string parseHttp(std::string payload) {
    size_t index = payload.find("\r\n\r\n");
    if (index == std::string::npos) {
        qDebug() << "http body not found, dropped.";
        return "";
    }
    std::string body = payload.substr(index + 4);
    if (payload.find("Content-Encoding: gzip") == std::string::npos){
        return body;
    } else {
        return decompress_string(body);
    }
}
kaylum
  • 13,833
  • 2
  • 22
  • 31
KevinFox
  • 87
  • 5
  • Does the answer here help: [ZLib Inflate() failing with -3 Z_DATA_ERROR](http://stackoverflow.com/questions/18700656/zlib-inflate-failing-with-3-z-data-error) – kicken Jul 03 '16 at 12:59
  • Well maybe, I'm not sure I understand it all, but if I just replace inflate(&zs, 0) by inflateInit2(&zs, -MAX_WBITS), it still doesn't work. – KevinFox Jul 03 '16 at 13:42

1 Answers1

3

It's probably in gzip format. Try using inflateInit2() with wbits set to 31 to decode the gzip format. gzip data starts with 1f 8b 08.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • So basically replace inflate(&zs, 0) by inflateInit2(&zs, -MAX_WBITS) ? As I said in my comment to kicken, that's what I tried to do, and it doesn't work – KevinFox Jul 03 '16 at 13:58
  • 1
    Sigh. My son has the same problem with reading comprehension. I did not say `-MAX_WBITS`. I said `31`. – Mark Adler Jul 03 '16 at 15:01
  • I also tried to replace ret = inflate(&zs, 0); by ret = inflateInit2(&zs, 31);, but it doesn't work either, the program terminates. I also checked that the first bytes of the payload are indeed 1F 8B 08 00 00 00 00 00 00 00 BD. – KevinFox Jul 03 '16 at 15:24
  • 1
    What? No, you replace `inflateInit(&zs)` with `inflateInit2(&zs, 31)`. You do not replace `inflate()` with `inflateInit2()`! – Mark Adler Jul 03 '16 at 15:34