diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-07 22:17:44 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-07 22:17:44 +0000 |
commit | bfb6a560ee1f95a5350e280e61a5a3d2e01b92c2 (patch) | |
tree | 2c4c231716b6aa30ace2c3dd1f83f172337368f0 | |
parent | 2e7c2aa7179907202155d4714913e0a0724e11a5 (diff) | |
download | chromium_src-bfb6a560ee1f95a5350e280e61a5a3d2e01b92c2.zip chromium_src-bfb6a560ee1f95a5350e280e61a5a3d2e01b92c2.tar.gz chromium_src-bfb6a560ee1f95a5350e280e61a5a3d2e01b92c2.tar.bz2 |
zlib decompression for chromoting
Using zlib for decompression. Also revised the API and usage of zlib
for compression.
TEST=none
BUG=none
Review URL: http://codereview.chromium.org/2815043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51785 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | remoting/base/compressor.h | 25 | ||||
-rw-r--r-- | remoting/base/compressor_zlib.cc | 29 | ||||
-rw-r--r-- | remoting/base/compressor_zlib.h | 7 | ||||
-rw-r--r-- | remoting/base/compressor_zlib_unittest.cc | 34 | ||||
-rw-r--r-- | remoting/base/decompressor.h | 40 | ||||
-rw-r--r-- | remoting/base/decompressor_zlib.cc | 63 | ||||
-rw-r--r-- | remoting/base/decompressor_zlib.h | 32 | ||||
-rw-r--r-- | remoting/base/decompressor_zlib_unittest.cc | 136 | ||||
-rw-r--r-- | remoting/remoting.gyp | 10 |
9 files changed, 322 insertions, 54 deletions
diff --git a/remoting/base/compressor.h b/remoting/base/compressor.h index 4c242bd..fd4c412 100644 --- a/remoting/base/compressor.h +++ b/remoting/base/compressor.h @@ -21,27 +21,20 @@ class Compressor { // Compress |input_data| with |input_size| bytes. // // |output_data| is provided by the caller and |output_size| is the - // size of |output_data|. + // size of |output_data|. |output_size| must be greater than 0. + // + // |input_size| is set to 0 to indicate the end of input stream. // // Compressed data is written to |output_data|. |consumed| will // contain the number of bytes consumed from the input. |written| // contains the number of bytes written to output. - virtual void Write(const uint8* input_data, int input_size, - uint8* output_data, int output_size, - int* consumed, int* written) = 0; - - // Flush all the remaining data in the compressor. - // - // |output_data| is provided by the caller and |output_size| is called - // with the size of |output_data|. - // - // Output data is written to |output_data| and |written| contains the - // number of bytes written. // - // Returns true if |output_data| is not large enough to carry all - // compressed data and caller needs to call Flush() again. - virtual bool Flush(uint8* output_data, int output_size, - int* written) = 0; + // Returns true if this method needs to be called again because + // there is more data to be written out. This is particularly + // useful for end of the compression stream. + virtual bool Process(const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* consumed, int* written) = 0; }; } // namespace remoting diff --git a/remoting/base/compressor_zlib.cc b/remoting/base/compressor_zlib.cc index e08d2f9..ae5b388 100644 --- a/remoting/base/compressor_zlib.cc +++ b/remoting/base/compressor_zlib.cc @@ -35,39 +35,28 @@ CompressorZlib::~CompressorZlib() { deflateEnd(stream_.get()); } -void CompressorZlib::Write(const uint8* input_data, int input_size, - uint8* output_data, int output_size, - int* consumed, int* written) { +bool CompressorZlib::Process(const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* consumed, int* written) { + DCHECK_GT(output_size, 0); + // Setup I/O parameters. stream_->avail_in = input_size; stream_->next_in = (Bytef*)input_data; stream_->avail_out = output_size; stream_->next_out = (Bytef*)output_data; - int ret = deflate(stream_.get(), Z_NO_FLUSH); + int ret = deflate(stream_.get(), input_size ? Z_NO_FLUSH : Z_FINISH); if (ret == Z_STREAM_ERROR) { NOTREACHED() << "zlib compression failed"; } *consumed = input_size - stream_->avail_in; *written = output_size - stream_->avail_out; -} - -bool CompressorZlib::Flush(uint8* output_data, int output_size, - int* written) { - // Setup I/O parameters. - stream_->avail_in = 0; - stream_->next_in = NULL; - stream_->avail_out = output_size; - stream_->next_out = (Bytef*)output_data; - int ret = deflate(stream_.get(), Z_FINISH); - if (ret == Z_STREAM_ERROR) { - NOTREACHED() << "zlib compression failed"; - } - - *written = output_size - stream_->avail_out; - return ret == Z_OK; + // Return true when we get Z_BUF_ERROR, this way we can feed more data + // to zlib. + return ret == Z_OK || ret == Z_BUF_ERROR; } } // namespace remoting diff --git a/remoting/base/compressor_zlib.h b/remoting/base/compressor_zlib.h index 93bd5ca..5c8b91c 100644 --- a/remoting/base/compressor_zlib.h +++ b/remoting/base/compressor_zlib.h @@ -19,10 +19,9 @@ class CompressorZlib : public Compressor { virtual ~CompressorZlib(); // Compressor implementations. - virtual void Write(const uint8* input_data, int input_size, - uint8* output_data, int output_size, - int* consumed, int* written); - virtual bool Flush(uint8* output_data, int output_size, int* written); + virtual bool Process(const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* consumed, int* written); private: scoped_ptr<z_stream> stream_; diff --git a/remoting/base/compressor_zlib_unittest.cc b/remoting/base/compressor_zlib_unittest.cc index 9d65aad..8127350 100644 --- a/remoting/base/compressor_zlib_unittest.cc +++ b/remoting/base/compressor_zlib_unittest.cc @@ -21,22 +21,17 @@ static void Compress(remoting::Compressor* compressor, // Feed data into the compress until the end. // This loop will rewrite |output_data| continuously. - while (input_size) { - int consumed, written; - compressor->Write(input_data, input_size, - output_data, output_size, - &consumed, &written); + int consumed = 0; + int written = 0; + while (compressor->Process(input_data, input_size, + output_data, output_size, + &consumed, &written)) { input_data += consumed; input_size -= consumed; } - - // And then flush the remaining data from the compressor. - int written; - while (compressor->Flush(output_data, output_size, &written)) { - } } -TEST(CompressorZlibTest, SimpleCompress) { +TEST(CompressorZlibTest, Compress) { static const int kRawDataSize = 1024 * 128; static const int kCompressedDataSize = 256; uint8 raw_data[kRawDataSize]; @@ -50,3 +45,20 @@ TEST(CompressorZlibTest, SimpleCompress) { Compress(&compressor, raw_data, kRawDataSize, compressed_data, kCompressedDataSize); } + +// Checks that zlib can work with a small output buffer by reading +// less from the input. +TEST(CompressorZlibTest, SmallOutputBuffer) { + static const int kRawDataSize = 1024 * 128; + static const int kCompressedDataSize = 1; + uint8 raw_data[kRawDataSize]; + uint8 compressed_data[kCompressedDataSize]; + + // Generate the test data.g + GenerateTestData(raw_data, kRawDataSize, 99); + + // Then use the compressor to compress. + remoting::CompressorZlib compressor; + Compress(&compressor, raw_data, kRawDataSize, + compressed_data, kCompressedDataSize); +} diff --git a/remoting/base/decompressor.h b/remoting/base/decompressor.h new file mode 100644 index 0000000..b688b3b --- /dev/null +++ b/remoting/base/decompressor.h @@ -0,0 +1,40 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_BASE_DECOMPRESSOR_H_ +#define REMOTING_BASE_DECOMPRESSOR_H_ + +#include "base/basictypes.h" + +namespace remoting { + +// An object to decompress data losslessly. This is used in pair with +// a compressor. +// +// Note that a decompressor can only be used on one stream during its +// lifetime. This object should be destroyed after use. +class Decompressor { + public: + virtual ~Decompressor() {} + + // Decompress |input_data| with |input_size| bytes. + // + // |output_data| is provided by the caller and |output_size| is the + // size of |output_data|. |output_size| must be greater than 0. + // + // Decompressed data is written to |output_data|. |consumed| will + // contain the number of bytes consumed from the input. |written| + // contains the number of bytes written to output. + // + // Returns true if this method needs to be called again because + // there is more bytes to be decompressed or more input data is + // needed. + virtual bool Process(const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* consumed, int* written) = 0; +}; + +} // namespace remoting + +#endif // REMOTING_BASE_DECOMPRESSOR_H_ diff --git a/remoting/base/decompressor_zlib.cc b/remoting/base/decompressor_zlib.cc new file mode 100644 index 0000000..e3060bd --- /dev/null +++ b/remoting/base/decompressor_zlib.cc @@ -0,0 +1,63 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "remoting/base/decompressor_zlib.h" + +#if defined(USE_SYSTEM_ZLIB) +#include <zlib.h> +// The code below uses the MOZ_Z_ forms of these functions in order that things +// should work on Windows. In order to make this code cross platform, we map +// back to the normal functions here in the case that we are using the system +// zlib. +#define MOZ_Z_inflate inflate +#define MOZ_Z_inflateEnd inflateEnd +#define MOZ_Z_inflateInit_ inflateInit_ +#else +#include "third_party/zlib/zlib.h" +#endif +#include "base/logging.h" + +namespace remoting { + +DecompressorZlib::DecompressorZlib() { + stream_.reset(new z_stream()); + + stream_->next_in = Z_NULL; + stream_->zalloc = Z_NULL; + stream_->zfree = Z_NULL; + stream_->opaque = Z_NULL; + + inflateInit(stream_.get()); +} + +DecompressorZlib::~DecompressorZlib() { + inflateEnd(stream_.get()); +} + +bool DecompressorZlib::Process(const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* consumed, int* written) { + DCHECK_GT(output_size, 0); + + // Setup I/O parameters. + stream_->avail_in = input_size; + stream_->next_in = (Bytef*)input_data; + stream_->avail_out = output_size; + stream_->next_out = (Bytef*)output_data; + + int ret = inflate(stream_.get(), Z_NO_FLUSH); + if (ret == Z_STREAM_ERROR) { + NOTREACHED() << "zlib compression failed"; + } + + *consumed = input_size - stream_->avail_in; + *written = output_size - stream_->avail_out; + + // Since we check that output is always greater than 0, the only + // reason for us to get Z_BUF_ERROR is when zlib requires more input + // data. + return ret == Z_OK || ret == Z_BUF_ERROR; +} + +} // namespace remoting diff --git a/remoting/base/decompressor_zlib.h b/remoting/base/decompressor_zlib.h new file mode 100644 index 0000000..b762480 --- /dev/null +++ b/remoting/base/decompressor_zlib.h @@ -0,0 +1,32 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef REMOTING_BASE_DECOMPRESSOR_ZLIB_H_ +#define REMOTING_BASE_DECOMPRESSOR_ZLIB_H_ + +#include "base/scoped_ptr.h" +#include "remoting/base/decompressor.h" + +typedef struct z_stream_s z_stream; + +namespace remoting { + +// A lossless decompressor using zlib. +class DecompressorZlib : public Decompressor { + public: + DecompressorZlib(); + virtual ~DecompressorZlib(); + + // Decompressor implementations. + virtual bool Process(const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* consumed, int* written); + + private: + scoped_ptr<z_stream> stream_; +}; + +} // namespace remoting + +#endif // REMOTING_BASE_DECOMPRESSOR_ZLIB_H_ diff --git a/remoting/base/decompressor_zlib_unittest.cc b/remoting/base/decompressor_zlib_unittest.cc new file mode 100644 index 0000000..d04a923 --- /dev/null +++ b/remoting/base/decompressor_zlib_unittest.cc @@ -0,0 +1,136 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include <stdlib.h> + +#include "remoting/base/compressor_zlib.h" +#include "remoting/base/decompressor_zlib.h" +#include "testing/gtest/include/gtest/gtest.h" + +static void GenerateTestData(uint8* data, int size, int seed) { + srand(seed); + for (int i = 0; i < size; ++i) + data[i] = rand() % 256; +} + +// Keep compressing |input_data| into |output_data| until the last +// bytes is consumed. +// +// The compressed size is written to |compressed_size|. +static void Compress(remoting::Compressor* compressor, + const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* compressed_size) { + *compressed_size = 0; + while (true) { + int consumed, written; + bool ret = compressor->Process(input_data, input_size, + output_data, output_size, + &consumed, &written); + input_data += consumed; + input_size -= consumed; + output_data += written; + output_size -= written; + *compressed_size += written; + + if (!ret) + break; + } +} + +// The decompressed size is written to |decompressed_size|. +static void Decompress(remoting::Decompressor* decompressor, + const uint8* input_data, int input_size, + uint8* output_data, int output_size, + int* decompressed_size) { + *decompressed_size = 0; + while (true) { + int consumed, written; + bool ret = decompressor->Process(input_data, input_size, + output_data, output_size, + &consumed, &written); + input_data += consumed; + input_size -= consumed; + output_data += written; + output_size -= written; + *decompressed_size += written; + + if (!ret) + break; + } +} + +TEST(DecompressorZlibTest, CompressAndDecompress) { + static const int kRawDataSize = 1024 * 128; + static const int kCompressedDataSize = 2 * kRawDataSize; + static const int kDecompressedDataSize = kRawDataSize; + + uint8 raw_data[kRawDataSize]; + uint8 compressed_data[kCompressedDataSize]; + uint8 decompressed_data[kDecompressedDataSize]; + + // Generate the test data.g + GenerateTestData(raw_data, kRawDataSize, 99); + + // Then use the compressor. + remoting::CompressorZlib compressor; + int compressed_size = 0; + Compress(&compressor, raw_data, kRawDataSize, + compressed_data, kCompressedDataSize, + &compressed_size); + + // Then use the decompressor. + remoting::DecompressorZlib decompressor; + int decompressed_size = 0; + Decompress(&decompressor, compressed_data, compressed_size, + decompressed_data, kDecompressedDataSize, + &decompressed_size); + + EXPECT_EQ(kRawDataSize, decompressed_size); + EXPECT_EQ(0, memcmp(raw_data, decompressed_data, decompressed_size)); +} + +// Checks that zlib can work with a small output buffer by limiting +// number of bytes it gets from the input. +TEST(DecompressorZlibTest, SmallOutputBuffer) { + static const int kRawDataSize = 1024 * 128; + static const int kCompressedDataSize = 2 * kRawDataSize; + + uint8 raw_data[kRawDataSize]; + uint8 compressed_data[kCompressedDataSize]; + + // Generate the test data. + GenerateTestData(raw_data, kRawDataSize, 99); + + // Then use the compressor to compress. + remoting::CompressorZlib compressor; + int compressed_size = 0; + Compress(&compressor, raw_data, kRawDataSize, + compressed_data, kCompressedDataSize, + &compressed_size); + + // Then use the decompressor. We decompress into a 1 byte buffer + // every time. + remoting::DecompressorZlib decompressor; + uint8* input_data = compressed_data; + int input_size = compressed_size; + int decompressed_size = 0; + while (true) { + int consumed, written; + uint8 output_data; + bool ret = decompressor.Process(input_data, input_size, + &output_data, 1, + &consumed, &written); + input_data += consumed; + input_size -= consumed; + + // Expect that there's only one byte written. + EXPECT_EQ(1, written); + decompressed_size += written; + + if (!ret) + break; + } + EXPECT_EQ(kRawDataSize, decompressed_size); +} diff --git a/remoting/remoting.gyp b/remoting/remoting.gyp index b045a62..82df802 100644 --- a/remoting/remoting.gyp +++ b/remoting/remoting.gyp @@ -122,17 +122,20 @@ # depend on chromotocol_proto_lib for headers. 'hard_dependency': 1, 'sources': [ + 'base/compressor.h', + 'base/compressor_zlib.cc', + 'base/compressor_zlib.h', 'base/constants.cc', 'base/constants.h', - 'base/lossless_compressor.h', + 'base/decompressor.h', + 'base/decompressor_zlib.cc', + 'base/decompressor_zlib.h', 'base/multiple_array_input_stream.cc', 'base/multiple_array_input_stream.h', 'base/protocol_decoder.cc', 'base/protocol_decoder.h', 'base/protocol_util.cc', 'base/protocol_util.h', - 'base/compressor_zlib.cc', - 'base/compressor_zlib.h', ], }, # end of target 'chromoting_base' @@ -328,6 +331,7 @@ ], 'sources': [ 'base/compressor_zlib_unittest.cc', + 'base/decompressor_zlib_unittest.cc', 'base/mock_objects.h', 'base/multiple_array_input_stream_unittest.cc', 'base/protocol_decoder_unittest.cc', |