| 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 #else | |
| 10 #include "third_party/zlib/zlib.h" | |
| 11 #endif | |
| 12 #include "base/logging.h" | |
| 13 | |
| 14 namespace remoting { | |
| 15 | |
| 16 CompressorZlib::CompressorZlib() { | |
| 17 Reset(); | |
| 18 } | |
| 19 | |
| 20 CompressorZlib::~CompressorZlib() { | |
| 21 deflateEnd(stream_.get()); | |
| 22 } | |
| 23 | |
| 24 void CompressorZlib::Reset() { | |
| 25 if (stream_.get()) | |
| 26 deflateEnd(stream_.get()); | |
| 27 | |
| 28 stream_.reset(new z_stream()); | |
| 29 | |
| 30 stream_->next_in = Z_NULL; | |
| 31 stream_->zalloc = Z_NULL; | |
| 32 stream_->zfree = Z_NULL; | |
| 33 stream_->opaque = Z_NULL; | |
| 34 | |
| 35 deflateInit(stream_.get(), Z_BEST_SPEED); | |
| 36 } | |
| 37 | |
| 38 bool CompressorZlib::Process(const uint8* input_data, int input_size, | |
| 39 uint8* output_data, int output_size, | |
| 40 CompressorFlush flush, int* consumed, | |
| 41 int* written) { | |
| 42 DCHECK_GT(output_size, 0); | |
| 43 | |
| 44 // Setup I/O parameters. | |
| 45 stream_->avail_in = input_size; | |
| 46 stream_->next_in = (Bytef*)input_data; | |
| 47 stream_->avail_out = output_size; | |
| 48 stream_->next_out = (Bytef*)output_data; | |
| 49 | |
| 50 int z_flush = 0; | |
| 51 if (flush == CompressorSyncFlush) { | |
| 52 z_flush = Z_SYNC_FLUSH; | |
| 53 } else if (flush == CompressorFinish) { | |
| 54 z_flush = Z_FINISH; | |
| 55 } else if (flush == CompressorNoFlush) { | |
| 56 z_flush = Z_NO_FLUSH; | |
| 57 } else { | |
| 58 NOTREACHED() << "Unsupported flush mode"; | |
| 59 } | |
| 60 | |
| 61 int ret = deflate(stream_.get(), z_flush); | |
| 62 if (ret == Z_STREAM_ERROR) { | |
| 63 NOTREACHED() << "zlib compression failed"; | |
| 64 } | |
| 65 | |
| 66 *consumed = input_size - stream_->avail_in; | |
| 67 *written = output_size - stream_->avail_out; | |
| 68 | |
| 69 // If |ret| equals Z_STREAM_END we have reached the end of stream. | |
| 70 // If |ret| equals Z_BUF_ERROR we have the end of the synchronication point. | |
| 71 // For these two cases we need to stop compressing. | |
| 72 if (ret == Z_OK) { | |
| 73 return true; | |
| 74 } else if (ret == Z_STREAM_END) { | |
| 75 return false; | |
| 76 } else if (ret == Z_BUF_ERROR) { | |
| 77 return stream_->avail_out == 0; | |
| 78 } else { | |
| 79 NOTREACHED() << "Unexpected zlib error: " << ret; | |
| 80 return false; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 } // namespace remoting | |
| OLD | NEW |