commands/chkdeps/sf_inflate.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     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 the License "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 // Accenture - minor tweaks to build as fshell command
       
    13 //
       
    14 // Description:
       
    15 // f32\sfile\sf_inflate.h
       
    16 // 
       
    17 //
       
    18 
       
    19 #include "sf_deflate.h"
       
    20 //#include "sf_ldr.h"
       
    21 
       
    22 //BEGIN fshell patches
       
    23 #include <fshell/ioutils.h>
       
    24 #define CHECK_FAILURE(x) if (x != 0) { IoUtils::CCommandBase::Static().PrintWarning(_L("sf_inflate check failure at line %d err=%d"), __LINE__, x); }
       
    25 #define LEAVE_FAILURE(x) StaticLeaveIfErr(x, _L("sf_inflate error at line %d"), __LINE__)
       
    26 //END fshell patches
       
    27 
       
    28 // Class RInflater
       
    29 //
       
    30 // The inflation algorithm, complete with huffman decoding
       
    31 
       
    32 inline CInflater::CInflater(TBitInput& aInput)
       
    33 	:iBits(&aInput),iEncoding(0),iOut(0)
       
    34 	{}
       
    35 
       
    36 void CInflater::ConstructL()
       
    37 	{
       
    38 	iEncoding=new(ELeave) TEncoding;
       
    39 	InitL();
       
    40 	iLen=0;
       
    41 	iOut=new(ELeave) TUint8[KDeflateMaxDistance];
       
    42 	iAvail=iLimit=iOut;
       
    43 	}
       
    44 
       
    45 CInflater* CInflater::NewLC(TBitInput& aInput)
       
    46 	{
       
    47 	CInflater* self=new(ELeave) CInflater(aInput);
       
    48 	CleanupStack::PushL(self);
       
    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, TMemoryMoveFunction aMemMovefn)
       
    60 	{
       
    61 	TInt tfr=0;
       
    62 	for (;;)
       
    63 		{
       
    64 		TInt len=Min(aLength,iLimit-iAvail);
       
    65 		if (len && aBuffer)
       
    66 			{
       
    67 			aMemMovefn(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,Mem::Move);
       
    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 		LEAVE_FAILURE(KErrCorrupt);
       
    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 			{
       
   143 			// length code... get the code
       
   144 			if(TUint(code)>TUint(KDeflateMaxLength-KDeflateMinLength))
       
   145 				{
       
   146 				CHECK_FAILURE(KErrCorrupt);
       
   147 				goto error;
       
   148 				}
       
   149 			iLen=code+KDeflateMinLength;
       
   150 			tree=iEncoding->iDistance;
       
   151 			continue;			// read the huffman code
       
   152 			}
       
   153 		// distance code
       
   154 		if(TUint(code)>TUint(KDeflateMaxDistance-1))
       
   155 			{
       
   156 			CHECK_FAILURE(KErrCorrupt);
       
   157 			goto error;
       
   158 			}
       
   159 		iRptr=out-(code+1);
       
   160 		if (iRptr+KDeflateMaxDistance<end)
       
   161 			iRptr+=KDeflateMaxDistance;
       
   162 		if(!iLen)
       
   163 			{
       
   164 			CHECK_FAILURE(KErrCorrupt);
       
   165 			goto error;
       
   166 			}
       
   167 		}
       
   168 useHistory:
       
   169 		{
       
   170 		TInt tfr=Min(end-out,iLen);
       
   171 		iLen-=tfr;
       
   172 		const TUint8* from=iRptr;
       
   173 		do
       
   174 			{
       
   175 			*out++=*from++;
       
   176 			if (from==end)
       
   177 				from-=KDeflateMaxDistance;
       
   178 			} while (--tfr!=0);
       
   179 		iRptr=from;
       
   180 		tree=iEncoding->iLitLen;
       
   181 		}
       
   182 
       
   183 		};
       
   184 	return out-iOut;
       
   185 
       
   186 error:
       
   187 	LEAVE_FAILURE(KErrCorrupt);
       
   188 	return 0;
       
   189 	}
       
   190