| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/decompressor_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 DecompressorZlib::DecompressorZlib() { | |
| 17 InitStream(); | |
| 18 } | |
| 19 | |
| 20 DecompressorZlib::~DecompressorZlib() { | |
| 21 inflateEnd(stream_.get()); | |
| 22 } | |
| 23 | |
| 24 void DecompressorZlib::Reset() { | |
| 25 inflateEnd(stream_.get()); | |
| 26 InitStream(); | |
| 27 } | |
| 28 | |
| 29 bool DecompressorZlib::Process(const uint8* input_data, int input_size, | |
| 30 uint8* output_data, int output_size, | |
| 31 int* consumed, int* written) { | |
| 32 DCHECK_GT(output_size, 0); | |
| 33 | |
| 34 // Setup I/O parameters. | |
| 35 stream_->avail_in = input_size; | |
| 36 stream_->next_in = (Bytef*)input_data; | |
| 37 stream_->avail_out = output_size; | |
| 38 stream_->next_out = (Bytef*)output_data; | |
| 39 | |
| 40 int ret = inflate(stream_.get(), Z_NO_FLUSH); | |
| 41 if (ret == Z_STREAM_ERROR) { | |
| 42 NOTREACHED() << "zlib compression failed"; | |
| 43 } | |
| 44 | |
| 45 *consumed = input_size - stream_->avail_in; | |
| 46 *written = output_size - stream_->avail_out; | |
| 47 | |
| 48 // Since we check that output is always greater than 0, the only | |
| 49 // reason for us to get Z_BUF_ERROR is when zlib requires more input | |
| 50 // data. | |
| 51 return ret == Z_OK || ret == Z_BUF_ERROR; | |
| 52 } | |
| 53 | |
| 54 void DecompressorZlib::InitStream() { | |
| 55 stream_.reset(new z_stream()); | |
| 56 | |
| 57 stream_->next_in = Z_NULL; | |
| 58 stream_->zalloc = Z_NULL; | |
| 59 stream_->zfree = Z_NULL; | |
| 60 stream_->opaque = Z_NULL; | |
| 61 | |
| 62 int ret = inflateInit(stream_.get()); | |
| 63 DCHECK_EQ(ret, Z_OK); | |
| 64 } | |
| 65 | |
| 66 } // namespace remoting | |
| OLD | NEW |