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