// =====================================================================
//  $Id: TDataInflation.cc,v 1.5 2004/03/07 16:56:04 goiwai Exp $
//  $Name: CLDAQ-1-14-03 $
//  $Log: TDataInflation.cc,v $
//  Revision 1.5  2004/03/07 16:56:04  goiwai
//  *** empty log message ***
//
//  Revision 1.4  2004/03/07 10:30:34  goiwai
//  ROOTに組みこむためのおまじないマクロを埋めこみました。
//  全てにおいて完全に動作するわけではありません。
//
//  Revision 1.3  2003/10/06 16:38:26  goiwai
//  *** empty log message ***
//
//  Revision 1.2  2003/07/30 16:21:20  goiwai
//  ファイルにコミットログをつけることにしました.
//
// =====================================================================
#ifdef __CLDAQ_ZLIB_USE
#include "TDataInflation.hh"

TDataInflation::TDataInflation( Tint bufsize )
  : theInflationStream(), 
    theStatus( Z_OK ),
    theOutputBuffer( 0 ), theSizeOfOutputBuffer( bufsize ),
    theInflatedData( 0 ), theSizeOfInflatedData( -1 )
{
  theInflationStream.zalloc = Z_NULL;
  theInflationStream.zfree = Z_NULL;
  theInflationStream.opaque = Z_NULL;

  theInflationStream.next_in = Z_NULL;
  theInflationStream.avail_in = 0;
  theInflationStream.next_out = Z_NULL;
  theInflationStream.avail_out = 0;

  theStatus = inflateInit( &theInflationStream );
  if ( theStatus != Z_OK ) {
    Tcerr << "TDataInflation::TDataInflation: ";
    Tcerr << theInflationStream.msg << ", status: " << theStatus << Tendl;
  }

  SetOutputBuffer( theSizeOfOutputBuffer );
}

TDataInflation::TDataInflation( const TDataInflation& right )
  : theInflationStream(), 
    theStatus( Z_OK ),
    theOutputBuffer( 0 ), theSizeOfOutputBuffer( right.theSizeOfOutputBuffer ),
    theInflatedData( 0 ), theSizeOfInflatedData( -1 )
{
  theInflationStream.zalloc = Z_NULL;
  theInflationStream.zfree = Z_NULL;
  theInflationStream.opaque = Z_NULL;

  theInflationStream.next_in = Z_NULL;
  theInflationStream.avail_in = 0;
  theInflationStream.next_out = Z_NULL;
  theInflationStream.avail_out = 0;

  theStatus = inflateInit( &theInflationStream );
  if ( theStatus != Z_OK ) {
    Tcerr << "TDataInflation::TDataInflation: ";
    Tcerr << theInflationStream.msg << ", status: " << theStatus << Tendl;
  }

  SetOutputBuffer( theSizeOfOutputBuffer );  
}

TDataInflation::~TDataInflation()
{
  theStatus = inflateEnd( &theInflationStream );
  if ( theStatus != Z_OK ) {
    Tcerr << "TDataInflation::~TDataInflation: ";
    Tcerr << theInflationStream.msg << ", status: " << theStatus << Tendl;
  }
  delete [] theOutputBuffer;
}
const TDataInflation& TDataInflation::operator=( const TDataInflation& right )
{
  theInflationStream = right.theInflationStream;
  theStatus = right.theStatus;
  theSizeOfOutputBuffer = right.theSizeOfOutputBuffer;
  delete [] (Tbyte*)theOutputBuffer;
  theOutputBuffer = new Tbyte[ theSizeOfOutputBuffer ];
  theInflatedData = 0;
  theSizeOfInflatedData = -1;
  return *this;
}

Tvoid TDataInflation::Decompress( Tvoid* input, Tint nbyte )
{
  static const Tstring head = "TDataInflation::Decompress: ";

  theInflationStream.next_in = (Tbyte*)input;
  theInflationStream.avail_in = nbyte;
  theInflationStream.next_out = theOutputBuffer;
  theInflationStream.avail_out = theSizeOfOutputBuffer;

  theStatus = inflate( &theInflationStream, Z_NO_FLUSH );

  theInflatedData = theOutputBuffer;
  theSizeOfInflatedData = theSizeOfOutputBuffer - theInflationStream.avail_out;

  return;
}

Tvoid TDataInflation::SetOutputBuffer( Tint bufsize )
{
  if ( theOutputBuffer ) {
    delete [] theOutputBuffer;
  }
  theOutputBuffer = new Tbyte[ bufsize ];
  return;
}


#ifdef __CLDAQ_ROOT_DLL
    ClassImp(TDataInflation)
#endif

#endif
