2016-07-03 2 views
2

http 압축 메시지가 포함 된 tcp 패킷을 읽으려고하지만 'zlib 압축 해제 중 예외 : (-3) 잘못된 헤더 검사'와 함께 실패합니다. 내 코드가 잘못 되었나요? 아니면 저를 위해 라이브러리가 있습니까?http를 압축 해제하는 방법은 무엇입니까?

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); 
    } 
} 
+0

[ZLib Inflate()가 -3 Z_DATA_ERROR로 실패 함] (http://stackoverflow.com/questions/18700656/zlib-inflate-failing-with-3-z-data-error) – kicken

+0

아마도 모든 것을 이해할 수 있을지 모르겠지만 inflateInit2 (& zs, -MAX_WBITS)로 inflate (& zs, 0)를 바꾸면 여전히 작동하지 않습니다. – KevinFox

답변

2

아마도 gzip 형식입니다. ghzip 형식을 디코딩하려면 과 wbits31으로 설정해보십시오. gzip 데이터는 1f 8b 08으로 시작합니다.

+0

그래서 기본적으로 inflateInit2 (& zs, -MAX_WBITS)로 inflate (& zs, 0)를 대체 하시겠습니까? 내가 kicken에 대한 나의 코멘트에서 말했듯이, 내가하려고했던 것이고 그것은 작동하지 않는다. – KevinFox

+1

한숨. 아들은 독해력 문제도 있습니다. 나는'-MAX_WBITS'라고 말하지 않았다. 나는 31라고 말했다. –

+0

나는 또한 ret = inflate (& zs, 0)를 대체하려고 시도했다. ret = inflateInit2 (& zs, 31);로 작동하지만 작동하지 않으면 프로그램이 종료됩니다. 또한 페이로드의 첫 번째 바이트가 실제로 1F 8B 08 00 00 00 00 00 00 00 BD인지 확인했습니다. – KevinFox

관련 문제