lowlevellibsandfws/apputils/src/BADICTIONARYCOMPRESSION.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2001-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 //
       
    15 
       
    16 #include <e32std.h>
       
    17 #include <baflpan.h>
       
    18 #include "BADICTIONARYCOMPRESSION.H"
       
    19 
       
    20 RDictionaryCompressionBitStream::RDictionaryCompressionBitStream() :
       
    21 	iNumberOfBitsUsedForDictionaryTokens(0),
       
    22 	iOffsetToFirstBit(-1),
       
    23 	iOffsetToCurrentBit(-1),
       
    24 	iOffsetOnePastLastBit(-1),
       
    25 	iOwnsBitBuffer(EFalse),
       
    26 	iBuffer(NULL),
       
    27 	iAssertObj()
       
    28 	{
       
    29 	}
       
    30 
       
    31 void RDictionaryCompressionBitStream::OpenL(
       
    32 				TInt aNumberOfBitsUsedForDictionaryTokens,
       
    33 				TInt aOffsetToFirstBit,
       
    34 				TInt aOffsetOnePastLastBit,
       
    35 				TBool aTransferringOwnershipOfBuffer,
       
    36 				TUint8* aBuffer,
       
    37 				const TBaAssert& aAssertObj)
       
    38 	{
       
    39 	iNumberOfBitsUsedForDictionaryTokens = aNumberOfBitsUsedForDictionaryTokens;
       
    40 	iOffsetToFirstBit = aOffsetToFirstBit;
       
    41 	iOffsetToCurrentBit = aOffsetToFirstBit;
       
    42 	iOffsetOnePastLastBit = aOffsetOnePastLastBit;
       
    43 	iOwnsBitBuffer = aTransferringOwnershipOfBuffer;
       
    44 	iBuffer = aBuffer;
       
    45 	iAssertObj = aAssertObj;
       
    46 
       
    47 	iAssertObj.AssertDebL(aBuffer!=NULL,EBafPanicNullPointer);
       
    48 	iAssertObj.AssertDebL(aOffsetToFirstBit>=0,EBafPanicNegativeOffsetToFirstBit1);
       
    49 	iAssertObj.AssertDebL(aOffsetToFirstBit<=aOffsetOnePastLastBit,EBafPanicNegativeLengthOfBitBuffer);
       
    50 	}
       
    51 
       
    52 void RDictionaryCompressionBitStream::Close()
       
    53 	{
       
    54 	if (iOwnsBitBuffer)
       
    55 		{
       
    56 		iOwnsBitBuffer=EFalse;
       
    57 		delete [] iBuffer;
       
    58 		}
       
    59 	iBuffer=NULL;
       
    60 	}
       
    61 
       
    62 TBool RDictionaryCompressionBitStream::EndOfStreamL() const
       
    63 	{
       
    64 	__ASSERT_DEBUG(iBuffer!=NULL,Panic(EBafPanicNotConstructed1));
       
    65 	iAssertObj.AssertDebL(iOffsetToFirstBit>=0,EBafPanicNegativeOffsetToFirstBit2);
       
    66 	iAssertObj.AssertDebL(iOffsetToCurrentBit>=iOffsetToFirstBit,EBafPanicBadCurrentBitPosition1);
       
    67 	iAssertObj.AssertDebL(iOffsetToCurrentBit<=iOffsetOnePastLastBit,EBafPanicBadCurrentBitPosition2);
       
    68 	return iOffsetToCurrentBit>=iOffsetOnePastLastBit;
       
    69 	}
       
    70 
       
    71 TInt RDictionaryCompressionBitStream::IndexOfDictionaryEntryL()
       
    72 	{
       
    73 	// increments the current bit-position if it returns a value >=0; returns KErrNotFound if the next thing in the stream is plain data rather than the index of a dictionary entry
       
    74 	__ASSERT_DEBUG(iBuffer!=NULL,Panic(EBafPanicNotConstructed2));
       
    75 	iAssertObj.AssertDebL(!EndOfStreamL(),EBafPanicEndOfStream1);
       
    76 	if (!CurrentBitIsOn())
       
    77 		{
       
    78 		++iOffsetToCurrentBit;
       
    79 		return ReadIntegerL(iNumberOfBitsUsedForDictionaryTokens);
       
    80 		}
       
    81 	return KErrNotFound;
       
    82 	}
       
    83 
       
    84 void RDictionaryCompressionBitStream::ReadL(TDes8& aBufferToAppendTo,TBool aCalypsoFileFormat)
       
    85 	{
       
    86 	// can only be called if IndexOfDictionaryEntry returned a negative value
       
    87 	__ASSERT_DEBUG(iBuffer!=NULL,Panic(EBafPanicNotConstructed3));
       
    88 	iAssertObj.AssertDebL(!EndOfStreamL(),EBafPanicEndOfStream2);
       
    89 	TInt numberOfConsecutivePrefixBits=0;
       
    90 	TInt i;
       
    91 	for (i=0; i<4; ++i)
       
    92 		{
       
    93 		const TBool currentBitIsOn=CurrentBitIsOn();
       
    94 		++iOffsetToCurrentBit; // increment this regardless whether the current bit is on
       
    95 		if (!currentBitIsOn)
       
    96 			{
       
    97 			break;
       
    98 			}
       
    99 		++numberOfConsecutivePrefixBits;
       
   100 		}
       
   101 	iAssertObj.AssertDebL(numberOfConsecutivePrefixBits>0,EBafPanicBadNumberOfConsecutivePrefixBits1);
       
   102 	iAssertObj.AssertDebL(numberOfConsecutivePrefixBits<=4,EBafPanicBadNumberOfConsecutivePrefixBits2);
       
   103 	TInt numberOfBytesToRead;
       
   104 	if (numberOfConsecutivePrefixBits==3)
       
   105 		{
       
   106 		numberOfBytesToRead=3+ReadIntegerL(3);
       
   107 		}
       
   108 	else if (numberOfConsecutivePrefixBits==4)
       
   109 		{
       
   110 		numberOfBytesToRead=ReadIntegerL(8);
       
   111 		if (!aCalypsoFileFormat)
       
   112 			{
       
   113 			numberOfBytesToRead+=3+(1<<3);
       
   114 			}
       
   115 		}
       
   116 	else
       
   117 		{
       
   118 		numberOfBytesToRead=numberOfConsecutivePrefixBits;
       
   119 		}
       
   120 	const TInt numberOfBitsOffByteBoundary=iOffsetToCurrentBit%8;
       
   121 	const TUint8* currentByte=iBuffer+(iOffsetToCurrentBit/8);
       
   122 	iAssertObj.AssertDebL(
       
   123 		(numberOfBytesToRead + aBufferToAppendTo.Length()) <= aBufferToAppendTo.MaxLength(),
       
   124 		EBafPanicBufLength);
       
   125 	for (i=0; i<numberOfBytesToRead; ++i, ++currentByte)
       
   126 		{
       
   127 		TUint byte=*currentByte;
       
   128 		iAssertObj.AssertDebL(numberOfBitsOffByteBoundary>=0,EBafPanicBadNumberOfBitsOffByteBoundary1);
       
   129 		if (numberOfBitsOffByteBoundary>0)
       
   130 			{
       
   131 			byte>>=numberOfBitsOffByteBoundary;
       
   132 			byte|=(*(currentByte+1)<<(8-numberOfBitsOffByteBoundary));
       
   133 			byte&=0xff;
       
   134 			}
       
   135 		aBufferToAppendTo.Append(byte);
       
   136 		}
       
   137 	iOffsetToCurrentBit+=numberOfBytesToRead*8;
       
   138 	iAssertObj.AssertDebL(numberOfBitsOffByteBoundary==iOffsetToCurrentBit%8,EBafPanicBadNumberOfBitsOffByteBoundary2);
       
   139 	}
       
   140 
       
   141 TBool RDictionaryCompressionBitStream::CurrentBitIsOn() const
       
   142 	{
       
   143 	// does not increment the current bit-position
       
   144 	__ASSERT_DEBUG(iBuffer!=NULL,Panic(EBafPanicNotConstructed4));
       
   145 	return iBuffer[iOffsetToCurrentBit/8]&(1<<(iOffsetToCurrentBit%8));
       
   146 	}
       
   147 
       
   148 TUint RDictionaryCompressionBitStream::ReadIntegerL(TInt aNumberOfBits)
       
   149 	{
       
   150 	// increments the current bit-position
       
   151 	__ASSERT_DEBUG(iBuffer!=NULL,Panic(EBafPanicNotConstructed5));
       
   152 	TInt integer=0;
       
   153 	TInt numberOfBitsLeftToRead=aNumberOfBits;
       
   154 	FOREVER
       
   155 		{
       
   156 		const TInt offsetToFirstBitToReadInCurrentByte=iOffsetToCurrentBit%8;
       
   157 		const TInt offsetOnePastLastBitToReadInCurrentByte=Min(8,offsetToFirstBitToReadInCurrentByte+numberOfBitsLeftToRead);
       
   158 		const TInt numberOfBitsReadFromCurrentByte=offsetOnePastLastBitToReadInCurrentByte-offsetToFirstBitToReadInCurrentByte;
       
   159 		iAssertObj.AssertDebL(numberOfBitsReadFromCurrentByte>0,EBafPanicBadNumberOfBitsReadFromCurrentByte);
       
   160 		const TUint bitsReadFromCurrentByte=((iBuffer[iOffsetToCurrentBit/8]>>offsetToFirstBitToReadInCurrentByte)&((1<<numberOfBitsReadFromCurrentByte)-1));
       
   161 		integer|=(bitsReadFromCurrentByte<<(aNumberOfBits-numberOfBitsLeftToRead));
       
   162 		iOffsetToCurrentBit+=numberOfBitsReadFromCurrentByte;
       
   163 		numberOfBitsLeftToRead-=numberOfBitsReadFromCurrentByte;
       
   164 		iAssertObj.AssertDebL(numberOfBitsLeftToRead>=0,EBafPanicBadNumberOfBitsLeftToRead);
       
   165 		if (numberOfBitsLeftToRead<=0)
       
   166 			{
       
   167 			break;
       
   168 			}
       
   169 		}
       
   170 	return integer;
       
   171 	}
       
   172