]>
Commit | Line | Data |
---|---|---|
5526e819 | 1 | ///////////////////////////////////////////////////////////////////////////// |
00375592 VZ |
2 | // Name: zipstrm.cpp |
3 | // Purpose: Streams for Zip files | |
4 | // Author: Mike Wetherell | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) Mike Wetherell | |
65571936 | 7 | // Licence: wxWindows licence |
5526e819 VS |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
d1af991f RR |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
5526e819 | 12 | |
d1af991f RR |
13 | #ifdef __BORLANDC__ |
14 | #pragma hdrstop | |
5526e819 VS |
15 | #endif |
16 | ||
00375592 VZ |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/defs.h" | |
19 | #endif | |
20 | ||
21 | #if wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM | |
22 | ||
23 | #include "wx/zipstrm.h" | |
24 | #include "wx/log.h" | |
25 | #include "wx/intl.h" | |
26 | #include "wx/datstrm.h" | |
27 | #include "wx/zstream.h" | |
28 | #include "wx/mstream.h" | |
29 | #include "wx/utils.h" | |
30 | #include "wx/buffer.h" | |
31 | #include "wx/ptr_scpd.h" | |
32 | #include "wx/wfstream.h" | |
00375592 VZ |
33 | #include "zlib.h" |
34 | ||
35 | // value for the 'version needed to extract' field (20 means 2.0) | |
36 | enum { | |
37 | VERSION_NEEDED_TO_EXTRACT = 20 | |
38 | }; | |
39 | ||
40 | // signatures for the various records (PKxx) | |
41 | enum { | |
42 | CENTRAL_MAGIC = 0x02014b50, // central directory record | |
43 | LOCAL_MAGIC = 0x04034b50, // local header | |
44 | END_MAGIC = 0x06054b50, // end of central directory record | |
45 | SUMS_MAGIC = 0x08074b50 // data descriptor (info-zip) | |
46 | }; | |
47 | ||
48 | // unix file attributes. zip stores them in the high 16 bits of the | |
49 | // 'external attributes' field, hence the extra zeros. | |
50 | enum { | |
51 | wxZIP_S_IFMT = 0xF0000000, | |
52 | wxZIP_S_IFDIR = 0x40000000, | |
53 | wxZIP_S_IFREG = 0x80000000 | |
54 | }; | |
55 | ||
56 | // minimum sizes for the various records | |
57 | enum { | |
58 | CENTRAL_SIZE = 46, | |
59 | LOCAL_SIZE = 30, | |
60 | END_SIZE = 22, | |
61 | SUMS_SIZE = 12 | |
62 | }; | |
63 | ||
64 | // The number of bytes that must be written to an wxZipOutputStream before | |
65 | // a zip entry is created. The purpose of this latency is so that | |
66 | // OpenCompressor() can see a little data before deciding which compressor | |
67 | // it should use. | |
68 | enum { | |
69 | OUTPUT_LATENCY = 4096 | |
70 | }; | |
71 | ||
72 | // Some offsets into the local header | |
73 | enum { | |
74 | SUMS_OFFSET = 14 | |
75 | }; | |
76 | ||
77 | IMPLEMENT_DYNAMIC_CLASS(wxZipEntry, wxArchiveEntry) | |
78 | IMPLEMENT_DYNAMIC_CLASS(wxZipClassFactory, wxArchiveClassFactory) | |
79 | ||
851b16b9 MW |
80 | //FORCE_LINK_ME(zipstrm) |
81 | int _wx_link_dummy_func_zipstrm(); | |
82 | int _wx_link_dummy_func_zipstrm() | |
83 | { | |
84 | return 1; | |
85 | } | |
00375592 VZ |
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(); } | |
46263455 | 149 | virtual wxFileOffset GetLength() const { return m_len; } |
00375592 VZ |
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 | { | |
26481315 | 171 | size_t count = wxMin(size, (size_t)(m_len - m_pos)); |
00375592 VZ |
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 | ||
474 | wxZipMemory *AddRef() { m_ref++; return this; } | |
475 | void Release() { if (--m_ref == 0) delete this; } | |
476 | ||
477 | char *GetData() const { return m_data; } | |
478 | size_t GetSize() const { return m_size; } | |
479 | size_t GetCapacity() const { return m_capacity; } | |
480 | ||
481 | wxZipMemory *Unique(size_t size); | |
482 | ||
483 | private: | |
2e85922c | 484 | ~wxZipMemory() { delete [] m_data; } |
00375592 VZ |
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) { | |
2e85922c | 504 | delete [] zm->m_data; |
00375592 VZ |
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, | |
d13a08ab | 545 | wxIntegerEqual, wx__OffsetZipEntryMap); |
00375592 VZ |
546 | |
547 | class wxZipWeakLinks | |
548 | { | |
549 | public: | |
550 | wxZipWeakLinks() : m_ref(1) { } | |
551 | ||
552 | void Release(const wxZipInputStream* WXUNUSED(x)) | |
553 | { if (--m_ref == 0) delete this; } | |
554 | void Release(wxFileOffset key) | |
555 | { RemoveEntry(key); if (--m_ref == 0) delete this; } | |
556 | ||
557 | wxZipWeakLinks *AddEntry(wxZipEntry *entry, wxFileOffset key); | |
26481315 | 558 | void RemoveEntry(wxFileOffset key) |
d13a08ab | 559 | { m_entries.erase((wx__OffsetZipEntryMap::key_type)key); } |
00375592 VZ |
560 | wxZipEntry *GetEntry(wxFileOffset key) const; |
561 | bool IsEmpty() const { return m_entries.empty(); } | |
562 | ||
563 | private: | |
f8fbc92b | 564 | ~wxZipWeakLinks() { wxASSERT(IsEmpty()); } |
00375592 VZ |
565 | |
566 | int m_ref; | |
d13a08ab | 567 | wx__OffsetZipEntryMap m_entries; |
00375592 VZ |
568 | }; |
569 | ||
570 | wxZipWeakLinks *wxZipWeakLinks::AddEntry(wxZipEntry *entry, wxFileOffset key) | |
571 | { | |
d13a08ab | 572 | m_entries[(wx__OffsetZipEntryMap::key_type)key] = entry; |
00375592 VZ |
573 | m_ref++; |
574 | return this; | |
575 | } | |
576 | ||
577 | wxZipEntry *wxZipWeakLinks::GetEntry(wxFileOffset key) const | |
578 | { | |
d13a08ab MW |
579 | wx__OffsetZipEntryMap::const_iterator it = |
580 | m_entries.find((wx__OffsetZipEntryMap::key_type)key); | |
00375592 VZ |
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*/) | |
ba9bbf13 | 592 | : |
00375592 VZ |
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) | |
f44eaed6 RN |
625 | : wxArchiveEntry(e), |
626 | m_SystemMadeBy(e.m_SystemMadeBy), | |
00375592 VZ |
627 | m_VersionMadeBy(e.m_VersionMadeBy), |
628 | m_VersionNeeded(e.m_VersionNeeded), | |
629 | m_Flags(e.m_Flags), | |
630 | m_Method(e.m_Method), | |
631 | m_DateTime(e.m_DateTime), | |
632 | m_Crc(e.m_Crc), | |
633 | m_CompressedSize(e.m_CompressedSize), | |
634 | m_Size(e.m_Size), | |
635 | m_Name(e.m_Name), | |
636 | m_Key(e.m_Key), | |
637 | m_Offset(e.m_Offset), | |
638 | m_Comment(e.m_Comment), | |
639 | m_DiskStart(e.m_DiskStart), | |
640 | m_InternalAttributes(e.m_InternalAttributes), | |
641 | m_ExternalAttributes(e.m_ExternalAttributes), | |
642 | m_Extra(AddRef(e.m_Extra)), | |
643 | m_LocalExtra(AddRef(e.m_LocalExtra)), | |
f44eaed6 | 644 | m_zipnotifier(NULL), |
00375592 VZ |
645 | m_backlink(NULL) |
646 | { | |
647 | } | |
648 | ||
649 | wxZipEntry& wxZipEntry::operator=(const wxZipEntry& e) | |
650 | { | |
651 | if (&e != this) { | |
652 | m_SystemMadeBy = e.m_SystemMadeBy; | |
653 | m_VersionMadeBy = e.m_VersionMadeBy; | |
654 | m_VersionNeeded = e.m_VersionNeeded; | |
655 | m_Flags = e.m_Flags; | |
656 | m_Method = e.m_Method; | |
657 | m_DateTime = e.m_DateTime; | |
658 | m_Crc = e.m_Crc; | |
659 | m_CompressedSize = e.m_CompressedSize; | |
660 | m_Size = e.m_Size; | |
661 | m_Name = e.m_Name; | |
662 | m_Key = e.m_Key; | |
663 | m_Offset = e.m_Offset; | |
664 | m_Comment = e.m_Comment; | |
665 | m_DiskStart = e.m_DiskStart; | |
666 | m_InternalAttributes = e.m_InternalAttributes; | |
667 | m_ExternalAttributes = e.m_ExternalAttributes; | |
668 | Copy(m_Extra, e.m_Extra); | |
669 | Copy(m_LocalExtra, e.m_LocalExtra); | |
f44eaed6 | 670 | m_zipnotifier = NULL; |
00375592 VZ |
671 | if (m_backlink) { |
672 | m_backlink->Release(m_Key); | |
673 | m_backlink = NULL; | |
674 | } | |
675 | } | |
676 | return *this; | |
677 | } | |
678 | ||
679 | wxString wxZipEntry::GetName(wxPathFormat format /*=wxPATH_NATIVE*/) const | |
680 | { | |
681 | bool isDir = IsDir() && !m_Name.empty(); | |
682 | ||
d13a08ab | 683 | // optimisations for common (and easy) cases |
00375592 VZ |
684 | switch (wxFileName::GetFormat(format)) { |
685 | case wxPATH_DOS: | |
686 | { | |
687 | wxString name(isDir ? m_Name + _T("\\") : m_Name); | |
688 | for (size_t i = name.length() - 1; i > 0; --i) | |
689 | if (name[i] == _T('/')) | |
690 | name[i] = _T('\\'); | |
691 | return name; | |
692 | } | |
693 | ||
694 | case wxPATH_UNIX: | |
695 | return isDir ? m_Name + _T("/") : m_Name; | |
696 | ||
697 | default: | |
698 | ; | |
699 | } | |
700 | ||
701 | wxFileName fn; | |
702 | ||
703 | if (isDir) | |
704 | fn.AssignDir(m_Name, wxPATH_UNIX); | |
705 | else | |
706 | fn.Assign(m_Name, wxPATH_UNIX); | |
707 | ||
708 | return fn.GetFullPath(format); | |
709 | } | |
710 | ||
711 | // Static - Internally tars and zips use forward slashes for the path | |
712 | // separator, absolute paths aren't allowed, and directory names have a | |
713 | // trailing slash. This function converts a path into this internal format, | |
714 | // but without a trailing slash for a directory. | |
715 | // | |
716 | wxString wxZipEntry::GetInternalName(const wxString& name, | |
717 | wxPathFormat format /*=wxPATH_NATIVE*/, | |
718 | bool *pIsDir /*=NULL*/) | |
719 | { | |
720 | wxString internal; | |
721 | ||
722 | if (wxFileName::GetFormat(format) != wxPATH_UNIX) | |
723 | internal = wxFileName(name, format).GetFullPath(wxPATH_UNIX); | |
724 | else | |
725 | internal = name; | |
726 | ||
727 | bool isDir = !internal.empty() && internal.Last() == '/'; | |
728 | if (pIsDir) | |
729 | *pIsDir = isDir; | |
730 | if (isDir) | |
731 | internal.erase(internal.length() - 1); | |
732 | ||
733 | while (!internal.empty() && *internal.begin() == '/') | |
734 | internal.erase(0, 1); | |
735 | while (!internal.empty() && internal.compare(0, 2, _T("./")) == 0) | |
736 | internal.erase(0, 2); | |
737 | if (internal == _T(".") || internal == _T("..")) | |
738 | internal = wxEmptyString; | |
739 | ||
740 | return internal; | |
741 | } | |
742 | ||
743 | void wxZipEntry::SetSystemMadeBy(int system) | |
744 | { | |
745 | int mode = GetMode(); | |
746 | bool wasUnix = IsMadeByUnix(); | |
747 | ||
26481315 | 748 | m_SystemMadeBy = (wxUint8)system; |
00375592 VZ |
749 | |
750 | if (!wasUnix && IsMadeByUnix()) { | |
751 | SetIsDir(IsDir()); | |
752 | SetMode(mode); | |
753 | } else if (wasUnix && !IsMadeByUnix()) { | |
754 | m_ExternalAttributes &= 0xffff; | |
755 | } | |
756 | } | |
757 | ||
758 | void wxZipEntry::SetIsDir(bool isDir /*=true*/) | |
759 | { | |
760 | if (isDir) | |
761 | m_ExternalAttributes |= wxZIP_A_SUBDIR; | |
762 | else | |
763 | m_ExternalAttributes &= ~wxZIP_A_SUBDIR; | |
764 | ||
765 | if (IsMadeByUnix()) { | |
766 | m_ExternalAttributes &= ~wxZIP_S_IFMT; | |
767 | if (isDir) | |
768 | m_ExternalAttributes |= wxZIP_S_IFDIR; | |
769 | else | |
770 | m_ExternalAttributes |= wxZIP_S_IFREG; | |
771 | } | |
772 | } | |
773 | ||
774 | // Return unix style permission bits | |
775 | // | |
776 | int wxZipEntry::GetMode() const | |
777 | { | |
778 | // return unix permissions if present | |
779 | if (IsMadeByUnix()) | |
780 | return (m_ExternalAttributes >> 16) & 0777; | |
781 | ||
782 | // otherwise synthesize from the dos attribs | |
783 | int mode = 0644; | |
784 | if (m_ExternalAttributes & wxZIP_A_RDONLY) | |
785 | mode &= ~0200; | |
786 | if (m_ExternalAttributes & wxZIP_A_SUBDIR) | |
787 | mode |= 0111; | |
788 | ||
789 | return mode; | |
790 | } | |
791 | ||
792 | // Set unix permissions | |
793 | // | |
794 | void wxZipEntry::SetMode(int mode) | |
795 | { | |
796 | // Set dos attrib bits to be compatible | |
797 | if (mode & 0222) | |
798 | m_ExternalAttributes &= ~wxZIP_A_RDONLY; | |
799 | else | |
800 | m_ExternalAttributes |= wxZIP_A_RDONLY; | |
801 | ||
802 | // set the actual unix permission bits if the system type allows | |
803 | if (IsMadeByUnix()) { | |
804 | m_ExternalAttributes &= ~(0777L << 16); | |
805 | m_ExternalAttributes |= (mode & 0777L) << 16; | |
806 | } | |
807 | } | |
808 | ||
809 | const char *wxZipEntry::GetExtra() const | |
810 | { | |
811 | return m_Extra ? m_Extra->GetData() : NULL; | |
812 | } | |
813 | ||
814 | size_t wxZipEntry::GetExtraLen() const | |
815 | { | |
816 | return m_Extra ? m_Extra->GetSize() : 0; | |
817 | } | |
818 | ||
819 | void wxZipEntry::SetExtra(const char *extra, size_t len) | |
820 | { | |
821 | Unique(m_Extra, len); | |
822 | if (len) | |
823 | memcpy(m_Extra->GetData(), extra, len); | |
824 | } | |
825 | ||
826 | const char *wxZipEntry::GetLocalExtra() const | |
827 | { | |
828 | return m_LocalExtra ? m_LocalExtra->GetData() : NULL; | |
829 | } | |
830 | ||
831 | size_t wxZipEntry::GetLocalExtraLen() const | |
832 | { | |
833 | return m_LocalExtra ? m_LocalExtra->GetSize() : 0; | |
834 | } | |
835 | ||
836 | void wxZipEntry::SetLocalExtra(const char *extra, size_t len) | |
837 | { | |
838 | Unique(m_LocalExtra, len); | |
839 | if (len) | |
840 | memcpy(m_LocalExtra->GetData(), extra, len); | |
841 | } | |
842 | ||
843 | void wxZipEntry::SetNotifier(wxZipNotifier& notifier) | |
844 | { | |
845 | wxArchiveEntry::UnsetNotifier(); | |
846 | m_zipnotifier = ¬ifier; | |
847 | m_zipnotifier->OnEntryUpdated(*this); | |
848 | } | |
849 | ||
850 | void wxZipEntry::Notify() | |
851 | { | |
852 | if (m_zipnotifier) | |
853 | m_zipnotifier->OnEntryUpdated(*this); | |
854 | else if (GetNotifier()) | |
855 | GetNotifier()->OnEntryUpdated(*this); | |
856 | } | |
857 | ||
858 | void wxZipEntry::UnsetNotifier() | |
859 | { | |
860 | wxArchiveEntry::UnsetNotifier(); | |
861 | m_zipnotifier = NULL; | |
862 | } | |
863 | ||
864 | size_t wxZipEntry::ReadLocal(wxInputStream& stream, wxMBConv& conv) | |
865 | { | |
866 | wxUint16 nameLen, extraLen; | |
867 | wxUint32 compressedSize, size, crc; | |
868 | ||
869 | wxDataInputStream ds(stream); | |
870 | ||
871 | ds >> m_VersionNeeded >> m_Flags >> m_Method; | |
872 | SetDateTime(wxDateTime().SetFromDOS(ds.Read32())); | |
873 | ds >> crc >> compressedSize >> size >> nameLen >> extraLen; | |
874 | ||
875 | bool sumsValid = (m_Flags & wxZIP_SUMS_FOLLOW) == 0; | |
876 | ||
877 | if (sumsValid || crc) | |
878 | m_Crc = crc; | |
879 | if ((sumsValid || compressedSize) || m_Method == wxZIP_METHOD_STORE) | |
880 | m_CompressedSize = compressedSize; | |
881 | if ((sumsValid || size) || m_Method == wxZIP_METHOD_STORE) | |
882 | m_Size = size; | |
883 | ||
884 | SetName(ReadString(stream, nameLen, conv), wxPATH_UNIX); | |
885 | ||
886 | if (extraLen || GetLocalExtraLen()) { | |
887 | Unique(m_LocalExtra, extraLen); | |
888 | if (extraLen) | |
889 | stream.Read(m_LocalExtra->GetData(), extraLen); | |
890 | } | |
891 | ||
892 | return LOCAL_SIZE + nameLen + extraLen; | |
893 | } | |
894 | ||
895 | size_t wxZipEntry::WriteLocal(wxOutputStream& stream, wxMBConv& conv) const | |
896 | { | |
897 | wxString unixName = GetName(wxPATH_UNIX); | |
898 | const wxWX2MBbuf name_buf = conv.cWX2MB(unixName); | |
899 | const char *name = name_buf; | |
900 | if (!name) name = ""; | |
26481315 | 901 | wxUint16 nameLen = (wxUint16)strlen(name); |
00375592 VZ |
902 | |
903 | wxDataOutputStream ds(stream); | |
904 | ||
905 | ds << m_VersionNeeded << m_Flags << m_Method; | |
906 | ds.Write32(GetDateTime().GetAsDOS()); | |
ba9bbf13 | 907 | |
00375592 | 908 | ds.Write32(m_Crc); |
26481315 WS |
909 | ds.Write32(m_CompressedSize != wxInvalidOffset ? (wxUint32)m_CompressedSize : 0); |
910 | ds.Write32(m_Size != wxInvalidOffset ? (wxUint32)m_Size : 0); | |
00375592 VZ |
911 | |
912 | ds << nameLen; | |
26481315 | 913 | wxUint16 extraLen = (wxUint16)GetLocalExtraLen(); |
00375592 VZ |
914 | ds.Write16(extraLen); |
915 | ||
916 | stream.Write(name, nameLen); | |
917 | if (extraLen) | |
918 | stream.Write(m_LocalExtra->GetData(), extraLen); | |
919 | ||
920 | return LOCAL_SIZE + nameLen + extraLen; | |
921 | } | |
922 | ||
923 | size_t wxZipEntry::ReadCentral(wxInputStream& stream, wxMBConv& conv) | |
924 | { | |
925 | wxUint16 nameLen, extraLen, commentLen; | |
926 | ||
927 | wxDataInputStream ds(stream); | |
928 | ||
929 | ds >> m_VersionMadeBy >> m_SystemMadeBy; | |
930 | ||
931 | SetVersionNeeded(ds.Read16()); | |
932 | SetFlags(ds.Read16()); | |
933 | SetMethod(ds.Read16()); | |
934 | SetDateTime(wxDateTime().SetFromDOS(ds.Read32())); | |
935 | SetCrc(ds.Read32()); | |
936 | SetCompressedSize(ds.Read32()); | |
937 | SetSize(ds.Read32()); | |
938 | ||
939 | ds >> nameLen >> extraLen >> commentLen | |
940 | >> m_DiskStart >> m_InternalAttributes >> m_ExternalAttributes; | |
941 | SetOffset(ds.Read32()); | |
ba9bbf13 | 942 | |
00375592 VZ |
943 | SetName(ReadString(stream, nameLen, conv), wxPATH_UNIX); |
944 | ||
945 | if (extraLen || GetExtraLen()) { | |
946 | Unique(m_Extra, extraLen); | |
947 | if (extraLen) | |
948 | stream.Read(m_Extra->GetData(), extraLen); | |
949 | } | |
950 | ||
951 | if (commentLen) | |
952 | m_Comment = ReadString(stream, commentLen, conv); | |
953 | else | |
954 | m_Comment.clear(); | |
955 | ||
956 | return CENTRAL_SIZE + nameLen + extraLen + commentLen; | |
957 | } | |
958 | ||
959 | size_t wxZipEntry::WriteCentral(wxOutputStream& stream, wxMBConv& conv) const | |
960 | { | |
961 | wxString unixName = GetName(wxPATH_UNIX); | |
962 | const wxWX2MBbuf name_buf = conv.cWX2MB(unixName); | |
963 | const char *name = name_buf; | |
964 | if (!name) name = ""; | |
26481315 | 965 | wxUint16 nameLen = (wxUint16)strlen(name); |
00375592 VZ |
966 | |
967 | const wxWX2MBbuf comment_buf = conv.cWX2MB(m_Comment); | |
968 | const char *comment = comment_buf; | |
969 | if (!comment) comment = ""; | |
26481315 | 970 | wxUint16 commentLen = (wxUint16)strlen(comment); |
00375592 | 971 | |
26481315 | 972 | wxUint16 extraLen = (wxUint16)GetExtraLen(); |
00375592 VZ |
973 | |
974 | wxDataOutputStream ds(stream); | |
975 | ||
976 | ds << CENTRAL_MAGIC << m_VersionMadeBy << m_SystemMadeBy; | |
977 | ||
26481315 WS |
978 | ds.Write16((wxUint16)GetVersionNeeded()); |
979 | ds.Write16((wxUint16)GetFlags()); | |
980 | ds.Write16((wxUint16)GetMethod()); | |
00375592 VZ |
981 | ds.Write32(GetDateTime().GetAsDOS()); |
982 | ds.Write32(GetCrc()); | |
26481315 WS |
983 | ds.Write32((wxUint32)GetCompressedSize()); |
984 | ds.Write32((wxUint32)GetSize()); | |
00375592 VZ |
985 | ds.Write16(nameLen); |
986 | ds.Write16(extraLen); | |
987 | ||
988 | ds << commentLen << m_DiskStart << m_InternalAttributes | |
989 | << m_ExternalAttributes << (wxUint32)GetOffset(); | |
ba9bbf13 | 990 | |
00375592 VZ |
991 | stream.Write(name, nameLen); |
992 | if (extraLen) | |
993 | stream.Write(GetExtra(), extraLen); | |
994 | stream.Write(comment, commentLen); | |
995 | ||
996 | return CENTRAL_SIZE + nameLen + extraLen + commentLen; | |
997 | } | |
998 | ||
999 | // Info-zip prefixes this record with a signature, but pkzip doesn't. So if | |
1000 | // the 1st value is the signature then it is probably an info-zip record, | |
1001 | // though there is a small chance that it is in fact a pkzip record which | |
1002 | // happens to have the signature as it's CRC. | |
1003 | // | |
1004 | size_t wxZipEntry::ReadDescriptor(wxInputStream& stream) | |
1005 | { | |
1006 | wxDataInputStream ds(stream); | |
ba9bbf13 | 1007 | |
00375592 VZ |
1008 | m_Crc = ds.Read32(); |
1009 | m_CompressedSize = ds.Read32(); | |
1010 | m_Size = ds.Read32(); | |
1011 | ||
1012 | // if 1st value is the signature then this is probably an info-zip record | |
1013 | if (m_Crc == SUMS_MAGIC) | |
1014 | { | |
1015 | char buf[8]; | |
1016 | stream.Read(buf, sizeof(buf)); | |
1017 | wxUint32 u1 = CrackUint32(buf); | |
1018 | wxUint32 u2 = CrackUint32(buf + 4); | |
ba9bbf13 | 1019 | |
00375592 VZ |
1020 | // look for the signature of the following record to decide which |
1021 | if ((u1 == LOCAL_MAGIC || u1 == CENTRAL_MAGIC) && | |
1022 | (u2 != LOCAL_MAGIC && u2 != CENTRAL_MAGIC)) | |
1023 | { | |
1024 | // it's a pkzip style record after all! | |
1025 | stream.Ungetch(buf, sizeof(buf)); | |
1026 | } | |
1027 | else | |
1028 | { | |
1029 | // it's an info-zip record as expected | |
1030 | stream.Ungetch(buf + 4, sizeof(buf) - 4); | |
26481315 | 1031 | m_Crc = (wxUint32)m_CompressedSize; |
00375592 VZ |
1032 | m_CompressedSize = m_Size; |
1033 | m_Size = u1; | |
1034 | return SUMS_SIZE + 4; | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | return SUMS_SIZE; | |
1039 | } | |
1040 | ||
1041 | size_t wxZipEntry::WriteDescriptor(wxOutputStream& stream, wxUint32 crc, | |
1042 | wxFileOffset compressedSize, wxFileOffset size) | |
1043 | { | |
1044 | m_Crc = crc; | |
1045 | m_CompressedSize = compressedSize; | |
1046 | m_Size = size; | |
1047 | ||
1048 | wxDataOutputStream ds(stream); | |
1049 | ||
1050 | ds.Write32(crc); | |
26481315 WS |
1051 | ds.Write32((wxUint32)compressedSize); |
1052 | ds.Write32((wxUint32)size); | |
00375592 VZ |
1053 | |
1054 | return SUMS_SIZE; | |
1055 | } | |
1056 | ||
1057 | ||
1058 | ///////////////////////////////////////////////////////////////////////////// | |
1059 | // wxZipEndRec - holds the end of central directory record | |
1060 | ||
1061 | class wxZipEndRec | |
1062 | { | |
1063 | public: | |
1064 | wxZipEndRec(); | |
1065 | ||
1066 | int GetDiskNumber() const { return m_DiskNumber; } | |
1067 | int GetStartDisk() const { return m_StartDisk; } | |
1068 | int GetEntriesHere() const { return m_EntriesHere; } | |
1069 | int GetTotalEntries() const { return m_TotalEntries; } | |
1070 | wxFileOffset GetSize() const { return m_Size; } | |
1071 | wxFileOffset GetOffset() const { return m_Offset; } | |
1072 | wxString GetComment() const { return m_Comment; } | |
1073 | ||
26481315 WS |
1074 | void SetDiskNumber(int num) { m_DiskNumber = (wxUint16)num; } |
1075 | void SetStartDisk(int num) { m_StartDisk = (wxUint16)num; } | |
1076 | void SetEntriesHere(int num) { m_EntriesHere = (wxUint16)num; } | |
1077 | void SetTotalEntries(int num) { m_TotalEntries = (wxUint16)num; } | |
00375592 VZ |
1078 | void SetSize(wxFileOffset size) { m_Size = (wxUint32)size; } |
1079 | void SetOffset(wxFileOffset offset) { m_Offset = (wxUint32)offset; } | |
1080 | void SetComment(const wxString& comment) { m_Comment = comment; } | |
1081 | ||
1082 | bool Read(wxInputStream& stream, wxMBConv& conv); | |
1083 | bool Write(wxOutputStream& stream, wxMBConv& conv) const; | |
1084 | ||
1085 | private: | |
1086 | wxUint16 m_DiskNumber; | |
1087 | wxUint16 m_StartDisk; | |
1088 | wxUint16 m_EntriesHere; | |
1089 | wxUint16 m_TotalEntries; | |
1090 | wxUint32 m_Size; | |
1091 | wxUint32 m_Offset; | |
1092 | wxString m_Comment; | |
1093 | }; | |
1094 | ||
1095 | wxZipEndRec::wxZipEndRec() | |
1096 | : m_DiskNumber(0), | |
1097 | m_StartDisk(0), | |
1098 | m_EntriesHere(0), | |
1099 | m_TotalEntries(0), | |
1100 | m_Size(0), | |
1101 | m_Offset(0) | |
1102 | { | |
1103 | } | |
1104 | ||
1105 | bool wxZipEndRec::Write(wxOutputStream& stream, wxMBConv& conv) const | |
1106 | { | |
1107 | const wxWX2MBbuf comment_buf = conv.cWX2MB(m_Comment); | |
1108 | const char *comment = comment_buf; | |
1109 | if (!comment) comment = ""; | |
26481315 | 1110 | wxUint16 commentLen = (wxUint16)strlen(comment); |
00375592 VZ |
1111 | |
1112 | wxDataOutputStream ds(stream); | |
1113 | ||
1114 | ds << END_MAGIC << m_DiskNumber << m_StartDisk << m_EntriesHere | |
1115 | << m_TotalEntries << m_Size << m_Offset << commentLen; | |
1116 | ||
1117 | stream.Write(comment, commentLen); | |
1118 | ||
1119 | return stream.IsOk(); | |
1120 | } | |
1121 | ||
1122 | bool wxZipEndRec::Read(wxInputStream& stream, wxMBConv& conv) | |
1123 | { | |
1124 | wxDataInputStream ds(stream); | |
1125 | wxUint16 commentLen; | |
1126 | ||
1127 | ds >> m_DiskNumber >> m_StartDisk >> m_EntriesHere | |
1128 | >> m_TotalEntries >> m_Size >> m_Offset >> commentLen; | |
1129 | ||
1130 | if (commentLen) | |
1131 | m_Comment = ReadString(stream, commentLen, conv); | |
1132 | ||
1133 | if (stream.IsOk()) | |
1134 | if (m_DiskNumber == 0 && m_StartDisk == 0 && | |
1135 | m_EntriesHere == m_TotalEntries) | |
1136 | return true; | |
1137 | else | |
1138 | wxLogError(_("unsupported zip archive")); | |
1139 | ||
1140 | return false; | |
1141 | } | |
1142 | ||
1143 | ||
1144 | ///////////////////////////////////////////////////////////////////////////// | |
1145 | // A weak link from an input stream to an output stream | |
1146 | ||
1147 | class wxZipStreamLink | |
1148 | { | |
1149 | public: | |
1150 | wxZipStreamLink(wxZipOutputStream *stream) : m_ref(1), m_stream(stream) { } | |
1151 | ||
1152 | wxZipStreamLink *AddRef() { m_ref++; return this; } | |
1153 | wxZipOutputStream *GetOutputStream() const { return m_stream; } | |
1154 | ||
1155 | void Release(class wxZipInputStream *WXUNUSED(s)) | |
1156 | { if (--m_ref == 0) delete this; } | |
1157 | void Release(class wxZipOutputStream *WXUNUSED(s)) | |
1158 | { m_stream = NULL; if (--m_ref == 0) delete this; } | |
1159 | ||
1160 | private: | |
f8fbc92b | 1161 | ~wxZipStreamLink() { } |
00375592 VZ |
1162 | |
1163 | int m_ref; | |
1164 | wxZipOutputStream *m_stream; | |
1165 | }; | |
1166 | ||
1167 | ||
1168 | ///////////////////////////////////////////////////////////////////////////// | |
1169 | // Input stream | |
1170 | ||
d13a08ab MW |
1171 | // leave the default wxZipEntryPtr free for users |
1172 | wxDECLARE_SCOPED_PTR(wxZipEntry, wx__ZipEntryPtr) | |
1173 | wxDEFINE_SCOPED_PTR (wxZipEntry, wx__ZipEntryPtr) | |
00375592 VZ |
1174 | |
1175 | // constructor | |
1176 | // | |
1177 | wxZipInputStream::wxZipInputStream(wxInputStream& stream, | |
1178 | wxMBConv& conv /*=wxConvLocal*/) | |
1179 | : wxArchiveInputStream(stream, conv) | |
1180 | { | |
00375592 VZ |
1181 | Init(); |
1182 | } | |
1183 | ||
d13a08ab MW |
1184 | #if 1 //WXWIN_COMPATIBILITY_2_6 |
1185 | ||
fe47da7b MW |
1186 | // Part of the compatibility constructor, which has been made inline to |
1187 | // avoid a problem with it not being exported by mingw 3.2.3 | |
00375592 | 1188 | // |
fe47da7b | 1189 | void wxZipInputStream::Init(const wxString& file) |
00375592 VZ |
1190 | { |
1191 | // no error messages | |
1192 | wxLogNull nolog; | |
1193 | Init(); | |
d13a08ab | 1194 | m_allowSeeking = true; |
fe47da7b | 1195 | m_ffile = wx_static_cast(wxFFileInputStream*, m_parent_i_stream); |
d13a08ab | 1196 | wx__ZipEntryPtr entry; |
00375592 VZ |
1197 | |
1198 | if (m_ffile->Ok()) { | |
1199 | do { | |
1200 | entry.reset(GetNextEntry()); | |
1201 | } | |
1202 | while (entry.get() != NULL && entry->GetInternalName() != file); | |
1203 | } | |
1204 | ||
1205 | if (entry.get() == NULL) | |
1206 | m_lasterror = wxSTREAM_READ_ERROR; | |
1207 | } | |
1208 | ||
1209 | wxInputStream& wxZipInputStream::OpenFile(const wxString& archive) | |
1210 | { | |
1211 | wxLogNull nolog; | |
fe47da7b | 1212 | return *new wxFFileInputStream(archive); |
00375592 VZ |
1213 | } |
1214 | ||
d13a08ab MW |
1215 | #endif // WXWIN_COMPATIBILITY_2_6 |
1216 | ||
00375592 VZ |
1217 | void wxZipInputStream::Init() |
1218 | { | |
1219 | m_store = new wxStoredInputStream(*m_parent_i_stream); | |
1220 | m_inflate = NULL; | |
1221 | m_rawin = NULL; | |
1222 | m_raw = false; | |
1223 | m_headerSize = 0; | |
1224 | m_decomp = NULL; | |
1225 | m_parentSeekable = false; | |
1226 | m_weaklinks = new wxZipWeakLinks; | |
1227 | m_streamlink = NULL; | |
1228 | m_offsetAdjustment = 0; | |
1229 | m_position = wxInvalidOffset; | |
1230 | m_signature = 0; | |
1231 | m_TotalEntries = 0; | |
1232 | m_lasterror = m_parent_i_stream->GetLastError(); | |
fe47da7b MW |
1233 | m_ffile = NULL; |
1234 | #if 1 //WXWIN_COMPATIBILITY_2_6 | |
1235 | m_allowSeeking = false; | |
1236 | #endif | |
00375592 VZ |
1237 | } |
1238 | ||
1239 | wxZipInputStream::~wxZipInputStream() | |
1240 | { | |
1241 | CloseDecompressor(m_decomp); | |
1242 | ||
1243 | delete m_store; | |
1244 | delete m_inflate; | |
1245 | delete m_rawin; | |
1246 | delete m_ffile; | |
1247 | ||
1248 | m_weaklinks->Release(this); | |
ba9bbf13 | 1249 | |
00375592 VZ |
1250 | if (m_streamlink) |
1251 | m_streamlink->Release(this); | |
1252 | } | |
1253 | ||
1254 | wxString wxZipInputStream::GetComment() | |
1255 | { | |
1256 | if (m_position == wxInvalidOffset) | |
1257 | if (!LoadEndRecord()) | |
1258 | return wxEmptyString; | |
1259 | ||
1260 | if (!m_parentSeekable && Eof() && m_signature) { | |
1261 | m_lasterror = wxSTREAM_NO_ERROR; | |
1262 | m_lasterror = ReadLocal(true); | |
1263 | } | |
1264 | ||
1265 | return m_Comment; | |
1266 | } | |
1267 | ||
1268 | int wxZipInputStream::GetTotalEntries() | |
1269 | { | |
1270 | if (m_position == wxInvalidOffset) | |
1271 | LoadEndRecord(); | |
1272 | return m_TotalEntries; | |
1273 | } | |
1274 | ||
1275 | wxZipStreamLink *wxZipInputStream::MakeLink(wxZipOutputStream *out) | |
1276 | { | |
1277 | wxZipStreamLink *link = NULL; | |
1278 | ||
1279 | if (!m_parentSeekable && (IsOpened() || !Eof())) { | |
1280 | link = new wxZipStreamLink(out); | |
1281 | if (m_streamlink) | |
1282 | m_streamlink->Release(this); | |
1283 | m_streamlink = link->AddRef(); | |
1284 | } | |
1285 | ||
1286 | return link; | |
1287 | } | |
d78b3d64 | 1288 | |
00375592 VZ |
1289 | bool wxZipInputStream::LoadEndRecord() |
1290 | { | |
1291 | wxCHECK(m_position == wxInvalidOffset, false); | |
1292 | if (!IsOk()) | |
1293 | return false; | |
ea4f5235 | 1294 | |
00375592 VZ |
1295 | m_position = 0; |
1296 | ||
1297 | // First find the end-of-central-directory record. | |
1298 | if (!FindEndRecord()) { | |
1299 | // failed, so either this is a non-seekable stream (ok), or not a zip | |
1300 | if (m_parentSeekable) { | |
1301 | m_lasterror = wxSTREAM_READ_ERROR; | |
1302 | wxLogError(_("invalid zip file")); | |
1303 | return false; | |
1304 | } | |
1305 | else { | |
1306 | wxLogNull nolog; | |
1307 | wxFileOffset pos = m_parent_i_stream->TellI(); | |
1308 | // FIXME | |
1309 | //if (pos != wxInvalidOffset) | |
1310 | if (pos >= 0 && pos <= LONG_MAX) | |
1311 | m_offsetAdjustment = m_position = pos; | |
1312 | return true; | |
1313 | } | |
1314 | } | |
1315 | ||
1316 | wxZipEndRec endrec; | |
1317 | ||
1318 | // Read in the end record | |
1319 | wxFileOffset endPos = m_parent_i_stream->TellI() - 4; | |
1320 | if (!endrec.Read(*m_parent_i_stream, GetConv())) { | |
1321 | if (!*m_parent_i_stream) { | |
1322 | m_lasterror = wxSTREAM_READ_ERROR; | |
1323 | return false; | |
1324 | } | |
1325 | // TODO: try this out | |
1326 | wxLogWarning(_("assuming this is a multi-part zip concatenated")); | |
1327 | } | |
1328 | ||
1329 | m_TotalEntries = endrec.GetTotalEntries(); | |
1330 | m_Comment = endrec.GetComment(); | |
ea4f5235 | 1331 | |
00375592 | 1332 | // Now find the central-directory. we have the file offset of |
ba9bbf13 | 1333 | // the CD, so look there first. |
00375592 VZ |
1334 | if (m_parent_i_stream->SeekI(endrec.GetOffset()) != wxInvalidOffset && |
1335 | ReadSignature() == CENTRAL_MAGIC) { | |
1336 | m_signature = CENTRAL_MAGIC; | |
1337 | m_position = endrec.GetOffset(); | |
1338 | m_offsetAdjustment = 0; | |
1339 | return true; | |
1340 | } | |
ba9bbf13 | 1341 | |
00375592 VZ |
1342 | // If it's not there, then it could be that the zip has been appended |
1343 | // to a self extractor, so take the CD size (also in endrec), subtract | |
1344 | // it from the file offset of the end-central-directory and look there. | |
1345 | if (m_parent_i_stream->SeekI(endPos - endrec.GetSize()) | |
1346 | != wxInvalidOffset && ReadSignature() == CENTRAL_MAGIC) { | |
1347 | m_signature = CENTRAL_MAGIC; | |
1348 | m_position = endPos - endrec.GetSize(); | |
1349 | m_offsetAdjustment = m_position - endrec.GetOffset(); | |
1350 | return true; | |
1351 | } | |
1352 | ||
1353 | wxLogError(_("can't find central directory in zip")); | |
1354 | m_lasterror = wxSTREAM_READ_ERROR; | |
1355 | return false; | |
1356 | } | |
5526e819 | 1357 | |
00375592 VZ |
1358 | // Find the end-of-central-directory record. |
1359 | // If found the stream will be positioned just past the 4 signature bytes. | |
1360 | // | |
1361 | bool wxZipInputStream::FindEndRecord() | |
5526e819 | 1362 | { |
3c70014d MW |
1363 | if (!m_parent_i_stream->IsSeekable()) |
1364 | return false; | |
1365 | ||
00375592 | 1366 | // usually it's 22 bytes in size and the last thing in the file |
ba9bbf13 | 1367 | { |
00375592 VZ |
1368 | wxLogNull nolog; |
1369 | if (m_parent_i_stream->SeekI(-END_SIZE, wxFromEnd) == wxInvalidOffset) | |
1370 | return false; | |
1371 | } | |
5526e819 | 1372 | |
00375592 VZ |
1373 | m_parentSeekable = true; |
1374 | m_signature = 0; | |
1375 | char magic[4]; | |
1376 | if (m_parent_i_stream->Read(magic, 4).LastRead() != 4) | |
1377 | return false; | |
1378 | if ((m_signature = CrackUint32(magic)) == END_MAGIC) | |
1379 | return true; | |
1380 | ||
1381 | // unfortunately, the record has a comment field that can be up to 65535 | |
1382 | // bytes in length, so if the signature not found then search backwards. | |
1383 | wxFileOffset pos = m_parent_i_stream->TellI(); | |
1384 | const int BUFSIZE = 1024; | |
1385 | wxCharBuffer buf(BUFSIZE); | |
1386 | ||
1387 | memcpy(buf.data(), magic, 3); | |
1388 | wxFileOffset minpos = wxMax(pos - 65535L, 0); | |
1389 | ||
1390 | while (pos > minpos) { | |
26481315 | 1391 | size_t len = (size_t)(pos - wxMax(pos - (BUFSIZE - 3), minpos)); |
00375592 VZ |
1392 | memcpy(buf.data() + len, buf, 3); |
1393 | pos -= len; | |
ba9bbf13 | 1394 | |
00375592 VZ |
1395 | if (m_parent_i_stream->SeekI(pos, wxFromStart) == wxInvalidOffset || |
1396 | m_parent_i_stream->Read(buf.data(), len).LastRead() != len) | |
1397 | return false; | |
1398 | ||
1399 | char *p = buf.data() + len; | |
1400 | ||
1401 | while (p-- > buf.data()) { | |
1402 | if ((m_signature = CrackUint32(p)) == END_MAGIC) { | |
1403 | size_t remainder = buf.data() + len - p; | |
1404 | if (remainder > 4) | |
1405 | m_parent_i_stream->Ungetch(p + 4, remainder - 4); | |
1406 | return true; | |
1407 | } | |
1408 | } | |
5526e819 | 1409 | } |
00375592 VZ |
1410 | |
1411 | return false; | |
1412 | } | |
1413 | ||
1414 | wxZipEntry *wxZipInputStream::GetNextEntry() | |
1415 | { | |
1416 | if (m_position == wxInvalidOffset) | |
1417 | if (!LoadEndRecord()) | |
1418 | return NULL; | |
1419 | ||
1420 | m_lasterror = m_parentSeekable ? ReadCentral() : ReadLocal(); | |
1421 | if (!IsOk()) | |
1422 | return NULL; | |
1423 | ||
d13a08ab | 1424 | wx__ZipEntryPtr entry(new wxZipEntry(m_entry)); |
00375592 VZ |
1425 | entry->m_backlink = m_weaklinks->AddEntry(entry.get(), entry->GetKey()); |
1426 | return entry.release(); | |
1427 | } | |
1428 | ||
1429 | wxStreamError wxZipInputStream::ReadCentral() | |
1430 | { | |
1431 | if (!AtHeader()) | |
1432 | CloseEntry(); | |
1433 | ||
1434 | if (m_signature == END_MAGIC) | |
1435 | return wxSTREAM_EOF; | |
1436 | ||
1437 | if (m_signature != CENTRAL_MAGIC) { | |
1438 | wxLogError(_("error reading zip central directory")); | |
1439 | return wxSTREAM_READ_ERROR; | |
5526e819 | 1440 | } |
e90c1d2a | 1441 | |
00375592 VZ |
1442 | if (QuietSeek(*m_parent_i_stream, m_position + 4) == wxInvalidOffset) |
1443 | return wxSTREAM_READ_ERROR; | |
5526e819 | 1444 | |
00375592 VZ |
1445 | m_position += m_entry.ReadCentral(*m_parent_i_stream, GetConv()); |
1446 | if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) { | |
1447 | m_signature = 0; | |
1448 | return wxSTREAM_READ_ERROR; | |
5526e819 | 1449 | } |
00375592 VZ |
1450 | |
1451 | m_signature = ReadSignature(); | |
1452 | ||
1453 | if (m_offsetAdjustment) | |
1454 | m_entry.SetOffset(m_entry.GetOffset() + m_offsetAdjustment); | |
1455 | m_entry.SetKey(m_entry.GetOffset()); | |
1456 | ||
1457 | return wxSTREAM_NO_ERROR; | |
5526e819 VS |
1458 | } |
1459 | ||
00375592 VZ |
1460 | wxStreamError wxZipInputStream::ReadLocal(bool readEndRec /*=false*/) |
1461 | { | |
1462 | if (!AtHeader()) | |
1463 | CloseEntry(); | |
1464 | ||
1465 | if (!m_signature) | |
1466 | m_signature = ReadSignature(); | |
5526e819 | 1467 | |
00375592 VZ |
1468 | if (m_signature == CENTRAL_MAGIC || m_signature == END_MAGIC) { |
1469 | if (m_streamlink && !m_streamlink->GetOutputStream()) { | |
1470 | m_streamlink->Release(this); | |
1471 | m_streamlink = NULL; | |
1472 | } | |
1473 | } | |
5526e819 | 1474 | |
00375592 VZ |
1475 | while (m_signature == CENTRAL_MAGIC) { |
1476 | if (m_weaklinks->IsEmpty() && m_streamlink == NULL) | |
1477 | return wxSTREAM_EOF; | |
ba9bbf13 | 1478 | |
00375592 VZ |
1479 | m_position += m_entry.ReadCentral(*m_parent_i_stream, GetConv()); |
1480 | m_signature = 0; | |
1481 | if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) | |
1482 | return wxSTREAM_READ_ERROR; | |
1483 | ||
1484 | wxZipEntry *entry = m_weaklinks->GetEntry(m_entry.GetOffset()); | |
1485 | if (entry) { | |
1486 | entry->SetSystemMadeBy(m_entry.GetSystemMadeBy()); | |
1487 | entry->SetVersionMadeBy(m_entry.GetVersionMadeBy()); | |
1488 | entry->SetComment(m_entry.GetComment()); | |
1489 | entry->SetDiskStart(m_entry.GetDiskStart()); | |
1490 | entry->SetInternalAttributes(m_entry.GetInternalAttributes()); | |
1491 | entry->SetExternalAttributes(m_entry.GetExternalAttributes()); | |
1492 | Copy(entry->m_Extra, m_entry.m_Extra); | |
1493 | entry->Notify(); | |
1494 | m_weaklinks->RemoveEntry(entry->GetOffset()); | |
1495 | } | |
1496 | ||
1497 | m_signature = ReadSignature(); | |
1498 | } | |
1499 | ||
1500 | if (m_signature == END_MAGIC) { | |
1501 | if (readEndRec || m_streamlink) { | |
1502 | wxZipEndRec endrec; | |
1503 | endrec.Read(*m_parent_i_stream, GetConv()); | |
1504 | m_Comment = endrec.GetComment(); | |
1505 | m_signature = 0; | |
1506 | if (m_streamlink) { | |
1507 | m_streamlink->GetOutputStream()->SetComment(endrec.GetComment()); | |
1508 | m_streamlink->Release(this); | |
1509 | m_streamlink = NULL; | |
1510 | } | |
1511 | } | |
1512 | return wxSTREAM_EOF; | |
1513 | } | |
ba9bbf13 | 1514 | |
00375592 VZ |
1515 | if (m_signature != LOCAL_MAGIC) { |
1516 | wxLogError(_("error reading zip local header")); | |
1517 | return wxSTREAM_READ_ERROR; | |
1518 | } | |
1519 | ||
1520 | m_headerSize = m_entry.ReadLocal(*m_parent_i_stream, GetConv()); | |
1521 | m_signature = 0; | |
1522 | m_entry.SetOffset(m_position); | |
1523 | m_entry.SetKey(m_position); | |
1524 | ||
1525 | if (m_parent_i_stream->GetLastError() == wxSTREAM_READ_ERROR) { | |
1526 | return wxSTREAM_READ_ERROR; | |
1527 | } else { | |
1528 | m_TotalEntries++; | |
1529 | return wxSTREAM_NO_ERROR; | |
1530 | } | |
1531 | } | |
1532 | ||
1533 | wxUint32 wxZipInputStream::ReadSignature() | |
5526e819 | 1534 | { |
00375592 VZ |
1535 | char magic[4]; |
1536 | m_parent_i_stream->Read(magic, 4); | |
1537 | return m_parent_i_stream->LastRead() == 4 ? CrackUint32(magic) : 0; | |
1538 | } | |
1539 | ||
1540 | bool wxZipInputStream::OpenEntry(wxArchiveEntry& entry) | |
1541 | { | |
1542 | wxZipEntry *zipEntry = wxStaticCast(&entry, wxZipEntry); | |
1543 | return zipEntry ? OpenEntry(*zipEntry) : false; | |
1544 | } | |
1545 | ||
1546 | // Open an entry | |
1547 | // | |
1548 | bool wxZipInputStream::DoOpen(wxZipEntry *entry, bool raw) | |
1549 | { | |
1550 | if (m_position == wxInvalidOffset) | |
1551 | if (!LoadEndRecord()) | |
1552 | return false; | |
1553 | if (m_lasterror == wxSTREAM_READ_ERROR) | |
1554 | return false; | |
1555 | wxCHECK(!IsOpened(), false); | |
1556 | ||
1557 | m_raw = raw; | |
1558 | ||
1559 | if (entry) { | |
1560 | if (AfterHeader() && entry->GetKey() == m_entry.GetOffset()) | |
1561 | return true; | |
1562 | // can only open the current entry on a non-seekable stream | |
1563 | wxCHECK(m_parentSeekable, false); | |
1564 | } | |
ba9bbf13 | 1565 | |
00375592 | 1566 | m_lasterror = wxSTREAM_READ_ERROR; |
ba9bbf13 | 1567 | |
00375592 VZ |
1568 | if (entry) |
1569 | m_entry = *entry; | |
1570 | ||
1571 | if (m_parentSeekable) { | |
1572 | if (QuietSeek(*m_parent_i_stream, m_entry.GetOffset()) | |
1573 | == wxInvalidOffset) | |
1574 | return false; | |
1575 | if (ReadSignature() != LOCAL_MAGIC) { | |
1576 | wxLogError(_("bad zipfile offset to entry")); | |
1577 | return false; | |
1578 | } | |
1579 | } | |
1580 | ||
1581 | if (m_parentSeekable || AtHeader()) { | |
1582 | m_headerSize = m_entry.ReadLocal(*m_parent_i_stream, GetConv()); | |
1583 | if (m_parentSeekable) { | |
1584 | wxZipEntry *ref = m_weaklinks->GetEntry(m_entry.GetKey()); | |
1585 | if (ref) { | |
1586 | Copy(ref->m_LocalExtra, m_entry.m_LocalExtra); | |
1587 | ref->Notify(); | |
1588 | m_weaklinks->RemoveEntry(ref->GetKey()); | |
1589 | } | |
1590 | if (entry && entry != ref) { | |
1591 | Copy(entry->m_LocalExtra, m_entry.m_LocalExtra); | |
1592 | entry->Notify(); | |
1593 | } | |
1594 | } | |
1595 | } | |
1596 | ||
1597 | m_lasterror = m_parent_i_stream->GetLastError(); | |
1598 | return IsOk(); | |
1599 | } | |
1600 | ||
1601 | bool wxZipInputStream::OpenDecompressor(bool raw /*=false*/) | |
1602 | { | |
1603 | wxASSERT(AfterHeader()); | |
1604 | ||
1605 | wxFileOffset compressedSize = m_entry.GetCompressedSize(); | |
1606 | ||
1607 | if (raw) | |
1608 | m_raw = true; | |
1609 | ||
1610 | if (m_raw) { | |
1611 | if (compressedSize != wxInvalidOffset) { | |
1612 | m_store->Open(compressedSize); | |
1613 | m_decomp = m_store; | |
1614 | } else { | |
1615 | if (!m_rawin) | |
1616 | m_rawin = new wxRawInputStream(*m_parent_i_stream); | |
1617 | m_decomp = m_rawin->Open(OpenDecompressor(m_rawin->GetTee())); | |
1618 | } | |
1619 | } else { | |
1620 | if (compressedSize != wxInvalidOffset && | |
1621 | (m_entry.GetMethod() != wxZIP_METHOD_DEFLATE || | |
1622 | wxZlibInputStream::CanHandleGZip())) { | |
1623 | m_store->Open(compressedSize); | |
1624 | m_decomp = OpenDecompressor(*m_store); | |
1625 | } else { | |
1626 | m_decomp = OpenDecompressor(*m_parent_i_stream); | |
1627 | } | |
5526e819 | 1628 | } |
00375592 VZ |
1629 | |
1630 | m_crcAccumulator = crc32(0, Z_NULL, 0); | |
1631 | m_lasterror = m_decomp ? m_decomp->GetLastError() : wxSTREAM_READ_ERROR; | |
1632 | return IsOk(); | |
5526e819 VS |
1633 | } |
1634 | ||
00375592 VZ |
1635 | // Can be overriden to add support for additional decompression methods |
1636 | // | |
1637 | wxInputStream *wxZipInputStream::OpenDecompressor(wxInputStream& stream) | |
f6bcfd97 | 1638 | { |
00375592 VZ |
1639 | switch (m_entry.GetMethod()) { |
1640 | case wxZIP_METHOD_STORE: | |
1641 | if (m_entry.GetSize() == wxInvalidOffset) { | |
1642 | wxLogError(_("stored file length not in Zip header")); | |
1643 | break; | |
1644 | } | |
1645 | m_store->Open(m_entry.GetSize()); | |
1646 | return m_store; | |
1647 | ||
1648 | case wxZIP_METHOD_DEFLATE: | |
1649 | if (!m_inflate) | |
1650 | m_inflate = new wxZlibInputStream2(stream); | |
1651 | else | |
1652 | m_inflate->Open(stream); | |
1653 | return m_inflate; | |
f6bcfd97 | 1654 | |
00375592 VZ |
1655 | default: |
1656 | wxLogError(_("unsupported Zip compression method")); | |
1657 | } | |
1658 | ||
1659 | return NULL; | |
f6bcfd97 | 1660 | } |
5526e819 | 1661 | |
00375592 VZ |
1662 | bool wxZipInputStream::CloseDecompressor(wxInputStream *decomp) |
1663 | { | |
1664 | if (decomp && decomp == m_rawin) | |
1665 | return CloseDecompressor(m_rawin->GetFilterInputStream()); | |
1666 | if (decomp != m_store && decomp != m_inflate) | |
1667 | delete decomp; | |
1668 | return true; | |
1669 | } | |
5526e819 | 1670 | |
00375592 VZ |
1671 | // Closes the current entry and positions the underlying stream at the start |
1672 | // of the next entry | |
1673 | // | |
1674 | bool wxZipInputStream::CloseEntry() | |
5526e819 | 1675 | { |
00375592 VZ |
1676 | if (AtHeader()) |
1677 | return true; | |
1678 | if (m_lasterror == wxSTREAM_READ_ERROR) | |
1679 | return false; | |
f6bcfd97 | 1680 | |
00375592 VZ |
1681 | if (!m_parentSeekable) { |
1682 | if (!IsOpened() && !OpenDecompressor(true)) | |
1683 | return false; | |
1684 | ||
1685 | const int BUFSIZE = 8192; | |
1686 | wxCharBuffer buf(BUFSIZE); | |
1687 | while (IsOk()) | |
1688 | Read(buf.data(), BUFSIZE); | |
1689 | ||
1690 | m_position += m_headerSize + m_entry.GetCompressedSize(); | |
f6bcfd97 BP |
1691 | } |
1692 | ||
00375592 VZ |
1693 | if (m_lasterror == wxSTREAM_EOF) |
1694 | m_lasterror = wxSTREAM_NO_ERROR; | |
f6bcfd97 | 1695 | |
00375592 VZ |
1696 | CloseDecompressor(m_decomp); |
1697 | m_decomp = NULL; | |
1698 | m_entry = wxZipEntry(); | |
1699 | m_headerSize = 0; | |
1700 | m_raw = false; | |
f6bcfd97 | 1701 | |
00375592 | 1702 | return IsOk(); |
5526e819 VS |
1703 | } |
1704 | ||
00375592 VZ |
1705 | size_t wxZipInputStream::OnSysRead(void *buffer, size_t size) |
1706 | { | |
1707 | if (!IsOpened()) | |
1708 | if ((AtHeader() && !DoOpen()) || !OpenDecompressor()) | |
1709 | m_lasterror = wxSTREAM_READ_ERROR; | |
1710 | if (!IsOk() || !size) | |
1711 | return 0; | |
1712 | ||
1713 | size_t count = m_decomp->Read(buffer, size).LastRead(); | |
1714 | if (!m_raw) | |
1715 | m_crcAccumulator = crc32(m_crcAccumulator, (Byte*)buffer, count); | |
1716 | m_lasterror = m_decomp->GetLastError(); | |
1717 | ||
1718 | if (Eof()) { | |
1719 | if ((m_entry.GetFlags() & wxZIP_SUMS_FOLLOW) != 0) { | |
1720 | m_headerSize += m_entry.ReadDescriptor(*m_parent_i_stream); | |
1721 | wxZipEntry *entry = m_weaklinks->GetEntry(m_entry.GetKey()); | |
1722 | ||
1723 | if (entry) { | |
1724 | entry->SetCrc(m_entry.GetCrc()); | |
1725 | entry->SetCompressedSize(m_entry.GetCompressedSize()); | |
1726 | entry->SetSize(m_entry.GetSize()); | |
1727 | entry->Notify(); | |
1728 | } | |
1729 | } | |
1730 | ||
1731 | if (!m_raw) { | |
1732 | m_lasterror = wxSTREAM_READ_ERROR; | |
1733 | ||
1734 | if (m_parent_i_stream->IsOk()) { | |
1735 | if (m_entry.GetSize() != TellI()) | |
1736 | wxLogError(_("reading zip stream (entry %s): bad length"), | |
1737 | m_entry.GetName().c_str()); | |
1738 | else if (m_crcAccumulator != m_entry.GetCrc()) | |
1739 | wxLogError(_("reading zip stream (entry %s): bad crc"), | |
1740 | m_entry.GetName().c_str()); | |
1741 | else | |
1742 | m_lasterror = wxSTREAM_EOF; | |
1743 | } | |
1744 | } | |
1745 | } | |
5526e819 | 1746 | |
00375592 VZ |
1747 | return count; |
1748 | } | |
5526e819 | 1749 | |
d13a08ab MW |
1750 | #if 1 //WXWIN_COMPATIBILITY_2_6 |
1751 | ||
00375592 VZ |
1752 | // Borrowed from VS's zip stream (c) 1999 Vaclav Slavik |
1753 | // | |
4004775e | 1754 | wxFileOffset wxZipInputStream::OnSysSeek(wxFileOffset seek, wxSeekMode mode) |
5526e819 | 1755 | { |
d13a08ab MW |
1756 | // seeking works when the stream is created with the compatibility |
1757 | // constructor | |
1758 | if (!m_allowSeeking) | |
8019e604 MW |
1759 | return wxInvalidOffset; |
1760 | if (!IsOpened()) | |
1761 | if ((AtHeader() && !DoOpen()) || !OpenDecompressor()) | |
1762 | m_lasterror = wxSTREAM_READ_ERROR; | |
1763 | if (!IsOk()) | |
00375592 VZ |
1764 | return wxInvalidOffset; |
1765 | ||
cab1a605 | 1766 | // NB: since ZIP files don't natively support seeking, we have to |
03c0fc66 | 1767 | // implement a brute force workaround -- reading all the data |
cab1a605 | 1768 | // between current and the new position (or between beginning of |
03c0fc66 VS |
1769 | // the file and new position...) |
1770 | ||
4004775e | 1771 | wxFileOffset nextpos; |
00375592 | 1772 | wxFileOffset pos = TellI(); |
5526e819 | 1773 | |
03c0fc66 | 1774 | switch ( mode ) |
d1af991f | 1775 | { |
00375592 | 1776 | case wxFromCurrent : nextpos = seek + pos; break; |
5526e819 | 1777 | case wxFromStart : nextpos = seek; break; |
8019e604 | 1778 | case wxFromEnd : nextpos = GetLength() + seek; break; |
00375592 | 1779 | default : nextpos = pos; break; /* just to fool compiler, never happens */ |
5526e819 VS |
1780 | } |
1781 | ||
ba9bbf13 | 1782 | size_t toskip wxDUMMY_INITIALIZE(0); |
00375592 | 1783 | if ( nextpos >= pos ) |
d1af991f | 1784 | { |
26481315 | 1785 | toskip = (size_t)(nextpos - pos); |
5526e819 | 1786 | } |
03c0fc66 VS |
1787 | else |
1788 | { | |
00375592 VZ |
1789 | wxZipEntry current(m_entry); |
1790 | CloseEntry(); | |
1791 | if (!OpenEntry(current)) | |
e90c1d2a | 1792 | { |
2b5f62a0 | 1793 | m_lasterror = wxSTREAM_READ_ERROR; |
00375592 | 1794 | return pos; |
5526e819 | 1795 | } |
26481315 | 1796 | toskip = (size_t)nextpos; |
03c0fc66 | 1797 | } |
cab1a605 | 1798 | |
03c0fc66 VS |
1799 | if ( toskip > 0 ) |
1800 | { | |
1801 | const size_t BUFSIZE = 4096; | |
1802 | size_t sz; | |
1803 | char buffer[BUFSIZE]; | |
1804 | while ( toskip > 0 ) | |
1805 | { | |
1806 | sz = wxMin(toskip, BUFSIZE); | |
00375592 | 1807 | Read(buffer, sz); |
03c0fc66 VS |
1808 | toskip -= sz; |
1809 | } | |
5526e819 VS |
1810 | } |
1811 | ||
00375592 VZ |
1812 | pos = nextpos; |
1813 | return pos; | |
5526e819 VS |
1814 | } |
1815 | ||
d13a08ab MW |
1816 | #endif // WXWIN_COMPATIBILITY_2_6 |
1817 | ||
00375592 VZ |
1818 | |
1819 | ///////////////////////////////////////////////////////////////////////////// | |
1820 | // Output stream | |
1821 | ||
28997d37 | 1822 | #include "wx/listimpl.cpp" |
d13a08ab | 1823 | WX_DEFINE_LIST(wx__ZipEntryList); |
00375592 VZ |
1824 | |
1825 | wxZipOutputStream::wxZipOutputStream(wxOutputStream& stream, | |
1826 | int level /*=-1*/, | |
1827 | wxMBConv& conv /*=wxConvLocal*/) | |
1828 | : wxArchiveOutputStream(stream, conv), | |
1829 | m_store(new wxStoredOutputStream(stream)), | |
1830 | m_deflate(NULL), | |
1831 | m_backlink(NULL), | |
1832 | m_initialData(new char[OUTPUT_LATENCY]), | |
1833 | m_initialSize(0), | |
1834 | m_pending(NULL), | |
1835 | m_raw(false), | |
1836 | m_headerOffset(0), | |
1837 | m_headerSize(0), | |
1838 | m_entrySize(0), | |
1839 | m_comp(NULL), | |
1840 | m_level(level), | |
1841 | m_offsetAdjustment(wxInvalidOffset) | |
1842 | { | |
1843 | } | |
ba9bbf13 | 1844 | |
00375592 VZ |
1845 | wxZipOutputStream::~wxZipOutputStream() |
1846 | { | |
1847 | Close(); | |
d13a08ab | 1848 | WX_CLEAR_LIST(wx__ZipEntryList, m_entries); |
00375592 VZ |
1849 | delete m_store; |
1850 | delete m_deflate; | |
1851 | delete m_pending; | |
1852 | delete [] m_initialData; | |
1853 | if (m_backlink) | |
1854 | m_backlink->Release(this); | |
1855 | } | |
1856 | ||
1857 | bool wxZipOutputStream::PutNextEntry( | |
1858 | const wxString& name, | |
1859 | const wxDateTime& dt /*=wxDateTime::Now()*/, | |
1860 | wxFileOffset size /*=wxInvalidOffset*/) | |
1861 | { | |
1862 | return PutNextEntry(new wxZipEntry(name, dt, size)); | |
1863 | } | |
1864 | ||
1865 | bool wxZipOutputStream::PutNextDirEntry( | |
1866 | const wxString& name, | |
1867 | const wxDateTime& dt /*=wxDateTime::Now()*/) | |
1868 | { | |
1869 | wxZipEntry *entry = new wxZipEntry(name, dt); | |
1870 | entry->SetIsDir(); | |
1871 | return PutNextEntry(entry); | |
1872 | } | |
1873 | ||
1874 | bool wxZipOutputStream::CopyEntry(wxZipEntry *entry, | |
1875 | wxZipInputStream& inputStream) | |
1876 | { | |
d13a08ab | 1877 | wx__ZipEntryPtr e(entry); |
00375592 VZ |
1878 | |
1879 | return | |
1880 | inputStream.DoOpen(e.get(), true) && | |
1881 | DoCreate(e.release(), true) && | |
1882 | Write(inputStream).IsOk() && inputStream.Eof(); | |
1883 | } | |
1884 | ||
1885 | bool wxZipOutputStream::PutNextEntry(wxArchiveEntry *entry) | |
1886 | { | |
1887 | wxZipEntry *zipEntry = wxStaticCast(entry, wxZipEntry); | |
1888 | if (!zipEntry) | |
1889 | delete entry; | |
1890 | return PutNextEntry(zipEntry); | |
1891 | } | |
1892 | ||
1893 | bool wxZipOutputStream::CopyEntry(wxArchiveEntry *entry, | |
1894 | wxArchiveInputStream& stream) | |
1895 | { | |
1896 | wxZipEntry *zipEntry = wxStaticCast(entry, wxZipEntry); | |
1897 | ||
1898 | if (!zipEntry || !stream.OpenEntry(*zipEntry)) { | |
1899 | delete entry; | |
1900 | return false; | |
1901 | } | |
1902 | ||
1903 | return CopyEntry(zipEntry, wx_static_cast(wxZipInputStream&, stream)); | |
1904 | } | |
1905 | ||
1906 | bool wxZipOutputStream::CopyArchiveMetaData(wxZipInputStream& inputStream) | |
1907 | { | |
1908 | m_Comment = inputStream.GetComment(); | |
1909 | if (m_backlink) | |
1910 | m_backlink->Release(this); | |
1911 | m_backlink = inputStream.MakeLink(this); | |
1912 | return true; | |
1913 | } | |
1914 | ||
1915 | bool wxZipOutputStream::CopyArchiveMetaData(wxArchiveInputStream& stream) | |
1916 | { | |
1917 | return CopyArchiveMetaData(wx_static_cast(wxZipInputStream&, stream)); | |
1918 | } | |
1919 | ||
1920 | void wxZipOutputStream::SetLevel(int level) | |
1921 | { | |
1922 | if (level != m_level) { | |
1923 | if (m_comp != m_deflate) | |
1924 | delete m_deflate; | |
1925 | m_deflate = NULL; | |
1926 | m_level = level; | |
1927 | } | |
1928 | } | |
1929 | ||
1930 | bool wxZipOutputStream::DoCreate(wxZipEntry *entry, bool raw /*=false*/) | |
1931 | { | |
1932 | CloseEntry(); | |
1933 | ||
1934 | m_pending = entry; | |
1935 | if (!m_pending) | |
1936 | return false; | |
1937 | ||
1938 | // write the signature bytes right away | |
1939 | wxDataOutputStream ds(*m_parent_o_stream); | |
1940 | ds << LOCAL_MAGIC; | |
1941 | ||
1942 | // and if this is the first entry test for seekability | |
3c70014d | 1943 | if (m_headerOffset == 0 && m_parent_o_stream->IsSeekable()) { |
8add533e | 1944 | #if wxUSE_LOG |
00375592 VZ |
1945 | bool logging = wxLog::IsEnabled(); |
1946 | wxLogNull nolog; | |
8add533e | 1947 | #endif // wxUSE_LOG |
00375592 VZ |
1948 | wxFileOffset here = m_parent_o_stream->TellO(); |
1949 | ||
1950 | if (here != wxInvalidOffset && here >= 4) { | |
1951 | if (m_parent_o_stream->SeekO(here - 4) == here - 4) { | |
1952 | m_offsetAdjustment = here - 4; | |
8add533e | 1953 | #if wxUSE_LOG |
00375592 | 1954 | wxLog::EnableLogging(logging); |
8add533e | 1955 | #endif // wxUSE_LOG |
00375592 VZ |
1956 | m_parent_o_stream->SeekO(here); |
1957 | } | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | m_pending->SetOffset(m_headerOffset); | |
1962 | ||
1963 | m_crcAccumulator = crc32(0, Z_NULL, 0); | |
1964 | ||
1965 | if (raw) | |
1966 | m_raw = true; | |
1967 | ||
1968 | m_lasterror = wxSTREAM_NO_ERROR; | |
1969 | return true; | |
1970 | } | |
1971 | ||
1972 | // Can be overriden to add support for additional compression methods | |
1973 | // | |
1974 | wxOutputStream *wxZipOutputStream::OpenCompressor( | |
1975 | wxOutputStream& stream, | |
1976 | wxZipEntry& entry, | |
1977 | const Buffer bufs[]) | |
1978 | { | |
1979 | if (entry.GetMethod() == wxZIP_METHOD_DEFAULT) { | |
1980 | if (GetLevel() == 0 | |
1981 | && (IsParentSeekable() | |
1982 | || entry.GetCompressedSize() != wxInvalidOffset | |
1983 | || entry.GetSize() != wxInvalidOffset)) { | |
1984 | entry.SetMethod(wxZIP_METHOD_STORE); | |
1985 | } else { | |
1986 | int size = 0; | |
1987 | for (int i = 0; bufs[i].m_data; ++i) | |
1988 | size += bufs[i].m_size; | |
1989 | entry.SetMethod(size <= 6 ? | |
1990 | wxZIP_METHOD_STORE : wxZIP_METHOD_DEFLATE); | |
1991 | } | |
1992 | } | |
1993 | ||
1994 | switch (entry.GetMethod()) { | |
1995 | case wxZIP_METHOD_STORE: | |
1996 | if (entry.GetCompressedSize() == wxInvalidOffset) | |
1997 | entry.SetCompressedSize(entry.GetSize()); | |
1998 | return m_store; | |
1999 | ||
2000 | case wxZIP_METHOD_DEFLATE: | |
2001 | { | |
2002 | int defbits = wxZIP_DEFLATE_NORMAL; | |
2003 | switch (GetLevel()) { | |
2004 | case 0: case 1: | |
2005 | defbits = wxZIP_DEFLATE_SUPERFAST; | |
2006 | break; | |
2007 | case 2: case 3: case 4: | |
2008 | defbits = wxZIP_DEFLATE_FAST; | |
2009 | break; | |
2010 | case 8: case 9: | |
2011 | defbits = wxZIP_DEFLATE_EXTRA; | |
2012 | break; | |
2013 | } | |
2014 | entry.SetFlags((entry.GetFlags() & ~wxZIP_DEFLATE_MASK) | | |
2015 | defbits | wxZIP_SUMS_FOLLOW); | |
2016 | ||
2017 | if (!m_deflate) | |
2018 | m_deflate = new wxZlibOutputStream2(stream, GetLevel()); | |
2019 | else | |
2020 | m_deflate->Open(stream); | |
2021 | ||
2022 | return m_deflate; | |
2023 | } | |
2024 | ||
2025 | default: | |
2026 | wxLogError(_("unsupported Zip compression method")); | |
2027 | } | |
2028 | ||
2029 | return NULL; | |
2030 | } | |
2031 | ||
2032 | bool wxZipOutputStream::CloseCompressor(wxOutputStream *comp) | |
2033 | { | |
2034 | if (comp == m_deflate) | |
2035 | m_deflate->Close(); | |
2036 | else if (comp != m_store) | |
2037 | delete comp; | |
2038 | return true; | |
2039 | } | |
2040 | ||
2041 | // This is called when OUPUT_LATENCY bytes has been written to the | |
2042 | // wxZipOutputStream to actually create the zip entry. | |
2043 | // | |
2044 | void wxZipOutputStream::CreatePendingEntry(const void *buffer, size_t size) | |
2045 | { | |
2046 | wxASSERT(IsOk() && m_pending && !m_comp); | |
d13a08ab | 2047 | wx__ZipEntryPtr spPending(m_pending); |
00375592 VZ |
2048 | m_pending = NULL; |
2049 | ||
2050 | Buffer bufs[] = { | |
2051 | { m_initialData, m_initialSize }, | |
2052 | { (const char*)buffer, size }, | |
2053 | { NULL, 0 } | |
2054 | }; | |
2055 | ||
2056 | if (m_raw) | |
2057 | m_comp = m_store; | |
2058 | else | |
2059 | m_comp = OpenCompressor(*m_store, *spPending, | |
2060 | m_initialSize ? bufs : bufs + 1); | |
2061 | ||
2062 | if (IsParentSeekable() | |
2063 | || (spPending->m_Crc | |
2064 | && spPending->m_CompressedSize != wxInvalidOffset | |
2065 | && spPending->m_Size != wxInvalidOffset)) | |
2066 | spPending->m_Flags &= ~wxZIP_SUMS_FOLLOW; | |
2067 | else | |
2068 | if (spPending->m_CompressedSize != wxInvalidOffset) | |
2069 | spPending->m_Flags |= wxZIP_SUMS_FOLLOW; | |
2070 | ||
2071 | m_headerSize = spPending->WriteLocal(*m_parent_o_stream, GetConv()); | |
2072 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2073 | ||
2074 | if (IsOk()) { | |
2075 | m_entries.push_back(spPending.release()); | |
2076 | OnSysWrite(m_initialData, m_initialSize); | |
2077 | } | |
2078 | ||
2079 | m_initialSize = 0; | |
2080 | } | |
2081 | ||
2082 | // This is called to write out the zip entry when Close has been called | |
2083 | // before OUTPUT_LATENCY bytes has been written to the wxZipOutputStream. | |
2084 | // | |
2085 | void wxZipOutputStream::CreatePendingEntry() | |
2086 | { | |
2087 | wxASSERT(IsOk() && m_pending && !m_comp); | |
d13a08ab | 2088 | wx__ZipEntryPtr spPending(m_pending); |
00375592 VZ |
2089 | m_pending = NULL; |
2090 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2091 | ||
2092 | if (!m_raw) { | |
2093 | // Initially compresses the data to memory, then fall back to 'store' | |
2094 | // if the compressor makes the data larger rather than smaller. | |
2095 | wxMemoryOutputStream mem; | |
2096 | Buffer bufs[] = { { m_initialData, m_initialSize }, { NULL, 0 } }; | |
2097 | wxOutputStream *comp = OpenCompressor(mem, *spPending, bufs); | |
2098 | ||
2099 | if (!comp) | |
2100 | return; | |
2101 | if (comp != m_store) { | |
2102 | bool ok = comp->Write(m_initialData, m_initialSize).IsOk(); | |
2103 | CloseCompressor(comp); | |
2104 | if (!ok) | |
2105 | return; | |
2106 | } | |
2107 | ||
2108 | m_entrySize = m_initialSize; | |
2109 | m_crcAccumulator = crc32(0, (Byte*)m_initialData, m_initialSize); | |
2110 | ||
2111 | if (mem.GetSize() > 0 && mem.GetSize() < m_initialSize) { | |
2112 | m_initialSize = mem.GetSize(); | |
2113 | mem.CopyTo(m_initialData, m_initialSize); | |
2114 | } else { | |
2115 | spPending->SetMethod(wxZIP_METHOD_STORE); | |
2116 | } | |
2117 | ||
2118 | spPending->SetSize(m_entrySize); | |
2119 | spPending->SetCrc(m_crcAccumulator); | |
2120 | spPending->SetCompressedSize(m_initialSize); | |
2121 | } | |
2122 | ||
2123 | spPending->m_Flags &= ~wxZIP_SUMS_FOLLOW; | |
2124 | m_headerSize = spPending->WriteLocal(*m_parent_o_stream, GetConv()); | |
2125 | ||
2126 | if (m_parent_o_stream->IsOk()) { | |
2127 | m_entries.push_back(spPending.release()); | |
2128 | m_comp = m_store; | |
2129 | m_store->Write(m_initialData, m_initialSize); | |
2130 | } | |
2131 | ||
2132 | m_initialSize = 0; | |
2133 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2134 | } | |
2135 | ||
2136 | // Write the 'central directory' and the 'end-central-directory' records. | |
2137 | // | |
2138 | bool wxZipOutputStream::Close() | |
2139 | { | |
2140 | CloseEntry(); | |
2141 | ||
2142 | if (m_lasterror == wxSTREAM_WRITE_ERROR || m_entries.size() == 0) | |
2143 | return false; | |
2144 | ||
2145 | wxZipEndRec endrec; | |
2146 | ||
2147 | endrec.SetEntriesHere(m_entries.size()); | |
2148 | endrec.SetTotalEntries(m_entries.size()); | |
2149 | endrec.SetOffset(m_headerOffset); | |
2150 | endrec.SetComment(m_Comment); | |
2151 | ||
d13a08ab | 2152 | wx__ZipEntryList::iterator it; |
00375592 VZ |
2153 | wxFileOffset size = 0; |
2154 | ||
2155 | for (it = m_entries.begin(); it != m_entries.end(); ++it) { | |
2156 | size += (*it)->WriteCentral(*m_parent_o_stream, GetConv()); | |
2157 | delete *it; | |
2158 | } | |
2159 | m_entries.clear(); | |
2160 | ||
2161 | endrec.SetSize(size); | |
2162 | endrec.Write(*m_parent_o_stream, GetConv()); | |
2163 | ||
2164 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2165 | if (!IsOk()) | |
2166 | return false; | |
2167 | m_lasterror = wxSTREAM_EOF; | |
2168 | return true; | |
2169 | } | |
2170 | ||
2171 | // Finish writing the current entry | |
2172 | // | |
2173 | bool wxZipOutputStream::CloseEntry() | |
2174 | { | |
2175 | if (IsOk() && m_pending) | |
2176 | CreatePendingEntry(); | |
2177 | if (!IsOk()) | |
2178 | return false; | |
2179 | if (!m_comp) | |
2180 | return true; | |
2181 | ||
2182 | CloseCompressor(m_comp); | |
2183 | m_comp = NULL; | |
2184 | ||
2185 | wxFileOffset compressedSize = m_store->TellO(); | |
2186 | ||
2187 | wxZipEntry& entry = *m_entries.back(); | |
2188 | ||
2189 | // When writing raw the crc and size can't be checked | |
2190 | if (m_raw) { | |
2191 | m_crcAccumulator = entry.GetCrc(); | |
2192 | m_entrySize = entry.GetSize(); | |
2193 | } | |
2194 | ||
2195 | // Write the sums in the trailing 'data descriptor' if necessary | |
2196 | if (entry.m_Flags & wxZIP_SUMS_FOLLOW) { | |
2197 | wxASSERT(!IsParentSeekable()); | |
2198 | m_headerOffset += | |
2199 | entry.WriteDescriptor(*m_parent_o_stream, m_crcAccumulator, | |
2200 | compressedSize, m_entrySize); | |
2201 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2202 | } | |
2203 | ||
2204 | // If the local header didn't have the correct crc and size written to | |
2205 | // it then seek back and fix it | |
2206 | else if (m_crcAccumulator != entry.GetCrc() | |
2207 | || m_entrySize != entry.GetSize() | |
2208 | || compressedSize != entry.GetCompressedSize()) | |
2209 | { | |
2210 | if (IsParentSeekable()) { | |
2211 | wxFileOffset here = m_parent_o_stream->TellO(); | |
2212 | wxFileOffset headerOffset = m_headerOffset + m_offsetAdjustment; | |
2213 | m_parent_o_stream->SeekO(headerOffset + SUMS_OFFSET); | |
2214 | entry.WriteDescriptor(*m_parent_o_stream, m_crcAccumulator, | |
2215 | compressedSize, m_entrySize); | |
2216 | m_parent_o_stream->SeekO(here); | |
2217 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2218 | } else { | |
2219 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2220 | } | |
2221 | } | |
2222 | ||
2223 | m_headerOffset += m_headerSize + compressedSize; | |
26481315 WS |
2224 | m_headerSize = 0; |
2225 | m_entrySize = 0; | |
00375592 VZ |
2226 | m_store->Close(); |
2227 | m_raw = false; | |
2228 | ||
2229 | if (IsOk()) | |
2230 | m_lasterror = m_parent_o_stream->GetLastError(); | |
2231 | else | |
2232 | wxLogError(_("error writing zip entry '%s': bad crc or length"), | |
2233 | entry.GetName().c_str()); | |
2234 | return IsOk(); | |
2235 | } | |
2236 | ||
2237 | void wxZipOutputStream::Sync() | |
2238 | { | |
2239 | if (IsOk() && m_pending) | |
2240 | CreatePendingEntry(NULL, 0); | |
2241 | if (!m_comp) | |
2242 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2243 | if (IsOk()) { | |
2244 | m_comp->Sync(); | |
2245 | m_lasterror = m_comp->GetLastError(); | |
2246 | } | |
2247 | } | |
2248 | ||
2249 | size_t wxZipOutputStream::OnSysWrite(const void *buffer, size_t size) | |
2250 | { | |
2251 | if (IsOk() && m_pending) { | |
2252 | if (m_initialSize + size < OUTPUT_LATENCY) { | |
2253 | memcpy(m_initialData + m_initialSize, buffer, size); | |
2254 | m_initialSize += size; | |
2255 | return size; | |
2256 | } else { | |
2257 | CreatePendingEntry(buffer, size); | |
2258 | } | |
2259 | } | |
2260 | ||
2261 | if (!m_comp) | |
2262 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2263 | if (!IsOk() || !size) | |
2264 | return 0; | |
2265 | ||
2266 | if (m_comp->Write(buffer, size).LastWrite() != size) | |
2267 | m_lasterror = wxSTREAM_WRITE_ERROR; | |
2268 | m_crcAccumulator = crc32(m_crcAccumulator, (Byte*)buffer, size); | |
2269 | m_entrySize += m_comp->LastWrite(); | |
2270 | ||
2271 | return m_comp->LastWrite(); | |
2272 | } | |
2273 | ||
2274 | #endif // wxUSE_ZLIB && wxUSE_STREAMS && wxUSE_ZIPSTREAM |