1 // Copyright (c) 2010 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 // dictionarycompression.cpp |
|
15 // |
|
16 /** |
|
17 * @file dictionarycompression.cpp |
|
18 * |
|
19 * @internalComponent |
|
20 * @released |
|
21 */ |
|
22 |
|
23 #include "dictionarycompression.h" |
|
24 |
|
25 RDictionaryCompressionBitStream::RDictionaryCompressionBitStream() : |
|
26 iNumberOfBitsUsedForDictionaryTokens(0), |
|
27 iOffsetToFirstBit(-1), |
|
28 iOffsetToCurrentBit(-1), |
|
29 iOffsetOnePastLastBit(-1), |
|
30 iOwnsBitBuffer(false), |
|
31 iBuffer(NULL) |
|
32 { |
|
33 } |
|
34 |
|
35 void RDictionaryCompressionBitStream::OpenL( |
|
36 TInt aNumberOfBitsUsedForDictionaryTokens, |
|
37 TInt aOffsetToFirstBit, |
|
38 TInt aOffsetOnePastLastBit, |
|
39 TBool aTransferringOwnershipOfBuffer, |
|
40 TUint8* aBuffer) |
|
41 { |
|
42 iNumberOfBitsUsedForDictionaryTokens = aNumberOfBitsUsedForDictionaryTokens; |
|
43 iOffsetToFirstBit = aOffsetToFirstBit; |
|
44 iOffsetToCurrentBit = aOffsetToFirstBit; |
|
45 iOffsetOnePastLastBit = aOffsetOnePastLastBit; |
|
46 iOwnsBitBuffer = aTransferringOwnershipOfBuffer; |
|
47 iBuffer = aBuffer; |
|
48 |
|
49 assert(aBuffer!=NULL); |
|
50 assert(aOffsetToFirstBit >= 0); |
|
51 assert(aOffsetToFirstBit<=aOffsetOnePastLastBit); |
|
52 } |
|
53 |
|
54 void RDictionaryCompressionBitStream::Close() |
|
55 { |
|
56 if (iOwnsBitBuffer) |
|
57 { |
|
58 iOwnsBitBuffer=EFalse; |
|
59 delete [] iBuffer; |
|
60 } |
|
61 iBuffer=NULL; |
|
62 } |
|
63 |
|
64 TBool RDictionaryCompressionBitStream::EndOfStreamL() const |
|
65 { |
|
66 assert(iBuffer!=NULL); |
|
67 assert(iOffsetToFirstBit >= 0); |
|
68 assert(iOffsetToCurrentBit>=iOffsetToFirstBit); |
|
69 assert(iOffsetToCurrentBit<=iOffsetOnePastLastBit); |
|
70 return iOffsetToCurrentBit>=iOffsetOnePastLastBit; |
|
71 } |
|
72 |
|
73 TInt RDictionaryCompressionBitStream::IndexOfDictionaryEntryL() |
|
74 { |
|
75 // 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 |
|
76 assert(iBuffer!=NULL); |
|
77 assert(!EndOfStreamL()); |
|
78 if (!CurrentBitIsOn()) |
|
79 { |
|
80 ++iOffsetToCurrentBit; |
|
81 return ReadIntegerL(iNumberOfBitsUsedForDictionaryTokens); |
|
82 } |
|
83 return KErrNotFound; |
|
84 } |
|
85 |
|
86 void RDictionaryCompressionBitStream::ReadL(Ptr8 aBufferToAppendTo,TBool aCalypsoFileFormat) |
|
87 { |
|
88 // can only be called if IndexOfDictionaryEntry returned a negative value |
|
89 assert(iBuffer!=NULL); |
|
90 assert(!EndOfStreamL()); |
|
91 TInt numberOfConsecutivePrefixBits=0; |
|
92 TInt i; |
|
93 for (i=0; i<4; ++i) |
|
94 { |
|
95 const TBool currentBitIsOn=CurrentBitIsOn(); |
|
96 ++iOffsetToCurrentBit; // increment this regardless whether the current bit is on |
|
97 if (!currentBitIsOn) |
|
98 { |
|
99 break; |
|
100 } |
|
101 ++numberOfConsecutivePrefixBits; |
|
102 } |
|
103 assert(numberOfConsecutivePrefixBits>0); |
|
104 assert(numberOfConsecutivePrefixBits<=4); |
|
105 TInt numberOfBytesToRead = numberOfConsecutivePrefixBits; |
|
106 if (numberOfConsecutivePrefixBits==3) |
|
107 { |
|
108 numberOfBytesToRead=3+ReadIntegerL(3); |
|
109 } |
|
110 else if (numberOfConsecutivePrefixBits==4) |
|
111 { |
|
112 numberOfBytesToRead=ReadIntegerL(8); |
|
113 if (!aCalypsoFileFormat) |
|
114 { |
|
115 numberOfBytesToRead+=3+(1<<3); |
|
116 } |
|
117 } |
|
118 |
|
119 const TInt numberOfBitsOffByteBoundary=iOffsetToCurrentBit%8; |
|
120 const TUint8* currentByte=iBuffer+(iOffsetToCurrentBit/8); |
|
121 assert((numberOfBytesToRead + aBufferToAppendTo.GetLength()) <= aBufferToAppendTo.GetMaxLength()); |
|
122 for (i=0; i<numberOfBytesToRead; ++i, ++currentByte) |
|
123 { |
|
124 TUint byte=*currentByte; |
|
125 assert(numberOfBitsOffByteBoundary>=0); |
|
126 if (numberOfBitsOffByteBoundary>0) |
|
127 { |
|
128 byte>>=numberOfBitsOffByteBoundary; |
|
129 byte|=(*(currentByte+1)<<(8-numberOfBitsOffByteBoundary)); |
|
130 byte&=0xff; |
|
131 } |
|
132 aBufferToAppendTo.Append(byte,1); |
|
133 } |
|
134 iOffsetToCurrentBit+=numberOfBytesToRead*8; |
|
135 assert(numberOfBitsOffByteBoundary==iOffsetToCurrentBit%8); |
|
136 } |
|
137 |
|
138 TBool RDictionaryCompressionBitStream::CurrentBitIsOn() const |
|
139 { |
|
140 // does not increment the current bit-position |
|
141 assert(iBuffer!=NULL); |
|
142 return iBuffer[iOffsetToCurrentBit/8]&(1<<(iOffsetToCurrentBit%8)); |
|
143 } |
|
144 |
|
145 TUint RDictionaryCompressionBitStream::ReadIntegerL(TInt aNumberOfBits) |
|
146 { |
|
147 // increments the current bit-position |
|
148 assert(iBuffer!=NULL); |
|
149 TInt integer=0; |
|
150 TInt numberOfBitsLeftToRead=aNumberOfBits; |
|
151 // FOREVER |
|
152 while(1) |
|
153 { |
|
154 const TInt offsetToFirstBitToReadInCurrentByte=iOffsetToCurrentBit%8; |
|
155 const TInt offsetOnePastLastBitToReadInCurrentByte=Min(8,offsetToFirstBitToReadInCurrentByte+numberOfBitsLeftToRead); |
|
156 const TInt numberOfBitsReadFromCurrentByte=offsetOnePastLastBitToReadInCurrentByte-offsetToFirstBitToReadInCurrentByte; |
|
157 assert(numberOfBitsReadFromCurrentByte>0); |
|
158 const TUint bitsReadFromCurrentByte=((iBuffer[iOffsetToCurrentBit/8]>>offsetToFirstBitToReadInCurrentByte)&((1<<numberOfBitsReadFromCurrentByte)-1)); |
|
159 integer|=(bitsReadFromCurrentByte<<(aNumberOfBits-numberOfBitsLeftToRead)); |
|
160 iOffsetToCurrentBit+=numberOfBitsReadFromCurrentByte; |
|
161 numberOfBitsLeftToRead-=numberOfBitsReadFromCurrentByte; |
|
162 assert(numberOfBitsLeftToRead>=0); |
|
163 if (numberOfBitsLeftToRead<=0) |
|
164 { |
|
165 break; |
|
166 } |
|
167 } |
|
168 return integer; |
|
169 } |
|
170 |
|