Exemple #1
0
KBzip2Filter::Result KBzip2Filter::compress(bool finish)
{
    //qDebug() << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
    int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN);

    switch (result) {
    case BZ_OK:
    case BZ_FLUSH_OK:
    case BZ_RUN_OK:
    case BZ_FINISH_OK:
        return KFilterBase::Ok;
        break;
    case BZ_STREAM_END:
        //qDebug() << "  bzCompress returned " << result;
        return KFilterBase::End;
        break;
    default:
        //qDebug() << "  bzCompress returned " << result;
        return KFilterBase::Error;
        break;
    }
}
Exemple #2
0
KBzip2Filter::Result KBzip2Filter::compress( bool finish )
{
    //kdDebug(7118) << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable() << endl;
    int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN );

    switch (result) {
        case BZ_OK:
        case BZ_FLUSH_OK:
        case BZ_RUN_OK:
        case BZ_FINISH_OK:
                return OK;
                break;
        case BZ_STREAM_END:
                kdDebug(7118) << "  bzCompress returned " << result << endl;
                return END;
		break;
        default:
                kdDebug(7118) << "  bzCompress returned " << result << endl;
                return ERROR;
                break;
    }
}
void Compressor::FinishCompress(unsigned long *uncompressedsize,unsigned long *compressedsize)
   // Finishes the compression and stores the input data size and
   // the output data size in 'uncompressedsize' and 'compressedsize'
{
   char err;
   int   saveavail;

   do
   {
      // Let's get more space in the output buffer
#ifdef USE_BZIP
      state.next_out=output->GetBufPtr((int *)&state.avail_out);
#else
      state.next_out=(unsigned char *)output->GetBufPtr((int *)&state.avail_out);
#endif

      saveavail=state.avail_out;

#ifdef USE_BZIP
      err=bzCompress(&state,BZ_FINISH);
#else
      err=deflate(&state,Z_FINISH);
#endif

      output->SaveBytes(saveavail-state.avail_out);

#ifdef USE_BZIP
      if(err==BZ_STREAM_END)
         break;
      if(err!=BZ_FINISH_OK)
#else
      if(err==Z_STREAM_END)
         break;
      if(err!=Z_OK)
#endif
      {
         Error("Error while compressing container!");
         Exit();
      }

      // Z_OK means that the output buffer is full ! ==> We flush
      output->Flush();
   }
   while(1);

   // Let's store the input and output size
   if(uncompressedsize!=NULL) *uncompressedsize =state.total_in;
   if(compressedsize!=NULL)   *compressedsize   =state.total_out;

   state.total_out=0;
   state.total_in=0;

   // Finally, we release the internal memory
#ifdef USE_BZIP
   if(bzCompressEnd(&state)!=BZ_OK)
#else
   if(deflateReset(&state)!=Z_OK)
#endif
   {  
      Error("Error while compressing container!");
      Exit();
   }
#ifdef USE_BZIP
   // bzip must be reinitialized
   isinitialized=0;
#endif
}
void Compressor::CompressData(unsigned char *ptr,unsigned len)
   // Compresses the data at position 'ptr' of length 'len'
{
   int            saveavail;

   // Let's get some space in the output buffer
#ifdef USE_BZIP
   state.next_out=output->GetBufPtr((int *)&state.avail_out);
   state.next_in=(char *)ptr;
#else
   state.next_out=(unsigned char *)output->GetBufPtr((int *)&state.avail_out);
   state.next_in=ptr;
#endif

   state.avail_in=len;

   // If we haven't initialized the compressor yet, then let's do it now
   if(isinitialized==0)
   {
#ifdef USE_BZIP
      if(bzCompressInit(&state,7,0,0)!=BZ_OK)
#else
      if(deflateInit(&state,zlib_compressidx)!=Z_OK)
#endif
      {
         Error("Error while compressing container!");
         Exit();
      }
      state.total_out=0;
      state.total_in=0;
      isinitialized=1;
   }

   do
   {
      saveavail=state.avail_out;

      // The actual compression
#ifdef USE_BZIP
      if(bzCompress(&state,BZ_RUN)!=BZ_RUN_OK)
#else
      if(deflate(&state,Z_NO_FLUSH)!=Z_OK)
#endif
      {
         Error("Error while compressing container!");
         Exit();
      }
      output->SaveBytes(saveavail-state.avail_out);
         // We tell the output stream that 'saveavail-state.avail_out' bytes
         // are now ready for output

      if((state.avail_in==0)&&(state.avail_out>0))
         // Is the input buffer is empty ? ==> We go to next block
         break;

      // If the output buffer is full, we flush
      output->Flush();

      // We get the next piece of output buffer that will be filled up
#ifdef USE_BZIP
      state.next_out=output->GetBufPtr((int *)&state.avail_out);
#else
      state.next_out=(unsigned char *)output->GetBufPtr((int *)&state.avail_out);
#endif
   }
   while(1);
}
void Compressor::CompressMemStream(MemStreamer *memstream)
   // Reads the data from 'memstream' and sends
   // it to the compressor
{
   MemStreamBlock *curblock=memstream->GetFirstBlock();
   char           first=1;
   int            saveavail;

   // Any data there? 
   if(memstream->GetSize()==0)
      return;

#ifdef USE_BZIP
   state.next_out=(char *)output->GetBufPtr((int *)&state.avail_out);
   state.next_in=(char *)curblock->data;
#else
   state.next_out=(unsigned char *)output->GetBufPtr((int *)&state.avail_out);
   state.next_in=(unsigned char *)curblock->data;
#endif

   // If there is no space in the output buffer, we need to flush
   if(state.avail_out==0)
   {
      // If the output buffer is full, we flush
      output->Flush();

      // We get the next piece of output buffer that will be filled up
#ifdef USE_BZIP
      state.next_out=(char *)output->GetBufPtr((int *)&state.avail_out);
#else
      state.next_out=(unsigned char *)output->GetBufPtr((int *)&state.avail_out);
#endif
   }

//   state.avail_in=curblock->cursize;

   // If we haven't initialized yet, we do that now
   if(isinitialized==0)
   {
#ifdef USE_BZIP
      if(bzCompressInit(&state,7,0,0)!=BZ_OK)
#else
      if(deflateInit(&state,zlib_compressidx)!=Z_OK)
#endif
      {
         Error("Error while compressing container!");
         Exit();
      }
      state.total_out=0;
      state.total_in=0;

      isinitialized=1;
   }

   do
   {
      // Let's the input block to 'curblock'
#ifdef USE_BZIP
      state.next_in=(char *)curblock->data;
#else
      state.next_in=(unsigned char *)curblock->data;
#endif
      state.avail_in=curblock->cursize;

      // As long as we still have data in the input block, we continue
      while(state.avail_in>0)
      {
         saveavail=state.avail_out;

#ifdef USE_BZIP
         if(bzCompress(&state,BZ_RUN)!=BZ_RUN_OK)
#else
         if(deflate(&state,Z_NO_FLUSH)!=Z_OK)
#endif
         {
            Error("Error while compressing container!");
            Exit();
         }

         // We tell the output stream that 'saveavail-state.avail_out' bytes
         // are now ready for output
         output->SaveBytes(saveavail-state.avail_out);

         if((state.avail_in==0)&&(state.avail_out>0))
            // Is the input buffer is empty ? ==> We go to next block
            break;

         // If the output buffer is full, we flush
         output->Flush();

         // We get the next piece of output buffer that will be filled up
#ifdef USE_BZIP
         state.next_out=(char *)output->GetBufPtr((int *)&state.avail_out);
#else
         state.next_out=(unsigned char *)output->GetBufPtr((int *)&state.avail_out);
#endif
      }

      // There is no more input data available ==> We go to next block in MemStreamer
      curblock=curblock->next;
   }
   while(curblock!=NULL);
}