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.
5 #include "remoting/base/compressor_zlib.h"
7 #if defined(USE_SYSTEM_ZLIB)
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
13 #define MOZ_Z_deflate deflate
14 #define MOZ_Z_deflateEnd deflateEnd
15 #define MOZ_Z_deflateInit_ deflateInit_
17 #include "third_party/zlib/zlib.h"
19 #include "base/logging.h"
23 CompressorZlib::CompressorZlib() {
24 stream_
.reset(new z_stream());
26 stream_
->next_in
= Z_NULL
;
27 stream_
->zalloc
= Z_NULL
;
28 stream_
->zfree
= Z_NULL
;
29 stream_
->opaque
= Z_NULL
;
31 deflateInit(stream_
.get(), Z_BEST_SPEED
);
34 CompressorZlib::~CompressorZlib() {
35 deflateEnd(stream_
.get());
38 bool CompressorZlib::Process(const uint8
* input_data
, int input_size
,
39 uint8
* output_data
, int output_size
,
40 CompressorFlush flush
, int* consumed
,
42 DCHECK_GT(output_size
, 0);
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
;
51 if (flush
== CompressorSyncFlush
) {
52 z_flush
= Z_SYNC_FLUSH
;
53 } else if (flush
== CompressorFinish
) {
55 } else if (flush
== CompressorNoFlush
) {
58 NOTREACHED() << "Unsupported flush mode";
61 int ret
= deflate(stream_
.get(), z_flush
);
62 if (ret
== Z_STREAM_ERROR
) {
63 NOTREACHED() << "zlib compression failed";
66 *consumed
= input_size
- stream_
->avail_in
;
67 *written
= output_size
- stream_
->avail_out
;
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.
74 } else if (ret
== Z_STREAM_END
) {
76 } else if (ret
== Z_BUF_ERROR
) {
77 return stream_
->avail_out
== 0;
79 NOTREACHED() << "Unexpected zlib error: " << ret
;
84 } // namespace remoting