Initialize all data members in HTTPResponseInfo's new ctor and remove the related...
[chromium-blink-merge.git] / remoting / base / compressor_zlib.cc
blob6f7007319685f87f04d89a5f79ea0b4244ba26ed
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)
8 #include <zlib.h>
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
12 // zlib.
13 #define MOZ_Z_deflate deflate
14 #define MOZ_Z_deflateEnd deflateEnd
15 #define MOZ_Z_deflateInit_ deflateInit_
16 #else
17 #include "third_party/zlib/zlib.h"
18 #endif
19 #include "base/logging.h"
21 namespace remoting {
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,
41 int* written) {
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;
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";
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.
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;
84 } // namespace remoting