]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: zipstrm.cpp | |
3 | // Purpose: Streams for Zip files | |
4 | // Author: Mike Wetherell | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) Mike Wetherell | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
11 | #pragma implementation "zipstrm.h" | |
12 | #endif | |
13 | ||
14 | // For compilers that support precompilation, includes "wx.h". | |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/defs.h" | |
23 | #endif | |
24 | ||
25 | #if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM | |
26 | ||
27 | #include "wx/zipstrm.h" | |
28 | #include "wx/log.h" | |
29 | #include "wx/intl.h" | |
30 | #include "wx/datstrm.h" | |
31 | #include "wx/zstream.h" | |
32 | #include "wx/mstream.h" | |
33 | #include "wx/utils.h" | |
34 | #include "wx/buffer.h" | |
35 | #include "wx/ptr_scpd.h" | |
36 | #include "wx/wfstream.h" | |
37 | #include "wx/html/forcelnk.h" | |
38 | #include "zlib.h" | |
39 | ||
40 | // value for the 'version needed to extract' field (20 means 2.0) | |
41 | enum { | |
42 | VERSION_NEEDED_TO_EXTRACT = 20 | |
43 | }; | |
44 | ||
45 | // signatures for the various records (PKxx) | |
46 | enum { | |
47 | CENTRAL_MAGIC = 0x02014b50, // central directory record | |
48 | LOCAL_MAGIC = 0x04034b50, // local header | |
49 | END_MAGIC = 0x06054b50, // end of central directory record | |
50 | SUMS_MAGIC = 0x08074b50 // data descriptor (info-zip) | |
51 | }; | |
52 | ||
53 | // unix file attributes. zip stores them in the high 16 bits of the | |
54 | // 'external attributes' field, hence the extra zeros. | |
55 | enum { | |
56 | wxZIP_S_IFMT = 0xF0000000, | |
57 | wxZIP_S_IFDIR = 0x40000000, | |
58 | wxZIP_S_IFREG = 0x80000000 | |
59 | }; | |
60 | ||
61 | // minimum sizes for the various records | |
62 | enum { | |
63 | CENTRAL_SIZE = 46, | |
64 | LOCAL_SIZE = 30, | |
65 | END_SIZE = 22, | |
66 | SUMS_SIZE = 12 | |
67 | }; | |
68 | ||
69 | // The number of bytes that must be written to an wxZipOutputStream before | |
70 | // a zip entry is created. The purpose of this latency is so that | |
71 | // OpenCompressor() can see a little data before deciding which compressor | |
72 | // it should use. | |
73 | enum { | |
74 | OUTPUT_LATENCY = 4096 | |
75 | }; | |
76 | ||
77 | // Some offsets into the local header | |
78 | enum { | |
79 | SUMS_OFFSET = 14 | |
80 | }; | |
81 | ||
82 | IMPLEMENT_DYNAMIC_CLASS(wxZipEntry, wxArchiveEntry) | |
83 | IMPLEMENT_DYNAMIC_CLASS(wxZipClassFactory, wxArchiveClassFactory) | |
84 | ||
85 | FORCE_LINK_ME(zipstrm) | |
86 | ||
87 | ||
88 | ///////////////////////////////////////////////////////////////////////////// | |
89 | // Helpers | |
90 | ||
91 | // read a string of a given length | |
92 | // | |
93 | static wxString ReadString(wxInputStream& stream, wxUint16 len, wxMBConv& conv) | |
94 | { | |
95 | #if wxUSE_UNICODE | |
96 | wxCharBuffer buf(len); | |
97 | stream.Read(buf.data(), len); | |
98 | wxString str(buf, conv); | |
99 | #else | |
100 | wxString str; | |
101 | (void)conv; | |
102 | { | |
103 | wxStringBuffer buf(str, len); | |
104 | stream.Read(buf, len); | |
105 | } | |
106 | #endif | |
107 | ||
108 | return str; | |
109 | } | |
110 | ||
111 | // Decode a little endian wxUint32 number from a character array | |
112 | // | |
113 | static inline wxUint32 CrackUint32(const char *m) | |
114 | { | |
115 | const unsigned char *n = (const unsigned char*)m; | |
116 | return (n[3] << 24) | (n[2] << 16) | (n[1] << 8) | n[0]; | |
117 | } | |
118 | ||
119 | // Temporarily lower the logging level in debug mode to avoid a warning | |
120 | // from SeekI about seeking on a stream with data written back to it. | |
121 | // | |
122 | static wxFileOffset QuietSeek(wxInputStream& stream, wxFileOffset pos) | |
123 | { | |
124 | #ifdef __WXDEBUG__ | |
125 | wxLogLevel level = wxLog::GetLogLevel(); | |
126 | wxLog::SetLogLevel(wxLOG_Debug - 1); | |
127 | wxFileOffset result = stream.SeekI(pos); | |
128 | wxLog::SetLogLevel(level); | |
129 | return result; | |
130 | #else | |
131 | return stream.SeekI(pos); | |
132 | #endif | |
133 | } | |
134 | ||
135 | ||
136 | ///////////////////////////////////////////////////////////////////////////// | |
137 | // Stored input stream | |
138 | // Trival decompressor for files which are 'stored' in the zip file. | |
139 | ||
140 | class wxStoredInputStream : public wxFilterInputStream | |
141 | { | |
142 | public: | |
143 | wxStoredInputStream(wxInputStream& stream); | |
144 | ||
145 | void Open(wxFileOffset len) { Close(); m_len = len; } | |
146 | void Close() { m_pos = 0; m_lasterror = wxSTREAM_NO_ERROR; } | |
147 | ||
148 | virtual char Peek() { return wxInputStream::Peek(); } | |
149 | virtual wxFileOffset GetLength() const { return m_len; } | |
150 | ||
151 | protected: | |
152 | virtual size_t OnSysRead(void *buffer, size_t size); | |
153 | virtual wxFileOffset OnSysTell() const { return m_pos; } | |
154 | ||
155 | private: | |
156 | wxFileOffset m_pos; | |
157 | wxFileOffset m_len; | |
158 | ||
159 | DECLARE_NO_COPY_CLASS(wxStoredInputStream) | |
160 | }; | |
161 | ||
162 | wxStoredInputStream::wxStoredInputStream(wxInputStream& stream) | |
163 | : wxFilterInputStream(stream), | |
164 | m_pos(0), | |
165 | m_len(0) | |
166 | { | |
167 | } | |
168 | ||
169 | size_t wxStoredInputStream::OnSysRead(void *buffer, size_t size) | |
170 | { | |
171 | size_t count = wxMin(size, (size_t)(m_len - m_pos)); | |
172 | count = m_parent_i_stream->Read(buffer, count).LastRead(); | |
173 | m_pos += count; | |
174 | ||
175 | if (m_pos == m_len) | |
176 | m_lasterror = wxSTREAM_EOF; | |
177 | else if (!*m_parent_i_stream) | |
178 | m_lasterror = wxSTREAM_READ_ERROR; | |
179 | ||
180 | return count; | |
181 | } | |
182 | ||
183 | ||
184 | ///////////////////////////////////////////////////////////////////////////// | |
185 | // Stored output stream | |
186 | // Trival compressor for files which are 'stored' in the zip file. | |
187 | ||
188 | class wxStoredOutputStream : public wxFilterOutputStream | |
189 | { | |
190 | public: | |
191 | wxStoredOutputStream(wxOutputStream& stream) : | |
192 | wxFilterOutputStream(stream), m_pos(0) { } | |
193 | ||
194 | bool Close() { | |
195 | m_pos = 0; | |
196 | m_lasterror = wxSTREAM_NO_ERROR; | |
197 | return true; | |
198 | } | |
199 | ||
200 | protected: | |
201 | virtual size_t OnSysWrite(const void *buffer, size_t size); | |
202 | virtual wxFileOffset OnSysTell() const { return m_pos; } | |
203 | ||
204 | private: | |
205 | wxFileOffset m_pos; | |
206 | DECLARE_NO_COPY_CLASS(wxStoredOutputStream) | |
207 | }; | |
208 | ||
209 | size_t wxStoredOutputStream::OnSysWrite(const void *buffer, size_t size) | |
210 | { | |
211 | if (!IsOk() || !size) | |
212 | return 0; | |
213 | size_t count = m_parent_o_stream->Write(buffer, size).LastWrite(); | |
214 | if (count != size) | |
215 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
216 | m_pos += count; | |
217 | return count; | |
218 | } | |
219 | ||
220 | ||
221 | ///////////////////////////////////////////////////////////////////////////// | |
222 | // wxRawInputStream | |
223 | // | |
224 | // Used to handle the unusal case of raw copying an entry of unknown | |
225 | // length. This can only happen when the zip being copied from is being | |
226 | // read from a non-seekable stream, and also was original written to a | |
227 | // non-seekable stream. | |
228 | // | |
229 | // In this case there's no option but to decompress the stream to find | |
230 | // it's length, but we can still write the raw compressed data to avoid the | |
231 | // compression overhead (which is the greater one). | |
232 | // | |
233 | // Usage is like this: | |
234 | // m_rawin = new wxRawInputStream(*m_parent_i_stream); | |
235 | // m_decomp = m_rawin->Open(OpenDecompressor(m_rawin->GetTee())); | |
236 | // | |
237 | // The wxRawInputStream owns a wxTeeInputStream object, the role of which | |
238 | // is something like the unix 'tee' command; it is a transparent filter, but | |
239 | // allows the data read to be read a second time via an extra method 'GetData'. | |
240 | // | |
241 | // The wxRawInputStream then draws data through the tee using a decompressor | |
242 | // then instead of returning the decompressed data, retuns the raw data | |
243 | // from wxTeeInputStream::GetData(). | |
244 | ||
245 | class wxTeeInputStream : public wxFilterInputStream | |
246 | { | |
247 | public: | |
248 | wxTeeInputStream(wxInputStream& stream); | |
249 | ||
250 | size_t GetCount() const { return m_end - m_start; } | |
251 | size_t GetData(char *buffer, size_t size); | |
252 | ||
253 | void Open(); | |
254 | bool Final(); | |
255 | ||
256 | wxInputStream& Read(void *buffer, size_t size); | |
257 | ||
258 | protected: | |
259 | virtual size_t OnSysRead(void *buffer, size_t size); | |
260 | virtual wxFileOffset OnSysTell() const { return m_pos; } | |
261 | ||
262 | private: | |
263 | wxFileOffset m_pos; | |
264 | wxMemoryBuffer m_buf; | |
265 | size_t m_start; | |
266 | size_t m_end; | |
267 | ||
268 | DECLARE_NO_COPY_CLASS(wxTeeInputStream) | |
269 | }; | |
270 | ||
271 | wxTeeInputStream::wxTeeInputStream(wxInputStream& stream) | |
272 | : wxFilterInputStream(stream), | |
273 | m_pos(0), m_buf(8192), m_start(0), m_end(0) | |
274 | { | |
275 | } | |
276 | ||
277 | void wxTeeInputStream::Open() | |
278 | { | |
279 | m_pos = m_start = m_end = 0; | |
280 | m_lasterror = wxSTREAM_NO_ERROR; | |
281 | } | |
282 | ||
283 | bool wxTeeInputStream::Final() | |
284 | { | |
285 | bool final = m_end == m_buf.GetDataLen(); | |
286 | m_end = m_buf.GetDataLen(); | |
287 | return final; | |
288 | } | |
289 | ||
290 | wxInputStream& wxTeeInputStream::Read(void *buffer, size_t size) | |
291 | { | |
292 | size_t count = wxInputStream::Read(buffer, size).LastRead(); | |
293 | m_end = m_buf.GetDataLen(); | |
294 | m_buf.AppendData(buffer, count); | |
295 | return *this; | |
296 | } | |
297 | ||
298 | size_t wxTeeInputStream::OnSysRead(void *buffer, size_t size) | |
299 | { | |
300 | size_t count = m_parent_i_stream->Read(buffer, size).LastRead(); | |
301 | m_lasterror = m_parent_i_stream->GetLastError(); | |
302 | return count; | |
303 | } | |
304 | ||
305 | size_t wxTeeInputStream::GetData(char *buffer, size_t size) | |
306 | { | |
307 | if (m_wbacksize) { | |
308 | size_t len = m_buf.GetDataLen(); | |
309 | len = len > m_wbacksize ? len - m_wbacksize : 0; | |
310 | m_buf.SetDataLen(len); | |
311 | if (m_end > len) { | |
312 | wxFAIL; // we've already returned data that's now being ungot | |
313 | m_end = len; | |
314 | } | |
315 | m_parent_i_stream->Ungetch(m_wback, m_wbacksize); | |
316 | free(m_wback); | |
317 | m_wback = NULL; | |
318 | m_wbacksize = 0; | |
319 | m_wbackcur = 0; | |
320 | } | |
321 | ||
322 | if (size > GetCount()) | |
323 | size = GetCount(); | |
324 | if (size) { | |
325 | memcpy(buffer, m_buf + m_start, size); | |
326 | m_start += size; | |
327 | wxASSERT(m_start <= m_end); | |
328 | } | |
329 | ||
330 | if (m_start == m_end && m_start > 0 && m_buf.GetDataLen() > 0) { | |
331 | size_t len = m_buf.GetDataLen(); | |
332 | char *buf = (char*)m_buf.GetWriteBuf(len); | |
333 | len -= m_end; | |
334 | memmove(buf, buf + m_end, len); | |
335 | m_buf.UngetWriteBuf(len); | |
336 | m_start = m_end = 0; | |
337 | } | |
338 | ||
339 | return size; | |
340 | } | |
341 | ||
342 | class wxRawInputStream : public wxFilterInputStream | |
343 | { | |
344 | public: | |
345 | wxRawInputStream(wxInputStream& stream); | |
346 | virtual ~wxRawInputStream() { delete m_tee; } | |
347 | ||
348 | wxInputStream* Open(wxInputStream *decomp); | |
349 | wxInputStream& GetTee() const { return *m_tee; } | |
350 | ||
351 | protected: | |
352 | virtual size_t OnSysRead(void *buffer, size_t size); | |
353 | virtual wxFileOffset OnSysTell() const { return m_pos; } | |
354 | ||
355 | private: | |
356 | wxFileOffset m_pos; | |
357 | wxTeeInputStream *m_tee; | |
358 | ||
359 | enum { BUFSIZE = 8192 }; | |
360 | wxCharBuffer m_dummy; | |
361 | ||
362 | DECLARE_NO_COPY_CLASS(wxRawInputStream) | |
363 | }; | |
364 | ||
365 | wxRawInputStream::wxRawInputStream(wxInputStream& stream) | |
366 | : wxFilterInputStream(stream), | |
367 | m_pos(0), | |
368 | m_tee(new wxTeeInputStream(stream)), | |
369 | m_dummy(BUFSIZE) | |
370 | { | |
371 | } | |
372 | ||
373 | wxInputStream *wxRawInputStream::Open(wxInputStream *decomp) | |
374 | { | |
375 | if (decomp) { | |
376 | m_parent_i_stream = decomp; | |
377 | m_pos = 0; | |
378 | m_lasterror = wxSTREAM_NO_ERROR; | |
379 | m_tee->Open(); | |
380 | return this; | |
381 | } else { | |
382 | return NULL; | |
383 | } | |
384 | } | |
385 | ||
386 | size_t wxRawInputStream::OnSysRead(void *buffer, size_t size) | |
387 | { | |
388 | char *buf = (char*)buffer; | |
389 | size_t count = 0; | |
390 | ||
391 | while (count < size && IsOk()) | |
392 | { | |
393 | while (m_parent_i_stream->IsOk() && m_tee->GetCount() == 0) | |
394 | m_parent_i_stream->Read(m_dummy.data(), BUFSIZE); | |
395 | ||
396 | size_t n = m_tee->GetData(buf + count, size - count); | |
397 | count += n; | |
398 | ||
399 | if (n == 0 && m_tee->Final()) | |
400 | m_lasterror = m_parent_i_stream->GetLastError(); | |
401 | } | |
402 | ||
403 | m_pos += count; | |
404 | return count; | |
405 | } | |
406 | ||
407 | ||
408 | ///////////////////////////////////////////////////////////////////////////// | |
409 | // Zlib streams than can be reused without recreating. | |
410 | ||
411 | class wxZlibOutputStream2 : public wxZlibOutputStream | |
412 | { | |
413 | public: | |
414 | wxZlibOutputStream2(wxOutputStream& stream, int level) : | |
415 | wxZlibOutputStream(stream, level, wxZLIB_NO_HEADER) { } | |
416 | ||
417 | bool Open(wxOutputStream& stream); | |
418 | bool Close() { DoFlush(true); m_pos = wxInvalidOffset; return IsOk(); } | |
419 | }; | |
420 | ||
421 | bool wxZlibOutputStream2::Open(wxOutputStream& stream) | |
422 | { | |
423 | wxCHECK(m_pos == wxInvalidOffset, false); | |
424 | ||
425 | m_deflate->next_out = m_z_buffer; | |
426 | m_deflate->avail_out = m_z_size; | |
427 | m_pos = 0; | |
428 | m_lasterror = wxSTREAM_NO_ERROR; | |
429 | m_parent_o_stream = &stream; | |
430 | ||
431 | if (deflateReset(m_deflate) != Z_OK) { | |
432 | wxLogError(_("can't re-initialize zlib deflate stream")); | |
433 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
434 | return false; | |
435 | } | |
436 | ||
437 | return true; | |
438 | } | |
439 | ||
440 | class wxZlibInputStream2 : public wxZlibInputStream | |
441 | { | |
442 | public: | |
443 | wxZlibInputStream2(wxInputStream& stream) : | |
444 | wxZlibInputStream(stream, wxZLIB_NO_HEADER) { } | |
445 | ||
446 | bool Open(wxInputStream& stream); | |
447 | }; | |
448 | ||
449 | bool wxZlibInputStream2::Open(wxInputStream& stream) | |
450 | { | |
451 | m_inflate->avail_in = 0; | |
452 | m_pos = 0; | |
453 | m_lasterror = wxSTREAM_NO_ERROR; | |
454 | m_parent_i_stream = &stream; | |
455 | ||
456 | if (inflateReset(m_inflate) != Z_OK) { | |
457 | wxLogError(_("can't re-initialize zlib inflate stream")); | |
458 | m_lasterror = wxSTREAM_READ_ERROR; | |
459 | return false; | |
460 | } | |
461 | ||
462 | return true; | |
463 | } | |
464 | ||
465 | ||
466 | ///////////////////////////////////////////////////////////////////////////// | |
467 | // Class to hold wxZipEntry's Extra and LocalExtra fields | |
468 | ||
469 | class wxZipMemory | |
470 | { | |
471 | public: | |
472 | wxZipMemory() : m_data(NULL), m_size(0), m_capacity(0), m_ref(1) { } | |
473 | ~wxZipMemory() { delete m_data; } | |
474 | ||
475 | wxZipMemory *AddRef() { m_ref++; return this; } | |
476 | void Release() { if (--m_ref == 0) delete this; } | |
477 | ||
478 | char *GetData() const { return m_data; } | |
479 | size_t GetSize() const { return m_size; } | |
480 | size_t GetCapacity() const { return m_capacity; } | |
481 | ||
482 | wxZipMemory *Unique(size_t size); | |
483 | ||
484 | private: | |
485 | ||
486 | char *m_data; | |
487 | size_t m_size; | |
488 | size_t m_capacity; | |
489 | int m_ref; | |
490 | }; | |
491 | ||
492 | wxZipMemory *wxZipMemory::Unique(size_t size) | |
493 | { | |
494 | wxZipMemory *zm; | |
495 | ||
496 | if (m_ref > 1) { | |
497 | --m_ref; | |
498 | zm = new wxZipMemory; | |
499 | } else { | |
500 | zm = this; | |
501 | } | |
502 | ||
503 | if (zm->m_capacity < size) { | |
504 | delete zm->m_data; | |
505 | zm->m_data = new char[size]; | |
506 | zm->m_capacity = size; | |
507 | } | |
508 | ||
509 | zm->m_size = size; | |
510 | return zm; | |
511 | } | |
512 | ||
513 | static inline wxZipMemory *AddRef(wxZipMemory *zm) | |
514 | { | |
515 | if (zm) | |
516 | zm->AddRef(); | |
517 | return zm; | |
518 | } | |
519 | ||
520 | static inline void Release(wxZipMemory *zm) | |
521 | { | |
522 | if (zm) | |
523 | zm->Release(); | |
524 | } | |
525 | ||
526 | static void Copy(wxZipMemory*& dest, wxZipMemory *src) | |
527 | { | |
528 | Release(dest); | |
529 | dest = AddRef(src); | |
530 | } | |
531 | ||
532 | static void Unique(wxZipMemory*& zm, size_t size) | |
533 | { | |
534 | if (!zm && size) | |
535 | zm = new wxZipMemory; | |
536 | if (zm) | |
537 | zm = zm->Unique(size); | |
538 | } | |
539 | ||
540 | ||
541 | ///////////////////////////////////////////////////////////////////////////// | |
542 | // Collection of weak references to entries | |
543 | ||
544 | WX_DECLARE_HASH_MAP(long, wxZipEntry*, wxIntegerHash, | |
545 | wxIntegerEqual, _wxOffsetZipEntryMap); | |
546 | ||
547 | class wxZipWeakLinks | |
548 | { | |
549 | public: | |
550 | wxZipWeakLinks() : m_ref(1) { } | |
551 | ~wxZipWeakLinks() { wxASSERT(IsEmpty()); } | |
552 | ||
553 | void Release(const wxZipInputStream* WXUNUSED(x)) | |
554 | { if (--m_ref == 0) delete this; } | |
555 | void Release(wxFileOffset key) | |
556 | { RemoveEntry(key); if (--m_ref == 0) delete this; } | |
557 | ||
558 | wxZipWeakLinks *AddEntry(wxZipEntry *entry, wxFileOffset key); | |
559 | void RemoveEntry(wxFileOffset key) | |
560 | { m_entries.erase((_wxOffsetZipEntryMap::key_type)key); } | |
561 | wxZipEntry *GetEntry(wxFileOffset key) const; | |
562 | bool IsEmpty() const { return m_entries.empty(); } | |
563 | ||
564 | private: | |
565 | ||
566 | int m_ref; | |
567 | _wxOffsetZipEntryMap m_entries; | |
568 | }; | |
569 | ||
570 | wxZipWeakLinks *wxZipWeakLinks::AddEntry(wxZipEntry *entry, wxFileOffset key) | |
571 | { | |
572 | m_entries[(_wxOffsetZipEntryMap::key_type)key] = entry; | |
573 | m_ref++; | |
574 | return this; | |
575 | } | |
576 | ||
577 | wxZipEntry *wxZipWeakLinks::GetEntry(wxFileOffset key) const | |
578 | { | |
579 | _wxOffsetZipEntryMap::const_iterator it = | |
580 | m_entries.find((_wxOffsetZipEntryMap::key_type)key); | |
581 | return it != m_entries.end() ? it->second : NULL; | |
582 | } | |
583 | ||
584 | ||
585 | ///////////////////////////////////////////////////////////////////////////// | |
586 | // ZipEntry | |
587 | ||
588 | wxZipEntry::wxZipEntry( | |
589 | const wxString& name /*=wxEmptyString*/, | |
590 | const wxDateTime& dt /*=wxDateTime::Now()*/, | |
591 | wxFileOffset size /*=wxInvalidOffset*/) | |
592 | : | |
593 | m_SystemMadeBy(wxZIP_SYSTEM_MSDOS), | |
594 | m_VersionMadeBy(wxMAJOR_VERSION * 10 + wxMINOR_VERSION), | |
595 | m_VersionNeeded(VERSION_NEEDED_TO_EXTRACT), | |
596 | m_Flags(0), | |
597 | m_Method(wxZIP_METHOD_DEFAULT), | |
598 | m_DateTime(dt), | |
599 | m_Crc(0), | |
600 | m_CompressedSize(wxInvalidOffset), | |
601 | m_Size(size), | |
602 | m_Key(wxInvalidOffset), | |
603 | m_Offset(wxInvalidOffset), | |
604 | m_DiskStart(0), | |
605 | m_InternalAttributes(0), | |
606 | m_ExternalAttributes(0), | |
607 | m_Extra(NULL), | |
608 | m_LocalExtra(NULL), | |
609 | m_zipnotifier(NULL), | |
610 | m_backlink(NULL) | |
611 | { | |
612 | if (!name.empty()) | |
613 | SetName(name); | |
614 | } | |
615 | ||
616 | wxZipEntry::~wxZipEntry() | |
617 | { | |
618 | if (m_backlink) | |
619 | m_backlink->Release(m_Key); | |
620 | Release(m_Extra); | |
621 | Release(m_LocalExtra); | |
622 | } | |
623 | ||
624 | wxZipEntry::wxZipEntry(const wxZipEntry& e) | |
625 | : m_SystemMadeBy(e.m_SystemMadeBy), | |
626 | m_VersionMadeBy(e.m_VersionMadeBy), | |
627 | m_VersionNeeded(e.m_VersionNeeded), | |
628 | m_Flags(e.m_Flags), | |
629 | m_Method(e.m_Method), | |
630 | m_DateTime(e.m_DateTime), | |
631 | m_Crc(e.m_Crc), | |
632 | m_CompressedSize(e.m_CompressedSize), | |
633 | m_Size(e.m_Size), | |
634 | m_Name(e.m_Name), | |
635 | m_Key(e.m_Key), | |
636 | m_Offset(e.m_Offset), | |
637 | m_Comment(e.m_Comment), | |
638 | m_DiskStart(e.m_DiskStart), | |
639 | m_InternalAttributes(e.m_InternalAttributes), | |
640 | m_ExternalAttributes(e.m_ExternalAttributes), | |
641 | m_Extra(AddRef(e.m_Extra)), | |
642 | m_LocalExtra(AddRef(e.m_LocalExtra)), | |
643 | m_zipnotifier(e.m_zipnotifier), | |
644 | m_backlink(NULL) | |
645 | { | |
646 | } | |
647 | ||
648 | wxZipEntry& wxZipEntry::operator=(const wxZipEntry& e) | |
649 | { | |
650 | if (&e != this) { | |
651 | m_SystemMadeBy = e.m_SystemMadeBy; | |
652 | m_VersionMadeBy = e.m_VersionMadeBy; | |
653 | m_VersionNeeded = e.m_VersionNeeded; | |
654 | m_Flags = e.m_Flags; | |
655 | m_Method = e.m_Method; | |
656 | m_DateTime = e.m_DateTime; | |
657 | m_Crc = e.m_Crc; | |
658 | m_CompressedSize = e.m_CompressedSize; | |
659 | m_Size = e.m_Size; | |
660 | m_Name = e.m_Name; | |
661 | m_Key = e.m_Key; | |
662 | m_Offset = e.m_Offset; | |
663 | m_Comment = e.m_Comment; | |
664 | m_DiskStart = e.m_DiskStart; | |
665 | m_InternalAttributes = e.m_InternalAttributes; | |
666 | m_ExternalAttributes = e.m_ExternalAttributes; | |
667 | Copy(m_Extra, e.m_Extra); | |
668 | Copy(m_LocalExtra, e.m_LocalExtra); | |
669 | m_zipnotifier = e.m_zipnotifier; | |
670 | if (m_backlink) { | |
671 | m_backlink->Release(m_Key); | |
672 | m_backlink = NULL; | |
673 | } | |
674 | } | |
675 | return *this; | |
676 | } | |
677 | ||
678 | wxString wxZipEntry::GetName(wxPathFormat format /*=wxPATH_NATIVE*/) const | |
679 | { | |
680 | bool isDir = IsDir() && !m_Name.empty(); | |
681 | ||
682 | switch (wxFileName::GetFormat(format)) { | |
683 | case wxPATH_DOS: | |
684 | { | |
685 | wxString name(isDir ? m_Name + _T("\\") : m_Name); | |
686 | for (size_t i = name.length() - 1; i > 0; --i) | |
687 | if (name[i] == _T('/')) | |
688 | name[i] = _T('\\'); | |
689 | return name; | |
690 | } | |
691 | ||
692 | case wxPATH_UNIX: | |
693 | return isDir ? m_Name + _T("/") : m_Name; | |
694 | ||
695 | default: | |
696 | ; | |
697 | } | |
698 | ||
699 | wxFileName fn; | |
700 | ||
701 | if (isDir) | |
702 | fn.AssignDir(m_Name, wxPATH_UNIX); | |
703 | else | |
704 | fn.Assign(m_Name, wxPATH_UNIX); | |
705 | ||
706 | return fn.GetFullPath(format); | |
707 | } | |
708 | ||
709 | // Static - Internally tars and zips use forward slashes for the path | |
710 | // separator, absolute paths aren't allowed, and directory names have a | |
711 | // trailing slash. This function converts a path into this internal format, | |
712 | // but without a trailing slash for a directory. | |
713 | // | |
714 | wxString wxZipEntry::GetInternalName(const wxString& name, | |
715 | wxPathFormat format /*=wxPATH_NATIVE*/, | |
716 | bool *pIsDir /*=NULL*/) | |
717 | { | |
718 | wxString internal; | |
719 | ||
720 | if (wxFileName::GetFormat(format) != wxPATH_UNIX) | |
721 | internal = wxFileName(name, format).GetFullPath(wxPATH_UNIX); | |
722 | else | |
723 | internal = name; | |
724 | ||
725 | bool isDir = !internal.empty() && internal.Last() == '/'; | |
726 | if (pIsDir) | |
727 | *pIsDir = isDir; | |
728 | if (isDir) | |
729 | internal.erase(internal.length() - 1); | |
730 | ||
731 | while (!internal.empty() && *internal.begin() == '/') | |
732 | internal.erase(0, 1); | |
733 | while (!internal.empty() && internal.compare(0, 2, _T("./")) == 0) | |
734 | internal.erase(0, 2); | |
735 | if (internal == _T(".") || internal == _T("..")) | |
736 | internal = wxEmptyString; | |
737 | ||
738 | return internal; | |
739 | } | |
740 | ||
741 | void wxZipEntry::SetSystemMadeBy(int system) | |
742 | { | |
743 | int mode = GetMode(); | |
744 | bool wasUnix = IsMadeByUnix(); | |
745 | ||
746 | m_SystemMadeBy = (wxUint8)system; | |
747 | ||
748 | if (!wasUnix && IsMadeByUnix()) { | |
749 | SetIsDir(IsDir()); | |
750 | SetMode(mode); | |
751 | } else if (wasUnix && !IsMadeByUnix()) { | |
752 | m_ExternalAttributes &= 0xffff; | |
753 | } | |
754 | } | |
755 | ||
756 | void wxZipEntry::SetIsDir(bool isDir /*=true*/) | |
757 | { | |
758 | if (isDir) | |
759 | m_ExternalAttributes |= wxZIP_A_SUBDIR; | |
760 | else | |
761 | m_ExternalAttributes &= ~wxZIP_A_SUBDIR; | |
762 | ||
763 | if (IsMadeByUnix()) { | |
764 | m_ExternalAttributes &= ~wxZIP_S_IFMT; | |
765 | if (isDir) | |
766 | m_ExternalAttributes |= wxZIP_S_IFDIR; | |
767 | else | |
768 | m_ExternalAttributes |= wxZIP_S_IFREG; | |
769 | } | |
770 | } | |
771 | ||
772 | // Return unix style permission bits | |
773 | // | |
774 | int wxZipEntry::GetMode() const | |
775 | { | |
776 | // return unix permissions if present | |
777 | if (IsMadeByUnix()) | |
778 | return (m_ExternalAttributes >> 16) & 0777; | |
779 | ||
780 | // otherwise synthesize from the dos attribs | |
781 | int mode = 0644; | |
782 | if (m_ExternalAttributes & wxZIP_A_RDONLY) | |
783 | mode &= ~0200; | |
784 | if (m_ExternalAttributes & wxZIP_A_SUBDIR) | |
785 | mode |= 0111; | |
786 | ||
787 | return mode; | |
788 | } | |
789 | ||
790 | // Set unix permissions | |
791 | // | |
792 | void wxZipEntry::SetMode(int mode) | |
793 | { | |
794 | // Set dos attrib bits to be compatible | |
795 | if (mode & 0222) | |
796 | m_ExternalAttributes &= ~wxZIP_A_RDONLY; | |
797 | else | |
798 | m_ExternalAttributes |= wxZIP_A_RDONLY; | |
799 | ||
800 | // set the actual unix permission bits if the system type allows | |
801 | if (IsMadeByUnix()) { | |
802 | m_ExternalAttributes &= ~(0777L << 16); | |
803 | m_ExternalAttributes |= (mode & 0777L) << 16; | |
804 | } | |
805 | } | |
806 | ||
807 | const char *wxZipEntry::GetExtra() const | |
808 | { | |
809 | return m_Extra ? m_Extra->GetData() : NULL; | |
810 | } | |
811 | ||
812 | size_t wxZipEntry::GetExtraLen() const | |
813 | { | |
814 | return m_Extra ? m_Extra->GetSize() : 0; | |
815 | } | |
816 | ||
817 | void wxZipEntry::SetExtra(const char *extra, size_t len) | |
818 | { | |
819 | Unique(m_Extra, len); | |
820 | if (len) | |
821 | memcpy(m_Extra->GetData(), extra, len); | |
822 | } | |
823 | ||
824 | const char *wxZipEntry::GetLocalExtra() const | |
825 | { | |
826 | return m_LocalExtra ? m_LocalExtra->GetData() : NULL; | |
827 | } | |
828 | ||
829 | size_t wxZipEntry::GetLocalExtraLen() const | |
830 | { | |
831 | return m_LocalExtra ? m_LocalExtra->GetSize() : 0; | |
832 | } | |
833 | ||
834 | void wxZipEntry::SetLocalExtra(const char *extra, size_t len) | |
835 | { | |
836 | Unique(m_LocalExtra, len); | |
837 | if (len) | |
838 | memcpy(m_LocalExtra->GetData(), extra, len); | |
839 | } | |
840 | ||
841 | void wxZipEntry::SetNotifier(wxZipNotifier& notifier) | |
842 | { | |
843 | wxArchiveEntry::UnsetNotifier(); | |
844 | m_zipnotifier = ¬ifier; | |
845 | m_zipnotifier->OnEntryUpdated(*this); | |
846 | } | |
847 | ||
848 | void wxZipEntry::Notify() | |
849 | { | |
850 | if (m_zipnotifier) | |
851 | m_zipnotifier->OnEntryUpdated(*this); | |
852 | else if (GetNotifier()) | |
853 | GetNotifier()->OnEntryUpdated(*this); | |
854 | } | |
855 | ||
856 | void wxZipEntry::UnsetNotifier() | |
857 | { | |
858 | wxArchiveEntry::UnsetNotifier(); | |
859 | m_zipnotifier = NULL; | |
860 | } | |
861 | ||
862 | size_t wxZipEntry::ReadLocal(wxInputStream& stream, wxMBConv& conv) | |
863 | { | |
864 | wxUint16 nameLen, extraLen; | |
865 | wxUint32 compressedSize, size, crc; | |
866 | ||
867 | wxDataInputStream ds(stream); | |
868 | ||
869 | ds >> m_VersionNeeded >> m_Flags >> m_Method; | |
870 | SetDateTime(wxDateTime().SetFromDOS(ds.Read32())); | |
871 | ds >> crc >> compressedSize >> size >> nameLen >> extraLen; | |
872 | ||
873 | bool sumsValid = (m_Flags & wxZIP_SUMS_FOLLOW) == 0; | |
874 | ||
875 | if (sumsValid || crc) | |
876 | m_Crc = crc; | |
877 | if ((sumsValid || compressedSize) || m_Method == wxZIP_METHOD_STORE) | |
878 | m_CompressedSize = compressedSize; | |
879 | if ((sumsValid || size) || m_Method == wxZIP_METHOD_STORE) | |
880 | m_Size = size; | |
881 | ||
882 | SetName(ReadString(stream, nameLen, conv), wxPATH_UNIX); | |
883 | ||
884 | if (extraLen || GetLocalExtraLen()) { | |
885 | Unique(m_LocalExtra, extraLen); | |
886 | if (extraLen) | |
887 | stream.Read(m_LocalExtra->GetData(), extraLen); | |
888 | } | |
889 | ||
890 | return LOCAL_SIZE + nameLen + extraLen; | |
891 | } | |
892 | ||
893 | size_t wxZipEntry::WriteLocal(wxOutputStream& stream, wxMBConv& conv) const | |
894 | { | |
895 | wxString unixName = GetName(wxPATH_UNIX); | |
896 | const wxWX2MBbuf name_buf = conv.cWX2MB(unixName); | |
897 | const char *name = name_buf; | |
898 | if (!name) name = ""; | |
899 | wxUint16 nameLen = (wxUint16)strlen(name); | |
900 | ||
901 | wxDataOutputStream ds(stream); | |
902 | ||
903 | ds << m_VersionNeeded << m_Flags << m_Method; | |
904 | ds.Write32(GetDateTime().GetAsDOS()); | |
905 | ||
906 | ds.Write32(m_Crc); | |
907 | ds.Write32(m_CompressedSize != wxInvalidOffset ? (wxUint32)m_CompressedSize : 0); | |
908 | ds.Write32(m_Size != wxInvalidOffset ? (wxUint32)m_Size : 0); | |
909 | ||
910 | ds << nameLen; | |
911 | wxUint16 extraLen = (wxUint16)GetLocalExtraLen(); | |
912 | ds.Write16(extraLen); | |
913 | ||
914 | stream.Write(name, nameLen); | |
915 | if (extraLen) | |
916 | stream.Write(m_LocalExtra->GetData(), extraLen); | |
917 | ||
918 | return LOCAL_SIZE + nameLen + extraLen; | |
919 | } | |
920 | ||
921 | size_t wxZipEntry::ReadCentral(wxInputStream& stream, wxMBConv& conv) | |
922 | { | |
923 | wxUint16 nameLen, extraLen, commentLen; | |
924 | ||
925 | wxDataInputStream ds(stream); | |
926 | ||
927 | ds >> m_VersionMadeBy >> m_SystemMadeBy; | |
928 | ||
929 | SetVersionNeeded(ds.Read16()); | |
930 | SetFlags(ds.Read16()); | |
931 | SetMethod(ds.Read16()); | |
932 | SetDateTime(wxDateTime().SetFromDOS(ds.Read32())); | |
933 | SetCrc(ds.Read32()); | |
934 | SetCompressedSize(ds.Read32()); | |
935 | SetSize(ds.Read32()); | |
936 | ||
937 | ds >> nameLen >> extraLen >> commentLen | |
938 | >> m_DiskStart >> m_InternalAttributes >> m_ExternalAttributes; | |
939 | SetOffset(ds.Read32()); | |
940 | ||
941 | SetName(ReadString(stream, nameLen, conv), wxPATH_UNIX); | |
942 | ||
943 | if (extraLen || GetExtraLen()) { | |
944 | Unique(m_Extra, extraLen); | |
945 | if (extraLen) | |
946 | stream.Read(m_Extra->GetData(), extraLen); | |
947 | } | |
948 | ||
949 | if (commentLen) | |
950 | m_Comment = ReadString(stream, commentLen, conv); | |
951 | else | |
952 | m_Comment.clear(); | |
953 | ||
954 | return CENTRAL_SIZE + nameLen + extraLen + commentLen; | |
955 | } | |
956 | ||
957 | size_t wxZipEntry::WriteCentral(wxOutputStream& stream, wxMBConv& conv) const | |
958 | { | |
959 | wxString unixName = GetName(wxPATH_UNIX); | |
960 | const wxWX2MBbuf name_buf = conv.cWX2MB(unixName); | |
961 | const char *name = name_buf; | |
962 | if (!name) name = ""; | |
963 | wxUint16 nameLen = (wxUint16)strlen(name); | |
964 | ||
965 | const wxWX2MBbuf comment_buf = conv.cWX2MB(m_Comment); | |
966 | const char *comment = comment_buf; | |
967 | if (!comment) comment = ""; | |
968 | wxUint16 commentLen = (wxUint16)strlen(comment); | |
969 | ||
970 | wxUint16 extraLen = (wxUint16)GetExtraLen(); | |
971 | ||
972 | wxDataOutputStream ds(stream); | |
973 | ||
974 | ds << CENTRAL_MAGIC << m_VersionMadeBy << m_SystemMadeBy; | |
975 | ||
976 | ds.Write16((wxUint16)GetVersionNeeded()); | |
977 | ds.Write16((wxUint16)GetFlags()); | |
978 | ds.Write16((wxUint16)GetMethod()); | |
979 | ds.Write32(GetDateTime().GetAsDOS()); | |
980 | ds.Write32(GetCrc()); | |
981 | ds.Write32((wxUint32)GetCompressedSize()); | |
982 | ds.Write32((wxUint32)GetSize()); | |
983 | ds.Write16(nameLen); | |
984 | ds.Write16(extraLen); | |
985 | ||
986 | ds << commentLen << m_DiskStart << m_InternalAttributes | |
987 | << m_ExternalAttributes << (wxUint32)GetOffset(); | |
988 | ||
989 | stream.Write(name, nameLen); | |
990 | if (extraLen) | |
991 | stream.Write(GetExtra(), extraLen); | |
992 | stream.Write(comment, commentLen); | |
993 | ||
994 | return CENTRAL_SIZE + nameLen + extraLen + commentLen; | |
995 | } | |
996 | ||
997 | // Info-zip prefixes this record with a signature, but pkzip doesn't. So if | |
998 | // the 1st value is the signature then it is probably an info-zip record, | |
999 | // though there is a small chance that it is in fact a pkzip record which | |
1000 | // happens to have the signature as it's CRC. | |
1001 | // | |
1002 | size_t wxZipEntry::ReadDescriptor(wxInputStream& stream) | |
1003 | { | |
1004 | wxDataInputStream ds(stream); | |
1005 | ||
1006 | m_Crc = ds.Read32(); | |
1007 | m_CompressedSize = ds.Read32(); | |
1008 | m_Size = ds.Read32(); | |
1009 | ||
1010 | // if 1st value is the signature then this is probably an info-zip record | |
1011 | if (m_Crc == SUMS_MAGIC) | |
1012 | { | |
1013 | char buf[8]; | |
1014 | stream.Read(buf, sizeof(buf)); | |
1015 | wxUint32 u1 = CrackUint32(buf); | |
1016 | wxUint32 u2 = CrackUint32(buf + 4); | |
1017 | ||
1018 | // look for the signature of the following record to decide which | |
1019 | if ((u1 == LOCAL_MAGIC || u1 == CENTRAL_MAGIC) && | |
1020 | (u2 != LOCAL_MAGIC && u2 != CENTRAL_MAGIC)) | |
1021 | { | |
1022 | // it's a pkzip style record after all! | |
1023 | stream.Ungetch(buf, sizeof(buf)); | |
1024 | } | |
1025 | else | |
1026 | { | |
1027 | // it's an info-zip record as expected | |
1028 | stream.Ungetch(buf + 4, sizeof(buf) - 4); | |
1029 | m_Crc = (wxUint32)m_CompressedSize; | |
1030 | m_CompressedSize = m_Size; | |
1031 | m_Size = u1; | |
1032 | return SUMS_SIZE + 4; | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | return SUMS_SIZE; | |
1037 | } | |
1038 | ||
1039 | size_t wxZipEntry::WriteDescriptor(wxOutputStream& stream, wxUint32 crc, | |
1040 | wxFileOffset compressedSize, wxFileOffset size) | |
1041 | { | |
1042 | m_Crc = crc; | |
1043 | m_CompressedSize = compressedSize; | |
1044 | m_Size = size; | |
1045 | ||
1046 | wxDataOutputStream ds(stream); | |
1047 | ||
1048 | ds.Write32(crc); | |
1049 | ds.Write32((wxUint32)compressedSize); | |
1050 | ds.Write32((wxUint32)size); | |
1051 | ||
1052 | return SUMS_SIZE; | |
1053 | } | |
1054 | ||
1055 | ||
1056 | ///////////////////////////////////////////////////////////////////////////// | |
1057 | // wxZipEndRec - holds the end of central directory record | |
1058 | ||
1059 | class wxZipEndRec | |
1060 | { | |
1061 | public: | |
1062 | wxZipEndRec(); | |
1063 | ||
1064 | int GetDiskNumber() const { return m_DiskNumber; } | |
1065 | int GetStartDisk() const { return m_StartDisk; } | |
1066 | int GetEntriesHere() const { return m_EntriesHere; } | |
1067 | int GetTotalEntries() const { return m_TotalEntries; } | |
1068 | wxFileOffset GetSize() const { return m_Size; } | |
1069 | wxFileOffset GetOffset() const { return m_Offset; } | |
1070 | wxString GetComment() const { return m_Comment; } | |
1071 | ||
1072 | void SetDiskNumber(int num) { m_DiskNumber = (wxUint16)num; } | |
1073 | void SetStartDisk(int num) { m_StartDisk = (wxUint16)num; } | |
1074 | void SetEntriesHere(int num) { m_EntriesHere = (wxUint16)num; } | |
1075 | void SetTotalEntries(int num) { m_TotalEntries = (wxUint16)num; } | |
1076 | void SetSize(wxFileOffset size) { m_Size = (wxUint32)size; } | |
1077 | void SetOffset(wxFileOffset offset) { m_Offset = (wxUint32)offset; } | |
1078 | void SetComment(const wxString& comment) { m_Comment = comment; } | |
1079 | ||
1080 | bool Read(wxInputStream& stream, wxMBConv& conv); | |
1081 | bool Write(wxOutputStream& stream, wxMBConv& conv) const; | |
1082 | ||
1083 | private: | |
1084 | wxUint16 m_DiskNumber; | |
1085 | wxUint16 m_StartDisk; | |
1086 | wxUint16 m_EntriesHere; | |
1087 | wxUint16 m_TotalEntries; | |
1088 | wxUint32 m_Size; | |
1089 | wxUint32 m_Offset; | |
1090 | wxString m_Comment; | |
1091 | }; | |
1092 | ||
1093 | wxZipEndRec::wxZipEndRec() | |
1094 | : m_DiskNumber(0), | |
1095 | m_StartDisk(0), | |
1096 | m_EntriesHere(0), | |
1097 | m_TotalEntries(0), | |
1098 | m_Size(0), | |
1099 | m_Offset(0) | |
1100 | { | |
1101 | } | |
1102 | ||
1103 | bool wxZipEndRec::Write(wxOutputStream& stream, wxMBConv& conv) const | |
1104 | { | |
1105 | const wxWX2MBbuf comment_buf = conv.cWX2MB(m_Comment); | |
1106 | const char *comment = comment_buf; | |
1107 | if (!comment) comment = ""; | |
1108 | wxUint16 commentLen = (wxUint16)strlen(comment); | |
1109 | ||
1110 | wxDataOutputStream ds(stream); | |
1111 | ||
1112 | ds << END_MAGIC << m_DiskNumber << m_StartDisk << m_EntriesHere | |
1113 | << m_TotalEntries << m_Size << m_Offset << commentLen; | |
1114 | ||
1115 | stream.Write(comment, commentLen); | |
1116 | ||
1117 | return stream.IsOk(); | |
1118 | } | |
1119 | ||
1120 | bool wxZipEndRec::Read(wxInputStream& stream, wxMBConv& conv) | |
1121 | { | |
1122 | wxDataInputStream ds(stream); | |
1123 | wxUint16 commentLen; | |
1124 | ||
1125 | ds >> m_DiskNumber >> m_StartDisk >> m_EntriesHere | |
1126 | >> m_TotalEntries >> m_Size >> m_Offset >> commentLen; | |
1127 | ||
1128 | if (commentLen) | |
1129 | m_Comment = ReadString(stream, commentLen, conv); | |
1130 | ||
1131 | if (stream.IsOk()) | |
1132 | if (m_DiskNumber == 0 && m_StartDisk == 0 && | |
1133 | m_EntriesHere == m_TotalEntries) | |
1134 | return true; | |
1135 | else | |
1136 | wxLogError(_("unsupported zip archive")); | |
1137 | ||
1138 | return false; | |
1139 | } | |
1140 | ||
1141 | ||
1142 | ///////////////////////////////////////////////////////////////////////////// | |
1143 | // A weak link from an input stream to an output stream | |
1144 | ||
1145 | class wxZipStreamLink | |
1146 | { | |
1147 | public: | |
1148 | wxZipStreamLink(wxZipOutputStream *stream) : m_ref(1), m_stream(stream) { } | |
1149 | ~wxZipStreamLink() { } | |
1150 | ||
1151 | wxZipStreamLink *AddRef() { m_ref++; return this; } | |
1152 | wxZipOutputStream *GetOutputStream() const { return m_stream; } | |
1153 | ||
1154 | void Release(class wxZipInputStream *WXUNUSED(s)) | |
1155 | { if (--m_ref == 0) delete this; } | |
1156 | void Release(class wxZipOutputStream *WXUNUSED(s)) | |
1157 | { m_stream = NULL; if (--m_ref == 0) delete this; } | |
1158 | ||
1159 | private: | |
1160 | ||
1161 | int m_ref; | |
1162 | wxZipOutputStream *m_stream; | |
1163 | }; | |
1164 | ||
1165 | ||
1166 | ///////////////////////////////////////////////////////////////////////////// | |
1167 | // Input stream | |
1168 | ||
1169 | wxDECLARE_SCOPED_PTR(wxZipEntry, _wxZipEntryPtr) | |
1170 | wxDEFINE_SCOPED_PTR (wxZipEntry, _wxZipEntryPtr) | |
1171 | ||
1172 | // constructor | |
1173 | // | |
1174 | wxZipInputStream::wxZipInputStream(wxInputStream& stream, | |
1175 | wxMBConv& conv /*=wxConvLocal*/) | |
1176 | : wxArchiveInputStream(stream, conv) | |
1177 | { | |
1178 | m_ffile = NULL; | |
1179 | Init(); | |
1180 | } | |
1181 | ||
1182 | // Compatibility constructor | |
1183 | // | |
1184 | wxZipInputStream::wxZipInputStream(const wxString& archive, | |
1185 | const wxString& file) | |
1186 | : wxArchiveInputStream(OpenFile(archive), wxConvLocal) | |
1187 | { | |
1188 | // no error messages | |
1189 | wxLogNull nolog; | |
1190 | Init(); | |
1191 | _wxZipEntryPtr entry; | |
1192 | ||
1193 | if (m_ffile->Ok()) { | |
1194 | do { | |
1195 | entry.reset(GetNextEntry()); | |
1196 | } | |
1197 | while (entry.get() != NULL && entry->GetInternalName() != file); | |
1198 | } | |
1199 | ||
1200 | if (entry.get() == NULL) | |
1201 | m_lasterror = wxSTREAM_READ_ERROR; | |
1202 | } | |
1203 | ||
1204 | wxInputStream& wxZipInputStream::OpenFile(const wxString& archive) | |
1205 | { | |
1206 | wxLogNull nolog; | |
1207 | m_ffile = new wxFFileInputStream(archive); | |
1208 | return *m_ffile; | |
1209 | } | |
1210 | ||
1211 | void wxZipInputStream::Init() | |
1212 | { | |
1213 | m_store = new wxStoredInputStream(*m_parent_i_stream); | |
1214 | m_inflate = NULL; | |
1215 | m_rawin = NULL; | |
1216 | m_raw = false; | |
1217 | m_headerSize = 0; | |
1218 | m_decomp = NULL; | |
1219 | m_parentSeekable = false; | |
1220 | m_weaklinks = new wxZipWeakLinks; | |
1221 | m_streamlink = NULL; | |
1222 | m_offsetAdjustment = 0; | |
1223 | m_position = wxInvalidOffset; | |
1224 | m_signature = 0; | |
1225 | m_TotalEntries = 0; | |
1226 | m_lasterror = m_parent_i_stream->GetLastError(); | |
1227 | } | |
1228 | ||
1229 | wxZipInputStream::~wxZipInputStream() | |
1230 | { | |
1231 | CloseDecompressor(m_decomp); | |
1232 | ||
1233 | delete m_store; | |
1234 | delete m_inflate; | |
1235 | delete m_rawin; | |
1236 | delete m_ffile; | |
1237 | ||
1238 | m_weaklinks->Release(this); | |
1239 | ||
1240 | if (m_streamlink) | |
1241 | m_streamlink->Release(this); | |
1242 | } | |
1243 | ||
1244 | wxString wxZipInputStream::GetComment() | |
1245 | { | |
1246 | if (m_position == wxInvalidOffset) | |
1247 | if (!LoadEndRecord()) | |
1248 | return wxEmptyString; | |
1249 | ||
1250 | if (!m_parentSeekable && Eof() && m_signature) { | |
1251 | m_lasterror = wxSTREAM_NO_ERROR; | |
1252 | m_lasterror = ReadLocal(true); | |
1253 | } | |
1254 | ||
1255 | return m_Comment; | |
1256 | } | |
1257 | ||
1258 | int wxZipInputStream::GetTotalEntries() | |
1259 | { | |
1260 | if (m_position == wxInvalidOffset) | |
1261 | LoadEndRecord(); | |
1262 | return m_TotalEntries; | |
1263 | } | |
1264 | ||
1265 | wxZipStreamLink *wxZipInputStream::MakeLink(wxZipOutputStream *out) | |
1266 | { | |
1267 | wxZipStreamLink *link = NULL; | |
1268 | ||
1269 | if (!m_parentSeekable && (IsOpened() || !Eof())) { | |
1270 | link = new wxZipStreamLink(out); | |
1271 | if (m_streamlink) | |
1272 | m_streamlink->Release(this); | |
1273 | m_streamlink = link->AddRef(); | |
1274 | } | |
1275 | ||
1276 | return link; | |
1277 | } | |
1278 | ||
1279 | bool wxZipInputStream::LoadEndRecord() | |
1280 | { | |
1281 | wxCHECK(m_position == wxInvalidOffset, false); | |
1282 | if (!IsOk()) | |
1283 | return false; | |
1284 | ||
1285 | m_position = 0; | |
1286 | ||
1287 | // First find the end-of-central-directory record. | |
1288 | if (!FindEndRecord()) { | |
1289 | // failed, so either this is a non-seekable stream (ok), or not a zip | |
1290 | if (m_parentSeekable) { | |
1291 | m_lasterror = wxSTREAM_READ_ERROR; | |
1292 | wxLogError(_("invalid zip file")); | |
1293 | return false; | |
1294 | } | |
1295 | else { | |
1296 | wxLogNull nolog; | |
1297 | wxFileOffset pos = m_parent_i_stream->TellI(); | |
1298 | // FIXME | |
1299 | //if (pos != wxInvalidOffset) | |
1300 | if (pos >= 0 && pos <= LONG_MAX) | |
1301 | m_offsetAdjustment = m_position = pos; | |
1302 | return true; | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | wxZipEndRec endrec; | |
1307 | ||
1308 | // Read in the end record | |
1309 | wxFileOffset endPos = m_parent_i_stream->TellI() - 4; | |
1310 | if (!endrec.Read(*m_parent_i_stream, GetConv())) { | |
1311 | if (!*m_parent_i_stream) { | |
1312 | m_lasterror = wxSTREAM_READ_ERROR; | |
1313 | return false; | |
1314 | } | |
1315 | // TODO: try this out | |
1316 | wxLogWarning(_("assuming this is a multi-part zip concatenated")); | |
1317 | } | |
1318 | ||
1319 | m_TotalEntries = endrec.GetTotalEntries(); | |
1320 | m_Comment = endrec.GetComment(); | |
1321 | ||
1322 | // Now find the central-directory. we have the file offset of | |
1323 | // the CD, so look there first. | |
1324 | if (m_parent_i_stream->SeekI(endrec.GetOffset()) != wxInvalidOffset && | |
1325 | ReadSignature() == CENTRAL_MAGIC) { | |
1326 | m_signature = CENTRAL_MAGIC; | |
1327 | m_position = endrec.GetOffset(); | |
1328 | m_offsetAdjustment = 0; | |
1329 | return true; | |
1330 | } | |
1331 | ||
1332 | // If it's not there, then it could be that the zip has been appended | |
1333 | // to a self extractor, so take the CD size (also in endrec), subtract | |
1334 | // it from the file offset of the end-central-directory and look there. | |
1335 | if (m_parent_i_stream->SeekI(endPos - endrec.GetSize()) | |
1336 | != wxInvalidOffset && ReadSignature() == CENTRAL_MAGIC) { | |
1337 | m_signature = CENTRAL_MAGIC; | |
1338 | m_position = endPos - endrec.GetSize(); | |
1339 | m_offsetAdjustment = m_position - endrec.GetOffset(); | |
1340 | return true; | |
1341 | } | |
1342 | ||
1343 | wxLogError(_("can't find central directory in zip")); | |
1344 | m_lasterror = wxSTREAM_READ_ERROR; | |
1345 | return false; | |
1346 | } | |
1347 | ||
1348 | // Find the end-of-central-directory record. | |
1349 | // If found the stream will be positioned just past the 4 signature bytes. | |
1350 | // | |
1351 | bool wxZipInputStream::FindEndRecord() | |
1352 | { | |
1353 | // usually it's 22 bytes in size and the last thing in the file | |
1354 | { | |
1355 | wxLogNull nolog; | |
1356 | if (m_parent_i_stream->SeekI(-END_SIZE, wxFromEnd) == wxInvalidOffset) | |
1357 | return false; | |
1358 | } | |
1359 | ||
1360 | m_parentSeekable = true; | |
1361 | m_signature = 0; | |
1362 | char magic[4]; | |
1363 | if (m_parent_i_stream->Read(magic, 4).LastRead() != 4) | |
1364 | return false; | |
1365 | if ((m_signature = CrackUint32(magic)) == END_MAGIC) | |
1366 | return true; | |
1367 | ||
1368 | // unfortunately, the record has a comment field that can be up to 65535 | |
1369 | // bytes in length, so if the signature not found then search backwards. | |
1370 | wxFileOffset pos = m_parent_i_stream->TellI(); | |
1371 | const int BUFSIZE = 1024; | |
1372 | wxCharBuffer buf(BUFSIZE); | |
1373 | ||
1374 | memcpy(buf.data(), magic, 3); | |
1375 | wxFileOffset minpos = wxMax(pos - 65535L, 0); | |
1376 | ||
1377 | while (pos > minpos) { | |
1378 | size_t len = (size_t)(pos - wxMax(pos - (BUFSIZE - 3), minpos)); | |
1379 | memcpy(buf.data() + len, buf, 3); | |
1380 | pos -= len; | |
1381 | ||
1382 | if (m_parent_i_stream->SeekI(pos, wxFromStart) == wxInvalidOffset || | |
1383 | m_parent_i_stream->Read(buf.data(), len).LastRead() != len) | |
1384 | return false; | |
1385 | ||
1386 | char *p = buf.data() + len; | |
1387 | ||
1388 | while (p-- > buf.data()) { | |
1389 | if ((m_signature = CrackUint32(p)) == END_MAGIC) { | |
1390 | size_t remainder = buf.data() + len - p; | |
1391 | if (remainder > 4) | |
1392 | m_parent_i_stream->Ungetch(p + 4, remainder - 4); | |
1393 | return true; | |
1394 | } | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | return false; | |
1399 | } | |
1400 | ||
1401 | wxZipEntry *wxZipInputStream::GetNextEntry() | |
1402 | { | |
1403 | if (m_position == wxInvalidOffset) | |
1404 | if (!LoadEndRecord()) | |
1405 | return NULL; | |
1406 | ||
1407 | m_lasterror = m_parentSeekable ? ReadCentral() : ReadLocal(); | |
1408 | if (!IsOk()) | |
1409 | return NULL; | |
1410 | ||
1411 | _wxZipEntryPtr entry(new wxZipEntry(m_entry)); | |
1412 | entry->m_backlink = m_weaklinks->AddEntry(entry.get(), entry->GetKey()); | |
1413 | return entry.release(); | |
1414 | } | |
1415 | ||
1416 | wxStreamError wxZipInputStream::ReadCentral() | |
1417 | { | |
1418 | if (!AtHeader()) | |
1419 | CloseEntry(); | |
1420 | ||
1421 | if (m_signature == END_MAGIC) | |
1422 | return wxSTREAM_EOF; | |
1423 | ||
1424 | if (m_signature != CENTRAL_MAGIC) { | |
1425 | wxLogError(_("error reading zip central directory")); | |
1426 | return wxSTREAM_READ_ERROR; | |
1427 | } | |
1428 | ||
1429 | if (QuietSeek(*m_parent_i_stream, m_position + 4) == wxInvalidOffset) | |
1430 | return wxSTREAM_READ_ERROR; | |
1431 | ||
1432 | m_position += m_entry.ReadCentral(*m_parent_i_stream, GetConv()); | |
1433 | if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) { | |
1434 | m_signature = 0; | |
1435 | return wxSTREAM_READ_ERROR; | |
1436 | } | |
1437 | ||
1438 | m_signature = ReadSignature(); | |
1439 | ||
1440 | if (m_offsetAdjustment) | |
1441 | m_entry.SetOffset(m_entry.GetOffset() + m_offsetAdjustment); | |
1442 | m_entry.SetKey(m_entry.GetOffset()); | |
1443 | ||
1444 | return wxSTREAM_NO_ERROR; | |
1445 | } | |
1446 | ||
1447 | wxStreamError wxZipInputStream::ReadLocal(bool readEndRec /*=false*/) | |
1448 | { | |
1449 | if (!AtHeader()) | |
1450 | CloseEntry(); | |
1451 | ||
1452 | if (!m_signature) | |
1453 | m_signature = ReadSignature(); | |
1454 | ||
1455 | if (m_signature == CENTRAL_MAGIC || m_signature == END_MAGIC) { | |
1456 | if (m_streamlink && !m_streamlink->GetOutputStream()) { | |
1457 | m_streamlink->Release(this); | |
1458 | m_streamlink = NULL; | |
1459 | } | |
1460 | } | |
1461 | ||
1462 | while (m_signature == CENTRAL_MAGIC) { | |
1463 | if (m_weaklinks->IsEmpty() && m_streamlink == NULL) | |
1464 | return wxSTREAM_EOF; | |
1465 | ||
1466 | m_position += m_entry.ReadCentral(*m_parent_i_stream, GetConv()); | |
1467 | m_signature = 0; | |
1468 | if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) | |
1469 | return wxSTREAM_READ_ERROR; | |
1470 | ||
1471 | wxZipEntry *entry = m_weaklinks->GetEntry(m_entry.GetOffset()); | |
1472 | if (entry) { | |
1473 | entry->SetSystemMadeBy(m_entry.GetSystemMadeBy()); | |
1474 | entry->SetVersionMadeBy(m_entry.GetVersionMadeBy()); | |
1475 | entry->SetComment(m_entry.GetComment()); | |
1476 | entry->SetDiskStart(m_entry.GetDiskStart()); | |
1477 | entry->SetInternalAttributes(m_entry.GetInternalAttributes()); | |
1478 | entry->SetExternalAttributes(m_entry.GetExternalAttributes()); | |
1479 | Copy(entry->m_Extra, m_entry.m_Extra); | |
1480 | entry->Notify(); | |
1481 | m_weaklinks->RemoveEntry(entry->GetOffset()); | |
1482 | } | |
1483 | ||
1484 | m_signature = ReadSignature(); | |
1485 | } | |
1486 | ||
1487 | if (m_signature == END_MAGIC) { | |
1488 | if (readEndRec || m_streamlink) { | |
1489 | wxZipEndRec endrec; | |
1490 | endrec.Read(*m_parent_i_stream, GetConv()); | |
1491 | m_Comment = endrec.GetComment(); | |
1492 | m_signature = 0; | |
1493 | if (m_streamlink) { | |
1494 | m_streamlink->GetOutputStream()->SetComment(endrec.GetComment()); | |
1495 | m_streamlink->Release(this); | |
1496 | m_streamlink = NULL; | |
1497 | } | |
1498 | } | |
1499 | return wxSTREAM_EOF; | |
1500 | } | |
1501 | ||
1502 | if (m_signature != LOCAL_MAGIC) { | |
1503 | wxLogError(_("error reading zip local header")); | |
1504 | return wxSTREAM_READ_ERROR; | |
1505 | } | |
1506 | ||
1507 | m_headerSize = m_entry.ReadLocal(*m_parent_i_stream, GetConv()); | |
1508 | m_signature = 0; | |
1509 | m_entry.SetOffset(m_position); | |
1510 | m_entry.SetKey(m_position); | |
1511 | ||
1512 | if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) { | |
1513 | return wxSTREAM_READ_ERROR; | |
1514 | } else { | |
1515 | m_TotalEntries++; | |
1516 | return wxSTREAM_NO_ERROR; | |
1517 | } | |
1518 | } | |
1519 | ||
1520 | wxUint32 wxZipInputStream::ReadSignature() | |
1521 | { | |
1522 | char magic[4]; | |
1523 | m_parent_i_stream->Read(magic, 4); | |
1524 | return m_parent_i_stream->LastRead() == 4 ? CrackUint32(magic) : 0; | |
1525 | } | |
1526 | ||
1527 | bool wxZipInputStream::OpenEntry(wxArchiveEntry& entry) | |
1528 | { | |
1529 | wxZipEntry *zipEntry = wxStaticCast(&entry, wxZipEntry); | |
1530 | return zipEntry ? OpenEntry(*zipEntry) : false; | |
1531 | } | |
1532 | ||
1533 | // Open an entry | |
1534 | // | |
1535 | bool wxZipInputStream::DoOpen(wxZipEntry *entry, bool raw) | |
1536 | { | |
1537 | if (m_position == wxInvalidOffset) | |
1538 | if (!LoadEndRecord()) | |
1539 | return false; | |
1540 | if (m_lasterror == wxSTREAM_READ_ERROR) | |
1541 | return false; | |
1542 | wxCHECK(!IsOpened(), false); | |
1543 | ||
1544 | m_raw = raw; | |
1545 | ||
1546 | if (entry) { | |
1547 | if (AfterHeader() && entry->GetKey() == m_entry.GetOffset()) | |
1548 | return true; | |
1549 | // can only open the current entry on a non-seekable stream | |
1550 | wxCHECK(m_parentSeekable, false); | |
1551 | } | |
1552 | ||
1553 | m_lasterror = wxSTREAM_READ_ERROR; | |
1554 | ||
1555 | if (entry) | |
1556 | m_entry = *entry; | |
1557 | ||
1558 | if (m_parentSeekable) { | |
1559 | if (QuietSeek(*m_parent_i_stream, m_entry.GetOffset()) | |
1560 | == wxInvalidOffset) | |
1561 | return false; | |
1562 | if (ReadSignature() != LOCAL_MAGIC) { | |
1563 | wxLogError(_("bad zipfile offset to entry")); | |
1564 | return false; | |
1565 | } | |
1566 | } | |
1567 | ||
1568 | if (m_parentSeekable || AtHeader()) { | |
1569 | m_headerSize = m_entry.ReadLocal(*m_parent_i_stream, GetConv()); | |
1570 | if (m_parentSeekable) { | |
1571 | wxZipEntry *ref = m_weaklinks->GetEntry(m_entry.GetKey()); | |
1572 | if (ref) { | |
1573 | Copy(ref->m_LocalExtra, m_entry.m_LocalExtra); | |
1574 | ref->Notify(); | |
1575 | m_weaklinks->RemoveEntry(ref->GetKey()); | |
1576 | } | |
1577 | if (entry && entry != ref) { | |
1578 | Copy(entry->m_LocalExtra, m_entry.m_LocalExtra); | |
1579 | entry->Notify(); | |
1580 | } | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | m_lasterror = m_parent_i_stream->GetLastError(); | |
1585 | return IsOk(); | |
1586 | } | |
1587 | ||
1588 | bool wxZipInputStream::OpenDecompressor(bool raw /*=false*/) | |
1589 | { | |
1590 | wxASSERT(AfterHeader()); | |
1591 | ||
1592 | wxFileOffset compressedSize = m_entry.GetCompressedSize(); | |
1593 | ||
1594 | if (raw) | |
1595 | m_raw = true; | |
1596 | ||
1597 | if (m_raw) { | |
1598 | if (compressedSize != wxInvalidOffset) { | |
1599 | m_store->Open(compressedSize); | |
1600 | m_decomp = m_store; | |
1601 | } else { | |
1602 | if (!m_rawin) | |
1603 | m_rawin = new wxRawInputStream(*m_parent_i_stream); | |
1604 | m_decomp = m_rawin->Open(OpenDecompressor(m_rawin->GetTee())); | |
1605 | } | |
1606 | } else { | |
1607 | if (compressedSize != wxInvalidOffset && | |
1608 | (m_entry.GetMethod() != wxZIP_METHOD_DEFLATE || | |
1609 | wxZlibInputStream::CanHandleGZip())) { | |
1610 | m_store->Open(compressedSize); | |
1611 | m_decomp = OpenDecompressor(*m_store); | |
1612 | } else { | |
1613 | m_decomp = OpenDecompressor(*m_parent_i_stream); | |
1614 | } | |
1615 | } | |
1616 | ||
1617 | m_crcAccumulator = crc32(0, Z_NULL, 0); | |
1618 | m_lasterror = m_decomp ? m_decomp->GetLastError() : wxSTREAM_READ_ERROR; | |
1619 | return IsOk(); | |
1620 | } | |
1621 | ||
1622 | // Can be overriden to add support for additional decompression methods | |
1623 | // | |
1624 | wxInputStream *wxZipInputStream::OpenDecompressor(wxInputStream& stream) | |
1625 | { | |
1626 | switch (m_entry.GetMethod()) { | |
1627 | case wxZIP_METHOD_STORE: | |
1628 | if (m_entry.GetSize() == wxInvalidOffset) { | |
1629 | wxLogError(_("stored file length not in Zip header")); | |
1630 | break; | |
1631 | } | |
1632 | m_store->Open(m_entry.GetSize()); | |
1633 | return m_store; | |
1634 | ||
1635 | case wxZIP_METHOD_DEFLATE: | |
1636 | if (!m_inflate) | |
1637 | m_inflate = new wxZlibInputStream2(stream); | |
1638 | else | |
1639 | m_inflate->Open(stream); | |
1640 | return m_inflate; | |
1641 | ||
1642 | default: | |
1643 | wxLogError(_("unsupported Zip compression method")); | |
1644 | } | |
1645 | ||
1646 | return NULL; | |
1647 | } | |
1648 | ||
1649 | bool wxZipInputStream::CloseDecompressor(wxInputStream *decomp) | |
1650 | { | |
1651 | if (decomp && decomp == m_rawin) | |
1652 | return CloseDecompressor(m_rawin->GetFilterInputStream()); | |
1653 | if (decomp != m_store && decomp != m_inflate) | |
1654 | delete decomp; | |
1655 | return true; | |
1656 | } | |
1657 | ||
1658 | // Closes the current entry and positions the underlying stream at the start | |
1659 | // of the next entry | |
1660 | // | |
1661 | bool wxZipInputStream::CloseEntry() | |
1662 | { | |
1663 | if (AtHeader()) | |
1664 | return true; | |
1665 | if (m_lasterror == wxSTREAM_READ_ERROR) | |
1666 | return false; | |
1667 | ||
1668 | if (!m_parentSeekable) { | |
1669 | if (!IsOpened() && !OpenDecompressor(true)) | |
1670 | return false; | |
1671 | ||
1672 | const int BUFSIZE = 8192; | |
1673 | wxCharBuffer buf(BUFSIZE); | |
1674 | while (IsOk()) | |
1675 | Read(buf.data(), BUFSIZE); | |
1676 | ||
1677 | m_position += m_headerSize + m_entry.GetCompressedSize(); | |
1678 | } | |
1679 | ||
1680 | if (m_lasterror == wxSTREAM_EOF) | |
1681 | m_lasterror = wxSTREAM_NO_ERROR; | |
1682 | ||
1683 | CloseDecompressor(m_decomp); | |
1684 | m_decomp = NULL; | |
1685 | m_entry = wxZipEntry(); | |
1686 | m_headerSize = 0; | |
1687 | m_raw = false; | |
1688 | ||
1689 | return IsOk(); | |
1690 | } | |
1691 | ||
1692 | size_t wxZipInputStream::OnSysRead(void *buffer, size_t size) | |
1693 | { | |
1694 | if (!IsOpened()) | |
1695 | if ((AtHeader() && !DoOpen()) || !OpenDecompressor()) | |
1696 | m_lasterror = wxSTREAM_READ_ERROR; | |
1697 | if (!IsOk() || !size) | |
1698 | return 0; | |
1699 | ||
1700 | size_t count = m_decomp->Read(buffer, size).LastRead(); | |
1701 | if (!m_raw) | |
1702 | m_crcAccumulator = crc32(m_crcAccumulator, (Byte*)buffer, count); | |
1703 | m_lasterror = m_decomp->GetLastError(); | |
1704 | ||
1705 | if (Eof()) { | |
1706 | if ((m_entry.GetFlags() & wxZIP_SUMS_FOLLOW) != 0) { | |
1707 | m_headerSize += m_entry.ReadDescriptor(*m_parent_i_stream); | |
1708 | wxZipEntry *entry = m_weaklinks->GetEntry(m_entry.GetKey()); | |
1709 | ||
1710 | if (entry) { | |
1711 | entry->SetCrc(m_entry.GetCrc()); | |
1712 | entry->SetCompressedSize(m_entry.GetCompressedSize()); | |
1713 | entry->SetSize(m_entry.GetSize()); | |
1714 | entry->Notify(); | |
1715 | } | |
1716 | } | |
1717 | ||
1718 | if (!m_raw) { | |
1719 | m_lasterror = wxSTREAM_READ_ERROR; | |
1720 | ||
1721 | if (m_parent_i_stream->IsOk()) { | |
1722 | if (m_entry.GetSize() != TellI()) | |
1723 | wxLogError(_("reading zip stream (entry %s): bad length"), | |
1724 | m_entry.GetName().c_str()); | |
1725 | else if (m_crcAccumulator != m_entry.GetCrc()) | |
1726 | wxLogError(_("reading zip stream (entry %s): bad crc"), | |
1727 | m_entry.GetName().c_str()); | |
1728 | else | |
1729 | m_lasterror = wxSTREAM_EOF; | |
1730 | } | |
1731 | } | |
1732 | } | |
1733 | ||
1734 | return count; | |
1735 | } | |
1736 | ||
1737 | // Borrowed from VS's zip stream (c) 1999 Vaclav Slavik | |
1738 | // | |
1739 | wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) | |
1740 | { | |
1741 | if (!m_ffile || AtHeader()) | |
1742 | return wxInvalidOffset; | |
1743 | ||
1744 | // NB: since ZIP files don't natively support seeking, we have to | |
1745 | // implement a brute force workaround -- reading all the data | |
1746 | // between current and the new position (or between beginning of | |
1747 | // the file and new position...) | |
1748 | ||
1749 | wxFileOffset nextpos; | |
1750 | wxFileOffset pos = TellI(); | |
1751 | ||
1752 | switch ( mode ) | |
1753 | { | |
1754 | case wxFromCurrent : nextpos = seek + pos; break; | |
1755 | case wxFromStart : nextpos = seek; break; | |
1756 | case wxFromEnd : nextpos = GetLength() - 1 + seek; break; | |
1757 | default : nextpos = pos; break; /* just to fool compiler, never happens */ | |
1758 | } | |
1759 | ||
1760 | size_t toskip; | |
1761 | if ( nextpos >= pos ) | |
1762 | { | |
1763 | toskip = (size_t)(nextpos - pos); | |
1764 | } | |
1765 | else | |
1766 | { | |
1767 | wxZipEntry current(m_entry); | |
1768 | CloseEntry(); | |
1769 | if (!OpenEntry(current)) | |
1770 | { | |
1771 | m_lasterror = wxSTREAM_READ_ERROR; | |
1772 | return pos; | |
1773 | } | |
1774 | toskip = (size_t)nextpos; | |
1775 | } | |
1776 | ||
1777 | if ( toskip > 0 ) | |
1778 | { | |
1779 | const size_t BUFSIZE = 4096; | |
1780 | size_t sz; | |
1781 | char buffer[BUFSIZE]; | |
1782 | while ( toskip > 0 ) | |
1783 | { | |
1784 | sz = wxMin(toskip, BUFSIZE); | |
1785 | Read(buffer, sz); | |
1786 | toskip -= sz; | |
1787 | } | |
1788 | } | |
1789 | ||
1790 | pos = nextpos; | |
1791 | return pos; | |
1792 | } | |
1793 | ||
1794 | ||
1795 | ///////////////////////////////////////////////////////////////////////////// | |
1796 | // Output stream | |
1797 | ||
1798 | #include "wx/listimpl.cpp" | |
1799 | WX_DEFINE_LIST(_wxZipEntryList); | |
1800 | ||
1801 | wxZipOutputStream::wxZipOutputStream(wxOutputStream& stream, | |
1802 | int level /*=-1*/, | |
1803 | wxMBConv& conv /*=wxConvLocal*/) | |
1804 | : wxArchiveOutputStream(stream, conv), | |
1805 | m_store(new wxStoredOutputStream(stream)), | |
1806 | m_deflate(NULL), | |
1807 | m_backlink(NULL), | |
1808 | m_initialData(new char[OUTPUT_LATENCY]), | |
1809 | m_initialSize(0), | |
1810 | m_pending(NULL), | |
1811 | m_raw(false), | |
1812 | m_headerOffset(0), | |
1813 | m_headerSize(0), | |
1814 | m_entrySize(0), | |
1815 | m_comp(NULL), | |
1816 | m_level(level), | |
1817 | m_offsetAdjustment(wxInvalidOffset) | |
1818 | { | |
1819 | } | |
1820 | ||
1821 | wxZipOutputStream::~wxZipOutputStream() | |
1822 | { | |
1823 | Close(); | |
1824 | WX_CLEAR_LIST(_wxZipEntryList, m_entries); | |
1825 | delete m_store; | |
1826 | delete m_deflate; | |
1827 | delete m_pending; | |
1828 | delete [] m_initialData; | |
1829 | if (m_backlink) | |
1830 | m_backlink->Release(this); | |
1831 | } | |
1832 | ||
1833 | bool wxZipOutputStream::PutNextEntry( | |
1834 | const wxString& name, | |
1835 | const wxDateTime& dt /*=wxDateTime::Now()*/, | |
1836 | wxFileOffset size /*=wxInvalidOffset*/) | |
1837 | { | |
1838 | return PutNextEntry(new wxZipEntry(name, dt, size)); | |
1839 | } | |
1840 | ||
1841 | bool wxZipOutputStream::PutNextDirEntry( | |
1842 | const wxString& name, | |
1843 | const wxDateTime& dt /*=wxDateTime::Now()*/) | |
1844 | { | |
1845 | wxZipEntry *entry = new wxZipEntry(name, dt); | |
1846 | entry->SetIsDir(); | |
1847 | return PutNextEntry(entry); | |
1848 | } | |
1849 | ||
1850 | bool wxZipOutputStream::CopyEntry(wxZipEntry *entry, | |
1851 | wxZipInputStream& inputStream) | |
1852 | { | |
1853 | _wxZipEntryPtr e(entry); | |
1854 | ||
1855 | return | |
1856 | inputStream.DoOpen(e.get(), true) && | |
1857 | DoCreate(e.release(), true) && | |
1858 | Write(inputStream).IsOk() && inputStream.Eof(); | |
1859 | } | |
1860 | ||
1861 | bool wxZipOutputStream::PutNextEntry(wxArchiveEntry *entry) | |
1862 | { | |
1863 | wxZipEntry *zipEntry = wxStaticCast(entry, wxZipEntry); | |
1864 | if (!zipEntry) | |
1865 | delete entry; | |
1866 | return PutNextEntry(zipEntry); | |
1867 | } | |
1868 | ||
1869 | bool wxZipOutputStream::CopyEntry(wxArchiveEntry *entry, | |
1870 | wxArchiveInputStream& stream) | |
1871 | { | |
1872 | wxZipEntry *zipEntry = wxStaticCast(entry, wxZipEntry); | |
1873 | ||
1874 | if (!zipEntry || !stream.OpenEntry(*zipEntry)) { | |
1875 | delete entry; | |
1876 | return false; | |
1877 | } | |
1878 | ||
1879 | return CopyEntry(zipEntry, wx_static_cast(wxZipInputStream&, stream)); | |
1880 | } | |
1881 | ||
1882 | bool wxZipOutputStream::CopyArchiveMetaData(wxZipInputStream& inputStream) | |
1883 | { | |
1884 | m_Comment = inputStream.GetComment(); | |
1885 | if (m_backlink) | |
1886 | m_backlink->Release(this); | |
1887 | m_backlink = inputStream.MakeLink(this); | |
1888 | return true; | |
1889 | } | |
1890 | ||
1891 | bool wxZipOutputStream::CopyArchiveMetaData(wxArchiveInputStream& stream) | |
1892 | { | |
1893 | return CopyArchiveMetaData(wx_static_cast(wxZipInputStream&, stream)); | |
1894 | } | |
1895 | ||
1896 | void wxZipOutputStream::SetLevel(int level) | |
1897 | { | |
1898 | if (level != m_level) { | |
1899 | if (m_comp != m_deflate) | |
1900 | delete m_deflate; | |
1901 | m_deflate = NULL; | |
1902 | m_level = level; | |
1903 | } | |
1904 | } | |
1905 | ||
1906 | bool wxZipOutputStream::DoCreate(wxZipEntry *entry, bool raw /*=false*/) | |
1907 | { | |
1908 | CloseEntry(); | |
1909 | ||
1910 | m_pending = entry; | |
1911 | if (!m_pending) | |
1912 | return false; | |
1913 | ||
1914 | // write the signature bytes right away | |
1915 | wxDataOutputStream ds(*m_parent_o_stream); | |
1916 | ds << LOCAL_MAGIC; | |
1917 | ||
1918 | // and if this is the first entry test for seekability | |
1919 | if (m_headerOffset == 0) { | |
1920 | bool logging = wxLog::IsEnabled(); | |
1921 | wxLogNull nolog; | |
1922 | wxFileOffset here = m_parent_o_stream->TellO(); | |
1923 | ||
1924 | if (here != wxInvalidOffset && here >= 4) { | |
1925 | if (m_parent_o_stream->SeekO(here - 4) == here - 4) { | |
1926 | m_offsetAdjustment = here - 4; | |
1927 | wxLog::EnableLogging(logging); | |
1928 | m_parent_o_stream->SeekO(here); | |
1929 | } | |
1930 | } | |
1931 | } | |
1932 | ||
1933 | m_pending->SetOffset(m_headerOffset); | |
1934 | ||
1935 | m_crcAccumulator = crc32(0, Z_NULL, 0); | |
1936 | ||
1937 | if (raw) | |
1938 | m_raw = true; | |
1939 | ||
1940 | m_lasterror = wxSTREAM_NO_ERROR; | |
1941 | return true; | |
1942 | } | |
1943 | ||
1944 | // Can be overriden to add support for additional compression methods | |
1945 | // | |
1946 | wxOutputStream *wxZipOutputStream::OpenCompressor( | |
1947 | wxOutputStream& stream, | |
1948 | wxZipEntry& entry, | |
1949 | const Buffer bufs[]) | |
1950 | { | |
1951 | if (entry.GetMethod() == wxZIP_METHOD_DEFAULT) { | |
1952 | if (GetLevel() == 0 | |
1953 | && (IsParentSeekable() | |
1954 | || entry.GetCompressedSize() != wxInvalidOffset | |
1955 | || entry.GetSize() != wxInvalidOffset)) { | |
1956 | entry.SetMethod(wxZIP_METHOD_STORE); | |
1957 | } else { | |
1958 | int size = 0; | |
1959 | for (int i = 0; bufs[i].m_data; ++i) | |
1960 | size += bufs[i].m_size; | |
1961 | entry.SetMethod(size <= 6 ? | |
1962 | wxZIP_METHOD_STORE : wxZIP_METHOD_DEFLATE); | |
1963 | } | |
1964 | } | |
1965 | ||
1966 | switch (entry.GetMethod()) { | |
1967 | case wxZIP_METHOD_STORE: | |
1968 | if (entry.GetCompressedSize() == wxInvalidOffset) | |
1969 | entry.SetCompressedSize(entry.GetSize()); | |
1970 | return m_store; | |
1971 | ||
1972 | case wxZIP_METHOD_DEFLATE: | |
1973 | { | |
1974 | int defbits = wxZIP_DEFLATE_NORMAL; | |
1975 | switch (GetLevel()) { | |
1976 | case 0: case 1: | |
1977 | defbits = wxZIP_DEFLATE_SUPERFAST; | |
1978 | break; | |
1979 | case 2: case 3: case 4: | |
1980 | defbits = wxZIP_DEFLATE_FAST; | |
1981 | break; | |
1982 | case 8: case 9: | |
1983 | defbits = wxZIP_DEFLATE_EXTRA; | |
1984 | break; | |
1985 | } | |
1986 | entry.SetFlags((entry.GetFlags() & ~wxZIP_DEFLATE_MASK) | | |
1987 | defbits | wxZIP_SUMS_FOLLOW); | |
1988 | ||
1989 | if (!m_deflate) | |
1990 | m_deflate = new wxZlibOutputStream2(stream, GetLevel()); | |
1991 | else | |
1992 | m_deflate->Open(stream); | |
1993 | ||
1994 | return m_deflate; | |
1995 | } | |
1996 | ||
1997 | default: | |
1998 | wxLogError(_("unsupported Zip compression method")); | |
1999 | } | |
2000 | ||
2001 | return NULL; | |
2002 | } | |
2003 | ||
2004 | bool wxZipOutputStream::CloseCompressor(wxOutputStream *comp) | |
2005 | { | |
2006 | if (comp == m_deflate) | |
2007 | m_deflate->Close(); | |
2008 | else if (comp != m_store) | |
2009 | delete comp; | |
2010 | return true; | |
2011 | } | |
2012 | ||
2013 | // This is called when OUPUT_LATENCY bytes has been written to the | |
2014 | // wxZipOutputStream to actually create the zip entry. | |
2015 | // | |
2016 | void wxZipOutputStream::CreatePendingEntry(const void *buffer, size_t size) | |
2017 | { | |
2018 | wxASSERT(IsOk() && m_pending && !m_comp); | |
2019 | _wxZipEntryPtr spPending(m_pending); | |
2020 | m_pending = NULL; | |
2021 | ||
2022 | Buffer bufs[] = { | |
2023 | { m_initialData, m_initialSize }, | |
2024 | { (const char*)buffer, size }, | |
2025 | { NULL, 0 } | |
2026 | }; | |
2027 | ||
2028 | if (m_raw) | |
2029 | m_comp = m_store; | |
2030 | else | |
2031 | m_comp = OpenCompressor(*m_store, *spPending, | |
2032 | m_initialSize ? bufs : bufs + 1); | |
2033 | ||
2034 | if (IsParentSeekable() | |
2035 | || (spPending->m_Crc | |
2036 | && spPending->m_CompressedSize != wxInvalidOffset | |
2037 | && spPending->m_Size != wxInvalidOffset)) | |
2038 | spPending->m_Flags &= ~wxZIP_SUMS_FOLLOW; | |
2039 | else | |
2040 | if (spPending->m_CompressedSize != wxInvalidOffset) | |
2041 | spPending->m_Flags |= wxZIP_SUMS_FOLLOW; | |
2042 | ||
2043 | m_headerSize = spPending->WriteLocal(*m_parent_o_stream, GetConv()); | |
2044 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2045 | ||
2046 | if (IsOk()) { | |
2047 | m_entries.push_back(spPending.release()); | |
2048 | OnSysWrite(m_initialData, m_initialSize); | |
2049 | } | |
2050 | ||
2051 | m_initialSize = 0; | |
2052 | } | |
2053 | ||
2054 | // This is called to write out the zip entry when Close has been called | |
2055 | // before OUTPUT_LATENCY bytes has been written to the wxZipOutputStream. | |
2056 | // | |
2057 | void wxZipOutputStream::CreatePendingEntry() | |
2058 | { | |
2059 | wxASSERT(IsOk() && m_pending && !m_comp); | |
2060 | _wxZipEntryPtr spPending(m_pending); | |
2061 | m_pending = NULL; | |
2062 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2063 | ||
2064 | if (!m_raw) { | |
2065 | // Initially compresses the data to memory, then fall back to 'store' | |
2066 | // if the compressor makes the data larger rather than smaller. | |
2067 | wxMemoryOutputStream mem; | |
2068 | Buffer bufs[] = { { m_initialData, m_initialSize }, { NULL, 0 } }; | |
2069 | wxOutputStream *comp = OpenCompressor(mem, *spPending, bufs); | |
2070 | ||
2071 | if (!comp) | |
2072 | return; | |
2073 | if (comp != m_store) { | |
2074 | bool ok = comp->Write(m_initialData, m_initialSize).IsOk(); | |
2075 | CloseCompressor(comp); | |
2076 | if (!ok) | |
2077 | return; | |
2078 | } | |
2079 | ||
2080 | m_entrySize = m_initialSize; | |
2081 | m_crcAccumulator = crc32(0, (Byte*)m_initialData, m_initialSize); | |
2082 | ||
2083 | if (mem.GetSize() > 0 && mem.GetSize() < m_initialSize) { | |
2084 | m_initialSize = mem.GetSize(); | |
2085 | mem.CopyTo(m_initialData, m_initialSize); | |
2086 | } else { | |
2087 | spPending->SetMethod(wxZIP_METHOD_STORE); | |
2088 | } | |
2089 | ||
2090 | spPending->SetSize(m_entrySize); | |
2091 | spPending->SetCrc(m_crcAccumulator); | |
2092 | spPending->SetCompressedSize(m_initialSize); | |
2093 | } | |
2094 | ||
2095 | spPending->m_Flags &= ~wxZIP_SUMS_FOLLOW; | |
2096 | m_headerSize = spPending->WriteLocal(*m_parent_o_stream, GetConv()); | |
2097 | ||
2098 | if (m_parent_o_stream->IsOk()) { | |
2099 | m_entries.push_back(spPending.release()); | |
2100 | m_comp = m_store; | |
2101 | m_store->Write(m_initialData, m_initialSize); | |
2102 | } | |
2103 | ||
2104 | m_initialSize = 0; | |
2105 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2106 | } | |
2107 | ||
2108 | // Write the 'central directory' and the 'end-central-directory' records. | |
2109 | // | |
2110 | bool wxZipOutputStream::Close() | |
2111 | { | |
2112 | CloseEntry(); | |
2113 | ||
2114 | if (m_lasterror == wxSTREAM_WRITE_ERROR || m_entries.size() == 0) | |
2115 | return false; | |
2116 | ||
2117 | wxZipEndRec endrec; | |
2118 | ||
2119 | endrec.SetEntriesHere(m_entries.size()); | |
2120 | endrec.SetTotalEntries(m_entries.size()); | |
2121 | endrec.SetOffset(m_headerOffset); | |
2122 | endrec.SetComment(m_Comment); | |
2123 | ||
2124 | _wxZipEntryList::iterator it; | |
2125 | wxFileOffset size = 0; | |
2126 | ||
2127 | for (it = m_entries.begin(); it != m_entries.end(); ++it) { | |
2128 | size += (*it)->WriteCentral(*m_parent_o_stream, GetConv()); | |
2129 | delete *it; | |
2130 | } | |
2131 | m_entries.clear(); | |
2132 | ||
2133 | endrec.SetSize(size); | |
2134 | endrec.Write(*m_parent_o_stream, GetConv()); | |
2135 | ||
2136 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2137 | if (!IsOk()) | |
2138 | return false; | |
2139 | m_lasterror = wxSTREAM_EOF; | |
2140 | return true; | |
2141 | } | |
2142 | ||
2143 | // Finish writing the current entry | |
2144 | // | |
2145 | bool wxZipOutputStream::CloseEntry() | |
2146 | { | |
2147 | if (IsOk() && m_pending) | |
2148 | CreatePendingEntry(); | |
2149 | if (!IsOk()) | |
2150 | return false; | |
2151 | if (!m_comp) | |
2152 | return true; | |
2153 | ||
2154 | CloseCompressor(m_comp); | |
2155 | m_comp = NULL; | |
2156 | ||
2157 | wxFileOffset compressedSize = m_store->TellO(); | |
2158 | ||
2159 | wxZipEntry& entry = *m_entries.back(); | |
2160 | ||
2161 | // When writing raw the crc and size can't be checked | |
2162 | if (m_raw) { | |
2163 | m_crcAccumulator = entry.GetCrc(); | |
2164 | m_entrySize = entry.GetSize(); | |
2165 | } | |
2166 | ||
2167 | // Write the sums in the trailing 'data descriptor' if necessary | |
2168 | if (entry.m_Flags & wxZIP_SUMS_FOLLOW) { | |
2169 | wxASSERT(!IsParentSeekable()); | |
2170 | m_headerOffset += | |
2171 | entry.WriteDescriptor(*m_parent_o_stream, m_crcAccumulator, | |
2172 | compressedSize, m_entrySize); | |
2173 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2174 | } | |
2175 | ||
2176 | // If the local header didn't have the correct crc and size written to | |
2177 | // it then seek back and fix it | |
2178 | else if (m_crcAccumulator != entry.GetCrc() | |
2179 | || m_entrySize != entry.GetSize() | |
2180 | || compressedSize != entry.GetCompressedSize()) | |
2181 | { | |
2182 | if (IsParentSeekable()) { | |
2183 | wxFileOffset here = m_parent_o_stream->TellO(); | |
2184 | wxFileOffset headerOffset = m_headerOffset + m_offsetAdjustment; | |
2185 | m_parent_o_stream->SeekO(headerOffset + SUMS_OFFSET); | |
2186 | entry.WriteDescriptor(*m_parent_o_stream, m_crcAccumulator, | |
2187 | compressedSize, m_entrySize); | |
2188 | m_parent_o_stream->SeekO(here); | |
2189 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2190 | } else { | |
2191 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2192 | } | |
2193 | } | |
2194 | ||
2195 | m_headerOffset += m_headerSize + compressedSize; | |
2196 | m_headerSize = 0; | |
2197 | m_entrySize = 0; | |
2198 | m_store->Close(); | |
2199 | m_raw = false; | |
2200 | ||
2201 | if (IsOk()) | |
2202 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2203 | else | |
2204 | wxLogError(_("error writing zip entry '%s': bad crc or length"), | |
2205 | entry.GetName().c_str()); | |
2206 | return IsOk(); | |
2207 | } | |
2208 | ||
2209 | void wxZipOutputStream::Sync() | |
2210 | { | |
2211 | if (IsOk() && m_pending) | |
2212 | CreatePendingEntry(NULL, 0); | |
2213 | if (!m_comp) | |
2214 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2215 | if (IsOk()) { | |
2216 | m_comp->Sync(); | |
2217 | m_lasterror = m_comp->GetLastError(); | |
2218 | } | |
2219 | } | |
2220 | ||
2221 | size_t wxZipOutputStream::OnSysWrite(const void *buffer, size_t size) | |
2222 | { | |
2223 | if (IsOk() && m_pending) { | |
2224 | if (m_initialSize + size < OUTPUT_LATENCY) { | |
2225 | memcpy(m_initialData + m_initialSize, buffer, size); | |
2226 | m_initialSize += size; | |
2227 | return size; | |
2228 | } else { | |
2229 | CreatePendingEntry(buffer, size); | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | if (!m_comp) | |
2234 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2235 | if (!IsOk() || !size) | |
2236 | return 0; | |
2237 | ||
2238 | if (m_comp->Write(buffer, size).LastWrite() != size) | |
2239 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2240 | m_crcAccumulator = crc32(m_crcAccumulator, (Byte*)buffer, size); | |
2241 | m_entrySize += m_comp->LastWrite(); | |
2242 | ||
2243 | return m_comp->LastWrite(); | |
2244 | } | |
2245 | ||
2246 | #endif // wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM |