| 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/zstream.cpp |
| 3 | // Purpose: Compressed stream classes |
| 4 | // Author: Guilhem Lavaux |
| 5 | // Modified by: Mike Wetherell |
| 6 | // Created: 11/07/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Guilhem Lavaux |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // For compilers that support precompilation, includes "wx.h". |
| 13 | #include "wx/wxprec.h" |
| 14 | |
| 15 | #ifdef __BORLANDC__ |
| 16 | #pragma hdrstop |
| 17 | #endif |
| 18 | |
| 19 | #if wxUSE_ZLIB && wxUSE_STREAMS |
| 20 | |
| 21 | #include "wx/zstream.h" |
| 22 | #include "wx/versioninfo.h" |
| 23 | |
| 24 | #ifndef WX_PRECOMP |
| 25 | #include "wx/intl.h" |
| 26 | #include "wx/log.h" |
| 27 | #include "wx/utils.h" |
| 28 | #endif |
| 29 | |
| 30 | |
| 31 | // normally, the compiler options should contain -I../zlib, but it is |
| 32 | // apparently not the case for all MSW makefiles and so, unless we use |
| 33 | // configure (which defines __WX_SETUP_H__) or it is explicitly overridden by |
| 34 | // the user (who can define wxUSE_ZLIB_H_IN_PATH), we hardcode the path here |
| 35 | #if defined(__WXMSW__) && !defined(__WX_SETUP_H__) && !defined(wxUSE_ZLIB_H_IN_PATH) |
| 36 | #include "../zlib/zlib.h" |
| 37 | #else |
| 38 | #include "zlib.h" |
| 39 | #endif |
| 40 | |
| 41 | enum { |
| 42 | ZSTREAM_BUFFER_SIZE = 16384, |
| 43 | ZSTREAM_GZIP = 0x10, // gzip header |
| 44 | ZSTREAM_AUTO = 0x20 // auto detect between gzip and zlib |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | wxVersionInfo wxGetZlibVersionInfo() |
| 49 | { |
| 50 | return wxVersionInfo("zlib", |
| 51 | ZLIB_VERNUM >> 12, |
| 52 | (ZLIB_VERNUM >> 8) & 0x0F, |
| 53 | (ZLIB_VERNUM & 0xFF) / 0x10); |
| 54 | } |
| 55 | |
| 56 | ///////////////////////////////////////////////////////////////////////////// |
| 57 | // Zlib Class factory |
| 58 | |
| 59 | IMPLEMENT_DYNAMIC_CLASS(wxZlibClassFactory, wxFilterClassFactory) |
| 60 | |
| 61 | static wxZlibClassFactory g_wxZlibClassFactory; |
| 62 | |
| 63 | wxZlibClassFactory::wxZlibClassFactory() |
| 64 | { |
| 65 | if (this == &g_wxZlibClassFactory) |
| 66 | PushFront(); |
| 67 | } |
| 68 | |
| 69 | const wxChar * const * |
| 70 | wxZlibClassFactory::GetProtocols(wxStreamProtocolType type) const |
| 71 | { |
| 72 | static const wxChar *mimes[] = { wxT("application/x-deflate"), NULL }; |
| 73 | static const wxChar *encs[] = { wxT("deflate"), NULL }; |
| 74 | static const wxChar *empty[] = { NULL }; |
| 75 | |
| 76 | switch (type) { |
| 77 | case wxSTREAM_MIMETYPE: return mimes; |
| 78 | case wxSTREAM_ENCODING: return encs; |
| 79 | default: return empty; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | ///////////////////////////////////////////////////////////////////////////// |
| 85 | // Gzip Class factory |
| 86 | |
| 87 | IMPLEMENT_DYNAMIC_CLASS(wxGzipClassFactory, wxFilterClassFactory) |
| 88 | |
| 89 | static wxGzipClassFactory g_wxGzipClassFactory; |
| 90 | |
| 91 | wxGzipClassFactory::wxGzipClassFactory() |
| 92 | { |
| 93 | if (this == &g_wxGzipClassFactory && wxZlibInputStream::CanHandleGZip()) |
| 94 | PushFront(); |
| 95 | } |
| 96 | |
| 97 | const wxChar * const * |
| 98 | wxGzipClassFactory::GetProtocols(wxStreamProtocolType type) const |
| 99 | { |
| 100 | static const wxChar *protos[] = |
| 101 | { wxT("gzip"), NULL }; |
| 102 | static const wxChar *mimes[] = |
| 103 | { wxT("application/gzip"), wxT("application/x-gzip"), NULL }; |
| 104 | static const wxChar *encs[] = |
| 105 | { wxT("gzip"), NULL }; |
| 106 | static const wxChar *exts[] = |
| 107 | { wxT(".gz"), wxT(".gzip"), NULL }; |
| 108 | static const wxChar *empty[] = |
| 109 | { NULL }; |
| 110 | |
| 111 | switch (type) { |
| 112 | case wxSTREAM_PROTOCOL: return protos; |
| 113 | case wxSTREAM_MIMETYPE: return mimes; |
| 114 | case wxSTREAM_ENCODING: return encs; |
| 115 | case wxSTREAM_FILEEXT: return exts; |
| 116 | default: return empty; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | ////////////////////// |
| 122 | // wxZlibInputStream |
| 123 | ////////////////////// |
| 124 | |
| 125 | wxZlibInputStream::wxZlibInputStream(wxInputStream& stream, int flags) |
| 126 | : wxFilterInputStream(stream) |
| 127 | { |
| 128 | Init(flags); |
| 129 | } |
| 130 | |
| 131 | wxZlibInputStream::wxZlibInputStream(wxInputStream *stream, int flags) |
| 132 | : wxFilterInputStream(stream) |
| 133 | { |
| 134 | Init(flags); |
| 135 | } |
| 136 | |
| 137 | void wxZlibInputStream::Init(int flags) |
| 138 | { |
| 139 | m_inflate = NULL; |
| 140 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; |
| 141 | m_z_size = ZSTREAM_BUFFER_SIZE; |
| 142 | m_pos = 0; |
| 143 | |
| 144 | // if gzip is asked for but not supported... |
| 145 | if ((flags == wxZLIB_GZIP || flags == wxZLIB_AUTO) && !CanHandleGZip()) { |
| 146 | if (flags == wxZLIB_AUTO) { |
| 147 | // an error will come later if the input turns out not to be a zlib |
| 148 | flags = wxZLIB_ZLIB; |
| 149 | } |
| 150 | else { |
| 151 | wxLogError(_("Gzip not supported by this version of zlib")); |
| 152 | m_lasterror = wxSTREAM_READ_ERROR; |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (m_z_buffer) { |
| 158 | m_inflate = new z_stream_s; |
| 159 | |
| 160 | if (m_inflate) { |
| 161 | memset(m_inflate, 0, sizeof(z_stream_s)); |
| 162 | |
| 163 | // see zlib.h for documentation on windowBits |
| 164 | int windowBits = MAX_WBITS; |
| 165 | switch (flags) { |
| 166 | case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break; |
| 167 | case wxZLIB_ZLIB: windowBits = MAX_WBITS; break; |
| 168 | case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break; |
| 169 | case wxZLIB_AUTO: windowBits = MAX_WBITS | ZSTREAM_AUTO; break; |
| 170 | default: wxFAIL_MSG(wxT("Invalid zlib flag")); |
| 171 | } |
| 172 | |
| 173 | if (inflateInit2(m_inflate, windowBits) == Z_OK) |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | wxLogError(_("Can't initialize zlib inflate stream.")); |
| 179 | m_lasterror = wxSTREAM_READ_ERROR; |
| 180 | } |
| 181 | |
| 182 | wxZlibInputStream::~wxZlibInputStream() |
| 183 | { |
| 184 | inflateEnd(m_inflate); |
| 185 | delete m_inflate; |
| 186 | |
| 187 | delete [] m_z_buffer; |
| 188 | } |
| 189 | |
| 190 | size_t wxZlibInputStream::OnSysRead(void *buffer, size_t size) |
| 191 | { |
| 192 | wxASSERT_MSG(m_inflate && m_z_buffer, wxT("Inflate stream not open")); |
| 193 | |
| 194 | if (!m_inflate || !m_z_buffer) |
| 195 | m_lasterror = wxSTREAM_READ_ERROR; |
| 196 | if (!IsOk() || !size) |
| 197 | return 0; |
| 198 | |
| 199 | int err = Z_OK; |
| 200 | m_inflate->next_out = (unsigned char *)buffer; |
| 201 | m_inflate->avail_out = size; |
| 202 | |
| 203 | while (err == Z_OK && m_inflate->avail_out > 0) { |
| 204 | if (m_inflate->avail_in == 0 && m_parent_i_stream->IsOk()) { |
| 205 | m_parent_i_stream->Read(m_z_buffer, m_z_size); |
| 206 | m_inflate->next_in = m_z_buffer; |
| 207 | m_inflate->avail_in = m_parent_i_stream->LastRead(); |
| 208 | } |
| 209 | err = inflate(m_inflate, Z_SYNC_FLUSH); |
| 210 | } |
| 211 | |
| 212 | switch (err) { |
| 213 | case Z_OK: |
| 214 | break; |
| 215 | |
| 216 | case Z_STREAM_END: |
| 217 | if (m_inflate->avail_out) { |
| 218 | // Unread any data taken from past the end of the deflate stream, so that |
| 219 | // any additional data can be read from the underlying stream (the crc |
| 220 | // in a gzip for example) |
| 221 | if (m_inflate->avail_in) { |
| 222 | m_parent_i_stream->Reset(); |
| 223 | m_parent_i_stream->Ungetch(m_inflate->next_in, m_inflate->avail_in); |
| 224 | m_inflate->avail_in = 0; |
| 225 | } |
| 226 | m_lasterror = wxSTREAM_EOF; |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | case Z_BUF_ERROR: |
| 231 | // Indicates that zlib was expecting more data, but the parent stream |
| 232 | // has none. Other than Eof the error will have been already reported |
| 233 | // by the parent strean, |
| 234 | m_lasterror = wxSTREAM_READ_ERROR; |
| 235 | if (m_parent_i_stream->Eof()) |
| 236 | { |
| 237 | wxLogError(_("Can't read inflate stream: unexpected EOF in underlying stream.")); |
| 238 | } |
| 239 | break; |
| 240 | |
| 241 | default: |
| 242 | wxString msg(m_inflate->msg, *wxConvCurrent); |
| 243 | if (!msg) |
| 244 | msg = wxString::Format(_("zlib error %d"), err); |
| 245 | wxLogError(_("Can't read from inflate stream: %s"), msg.c_str()); |
| 246 | m_lasterror = wxSTREAM_READ_ERROR; |
| 247 | } |
| 248 | |
| 249 | size -= m_inflate->avail_out; |
| 250 | m_pos += size; |
| 251 | return size; |
| 252 | } |
| 253 | |
| 254 | /* static */ bool wxZlibInputStream::CanHandleGZip() |
| 255 | { |
| 256 | const char *dot = strchr(zlibVersion(), '.'); |
| 257 | int major = atoi(zlibVersion()); |
| 258 | int minor = dot ? atoi(dot + 1) : 0; |
| 259 | return major > 1 || (major == 1 && minor >= 2); |
| 260 | } |
| 261 | |
| 262 | bool wxZlibInputStream::SetDictionary(const char *data, const size_t datalen) |
| 263 | { |
| 264 | return (inflateSetDictionary(m_inflate, (Bytef*)data, datalen) == Z_OK); |
| 265 | } |
| 266 | |
| 267 | bool wxZlibInputStream::SetDictionary(const wxMemoryBuffer &buf) |
| 268 | { |
| 269 | return SetDictionary((char*)buf.GetData(), buf.GetDataLen()); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | ////////////////////// |
| 274 | // wxZlibOutputStream |
| 275 | ////////////////////// |
| 276 | |
| 277 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream& stream, |
| 278 | int level, |
| 279 | int flags) |
| 280 | : wxFilterOutputStream(stream) |
| 281 | { |
| 282 | Init(level, flags); |
| 283 | } |
| 284 | |
| 285 | wxZlibOutputStream::wxZlibOutputStream(wxOutputStream *stream, |
| 286 | int level, |
| 287 | int flags) |
| 288 | : wxFilterOutputStream(stream) |
| 289 | { |
| 290 | Init(level, flags); |
| 291 | } |
| 292 | |
| 293 | void wxZlibOutputStream::Init(int level, int flags) |
| 294 | { |
| 295 | m_deflate = NULL; |
| 296 | m_z_buffer = new unsigned char[ZSTREAM_BUFFER_SIZE]; |
| 297 | m_z_size = ZSTREAM_BUFFER_SIZE; |
| 298 | m_pos = 0; |
| 299 | |
| 300 | if ( level == -1 ) |
| 301 | { |
| 302 | level = Z_DEFAULT_COMPRESSION; |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | wxASSERT_MSG(level >= 0 && level <= 9, wxT("wxZlibOutputStream compression level must be between 0 and 9!")); |
| 307 | } |
| 308 | |
| 309 | // if gzip is asked for but not supported... |
| 310 | if (flags == wxZLIB_GZIP && !CanHandleGZip()) { |
| 311 | wxLogError(_("Gzip not supported by this version of zlib")); |
| 312 | m_lasterror = wxSTREAM_WRITE_ERROR; |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | if (m_z_buffer) { |
| 317 | m_deflate = new z_stream_s; |
| 318 | |
| 319 | if (m_deflate) { |
| 320 | memset(m_deflate, 0, sizeof(z_stream_s)); |
| 321 | m_deflate->next_out = m_z_buffer; |
| 322 | m_deflate->avail_out = m_z_size; |
| 323 | |
| 324 | // see zlib.h for documentation on windowBits |
| 325 | int windowBits = MAX_WBITS; |
| 326 | switch (flags) { |
| 327 | case wxZLIB_NO_HEADER: windowBits = -MAX_WBITS; break; |
| 328 | case wxZLIB_ZLIB: windowBits = MAX_WBITS; break; |
| 329 | case wxZLIB_GZIP: windowBits = MAX_WBITS | ZSTREAM_GZIP; break; |
| 330 | default: wxFAIL_MSG(wxT("Invalid zlib flag")); |
| 331 | } |
| 332 | |
| 333 | if (deflateInit2(m_deflate, level, Z_DEFLATED, windowBits, |
| 334 | 8, Z_DEFAULT_STRATEGY) == Z_OK) |
| 335 | return; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | wxLogError(_("Can't initialize zlib deflate stream.")); |
| 340 | m_lasterror = wxSTREAM_WRITE_ERROR; |
| 341 | } |
| 342 | |
| 343 | bool wxZlibOutputStream::Close() |
| 344 | { |
| 345 | DoFlush(true); |
| 346 | deflateEnd(m_deflate); |
| 347 | wxDELETE(m_deflate); |
| 348 | wxDELETEA(m_z_buffer); |
| 349 | |
| 350 | return wxFilterOutputStream::Close() && IsOk(); |
| 351 | } |
| 352 | |
| 353 | void wxZlibOutputStream::DoFlush(bool final) |
| 354 | { |
| 355 | if (!m_deflate || !m_z_buffer) |
| 356 | m_lasterror = wxSTREAM_WRITE_ERROR; |
| 357 | if (!IsOk()) |
| 358 | return; |
| 359 | |
| 360 | int err = Z_OK; |
| 361 | bool done = false; |
| 362 | |
| 363 | while (err == Z_OK || err == Z_STREAM_END) { |
| 364 | size_t len = m_z_size - m_deflate->avail_out; |
| 365 | if (len) { |
| 366 | if (m_parent_o_stream->Write(m_z_buffer, len).LastWrite() != len) { |
| 367 | m_lasterror = wxSTREAM_WRITE_ERROR; |
| 368 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); |
| 369 | break; |
| 370 | } |
| 371 | m_deflate->next_out = m_z_buffer; |
| 372 | m_deflate->avail_out = m_z_size; |
| 373 | } |
| 374 | |
| 375 | if (done) |
| 376 | break; |
| 377 | err = deflate(m_deflate, final ? Z_FINISH : Z_FULL_FLUSH); |
| 378 | done = m_deflate->avail_out != 0 || err == Z_STREAM_END; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | size_t wxZlibOutputStream::OnSysWrite(const void *buffer, size_t size) |
| 383 | { |
| 384 | wxASSERT_MSG(m_deflate && m_z_buffer, wxT("Deflate stream not open")); |
| 385 | |
| 386 | if (!m_deflate || !m_z_buffer) |
| 387 | { |
| 388 | // notice that this will make IsOk() test just below return false |
| 389 | m_lasterror = wxSTREAM_WRITE_ERROR; |
| 390 | } |
| 391 | |
| 392 | if (!IsOk() || !size) |
| 393 | return 0; |
| 394 | |
| 395 | int err = Z_OK; |
| 396 | m_deflate->next_in = (unsigned char *)buffer; |
| 397 | m_deflate->avail_in = size; |
| 398 | |
| 399 | while (err == Z_OK && m_deflate->avail_in > 0) { |
| 400 | if (m_deflate->avail_out == 0) { |
| 401 | m_parent_o_stream->Write(m_z_buffer, m_z_size); |
| 402 | if (m_parent_o_stream->LastWrite() != m_z_size) { |
| 403 | m_lasterror = wxSTREAM_WRITE_ERROR; |
| 404 | wxLogDebug(wxT("wxZlibOutputStream: Error writing to underlying stream")); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | m_deflate->next_out = m_z_buffer; |
| 409 | m_deflate->avail_out = m_z_size; |
| 410 | } |
| 411 | |
| 412 | err = deflate(m_deflate, Z_NO_FLUSH); |
| 413 | } |
| 414 | |
| 415 | if (err != Z_OK) { |
| 416 | m_lasterror = wxSTREAM_WRITE_ERROR; |
| 417 | wxString msg(m_deflate->msg, *wxConvCurrent); |
| 418 | if (!msg) |
| 419 | msg = wxString::Format(_("zlib error %d"), err); |
| 420 | wxLogError(_("Can't write to deflate stream: %s"), msg.c_str()); |
| 421 | } |
| 422 | |
| 423 | size -= m_deflate->avail_in; |
| 424 | m_pos += size; |
| 425 | return size; |
| 426 | } |
| 427 | |
| 428 | /* static */ bool wxZlibOutputStream::CanHandleGZip() |
| 429 | { |
| 430 | return wxZlibInputStream::CanHandleGZip(); |
| 431 | } |
| 432 | |
| 433 | bool wxZlibOutputStream::SetDictionary(const char *data, const size_t datalen) |
| 434 | { |
| 435 | return (deflateSetDictionary(m_deflate, (Bytef*)data, datalen) == Z_OK); |
| 436 | } |
| 437 | |
| 438 | bool wxZlibOutputStream::SetDictionary(const wxMemoryBuffer &buf) |
| 439 | { |
| 440 | return SetDictionary((char*)buf.GetData(), buf.GetDataLen()); |
| 441 | } |
| 442 | |
| 443 | #endif |
| 444 | // wxUSE_ZLIB && wxUSE_STREAMS |