summaryrefslogtreecommitdiffstats
path: root/remoting/base/decompressor_zlib.cc
blob: 770abd5a3b1a15b00b93a0e022f94e39ab73172b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/base/decompressor_zlib.h"

#if defined(USE_SYSTEM_ZLIB)
#include <zlib.h>
#else
#include "third_party/zlib/zlib.h"
#endif
#include "base/logging.h"

namespace remoting {

DecompressorZlib::DecompressorZlib() {
  InitStream();
}

DecompressorZlib::~DecompressorZlib() {
  inflateEnd(stream_.get());
}

void DecompressorZlib::Reset() {
  inflateEnd(stream_.get());
  InitStream();
}

bool DecompressorZlib::Process(const uint8* input_data, int input_size,
                               uint8* output_data, int output_size,
                               int* consumed, int* written) {
  DCHECK_GT(output_size, 0);

  // Setup I/O parameters.
  stream_->avail_in = input_size;
  stream_->next_in = (Bytef*)input_data;
  stream_->avail_out = output_size;
  stream_->next_out = (Bytef*)output_data;

  int ret = inflate(stream_.get(), Z_NO_FLUSH);
  if (ret == Z_STREAM_ERROR) {
    NOTREACHED() << "zlib compression failed";
  }

  *consumed = input_size - stream_->avail_in;
  *written = output_size - stream_->avail_out;

  // Since we check that output is always greater than 0, the only
  // reason for us to get Z_BUF_ERROR is when zlib requires more input
  // data.
  return ret == Z_OK || ret == Z_BUF_ERROR;
}

void DecompressorZlib::InitStream() {
  stream_.reset(new z_stream());

  stream_->next_in = Z_NULL;
  stream_->zalloc = Z_NULL;
  stream_->zfree = Z_NULL;
  stream_->opaque = Z_NULL;

  inflateInit(stream_.get());
}

}  // namespace remoting