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/compressor_zlib.h" | 5 #include "remoting/base/compressor_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 |
(...skipping 17 matching lines...) Expand all Loading... |
28 stream_->zfree = Z_NULL; | 28 stream_->zfree = Z_NULL; |
29 stream_->opaque = Z_NULL; | 29 stream_->opaque = Z_NULL; |
30 | 30 |
31 deflateInit(stream_.get(), Z_BEST_SPEED); | 31 deflateInit(stream_.get(), Z_BEST_SPEED); |
32 } | 32 } |
33 | 33 |
34 CompressorZlib::~CompressorZlib() { | 34 CompressorZlib::~CompressorZlib() { |
35 deflateEnd(stream_.get()); | 35 deflateEnd(stream_.get()); |
36 } | 36 } |
37 | 37 |
38 void CompressorZlib::Write(const uint8* input_data, int input_size, | 38 bool CompressorZlib::Process(const uint8* input_data, int input_size, |
39 uint8* output_data, int output_size, | 39 uint8* output_data, int output_size, |
40 int* consumed, int* written) { | 40 int* consumed, int* written) { |
| 41 DCHECK_GT(output_size, 0); |
| 42 |
41 // Setup I/O parameters. | 43 // Setup I/O parameters. |
42 stream_->avail_in = input_size; | 44 stream_->avail_in = input_size; |
43 stream_->next_in = (Bytef*)input_data; | 45 stream_->next_in = (Bytef*)input_data; |
44 stream_->avail_out = output_size; | 46 stream_->avail_out = output_size; |
45 stream_->next_out = (Bytef*)output_data; | 47 stream_->next_out = (Bytef*)output_data; |
46 | 48 |
47 int ret = deflate(stream_.get(), Z_NO_FLUSH); | 49 int ret = deflate(stream_.get(), input_size ? Z_NO_FLUSH : Z_FINISH); |
48 if (ret == Z_STREAM_ERROR) { | 50 if (ret == Z_STREAM_ERROR) { |
49 NOTREACHED() << "zlib compression failed"; | 51 NOTREACHED() << "zlib compression failed"; |
50 } | 52 } |
51 | 53 |
52 *consumed = input_size - stream_->avail_in; | 54 *consumed = input_size - stream_->avail_in; |
53 *written = output_size - stream_->avail_out; | 55 *written = output_size - stream_->avail_out; |
54 } | |
55 | 56 |
56 bool CompressorZlib::Flush(uint8* output_data, int output_size, | 57 // Return true when we get Z_BUF_ERROR, this way we can feed more data |
57 int* written) { | 58 // to zlib. |
58 // Setup I/O parameters. | 59 return ret == Z_OK || ret == Z_BUF_ERROR; |
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 } | 60 } |
72 | 61 |
73 } // namespace remoting | 62 } // namespace remoting |
OLD | NEW |