0

bzip2 1.0.6

任务:压缩网络数据包。问题:bzip2“BZ_FLUSH”不工作,只有“BZ_FINISH”工作,但我需要刷新当前数据并为每个连接保留流,而不是为每个数据包单独流。

此代码中有#if 预处理器标记:“#if 0”(现在是)使 bzip2 完成 - 它正在工作;"#if 1" 使 bzip2 刷新 - 不工作(永远卡在读取中)。

#include <bzlib.h>
#include <memory.h>
#include <assert.h>
#include <iostream>
using namespace std;

int main()
{
    typedef unsigned char U1;
    U1 temp[10000];
    size_t temp_count;

    U1 bbb[256];
    for ( size_t i=0; i<sizeof(bbb); ++i )
        bbb[i] = i;

    {
        bz_stream s;
        memset(&s,0,sizeof(s));
        BZ2_bzCompressInit(&s,1,4,0);
        s.next_in = (char*)&bbb[0];
        s.avail_in = sizeof(bbb);
        s.next_out = (char*)&temp[0];
        s.avail_out = sizeof(temp);
        do
        {
            int res = BZ2_bzCompress(&s,BZ_RUN);
            assert( res == BZ_RUN_OK );
            cout << "s.avail_in=" << s.avail_in << endl;
        }
        while ( s.avail_in );
#if 0
        do
        {
            int res = BZ2_bzCompress(&s,BZ_FLUSH);
            cout << "res=" << res << endl;
            cout << "s.avail_in=" << s.avail_in << endl;
            if ( res == BZ_RUN_OK )
                break;
            assert( res == BZ_FLUSH_OK );
        }
        while ( true );
#else
        do
        {
            int res = BZ2_bzCompress(&s,BZ_FINISH);
            cout << "res=" << res << endl;
            cout << "s.avail_in=" << s.avail_in << endl;
            if ( res == BZ_STREAM_END )
                break;
            assert( res == BZ_FINISH_OK );
        }
        while ( true );
#endif
        temp_count = sizeof(temp) - s.avail_out;
        BZ2_bzCompressEnd(&s);
        cout << "temp_count=" << temp_count << endl;
    }

    memset( &bbb[0], 0, sizeof(bbb) );

    {
        bz_stream s;
        memset(&s,0,sizeof(s));
        BZ2_bzDecompressInit(&s,4,0);
        s.next_in = (char*)&temp[0];
        s.avail_in = temp_count;
        s.next_out = (char*)&bbb[0];
        s.avail_out = sizeof(bbb);
        do
        {
            cout << "s.avail_in=" << s.avail_in << endl;
            int res = BZ2_bzDecompress(&s);
            cout << "res=" << res << endl;
            cout << "s.avail_out=" << s.avail_out << endl;
            if( res == BZ_OK )
                continue;
            assert( res == BZ_STREAM_END );
            assert( !s.avail_in );
            break;
        }
        while ( true );
        BZ2_bzDecompressEnd(&s);
    }

    for ( size_t i=0; i<sizeof(bbb); ++i )
        assert( bbb[i] == i );

    return 0;
}
4

0 回答 0