dom/file/ArchiveZipFile.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:13486b6c38d4
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "ArchiveZipFile.h"
8 #include "ArchiveZipEvent.h"
9
10 #include "nsIInputStream.h"
11 #include "zlib.h"
12 #include "mozilla/Attributes.h"
13
14 USING_FILE_NAMESPACE
15
16 #define ZIP_CHUNK 16384
17
18 /**
19 * Input stream object for zip files
20 */
21 class ArchiveInputStream MOZ_FINAL : public nsIInputStream,
22 public nsISeekableStream
23 {
24 public:
25 ArchiveInputStream(uint64_t aParentSize,
26 nsIInputStream* aInputStream,
27 nsString& aFilename,
28 uint32_t aStart,
29 uint32_t aLength,
30 ZipCentral& aCentral)
31 : mCentral(aCentral),
32 mFilename(aFilename),
33 mStart(aStart),
34 mLength(aLength),
35 mStatus(NotStarted)
36 {
37 MOZ_COUNT_CTOR(ArchiveInputStream);
38
39 // Reset the data:
40 memset(&mData, 0, sizeof(mData));
41
42 mData.parentSize = aParentSize;
43 mData.inputStream = aInputStream;
44 }
45
46 virtual ~ArchiveInputStream()
47 {
48 MOZ_COUNT_DTOR(ArchiveInputStream);
49 Close();
50 }
51
52 NS_DECL_THREADSAFE_ISUPPORTS
53 NS_DECL_NSIINPUTSTREAM
54 NS_DECL_NSISEEKABLESTREAM
55
56 private:
57 nsresult Init();
58
59 private: // data
60 ZipCentral mCentral;
61 nsString mFilename;
62 uint32_t mStart;
63 uint32_t mLength;
64
65 z_stream mZs;
66
67 enum {
68 NotStarted,
69 Started,
70 Done
71 } mStatus;
72
73 struct {
74 uint64_t parentSize;
75 nsCOMPtr<nsIInputStream> inputStream;
76
77 unsigned char input[ZIP_CHUNK];
78 uint32_t sizeToBeRead;
79 uint32_t cursor;
80
81 bool compressed; // a zip file can contain stored or compressed files
82 } mData;
83 };
84
85 NS_IMPL_ISUPPORTS(ArchiveInputStream,
86 nsIInputStream,
87 nsISeekableStream)
88
89 nsresult
90 ArchiveInputStream::Init()
91 {
92 nsresult rv;
93
94 memset(&mZs, 0, sizeof(z_stream));
95 int zerr = inflateInit2(&mZs, -MAX_WBITS);
96 if (zerr != Z_OK) {
97 return NS_ERROR_OUT_OF_MEMORY;
98 }
99
100 mData.sizeToBeRead = ArchiveZipItem::StrToInt32(mCentral.size);
101
102 uint32_t offset = ArchiveZipItem::StrToInt32(mCentral.localhdr_offset);
103
104 // The file is corrupt
105 if (offset + ZIPLOCAL_SIZE > mData.parentSize) {
106 return NS_ERROR_UNEXPECTED;
107 }
108
109 // From the input stream to a seekable stream
110 nsCOMPtr<nsISeekableStream> seekableStream;
111 seekableStream = do_QueryInterface(mData.inputStream);
112 if (!seekableStream) {
113 return NS_ERROR_UNEXPECTED;
114 }
115
116 // Seek + read the ZipLocal struct
117 seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, offset);
118 uint8_t buffer[ZIPLOCAL_SIZE];
119 uint32_t ret;
120
121 rv = mData.inputStream->Read((char*)buffer, ZIPLOCAL_SIZE, &ret);
122 if (NS_FAILED(rv) || ret != ZIPLOCAL_SIZE) {
123 return NS_ERROR_UNEXPECTED;
124 }
125
126 // Signature check:
127 if (ArchiveZipItem::StrToInt32(buffer) != LOCALSIG) {
128 return NS_ERROR_UNEXPECTED;
129 }
130
131 ZipLocal local;
132 memcpy(&local, buffer, ZIPLOCAL_SIZE);
133
134 // Seek to the real data:
135 offset += ZIPLOCAL_SIZE +
136 ArchiveZipItem::StrToInt16(local.filename_len) +
137 ArchiveZipItem::StrToInt16(local.extrafield_len);
138
139 // The file is corrupt if there is not enough data
140 if (offset + mData.sizeToBeRead > mData.parentSize) {
141 return NS_ERROR_UNEXPECTED;
142 }
143
144 // Data starts here:
145 seekableStream->Seek(nsISeekableStream::NS_SEEK_SET, offset);
146
147 // The file is compressed or not?
148 mData.compressed = (ArchiveZipItem::StrToInt16(mCentral.method) != 0);
149
150 // We have to skip the first mStart bytes:
151 if (mStart != 0) {
152 rv = Seek(NS_SEEK_SET, mStart);
153 NS_ENSURE_SUCCESS(rv, rv);
154 }
155
156 return NS_OK;
157 }
158
159 NS_IMETHODIMP
160 ArchiveInputStream::Close()
161 {
162 if (mStatus != NotStarted) {
163 inflateEnd(&mZs);
164 mStatus = NotStarted;
165 }
166
167 return NS_OK;
168 }
169
170 NS_IMETHODIMP
171 ArchiveInputStream::Available(uint64_t* _retval)
172 {
173 *_retval = mLength - mData.cursor - mStart;
174 return NS_OK;
175 }
176
177 NS_IMETHODIMP
178 ArchiveInputStream::Read(char* aBuffer,
179 uint32_t aCount,
180 uint32_t* _retval)
181 {
182 NS_ENSURE_ARG_POINTER(aBuffer);
183 NS_ENSURE_ARG_POINTER(_retval);
184
185 nsresult rv;
186
187 // This is the first time:
188 if (mStatus == NotStarted) {
189 mStatus = Started;
190
191 rv = Init();
192 if (NS_FAILED(rv)) {
193 return rv;
194 }
195
196 // Let's set avail_out to -1 so we read something from the stream.
197 mZs.avail_out = (uInt)-1;
198 }
199
200 // Nothing more can be read
201 if (mStatus == Done) {
202 *_retval = 0;
203 return NS_OK;
204 }
205
206 // Stored file:
207 if (!mData.compressed) {
208 rv = mData.inputStream->Read(aBuffer,
209 (mData.sizeToBeRead > aCount ?
210 aCount : mData.sizeToBeRead),
211 _retval);
212 if (NS_SUCCEEDED(rv)) {
213 mData.sizeToBeRead -= *_retval;
214 mData.cursor += *_retval;
215
216 if (mData.sizeToBeRead == 0) {
217 mStatus = Done;
218 }
219 }
220
221 return rv;
222 }
223
224 // We have nothing ready to be processed:
225 if (mZs.avail_out != 0 && mData.sizeToBeRead != 0) {
226 uint32_t ret;
227 rv = mData.inputStream->Read((char*)mData.input,
228 (mData.sizeToBeRead > sizeof(mData.input) ?
229 sizeof(mData.input) : mData.sizeToBeRead),
230 &ret);
231 if (NS_FAILED(rv)) {
232 return rv;
233 }
234
235 // Terminator:
236 if (ret == 0) {
237 *_retval = 0;
238 return NS_OK;
239 }
240
241 mData.sizeToBeRead -= ret;
242 mZs.avail_in = ret;
243 mZs.next_in = mData.input;
244 }
245
246 mZs.avail_out = aCount;
247 mZs.next_out = (unsigned char*)aBuffer;
248
249 int ret = inflate(&mZs, mData.sizeToBeRead ? Z_NO_FLUSH : Z_FINISH);
250 if (ret != Z_BUF_ERROR && ret != Z_OK && ret != Z_STREAM_END) {
251 return NS_ERROR_UNEXPECTED;
252 }
253
254 if (ret == Z_STREAM_END) {
255 mStatus = Done;
256 }
257
258 *_retval = aCount - mZs.avail_out;
259 mData.cursor += *_retval;
260 return NS_OK;
261 }
262
263 NS_IMETHODIMP
264 ArchiveInputStream::ReadSegments(nsWriteSegmentFun aWriter,
265 void* aClosure,
266 uint32_t aCount,
267 uint32_t* _retval)
268 {
269 // don't have a buffer to read from, so this better not be called!
270 NS_NOTREACHED("Consumers should be using Read()!");
271 return NS_ERROR_NOT_IMPLEMENTED;
272 }
273
274 NS_IMETHODIMP
275 ArchiveInputStream::IsNonBlocking(bool* _retval)
276 {
277 // We are blocking
278 *_retval = false;
279 return NS_OK;
280 }
281
282 NS_IMETHODIMP
283 ArchiveInputStream::Seek(int32_t aWhence, int64_t aOffset)
284 {
285 int64_t pos = aOffset;
286
287 switch (aWhence) {
288 case NS_SEEK_SET:
289 break;
290
291 case NS_SEEK_CUR:
292 pos += mData.cursor;
293 break;
294
295 case NS_SEEK_END:
296 pos += mLength;
297 break;
298
299 default:
300 NS_NOTREACHED("unexpected whence value");
301 return NS_ERROR_UNEXPECTED;
302 }
303
304 if (pos == int64_t(mData.cursor)) {
305 return NS_OK;
306 }
307
308 if (pos < 0 || pos >= mLength) {
309 return NS_ERROR_FAILURE;
310 }
311
312 // We have to terminate the previous operation:
313 nsresult rv;
314 if (mStatus != NotStarted) {
315 rv = Close();
316 NS_ENSURE_SUCCESS(rv, rv);
317 }
318
319 // Reset the cursor:
320 mData.cursor = 0;
321
322 // Note: This code is heavy but inflate does not have any seek() support:
323 uint32_t ret;
324 char buffer[1024];
325 while (pos > 0) {
326 rv = Read(buffer, pos > int64_t(sizeof(buffer)) ? sizeof(buffer) : pos, &ret);
327 if (NS_FAILED(rv)) {
328 return rv;
329 }
330
331 if (ret == 0) {
332 return NS_ERROR_UNEXPECTED;
333 }
334
335 pos -= ret;
336 }
337
338 return NS_OK;
339 }
340
341 NS_IMETHODIMP
342 ArchiveInputStream::Tell(int64_t *aResult)
343 {
344 *aResult = mData.cursor;
345 return NS_OK;
346 }
347
348 NS_IMETHODIMP
349 ArchiveInputStream::SetEOF()
350 {
351 return NS_ERROR_NOT_IMPLEMENTED;
352 }
353
354 // ArchiveZipFile
355
356 NS_IMETHODIMP
357 ArchiveZipFile::GetInternalStream(nsIInputStream** aStream)
358 {
359 if (mLength > INT32_MAX) {
360 return NS_ERROR_FAILURE;
361 }
362
363 uint64_t size;
364 nsresult rv = mArchiveReader->GetSize(&size);
365 NS_ENSURE_SUCCESS(rv, rv);
366
367 nsCOMPtr<nsIInputStream> inputStream;
368 rv = mArchiveReader->GetInputStream(getter_AddRefs(inputStream));
369 if (NS_FAILED(rv) || !inputStream) {
370 return NS_ERROR_UNEXPECTED;
371 }
372
373 nsRefPtr<ArchiveInputStream> stream = new ArchiveInputStream(size,
374 inputStream,
375 mFilename,
376 mStart,
377 mLength,
378 mCentral);
379 NS_ADDREF(stream);
380
381 *aStream = stream;
382 return NS_OK;
383 }
384
385 already_AddRefed<nsIDOMBlob>
386 ArchiveZipFile::CreateSlice(uint64_t aStart,
387 uint64_t aLength,
388 const nsAString& aContentType)
389 {
390 nsCOMPtr<nsIDOMBlob> t = new ArchiveZipFile(mFilename,
391 mContentType,
392 aStart,
393 mLength,
394 mCentral,
395 mArchiveReader);
396 return t.forget();
397 }
398
399 NS_IMPL_CYCLE_COLLECTION(ArchiveZipFile,
400 mArchiveReader)
401
402 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ArchiveZipFile)
403 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFile)
404 NS_INTERFACE_MAP_ENTRY(nsIDOMBlob)
405 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIDOMFile, mIsFile)
406 NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
407 NS_INTERFACE_MAP_ENTRY(nsIMutable)
408 NS_INTERFACE_MAP_END_INHERITING(nsDOMFileCC)
409
410 NS_IMPL_ADDREF_INHERITED(ArchiveZipFile, nsDOMFileCC)
411 NS_IMPL_RELEASE_INHERITED(ArchiveZipFile, nsDOMFileCC)

mercurial