|
1 // Copyright (c) 2003-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 |
|
17 #include <zipfile.h> |
|
18 #include <zipfilememberinputstream.h> |
|
19 #include "libzcore.h" |
|
20 |
|
21 RZipFileMemberReaderStream::RZipFileMemberReaderStream( |
|
22 CZipFile& aZipFile, |
|
23 TUint32 aDataOffset, |
|
24 TUint32 aCompressedSize, |
|
25 TUint32 aUncompressedSize, |
|
26 TUint32 aCompressionMethod): |
|
27 iZipFile(aZipFile), |
|
28 iCompressionMethod(aCompressionMethod), |
|
29 iCompressedSize(aCompressedSize), |
|
30 iUncompressedSize(aUncompressedSize), |
|
31 iFileOffset(aDataOffset) |
|
32 { |
|
33 } |
|
34 |
|
35 |
|
36 RZipFileMemberReaderStream* RZipFileMemberReaderStream::NewL( |
|
37 CZipFile& aZipFile, |
|
38 TUint32 aDataOffset, |
|
39 TUint32 aCompressedSize, |
|
40 TUint32 aUncompressedSize, |
|
41 TUint32 aCompressionMethod) |
|
42 { |
|
43 RZipFileMemberReaderStream* me = new(ELeave) RZipFileMemberReaderStream(aZipFile, aDataOffset, aCompressedSize,aUncompressedSize, aCompressionMethod); |
|
44 CleanupStack::PushL(me); |
|
45 me->ConstructL(); |
|
46 CleanupStack::Pop(me); |
|
47 return me; |
|
48 } |
|
49 |
|
50 /** |
|
51 Creates input stream to be used for reading the contents of the compressed file. |
|
52 */ |
|
53 void RZipFileMemberReaderStream::ConstructL() |
|
54 { |
|
55 TInt err = inflateInit2_r(&iStream, -MAX_WBITS); |
|
56 if (err == Z_MEM_ERROR) |
|
57 { |
|
58 User::Leave(KErrNoMemory); |
|
59 } |
|
60 } |
|
61 |
|
62 /** |
|
63 Destructor. All dynamically allocated data structures for this stream are freed. |
|
64 */ |
|
65 EXPORT_C RZipFileMemberReaderStream::~RZipFileMemberReaderStream() |
|
66 { |
|
67 inflateEnd_r(&iStream); |
|
68 } |
|
69 |
|
70 /** |
|
71 Reads data from the stream buffer into the specified descriptor. |
|
72 On return, contains the data read from the stream buffer |
|
73 |
|
74 @param aDes The target descriptor for the data read from the stream buffer |
|
75 @param aLength The maximum number of bytes to be read |
|
76 @return KErrNone If all bytes read successfully. |
|
77 @return KErrCorrupt If reading fails. |
|
78 @return KErrEof If end of file is reached. |
|
79 @return ... Any one of the system-wide error codes for other errors. |
|
80 */ |
|
81 EXPORT_C TInt RZipFileMemberReaderStream::Read(TDes16& aDes, TInt aLength) |
|
82 { |
|
83 TUint32 numBytesRead = 0; |
|
84 TInt err = Read(CONST_CAST(TByte*,(const TByte*)aDes.Ptr()), 2*aLength, &numBytesRead); |
|
85 if (err != KErrNone) |
|
86 { |
|
87 aDes.SetLength( (err==KErrEof) ? numBytesRead>>2 : 0 ); |
|
88 return err; |
|
89 } |
|
90 |
|
91 aDes.SetLength(numBytesRead>>2); |
|
92 return KErrNone; |
|
93 } |
|
94 |
|
95 TInt RZipFileMemberReaderStream::Read(void) |
|
96 { |
|
97 TByte b; |
|
98 TUint32 numRead = 0; |
|
99 |
|
100 if (Read(&b, 1, &numRead) == 1) |
|
101 { |
|
102 return b; |
|
103 } |
|
104 else |
|
105 { |
|
106 return -1; |
|
107 } |
|
108 } |
|
109 |
|
110 TInt RZipFileMemberReaderStream::Read(TDes8& aDes, TInt aLength) |
|
111 { |
|
112 TUint32 numBytesRead = 0; |
|
113 TInt err = Read(CONST_CAST(TByte*,aDes.Ptr()), aLength, &numBytesRead); |
|
114 if (err != KErrNone) |
|
115 { |
|
116 aDes.SetLength( (err==KErrEof) ? numBytesRead : 0 ); |
|
117 return err; |
|
118 } |
|
119 |
|
120 aDes.SetLength(numBytesRead); |
|
121 return KErrNone; |
|
122 } |
|
123 |
|
124 void RZipFileMemberReaderStream::ReadL(TDes16& aDes, TInt aLength) |
|
125 { |
|
126 User::LeaveIfError(Read(aDes, aLength)); |
|
127 } |
|
128 |
|
129 void RZipFileMemberReaderStream::Release() |
|
130 {} |
|
131 |
|
132 void RZipFileMemberReaderStream::Close() |
|
133 {} |
|
134 |
|
135 |
|
136 TInt RZipFileMemberReaderStream::Read(TByte* aBytes, TUint32 aLength, TUint32* aRetLength) |
|
137 { |
|
138 if (iCompressionMethod == CZipArchive::EDeflated) |
|
139 { |
|
140 return GetBytes(aBytes, aLength, aRetLength); |
|
141 } |
|
142 else |
|
143 { |
|
144 return GetStoredBytes(aBytes, aLength, aRetLength); |
|
145 } |
|
146 } |
|
147 |
|
148 TInt RZipFileMemberReaderStream::GetBytes(TByte* aBytes, TUint32 aLength, TUint32* aRetLength) |
|
149 { |
|
150 TUint32 bytesLeftToRead; |
|
151 TInt status; |
|
152 |
|
153 iBytesLength = 0; |
|
154 |
|
155 // Be careful not to confuse compressed bytes and uncompressed bytes. |
|
156 // The length passed in is in uncompressed bytes, compressed bytes |
|
157 // are mainly used in the GetCompressedBytes() function called. |
|
158 // iBytesLength refers to the number of bytes already read. |
|
159 // If the request is to read past the end of the file |
|
160 // we should return KErrNone on the first instance, i.e. return all bytes |
|
161 // read successfully. On the second instance return KErrEof, i.e. we have |
|
162 // already read all the bytes when another request comes in, so return KErrEof. |
|
163 // This follows the rules for reading an uncompressed file within this component |
|
164 // and this is also the way that RFile::ReadL() does it. |
|
165 |
|
166 if (aLength > iUncompressedSize) |
|
167 { |
|
168 aLength = iUncompressedSize; // no point trying to read more than we have |
|
169 } |
|
170 |
|
171 bytesLeftToRead = aLength; |
|
172 |
|
173 while (bytesLeftToRead > 0) |
|
174 { |
|
175 if (iStream.avail_in == 0) |
|
176 { |
|
177 if (GetCompressedBytes() != KErrNone) |
|
178 { |
|
179 return KErrCorrupt; |
|
180 } |
|
181 } |
|
182 |
|
183 // The decompression will be done in the user provided memory. |
|
184 iStream.next_out = &aBytes[iBytesLength]; |
|
185 iStream.avail_out = aLength - iBytesLength; |
|
186 status = inflate_r(&iStream, Z_SYNC_FLUSH); |
|
187 |
|
188 switch (status) |
|
189 { |
|
190 case Z_OK: |
|
191 iBytesLength = aLength - iStream.avail_out; |
|
192 break; |
|
193 |
|
194 case Z_STREAM_END: //EOF |
|
195 if (iBytesLength == aLength - iStream.avail_out) |
|
196 { |
|
197 *aRetLength = iBytesLength; |
|
198 return KErrEof; |
|
199 } |
|
200 else |
|
201 { |
|
202 iBytesLength = aLength - iStream.avail_out; |
|
203 break; |
|
204 } |
|
205 case Z_MEM_ERROR: |
|
206 return KErrNoMemory; |
|
207 |
|
208 default: |
|
209 return KErrCorrupt; |
|
210 |
|
211 } |
|
212 bytesLeftToRead = aLength - iBytesLength; |
|
213 } |
|
214 |
|
215 *aRetLength = iBytesLength; |
|
216 return KErrNone; |
|
217 } |
|
218 |
|
219 TInt RZipFileMemberReaderStream::GetCompressedBytes(void) |
|
220 { |
|
221 if (iOffset < iCompressedSize) |
|
222 { |
|
223 TUint32 nBytesLeft; |
|
224 TUint32 nBytesToRead; |
|
225 |
|
226 nBytesLeft = iCompressedSize - iOffset; |
|
227 nBytesToRead = (nBytesLeft > sizeof(iCompressedBytes)) ? sizeof(iCompressedBytes) : nBytesLeft; |
|
228 if (iZipFile.Seek(iFileOffset) != KErrNone) |
|
229 { |
|
230 return KErrCorrupt; |
|
231 } |
|
232 else |
|
233 if (iZipFile.Read(iCompressedBytes, nBytesToRead) != KErrNone) |
|
234 { |
|
235 return KErrCorrupt; |
|
236 } |
|
237 iFileOffset += nBytesToRead; |
|
238 iOffset += nBytesToRead; |
|
239 iStream.next_in = iCompressedBytes; |
|
240 iStream.avail_in = nBytesToRead; |
|
241 return KErrNone; |
|
242 } |
|
243 else |
|
244 if (iDone == EFalse) |
|
245 { |
|
246 iCompressedBytes[0] = 0; |
|
247 iStream.avail_in = 1; |
|
248 iStream.next_in = iCompressedBytes; |
|
249 iDone = ETrue; |
|
250 return KErrNone; |
|
251 } |
|
252 else |
|
253 { |
|
254 return KErrCorrupt; |
|
255 } |
|
256 } |
|
257 |
|
258 TInt RZipFileMemberReaderStream::GetStoredBytes(TByte* aBytes, TUint32 aLength, TUint32* aRetLength) |
|
259 { |
|
260 TInt status; |
|
261 |
|
262 if (aLength > iUncompressedSize) |
|
263 { |
|
264 aLength = iUncompressedSize; // no point trying to read more than we have |
|
265 } |
|
266 if (aLength == 0) // empty file like a directory |
|
267 { |
|
268 return KErrNone; |
|
269 } |
|
270 if (iOffset == iCompressedSize) // end of zip item. |
|
271 { |
|
272 return KErrEof; |
|
273 } |
|
274 if ((iOffset + aLength) > iCompressedSize) |
|
275 { |
|
276 aLength = iCompressedSize - iOffset; // adjust read to what is left |
|
277 if ( aLength <= 0 ) |
|
278 { |
|
279 return KErrCorrupt; |
|
280 } |
|
281 } |
|
282 if (iZipFile.Seek(iFileOffset) != KErrNone) |
|
283 { |
|
284 return KErrCorrupt; |
|
285 } |
|
286 status = iZipFile.Read(aBytes, aLength); |
|
287 if (status == KErrNone) |
|
288 { |
|
289 iFileOffset += aLength; |
|
290 iOffset += aLength; |
|
291 *aRetLength = aLength; |
|
292 } |
|
293 return status; |
|
294 } |