OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/base/compressor_zlib.h" |
| 6 |
| 7 #if defined(USE_SYSTEM_ZLIB) |
| 8 #include <zlib.h> |
| 9 // The code below uses the MOZ_Z_ forms of these functions in order that things |
| 10 // should work on Windows. In order to make this code cross platform, we map |
| 11 // back to the normal functions here in the case that we are using the system |
| 12 // zlib. |
| 13 #define MOZ_Z_deflate deflate |
| 14 #define MOZ_Z_deflateEnd deflateEnd |
| 15 #define MOZ_Z_deflateInit_ deflateInit_ |
| 16 #else |
| 17 #include "third_party/zlib/zlib.h" |
| 18 #endif |
| 19 #include "base/logging.h" |
| 20 |
| 21 namespace remoting { |
| 22 |
| 23 CompressorZlib::CompressorZlib() { |
| 24 stream_.reset(new z_stream()); |
| 25 |
| 26 stream_->next_in = Z_NULL; |
| 27 stream_->zalloc = Z_NULL; |
| 28 stream_->zfree = Z_NULL; |
| 29 stream_->opaque = Z_NULL; |
| 30 |
| 31 deflateInit(stream_.get(), Z_BEST_SPEED); |
| 32 } |
| 33 |
| 34 CompressorZlib::~CompressorZlib() { |
| 35 deflateEnd(stream_.get()); |
| 36 } |
| 37 |
| 38 void CompressorZlib::Write(const uint8* input_data, int input_size, |
| 39 uint8* output_data, int output_size, |
| 40 int* consumed, int* written) { |
| 41 // Setup I/O parameters. |
| 42 stream_->avail_in = input_size; |
| 43 stream_->next_in = (Bytef*)input_data; |
| 44 stream_->avail_out = output_size; |
| 45 stream_->next_out = (Bytef*)output_data; |
| 46 |
| 47 int ret = deflate(stream_.get(), Z_NO_FLUSH); |
| 48 if (ret == Z_STREAM_ERROR) { |
| 49 NOTREACHED() << "zlib compression failed"; |
| 50 } |
| 51 |
| 52 *consumed = input_size - stream_->avail_in; |
| 53 *written = output_size - stream_->avail_out; |
| 54 } |
| 55 |
| 56 bool CompressorZlib::Flush(uint8* output_data, int output_size, |
| 57 int* written) { |
| 58 // Setup I/O parameters. |
| 59 stream_->avail_in = 0; |
| 60 stream_->next_in = NULL; |
| 61 stream_->avail_out = output_size; |
| 62 stream_->next_out = (Bytef*)output_data; |
| 63 |
| 64 int ret = deflate(stream_.get(), Z_FINISH); |
| 65 if (ret == Z_STREAM_ERROR) { |
| 66 NOTREACHED() << "zlib compression failed"; |
| 67 } |
| 68 |
| 69 *written = output_size - stream_->avail_out; |
| 70 return ret == Z_OK; |
| 71 } |
| 72 |
| 73 } // namespace remoting |
OLD | NEW |