|
1 /* |
|
2 * Copyright (c) 2004-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 * Definition of the Swi::Sis::CCompressed |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "siscompressed.h" |
|
21 #include "sisfiledata.h" |
|
22 #include "compresseddataprovider.h" |
|
23 |
|
24 using namespace Swi; |
|
25 using namespace Swi::Sis; |
|
26 |
|
27 const TInt KMaxTemporaryBufferSize = 0x8000; |
|
28 |
|
29 /*static*/ CCompressed* CCompressed::NewLC(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour) |
|
30 { |
|
31 CCompressed* self = new (ELeave) CCompressed(aDataProvider); |
|
32 CleanupStack::PushL(self); |
|
33 self->ConstructL(aBytesRead, aTypeReadBehaviour); |
|
34 return self; |
|
35 } |
|
36 |
|
37 /*static*/ CCompressed* CCompressed::NewL(MSisDataProvider& aDataProvider, TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour) |
|
38 { |
|
39 CCompressed* self = NewLC(aDataProvider, aBytesRead, aTypeReadBehaviour); |
|
40 CleanupStack::Pop(self); |
|
41 return self; |
|
42 } |
|
43 |
|
44 CCompressed::CCompressed(MSisDataProvider& aDataProvider) : iDataProvider(aDataProvider) |
|
45 { |
|
46 } |
|
47 |
|
48 CCompressed::~CCompressed() |
|
49 { |
|
50 delete iCompressedReader; |
|
51 } |
|
52 |
|
53 void CCompressed::ConstructL(TInt64& aBytesRead, TReadTypeBehaviour aTypeReadBehaviour) |
|
54 { |
|
55 CField::ConstructL(iDataProvider, EFieldTypeCompressed, aBytesRead, aTypeReadBehaviour); |
|
56 User::LeaveIfError(iDataProvider.Seek(ESeekCurrent, iOffset)); |
|
57 |
|
58 // Calculate CRC of header and field data |
|
59 TInt64 fieldOffset = iOffset - HeaderSize(); |
|
60 User::LeaveIfError(iDataProvider.Seek(ESeekStart, fieldOffset)); |
|
61 CField::CalculateCrcL(iDataProvider, HeaderSize() + Length() + PaddingSize(), iCrc ); |
|
62 aBytesRead += Length() + PaddingSize(); |
|
63 } |
|
64 |
|
65 |
|
66 MSisDataProvider& CCompressed::DataProviderL() |
|
67 { |
|
68 TInt64 bytesRead=0; |
|
69 |
|
70 User::LeaveIfError(iDataProvider.Seek (ESeekStart, iOffset)); |
|
71 |
|
72 TCompressionAlgorithm alg; |
|
73 |
|
74 CField::ReadEnumL<TCompressionAlgorithm,TUint32>(iDataProvider, alg, bytesRead); |
|
75 |
|
76 CField::ReadTTypeL(iDataProvider, iUncompressedLength, bytesRead); |
|
77 |
|
78 // SISX defines the maximum to be 2^31 - 1 bytes. However, RHeap::Alloc will panic if you |
|
79 // ask for KMaxTInt / 2 or more bytes and at the same time length shouldn't be negative. |
|
80 if ((iUncompressedLength >= KMaxTInt / 2) || (iUncompressedLength < 0)) |
|
81 { |
|
82 User::Leave(KErrSISFieldLengthInvalid); |
|
83 } |
|
84 |
|
85 TFieldLength compressedLength = Length() - sizeof(TUint32) - sizeof(TFieldLength); // 12 = length(TCompressionAlgorithm) + length (TFieldLength) |
|
86 |
|
87 switch (alg) |
|
88 { |
|
89 case ECompressDeflate: |
|
90 { |
|
91 if (iCompressedReader) |
|
92 { |
|
93 delete iCompressedReader; |
|
94 iCompressedReader = NULL; |
|
95 } |
|
96 iCompressedReader = CCompressedDataProvider::NewL(iDataProvider, I64INT(compressedLength)); |
|
97 return *iCompressedReader; |
|
98 } |
|
99 |
|
100 case ECompressNone: |
|
101 { |
|
102 if (iUncompressedLength != compressedLength) |
|
103 { |
|
104 User::Leave(KErrSISFieldLengthInvalid); |
|
105 } |
|
106 return iDataProvider; |
|
107 } |
|
108 |
|
109 default: |
|
110 { |
|
111 User::Leave(KErrSISCompressionNotSupported); |
|
112 } |
|
113 } |
|
114 return iDataProvider; // Will not get here |
|
115 } |
|
116 |
|
117 void CCompressed::ExtractDataFieldL(RFile& aFile) |
|
118 { |
|
119 MSisDataProvider& provider = DataProviderL(); |
|
120 ExtractDataFieldL(provider, aFile, iUncompressedLength); |
|
121 } |
|
122 |
|
123 void CCompressed::ExtractDataFieldL(RFile& aFile, TInt64 aLength) |
|
124 { |
|
125 if (iCurrentDataProvider==NULL) |
|
126 { |
|
127 iBytesExtracted=0; |
|
128 iCurrentDataProvider=&DataProviderL(); |
|
129 } |
|
130 ExtractDataFieldL(*iCurrentDataProvider, aFile, aLength); |
|
131 } |
|
132 |
|
133 void CCompressed::ExtractDataFieldL(MSisDataProvider& aDataProvider, RFile& aFile, TInt64 aLength) |
|
134 { |
|
135 HBufC8* buffer = HBufC8::NewMaxLC(KMaxTemporaryBufferSize); |
|
136 |
|
137 TInt32 endPosition = iBytesExtracted + I64INT(aLength); |
|
138 |
|
139 TPtr8 bufferPtr(buffer->Des()); |
|
140 |
|
141 while (buffer->Length() > 0 && iBytesExtracted != endPosition) |
|
142 { |
|
143 TInt requestSize = ((endPosition - iBytesExtracted) < KMaxTemporaryBufferSize) ? (endPosition - iBytesExtracted) : KMaxTemporaryBufferSize; |
|
144 User::LeaveIfError(aDataProvider.Read(bufferPtr, requestSize)); |
|
145 User::LeaveIfError(aFile.Write(*buffer)); |
|
146 iBytesExtracted += buffer->Length(); |
|
147 } |
|
148 |
|
149 |
|
150 if (iBytesExtracted != endPosition) |
|
151 { |
|
152 User::Leave(KErrSISFieldBufferTooShort); |
|
153 } |
|
154 |
|
155 if(iBytesExtracted == iUncompressedLength) |
|
156 { |
|
157 // reached end of field, nothing left to extract so free memory |
|
158 delete iCompressedReader; |
|
159 iCompressedReader = NULL; |
|
160 iCurrentDataProvider = NULL; |
|
161 } |
|
162 |
|
163 CleanupStack::PopAndDestroy(buffer); |
|
164 } |
|
165 |
|
166 |
|
167 HBufC8* CCompressed::ReadControllerDataL() |
|
168 { |
|
169 MSisDataProvider& provider = DataProviderL(); |
|
170 |
|
171 HBufC8* controller = HBufC8::NewMaxLC(iUncompressedLength); |
|
172 TPtr8 controllerBuf(controller->Des()); |
|
173 |
|
174 TInt err = provider.Read(controllerBuf, iUncompressedLength); |
|
175 |
|
176 User::LeaveIfError(err); |
|
177 |
|
178 if (controllerBuf.Length() != iUncompressedLength) |
|
179 { |
|
180 User::Leave(KErrSISFieldBufferTooShort); |
|
181 } |
|
182 |
|
183 CleanupStack::Pop(controller); // pass on ownership |
|
184 |
|
185 return controller; |
|
186 } |