e32tools/e32lib/e32image/deflate/inflate.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\inflate.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "deflate.h"
       
    19 #include "panic.h"
       
    20 #include "h_utl.h"
       
    21 
       
    22 // Class RInflater
       
    23 //
       
    24 // The inflation algorithm, complete with huffman decoding
       
    25 
       
    26 inline CInflater::CInflater(TBitInput& aInput)
       
    27 	:iBits(&aInput),iEncoding(0),iOut(0)
       
    28 	{}
       
    29 
       
    30 void CInflater::ConstructL()
       
    31 	{
       
    32 	iEncoding=new TEncoding;
       
    33 	if(iEncoding==NULL)
       
    34 		Panic(EHuffmanOutOfMemory);
       
    35 	InitL();
       
    36 	iLen=0;
       
    37 	iOut=new TUint8[KDeflateMaxDistance];
       
    38 	if(iOut==NULL)
       
    39 		Panic(EHuffmanOutOfMemory);
       
    40 
       
    41 	iAvail=iLimit=iOut;
       
    42 	}
       
    43 
       
    44 CInflater* CInflater::NewLC(TBitInput& aInput)
       
    45 	{
       
    46 	CInflater* self=new CInflater(aInput);
       
    47 	if(self==NULL)
       
    48 		Panic(EHuffmanOutOfMemory);
       
    49 	self->ConstructL();
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 CInflater::~CInflater()
       
    54 	{
       
    55 	delete iEncoding;
       
    56 	delete [] iOut;
       
    57 	}
       
    58 
       
    59 TInt CInflater::ReadL(TUint8* aBuffer,TInt aLength)
       
    60 	{
       
    61 	TInt tfr=0;
       
    62 	for (;;)
       
    63 		{
       
    64 		TInt len=Min(aLength,iLimit-iAvail);
       
    65 		if (len && aBuffer)
       
    66 			{
       
    67 			HMem::Copy(aBuffer,iAvail,len);
       
    68 			aBuffer+=len;
       
    69 			}
       
    70 		aLength-=len;
       
    71 		iAvail+=len;
       
    72 		tfr+=len;
       
    73 		if (aLength==0)
       
    74 			return tfr;
       
    75 		len=InflateL();
       
    76 		if (len==0)
       
    77 			return tfr;
       
    78 		iAvail=iOut;
       
    79 		iLimit=iAvail+len;
       
    80 		}
       
    81 	}
       
    82 
       
    83 TInt CInflater::SkipL(TInt aLength)
       
    84 	{
       
    85 	return ReadL(0,aLength);
       
    86 	}
       
    87 
       
    88 void CInflater::InitL()
       
    89 	{
       
    90 // read the encoding
       
    91 	Huffman::InternalizeL(*iBits,iEncoding->iLitLen,KDeflationCodes);
       
    92 // validate the encoding
       
    93 	if (!Huffman::IsValid(iEncoding->iLitLen,TEncoding::ELitLens) ||
       
    94 		!Huffman::IsValid(iEncoding->iDistance,TEncoding::EDistances))
       
    95 		Panic(EHuffmanCorruptFile);
       
    96 // convert the length tables into huffman decoding trees
       
    97 	Huffman::Decoding(iEncoding->iLitLen,TEncoding::ELitLens,iEncoding->iLitLen);
       
    98 	Huffman::Decoding(iEncoding->iDistance,TEncoding::EDistances,iEncoding->iDistance,KDeflateDistCodeBase);
       
    99 	}
       
   100 
       
   101 TInt CInflater::InflateL()
       
   102 //
       
   103 // consume all data lag in the history buffer, then decode to fill up the output buffer
       
   104 // return the number of available bytes in the output buffer. This is only ever less than
       
   105 // the buffer size if the end of stream marker has been read
       
   106 //
       
   107 	{
       
   108 // empty the history buffer into the output
       
   109 	TUint8* out=iOut;
       
   110 	TUint8* const end=out+KDeflateMaxDistance;
       
   111 	const TUint32* tree=iEncoding->iLitLen;
       
   112 	if (iLen<0)	// EOF
       
   113 		return 0;
       
   114 	if (iLen>0)
       
   115 		goto useHistory;
       
   116 //
       
   117 	while (out<end)
       
   118 		{
       
   119 		// get a huffman code
       
   120 		{
       
   121 		TInt val=iBits->HuffmanL(tree)-TEncoding::ELiterals;
       
   122 		if (val<0)
       
   123 			{
       
   124 			*out++=TUint8(val);
       
   125 			continue;			// another literal/length combo
       
   126 			}
       
   127 		if (val==TEncoding::EEos-TEncoding::ELiterals)
       
   128 			{	// eos marker. we're done
       
   129 			iLen=-1;
       
   130 			break;
       
   131 			}
       
   132 		// get the extra bits for the code
       
   133 		TInt code=val&0xff;
       
   134 		if (code>=8)
       
   135 			{	// xtra bits
       
   136 			TInt xtra=(code>>2)-1;
       
   137 			code-=xtra<<2;
       
   138 			code<<=xtra;
       
   139 			code|=iBits->ReadL(xtra);
       
   140 			}
       
   141 		if (val<KDeflateDistCodeBase-TEncoding::ELiterals)
       
   142 			{	// length code... get the code
       
   143 			iLen=code+KDeflateMinLength;
       
   144 			tree=iEncoding->iDistance;
       
   145 			continue;			// read the huffman code
       
   146 			}
       
   147 		// distance code
       
   148 		iRptr=out-(code+1);
       
   149 		if (iRptr+KDeflateMaxDistance<end)
       
   150 			iRptr+=KDeflateMaxDistance;
       
   151 		}
       
   152 useHistory:
       
   153 		TInt tfr=Min(end-out,iLen);
       
   154 		iLen-=tfr;
       
   155 		const TUint8* from=iRptr;
       
   156 		do
       
   157 			{
       
   158 			*out++=*from++;
       
   159 			if (from==end)
       
   160 				from-=KDeflateMaxDistance;
       
   161 			} while (--tfr!=0);
       
   162 		iRptr=from;
       
   163 		tree=iEncoding->iLitLen;
       
   164 		};
       
   165 	return out-iOut;
       
   166 	}
       
   167 
       
   168 
       
   169