From b398dcabaffbbc53d24d296fed3f64b0b8ecd519 Mon Sep 17 00:00:00 2001 From: Einar Johan Trøan Sømåen Date: Wed, 18 Jul 2012 13:54:15 +0200 Subject: COMMON: Add an optional argument to wrapCompressedReadStream, to simplify using streams that can't tell their size() --- common/zlib.cpp | 9 +++++---- common/zlib.h | 10 +++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/common/zlib.cpp b/common/zlib.cpp index 7d765fc539..76e34485da 100644 --- a/common/zlib.cpp +++ b/common/zlib.cpp @@ -107,7 +107,7 @@ protected: public: - GZipReadStream(SeekableReadStream *w) : _wrapped(w), _stream() { + GZipReadStream(SeekableReadStream *w, uint32 knownSize = 0) : _wrapped(w), _stream() { assert(w != 0); // Verify file header is correct @@ -122,7 +122,8 @@ public: _origSize = w->readUint32LE(); } else { // Original size not available in zlib format - _origSize = 0; + // use an otherwise known size if supplied. + _origSize = knownSize; } _pos = 0; w->seek(0, SEEK_SET); @@ -336,7 +337,7 @@ public: #endif // USE_ZLIB -SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped) { +SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped, uint32 knownSize) { #if defined(USE_ZLIB) if (toBeWrapped) { uint16 header = toBeWrapped->readUint16BE(); @@ -345,7 +346,7 @@ SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped) { header % 31 == 0)); toBeWrapped->seek(-2, SEEK_CUR); if (isCompressed) - return new GZipReadStream(toBeWrapped); + return new GZipReadStream(toBeWrapped, knownSize); } #endif return toBeWrapped; diff --git a/common/zlib.h b/common/zlib.h index 61322c286a..8372499922 100644 --- a/common/zlib.h +++ b/common/zlib.h @@ -86,10 +86,18 @@ bool inflateZlibHeaderless(byte *dst, uint dstLen, const byte *src, uint srcLen, * format. In the former case, the original stream is returned unmodified * (and in particular, not wrapped). * + * Certain GZip-formats don't supply an easily readable length, if you + * still need the length carried along with the stream, and you know + * the decompressed length at wrap-time, then it can be supplied as knownSize + * here. knownSize will be ignored if the GZip-stream DOES include a length. + * * It is safe to call this with a NULL parameter (in this case, NULL is * returned). + * + * @param toBeWrapped the stream to be wrapped (if it is in gzip-format) + * @param knownSize a supplied length of the compressed data (if not available directly) */ -SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped); +SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped, uint32 knownSize = 0); /** * Take an arbitrary WriteStream and wrap it in a custom stream which provides -- cgit v1.2.3