OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/base/decompressor_zlib.h" | 5 #include "remoting/base/decompressor_zlib.h" |
6 | 6 |
7 #if defined(USE_SYSTEM_ZLIB) | 7 #if defined(USE_SYSTEM_ZLIB) |
8 #include <zlib.h> | 8 #include <zlib.h> |
9 // The code below uses the MOZ_Z_ forms of these functions in order that things | 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 | 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 | 11 // back to the normal functions here in the case that we are using the system |
12 // zlib. | 12 // zlib. |
13 #define MOZ_Z_inflate inflate | 13 #define MOZ_Z_inflate inflate |
14 #define MOZ_Z_inflateEnd inflateEnd | 14 #define MOZ_Z_inflateEnd inflateEnd |
15 #define MOZ_Z_inflateInit_ inflateInit_ | 15 #define MOZ_Z_inflateInit_ inflateInit_ |
16 #else | 16 #else |
17 #include "third_party/zlib/zlib.h" | 17 #include "third_party/zlib/zlib.h" |
18 #endif | 18 #endif |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
20 | 20 |
21 namespace remoting { | 21 namespace remoting { |
22 | 22 |
23 DecompressorZlib::DecompressorZlib() { | 23 DecompressorZlib::DecompressorZlib() { |
24 stream_.reset(new z_stream()); | 24 InitStream(); |
25 | |
26 stream_->next_in = Z_NULL; | |
27 stream_->zalloc = Z_NULL; | |
28 stream_->zfree = Z_NULL; | |
29 stream_->opaque = Z_NULL; | |
30 | |
31 inflateInit(stream_.get()); | |
32 } | 25 } |
33 | 26 |
34 DecompressorZlib::~DecompressorZlib() { | 27 DecompressorZlib::~DecompressorZlib() { |
| 28 Reset(); |
| 29 } |
| 30 |
| 31 void DecompressorZlib::Reset() { |
35 inflateEnd(stream_.get()); | 32 inflateEnd(stream_.get()); |
| 33 InitStream(); |
36 } | 34 } |
37 | 35 |
38 bool DecompressorZlib::Process(const uint8* input_data, int input_size, | 36 bool DecompressorZlib::Process(const uint8* input_data, int input_size, |
39 uint8* output_data, int output_size, | 37 uint8* output_data, int output_size, |
40 int* consumed, int* written) { | 38 int* consumed, int* written) { |
41 DCHECK_GT(output_size, 0); | 39 DCHECK_GT(output_size, 0); |
42 | 40 |
43 // Setup I/O parameters. | 41 // Setup I/O parameters. |
44 stream_->avail_in = input_size; | 42 stream_->avail_in = input_size; |
45 stream_->next_in = (Bytef*)input_data; | 43 stream_->next_in = (Bytef*)input_data; |
46 stream_->avail_out = output_size; | 44 stream_->avail_out = output_size; |
47 stream_->next_out = (Bytef*)output_data; | 45 stream_->next_out = (Bytef*)output_data; |
48 | 46 |
49 int ret = inflate(stream_.get(), Z_NO_FLUSH); | 47 int ret = inflate(stream_.get(), Z_NO_FLUSH); |
50 if (ret == Z_STREAM_ERROR) { | 48 if (ret == Z_STREAM_ERROR) { |
51 NOTREACHED() << "zlib compression failed"; | 49 NOTREACHED() << "zlib compression failed"; |
52 } | 50 } |
53 | 51 |
54 *consumed = input_size - stream_->avail_in; | 52 *consumed = input_size - stream_->avail_in; |
55 *written = output_size - stream_->avail_out; | 53 *written = output_size - stream_->avail_out; |
56 | 54 |
57 // Since we check that output is always greater than 0, the only | 55 // Since we check that output is always greater than 0, the only |
58 // reason for us to get Z_BUF_ERROR is when zlib requires more input | 56 // reason for us to get Z_BUF_ERROR is when zlib requires more input |
59 // data. | 57 // data. |
60 return ret == Z_OK || ret == Z_BUF_ERROR; | 58 return ret == Z_OK || ret == Z_BUF_ERROR; |
61 } | 59 } |
62 | 60 |
| 61 void DecompressorZlib::InitStream() { |
| 62 stream_.reset(new z_stream()); |
| 63 |
| 64 stream_->next_in = Z_NULL; |
| 65 stream_->zalloc = Z_NULL; |
| 66 stream_->zfree = Z_NULL; |
| 67 stream_->opaque = Z_NULL; |
| 68 |
| 69 inflateInit(stream_.get()); |
| 70 } |
| 71 |
63 } // namespace remoting | 72 } // namespace remoting |
OLD | NEW |