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