]> git.saurik.com Git - wxWidgets.git/blame - src/common/gzstream.cpp
Fixes for previous fixes
[wxWidgets.git] / src / common / gzstream.cpp
CommitLineData
6f96ac03
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: gzstream.cpp
3// Purpose: Streams for Gzip files
4// Author: Mike Wetherell
5// RCS-ID: $Id$
6// Copyright: (c) 2003 Mike Wetherell
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11#pragma implementation "gzstream.h"
12#endif
13
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
21#if wxUSE_STREAMS && wxUSE_GZSTREAM && wxUSE_ZLIB
22
23#include "wx/log.h"
24#include "wx/intl.h"
25#include "wx/datstrm.h"
26#include "wx/txtstrm.h"
27#include "wx/filename.h"
28#include "wx/zstream.h"
29#include "wx/gzstream.h"
30
31#if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH)
32 #include "../zlib/zlib.h"
33#else
34 #include "zlib.h"
35#endif
36
37
38/////////////////////////////////////////////////////////////////////////////
39// Notes:
40//
41// See RFC-1952 and the Gzip/Zlib sources for the details of the Gzip format
42//
43// Errors are displayed with wxLogError, but not errors from the compressor
44// or underlying stream, since they will display their own errors.
45//
46// Gzip doesn't use flag 2 to indicate a header crc, so I think it's
47// probably better not to use it for the moment.
48//
49
50// Flags
51enum {
52 GZ_ASCII_FLAG = 0x01,
53 GZ_HEAD_CRC = 0x02,
54 GZ_EXTRA_FIELD = 0x04,
55 GZ_ORIG_NAME = 0x08,
56 GZ_COMMENT = 0x10,
57 GZ_RESERVED = 0xE0
58};
59
60// Extra flags
61enum {
62 GZ_SLOWEST = 2,
63 GZ_FASTEST = 4
64};
65
66const wxUint16 GZ_MAGIC = 0x8b1f;
67
68
69/////////////////////////////////////////////////////////////////////////////
70// Input stream
71
72wxGzipInputStream::wxGzipInputStream(wxInputStream& stream,
73 wxMBConv& conv /*=wxConvFile*/)
74 : wxFilterInputStream(stream)
75{
76 m_decomp = NULL;
77 m_crc = crc32(0, Z_NULL, 0);
78
79 // Try to read the Gzip magic numbers 0x1f, 0x8b. If not found then the
80 // underlying stream isn't gzipped after all, so unread the bytes taken
81 // so that the underlying stream can be read directly instead.
82 wxUint8 magic[2];
83 size_t n = m_parent_i_stream->Read(magic, sizeof(magic)).LastRead();
84
85 if (n < sizeof(magic) || ((magic[1] << 8) | magic[0]) != GZ_MAGIC) {
86 if (n)
87 m_parent_i_stream->Ungetch(magic, n);
88 // Set EOF rather than error to indicate no gzip data
89 m_lasterror = wxSTREAM_EOF;
90 return;
91 }
92
93 wxDataInputStream ds(*m_parent_i_stream);
94
95 // read method, flags, timestamp, extra flags and OS-code
96 int method = ds.Read8();
97 int flags = ds.Read8();
98#if wxUSE_DATETIME
99 wxUint32 datetime = ds.Read32();
100 if (datetime) // zero means not set (not -1 as usual for time_t)
101 m_datetime = wxLongLong(0, datetime) * 1000L;
102#else
103 ds.Read32();
104#endif
105 ds.Read8();
106 ds.Read8();
107
108 if (flags & GZ_HEAD_CRC)
109 ds.Read16();
110
111 if (flags & GZ_EXTRA_FIELD)
112 for (int i = ds.Read16(); i > 0 && m_parent_i_stream->IsOk(); i--)
113 m_parent_i_stream->GetC();
114
115 // RFC-1952 specifies ISO-8859-1 for these fields
116 if (flags & GZ_ORIG_NAME) {
117#if wxUSE_UNICODE
118 wxTextInputStream tis(*m_parent_i_stream, wxT(" \t"), conv);
119#else
120 wxTextInputStream tis(*m_parent_i_stream);
121 (void)conv;
122#endif
123 wxChar c;
124 while ((c = tis.GetChar()) != 0 && m_parent_i_stream->IsOk())
125 m_name += c;
126 }
127
128 if (flags & GZ_COMMENT)
129 while (m_parent_i_stream->GetC() != 0 && m_parent_i_stream->IsOk())
130 ; // empty loop
131
132 m_lasterror = wxSTREAM_READ_ERROR;
133 if (!*m_parent_i_stream) {
134 wxLogDebug(wxT("Error reading Gzip header"));
135 return;
136 }
137
138 if (flags & GZ_RESERVED)
139 wxLogWarning(_("Unsupported flag in Gzip header"));
140
141 switch (method) {
142 case Z_DEFLATED:
143 m_decomp = new wxZlibInputStream(*m_parent_i_stream, wxZLIB_NO_HEADER);
144 break;
145
146 default:
147 wxLogError(_("unsupported compression method in Gzip stream"));
148 return;
149 }
150
151 if (m_decomp)
152 m_lasterror = m_decomp->GetLastError();
153}
154
155
156wxGzipInputStream::~wxGzipInputStream()
157{
158 delete m_decomp;
159}
160
161
162size_t wxGzipInputStream::OnSysRead(void *buffer, size_t size)
163{
164 wxASSERT_MSG(m_decomp, wxT("Gzip not open"));
165
166 if (!m_decomp)
167 m_lasterror = wxSTREAM_READ_ERROR;
168 if (!IsOk() || !size)
169 return 0;
170
171 m_decomp->Read(buffer, size);
172 m_crc = crc32(m_crc, (Byte*)buffer, m_decomp->LastRead());
173
174 if (m_decomp->Eof()) {
175 wxDataInputStream ds(*m_parent_i_stream);
176 m_lasterror = wxSTREAM_READ_ERROR;
177
178 if (m_parent_i_stream->IsOk() && ds.Read32() != m_crc)
179 wxLogError(_("reading Gzip stream: bad crc"));
180 else if (m_parent_i_stream->IsOk() && ds.Read32() != (wxUint32)TellI())
181 wxLogError(_("reading Gzip stream: incorrect length"));
182 else if (m_parent_i_stream->IsOk())
183 m_lasterror = wxSTREAM_EOF;
184 }
185 else if (!*m_decomp) {
186 m_lasterror = wxSTREAM_READ_ERROR;
187 }
188
189 return m_decomp->LastRead();
190}
191
192
193/////////////////////////////////////////////////////////////////////////////
194// Output stream
195
196wxGzipOutputStream::wxGzipOutputStream(
197 wxOutputStream& stream,
198 const wxString& originalName /*=wxEmptyString*/,
199 int level /*=-1*/,
200 wxMBConv& conv /*=wxConvFile*/)
201 : wxFilterOutputStream(stream)
202{
203 m_comp = NULL;
204 m_crc = crc32(0, Z_NULL, 0);
205
206 wxFileName filename(originalName);
207
208#if wxUSE_DATETIME
209 wxDateTime datetime;
210
211 if (filename.FileExists())
212 datetime = filename.GetModificationTime();
213 else
214 datetime = wxDateTime::Now();
215
216 wxUint32 timestamp = (datetime.GetValue() / 1000L).GetLo();
217#else
218 wxUint32 timestamp = 0;
219#endif
220
221 // RFC-1952 specifies ISO-8859-1 for the name. Also it should be just the
222 // name part, no directory, folded to lowercase if case insensitive
223 wxString name = filename.GetFullName();
224 const wxWX2MBbuf mbName = conv.cWX2MB(name);
225
226 wxDataOutputStream ds(*m_parent_o_stream);
227
228 // write signature, method, flags, timestamp, extra flags and OS-code
229 ds.Write16(GZ_MAGIC);
230 ds.Write8(Z_DEFLATED);
231 ds.Write8(mbName && *mbName ? GZ_ORIG_NAME : 0);
232 ds.Write32(timestamp);
233 ds.Write8(level == 1 ? GZ_FASTEST : level == 9 ? GZ_SLOWEST : 0);
234 ds.Write8(255);
235
236 if (mbName && *mbName)
237 m_parent_o_stream->Write(mbName, strlen(mbName) + 1);
238
239 m_lasterror = wxSTREAM_WRITE_ERROR;
240 if (!*m_parent_o_stream) {
241 wxLogDebug(wxT("Error writing Gzip header"));
242 return;
243 }
244
245 m_comp = new wxZlibOutputStream(*m_parent_o_stream, level, wxZLIB_NO_HEADER);
246
247 if (m_comp)
248 m_lasterror = m_comp->GetLastError();
249}
250
251
252wxGzipOutputStream::~wxGzipOutputStream()
253{
254 if (m_comp && m_comp->IsOk()) {
255 wxUint32 len = (wxUint32)m_comp->TellO();
256 delete m_comp;
257 if (m_parent_o_stream->IsOk()) {
258 wxDataOutputStream ds(*m_parent_o_stream);
259 ds.Write32(m_crc);
260 ds.Write32(len); // underlying stream will report errors
261 }
262 } else {
263 delete m_comp;
264 }
265}
266
267
268void wxGzipOutputStream::Sync()
269{
270 wxASSERT_MSG(m_comp, wxT("Gzip not open"));
271
272 if (!m_comp)
273 m_lasterror = wxSTREAM_WRITE_ERROR;
274 if (IsOk())
275 m_comp->Sync();
276}
277
278
279size_t wxGzipOutputStream::OnSysWrite(const void *buffer, size_t size)
280{
281 wxASSERT_MSG(m_comp, wxT("Gzip not open"));
282
283 if (!m_comp)
284 m_lasterror = wxSTREAM_WRITE_ERROR;
285 if (!IsOk() || !size)
286 return 0;
287
288 if (m_comp->Write(buffer, size).LastWrite() != size)
289 m_lasterror = wxSTREAM_WRITE_ERROR;
290 m_crc = crc32(m_crc, (Byte*)buffer, size);
291
292 return m_comp->LastWrite();
293}
294
295
296#endif // wxUSE_STREAMS && wxUSE_GZSTREAM && wxUSE_ZLIB