Example #1
0
bool KBzip2Filter::init(int mode)
{
    if (d->isInitialized) {
        terminate();
    }

    d->zStream.next_in = 0;
    d->zStream.avail_in = 0;
    if (mode == QIODevice::ReadOnly) {
        const int result = bzDecompressInit(&d->zStream, 0, 0);
        if (result != BZ_OK) {
            //qDebug() << "bzDecompressInit returned " << result;
            return false;
        }
    } else if (mode == QIODevice::WriteOnly) {
        const int result = bzCompressInit(&d->zStream, 5, 0, 0);
        if (result != BZ_OK) {
            //qDebug() << "bzDecompressInit returned " << result;
            return false;
        }
    } else {
        //qWarning() << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
        return false;
    }
    d->mode = mode;
    d->isInitialized = true;
    return true;
}
Example #2
0
void KBzip2Filter::init( int mode )
{
    d->zStream.next_in = 0;
    d->zStream.avail_in = 0;
    if ( mode == IO_ReadOnly )
    {
        (void)bzDecompressInit(&d->zStream, 0, 0);
        //kdDebug(7118) << "bzDecompressInit returned " << result << endl;
        // No idea what to do with result :)
    } else if ( mode == IO_WriteOnly ) {
        (void)bzCompressInit(&d->zStream, 5, 0, 0);
        //kdDebug(7118) << "bzDecompressInit returned " << result << endl;
    } else
        kdWarning(7118) << "Unsupported mode " << mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
    m_mode = mode;
}
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);
}