e32tools/e32lib/e32image/deflate/compress.cpp
changeset 0 044383f39525
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32tools\petran\Szip\compress.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "deflate.h"
       
    19 
       
    20 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
    21 #include <fstream>
       
    22 #else //__MSVCDOTNET__
       
    23 #include <fstream.h>
       
    24 #endif //__MSVCDOTNET__
       
    25 
       
    26 #include <assert.h>
       
    27 #include "h_utl.h"
       
    28 
       
    29 class TFileOutput : public TBitOutput
       
    30 	{
       
    31 	enum {KBufSize=0x1000};
       
    32 public:
       
    33 	TFileOutput(ostream& os);
       
    34 	void FlushL();
       
    35 	TUint32 iDataCount; 
       
    36 private:
       
    37 	void OverflowL();
       
    38 private:
       
    39 	ostream& iOutStream;
       
    40 	TUint8 iBuf[KBufSize];
       
    41 	};
       
    42 
       
    43 TFileOutput::TFileOutput(ostream& os)
       
    44 	:iOutStream(os)
       
    45 	{
       
    46 	Set(iBuf,KBufSize);
       
    47 	}
       
    48 
       
    49 void TFileOutput::OverflowL()
       
    50 //
       
    51 // empty the buffer and reset the pointers
       
    52 //
       
    53 	{
       
    54 	FlushL();
       
    55 	Set(iBuf,KBufSize);
       
    56 	}
       
    57 
       
    58 void TFileOutput::FlushL()
       
    59 //
       
    60 // write out the contents of the buffer
       
    61 //
       
    62 	{
       
    63 	TInt len=Ptr()-iBuf;
       
    64 	if (len)
       
    65 		{
       
    66 		iOutStream.write(reinterpret_cast<char *>(iBuf), len); // write extended header
       
    67 		iDataCount += len;
       
    68 		}
       
    69 	}
       
    70 
       
    71 void DeflateCompress(char *bytes,TInt size,ostream &os)
       
    72 	{
       
    73 	TFileOutput* output=new TFileOutput(os);
       
    74 	output->iDataCount = 0;
       
    75 	DeflateL((TUint8*)bytes,size,*output);
       
    76 	output->FlushL();
       
    77 	delete output;
       
    78 	}
       
    79 
       
    80 TUint32 DeflateCompressCheck(char *bytes,TInt size,ostream &os)
       
    81 	{
       
    82 	TUint32 r = 0;
       
    83 	TFileOutput* output=new TFileOutput(os);
       
    84 	output->iDataCount = 0;
       
    85 	DeflateL((TUint8*)bytes,size,*output);
       
    86 	output->FlushL();
       
    87 
       
    88 	r = output->iDataCount; // Preserve the compressed count
       
    89 	delete output;
       
    90 	return r; // Return the compressed size
       
    91 	}
       
    92 
       
    93 class TFileInput : public TBitInput
       
    94 	{
       
    95 public:
       
    96 	TFileInput(unsigned char* source,int size);
       
    97 
       
    98 private:
       
    99 	void UnderflowL();
       
   100 private:
       
   101 	TUint8* iReadBuf;
       
   102 	TInt iSize;
       
   103 	};
       
   104 
       
   105 TFileInput::TFileInput(unsigned char* source,int size)
       
   106 	:iReadBuf(source),iSize(size)
       
   107 	{
       
   108 	Set(source,iSize*8);
       
   109 	}
       
   110 
       
   111 
       
   112 void TFileInput::UnderflowL()
       
   113 	{
       
   114 	Print(ESevereError,"Buffer underflow on deflate\n");
       
   115 	}
       
   116 
       
   117 void InflateUnCompress(unsigned char* source, int sourcesize,unsigned char* dest, int destsize)
       
   118 	{
       
   119 	TFileInput* input = new TFileInput(source, sourcesize);
       
   120 	CInflater* inflater=CInflater::NewLC(*input);
       
   121 	inflater->ReadL(dest,destsize);
       
   122 	delete input;
       
   123 	//delete inflater;
       
   124 	}
       
   125 
       
   126