diff options
Diffstat (limited to 'src/google/protobuf/io/gzip_stream.cc')
-rw-r--r-- | src/google/protobuf/io/gzip_stream.cc | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/src/google/protobuf/io/gzip_stream.cc b/src/google/protobuf/io/gzip_stream.cc index ee28696..84d277f 100644 --- a/src/google/protobuf/io/gzip_stream.cc +++ b/src/google/protobuf/io/gzip_stream.cc @@ -1,6 +1,6 @@ // Protocol Buffers - Google's data interchange format // Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ +// http://code.google.com/p/protobuf/ // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are @@ -73,17 +73,6 @@ GzipInputStream::~GzipInputStream() { zerror_ = inflateEnd(&zcontext_); } -static inline int internalInflateInit2( - z_stream* zcontext, GzipInputStream::Format format) { - int windowBitsFormat = 0; - switch (format) { - case GzipInputStream::GZIP: windowBitsFormat = 16; break; - case GzipInputStream::AUTO: windowBitsFormat = 32; break; - case GzipInputStream::ZLIB: windowBitsFormat = 0; break; - } - return inflateInit2(zcontext, /* windowBits */15 | windowBitsFormat); -} - int GzipInputStream::Inflate(int flush) { if ((zerror_ == Z_OK) && (zcontext_.avail_out == 0)) { // previous inflate filled output buffer. don't change input params yet. @@ -100,7 +89,14 @@ int GzipInputStream::Inflate(int flush) { zcontext_.next_in = static_cast<Bytef*>(const_cast<void*>(in)); zcontext_.avail_in = in_size; if (first) { - int error = internalInflateInit2(&zcontext_, format_); + int windowBitsFormat = 0; + switch (format_) { + case GZIP: windowBitsFormat = 16; break; + case AUTO: windowBitsFormat = 32; break; + case ZLIB: windowBitsFormat = 0; break; + } + int error = inflateInit2(&zcontext_, + /* windowBits */15 | windowBitsFormat); if (error != Z_OK) { return error; } @@ -131,21 +127,9 @@ bool GzipInputStream::Next(const void** data, int* size) { return true; } if (zerror_ == Z_STREAM_END) { - if (zcontext_.next_out != NULL) { - // sub_stream_ may have concatenated streams to follow - zerror_ = inflateEnd(&zcontext_); - if (zerror_ != Z_OK) { - return false; - } - zerror_ = internalInflateInit2(&zcontext_, format_); - if (zerror_ != Z_OK) { - return false; - } - } else { - *data = NULL; - *size = 0; - return false; - } + *data = NULL; + *size = 0; + return false; } zerror_ = Inflate(Z_NO_FLUSH); if ((zerror_ == Z_STREAM_END) && (zcontext_.next_out == NULL)) { @@ -199,6 +183,16 @@ GzipOutputStream::GzipOutputStream(ZeroCopyOutputStream* sub_stream, Init(sub_stream, options); } +GzipOutputStream::GzipOutputStream( + ZeroCopyOutputStream* sub_stream, Format format, int buffer_size) { + Options options; + options.format = format; + if (buffer_size != -1) { + options.buffer_size = buffer_size; + } + Init(sub_stream, options); +} + void GzipOutputStream::Init(ZeroCopyOutputStream* sub_stream, const Options& options) { sub_stream_ = sub_stream; @@ -257,7 +251,8 @@ int GzipOutputStream::Deflate(int flush) { } error = deflate(&zcontext_, flush); } while (error == Z_OK && zcontext_.avail_out == 0); - if ((flush == Z_FULL_FLUSH) || (flush == Z_FINISH)) { + if (((flush == Z_FULL_FLUSH) || (flush == Z_FINISH)) + && (zcontext_.avail_out != sub_data_size_)) { // Notify lower layer of data. sub_stream_->BackUp(zcontext_.avail_out); // We don't own the buffer anymore. @@ -299,11 +294,10 @@ int64 GzipOutputStream::ByteCount() const { } bool GzipOutputStream::Flush() { - zerror_ = Deflate(Z_FULL_FLUSH); - // Return true if the flush succeeded or if it was a no-op. - return (zerror_ == Z_OK) || - (zerror_ == Z_BUF_ERROR && zcontext_.avail_in == 0 && - zcontext_.avail_out != 0); + do { + zerror_ = Deflate(Z_FULL_FLUSH); + } while (zerror_ == Z_OK); + return zerror_ == Z_OK; } bool GzipOutputStream::Close() { |