C++ 使用boost库解压缩zlib

C++ 使用boost库解压缩zlib,c++,boost,zlib,C++,Boost,Zlib,我正在使用XMPP协议,它是zlib压缩的,我想知道如何使用boost库解压缩(和压缩)它的zlib数据。zlib数据以“0000ff FF”结尾,我在谷歌上搜索了一下,找到了一些关于sync/flush的关键字。boost的zlib.hpp中有一行: BOOST_IOSTREAMS_DECL extern const int sync_flush; 但是我找不到任何关于使用它的文件 从第三方程序捕获的示例数据包: 我猜数据包38和39,总共275个字节包含所有zlib数据,因为数据包41是

我正在使用XMPP协议,它是zlib压缩的,我想知道如何使用boost库解压缩(和压缩)它的zlib数据。zlib数据以“0000ff FF”结尾,我在谷歌上搜索了一下,找到了一些关于sync/flush的关键字。boost的zlib.hpp中有一行:

BOOST_IOSTREAMS_DECL extern const int sync_flush;
但是我找不到任何关于使用它的文件

从第三方程序捕获的示例数据包:

我猜数据包38和39,总共275个字节包含所有zlib数据,因为数据包41是服务器响应,对吗

这是我用来解压上述数据包的代码,但它崩溃了

#include <iostream>
#include <string>
using namespace std;

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/iostreams/filter/zlib.hpp>

std::string zlib_decompress(const std::string& data) {
    using namespace boost;
    using namespace boost::iostreams;

    boost::iostreams::filtering_streambuf<boost::iostreams::input>in;
    in.push(boost::iostreams::zlib_decompressor());
    in.push(boost::make_iterator_range(data));
    std::string decompressed;
    boost::iostreams::copy(in, boost::iostreams::back_inserter(decompressed));
    return decompressed;
}

int main() {
    // the 275 bytes data 
    string s("\x78\x9c\x2c\x8b\x41\x0a\xc0\x20\x0c\xc0\xbe\x22\x7d\x80\x6e\x57\x99\xfb\x8b\x4a\x99\x0e\xb5\xa3\x2d\xc3\xe7\xef\xe0\x4e\x81\x90\x1c\xa2\x8c\xb1\xfb\x05\xa3\x14\x00\xa3\x60\xa7\x64\x33\x75\x30\xb3\xb7\x21\x01\xee\x98\x12\xb2\xcf\xad\xe2\xd0\xdf\xfe\x4f\x80\xa2\xfa\x78\xe7\x50\x0b\xf2\xb4\x2b\xb5\xc4\x97\x5b\x81\x80\x79\x91\xa5\xd2\x08\xb0\xdb\x0d\xce\x0f\x00\x00\xff\xff\x3c\x8f\xc9\x0a\xc2\x30\x18\x84\x5f\x45\xbc\x8b\x5d\x14\xa4\x54\x21\xa5\x41\x03\xfe\xa9\x95\x88\xe8\x2d\x8d\x95\x9a\x2e\x84\x2e\x36\xcd\xd3\x9b\x5e\x3c\x7c\x0c\x0c\xcc\x0c\x13\xf2\xa1\x2f\x16\x75\x2e\x0a\xde\x7c\x3a\x5b\x70\x39\x23\x42\xff\x5b\x43\xdb\x04\x9f\xbc\x7f\x07\x8a\xb7\x36\x1d\x58\x3b\x68\x66\x51\x6a\xd5\xf1\xae\x5a\x1e\x84\xaf\xfa\xcc\x73\x0b\xe2\x47\x9d\x38\xa9\x37\x48\xa4\x29\xc3\x1a\x64\x39\x81\x21\xda\x32\x52\xf9\x74\x5e\x06\x0c\x8d\x1f\x4e\x82\x22\x93\xd7\xee\x37\xbb\x63\x29\x8e\x7a\xcc\xab\xdd\x04\x31\x76\x81\xe1\x29\x61\xc4\x80\xc4\x16\xe4\xd1\xe6\xea\x83\x01\x87\xca\x74\x83\x62\x32\x02\xbb\x69\x60\x64\x0b\x12\x26\x60\x33\xc2\x7d\x9d\x84\x6d\x4d\x3d\x1a\x97\xfb\x70\x3d\x3f\x39\xfc\x00\x00\x00\xff\xff", 275);
    cout << zlib_decompress(s) << endl; // it crashes
}
#包括
#包括
使用名称空间std;
#包括
#包括
#包括
#包括
std::string zlib_解压(const std::string&data){
使用名称空间boost;
使用名称空间boost::iostreams;
boost::iostreams::filtering_streambufin;
in.push(boost::iostreams::zlib_decompressor());
in.push(boost::make_iterator_range(data));
std::字符串解压缩;
boost::iostreams::copy(in,boost::iostreams::back_inserter(解压缩));
减压返回;
}
int main(){
//275字节的数据
串("\XX8\X8\X8 8 8 \X8 8 8 8 \X8 8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x43\xdb\x04\x9f\xbc\x7f\x07\x8a\xb7\x36\x1d\x58\x3b\x68\x66\x51\x6a\xd5\xf1\xae\x4\X7 7\x8 8 8 \X8 8 8 8 \X8 8 8 \X8 8 8 \X8 8 8 \X8 8 8 8 \X9 7 7 7 \x8 8 8 8 8 \X8 8 8 8 8 8 8 8 8 8 \X8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 4 \X8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 \ \ \ \ \ \ \ \ \X8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8\x83\x01\x87\xca\x74\x83\x62\x32\x02\xbb\x69\x60\x64\x0b\x12\x26\x60\x33\xc2\x7d\x9d\x84\x6d\x4d\x3d\x1a\x97\xfb\x70\x3d\x3f\x39\xfc\x00\x00\x00\xff\xff“,275);

难道这意味着你得到了一个部分zlib流。你没有说会发生什么,你试图解压缩它。@MarkAdler。代码崩溃了,我试图调试代码,但未能理解boost zlib的代码。我用更多信息更新了帖子。这意味着你得到了一个部分zlib流。你没有说你尝试会发生什么解压。@MarkAdler。代码崩溃了,我试图调试代码,但未能理解boost zlib的代码。我已经用更多信息更新了文章。