示例#1
0
void KBzip2Filter::terminate()
{
    if ( m_mode == IO_ReadOnly )
    {
        int result = bzDecompressEnd(&d->zStream);
        kdDebug(7118) << "bzDecompressEnd returned " << result << endl;
    } else if ( m_mode == IO_WriteOnly )
    {
        int result = bzCompressEnd(&d->zStream);
        kdDebug(7118) << "bzCompressEnd returned " << result << endl;
    } else
        kdWarning(7118) << "Unsupported mode " << m_mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
}
示例#2
0
bool KBzip2Filter::terminate()
{
    if (d->mode == QIODevice::ReadOnly) {
        const int result = bzDecompressEnd(&d->zStream);
        if (result != BZ_OK) {
            //qDebug() << "bzDecompressEnd returned " << result;
            return false;
        }
    } else if (d->mode == QIODevice::WriteOnly) {
        const int result = bzCompressEnd(&d->zStream);
        if (result != BZ_OK) {
            //qDebug() << "bzCompressEnd returned " << result;
            return false;
        }
    } else {
        //qWarning() << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
        return false;
    }
    d->isInitialized = false;
    return true;
}
char Uncompressor::Uncompress(Input *input,unsigned char *dataptr,unsigned long *len)
   // Decompresses the data from 'input' and stores
   // the result in 'dataptr'. It decompresses at most *len
   // bytes. Afterwards, '*len' is set to the actual number
   // of bytes uncompressed.
   // The function returns 1, if output buffer is full and
   // there is more data to read. Otherwise, the function returns 0.
{
   // We haven't initialized the object yet, we do that now
   if(isinitialized==0)
   {
#ifdef USE_BZIP
      state.bzalloc=zalloc;
      state.bzfree=zfree;
#else
      state.zalloc=zalloc;
      state.zfree=zfree;
#endif

#ifdef USE_BZIP
      if(bzDecompressInit(&state,0,0)!=BZ_OK)
#else
      if(inflateInit(&state)!=Z_OK)
#endif
      {
         Error("Error while compressing container!");
         Exit();
      }

      isinitialized=1;
   }

   int   save_in;

#ifdef USE_BZIP
   state.next_out=(char *)dataptr;
#else
   state.next_out=(unsigned char *)dataptr;
#endif

   // Let's remember how much space we have in the output ...
   state.avail_out=*len;

   // ... and we get the piece of data from the input
   state.avail_in=input->GetCurBlockPtr((char **)&(state.next_in));

   do
   {
      // We save the amount of input data that is available
      // This will be used to compute how much input data was
      // decompressed
      save_in=state.avail_in;

      // The actual decompression
#ifdef USE_BZIP
      switch(bzDecompress(&state))
#else
      switch(inflate(&state,Z_NO_FLUSH))
#endif
      {
         // Did we finish completely?
#ifdef USE_BZIP
      case BZ_STREAM_END:
#else
      case Z_STREAM_END:
#endif
         // We skip over the amount of data that was decompressed
         input->SkipData(save_in-state.avail_in);

         // Let's store the overall amount of "decompressed" data.
         *len=state.total_out;

         // Let's finish the decompression entirely
#ifdef USE_BZIP
         if(bzDecompressEnd(&state)!=BZ_OK)
#else
         if(inflateReset(&state)!=Z_OK)
#endif
         {
            Error("Error while compressing container!");
            Exit();
         }
#ifdef USE_BZIP
         // Only for BZIP, we need to reinitialize
         isinitialized=0;
#endif
         return 0;   // We reached the end

         
#ifdef USE_BZIP
      case BZ_OK:
#else
      case Z_OK:
#endif
         // Did we do the decompression correctly?
         // => Let's go to the next piece of data
         break;

      default:
         // In all other cases, we have an error
         Error("Error while uncompressing container!");
         Exit();
      }

      // Skip the input data that was decompressed
      input->SkipData(save_in-state.avail_in);

      if(state.avail_out==0)  // Output buffer is full
         return 1;

//      if(state.avail_in>0) // Something is wrong !
//         return -1;

      // Let's get the next input data block
      input->RefillAndGetCurBlockPtr((char **)&(state.next_in),(int *)&(state.avail_in));
   }
   while(1);
}