summaryrefslogtreecommitdiff
path: root/bz2_compressor.cc
blob: 50d4b2d0eaa2454409451479f226a15dea03d319 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2017 The Chromium OS 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 "bsdiff/bz2_compressor.h"

#include <string.h>

#include "bsdiff/logging.h"

namespace {

// The BZ2 compression level used. Smaller compression levels are nowadays
// pointless.
const int kCompressionLevel = 9;

}  // namespace

namespace bsdiff {

BZ2Compressor::BZ2Compressor() : comp_buffer_(1024 * 1024) {
  memset(&bz_strm_, 0, sizeof(bz_strm_));
  int bz_err = BZ2_bzCompressInit(&bz_strm_, kCompressionLevel,
                                  0 /* verbosity */, 0 /* workFactor */);
  if (bz_err != BZ_OK) {
    LOG(ERROR) << "Initializing bz_strm, bz_error=" << bz_err;
  } else {
    bz_strm_initialized_ = true;
  }
}

BZ2Compressor::~BZ2Compressor() {
  if (!bz_strm_initialized_)
    return;
  int bz_err = BZ2_bzCompressEnd(&bz_strm_);
  if (bz_err != BZ_OK) {
    LOG(ERROR) << "Deleting the compressor stream, bz_error=" << bz_err;
  }
}

bool BZ2Compressor::Write(const uint8_t* buf, size_t size) {
  if (!bz_strm_initialized_)
    return false;

  // The bz_stream struct defines the next_in as a non-const data pointer,
  // although the documentation says it won't modify it.
  bz_strm_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(buf));
  bz_strm_.avail_in = size;

  while (bz_strm_.avail_in) {
    bz_strm_.next_out = reinterpret_cast<char*>(comp_buffer_.buffer_data());
    bz_strm_.avail_out = comp_buffer_.buffer_size();
    int bz_err = BZ2_bzCompress(&bz_strm_, BZ_RUN);
    if (bz_err != BZ_RUN_OK) {
      LOG(ERROR) << "Compressing data, bz_error=" << bz_err;
      return false;
    }

    uint64_t output_bytes = comp_buffer_.buffer_size() - bz_strm_.avail_out;
    if (output_bytes) {
      comp_buffer_.AddDataToChunks(output_bytes);
    }
  }
  return true;
}

bool BZ2Compressor::Finish() {
  if (!bz_strm_initialized_)
    return false;
  bz_strm_.next_in = nullptr;
  bz_strm_.avail_in = 0;

  int bz_err = BZ_FINISH_OK;
  while (bz_err == BZ_FINISH_OK) {
    bz_strm_.next_out = reinterpret_cast<char*>(comp_buffer_.buffer_data());
    bz_strm_.avail_out = comp_buffer_.buffer_size();
    bz_err = BZ2_bzCompress(&bz_strm_, BZ_FINISH);

    uint64_t output_bytes = comp_buffer_.buffer_size() - bz_strm_.avail_out;
    if (output_bytes) {
      comp_buffer_.AddDataToChunks(output_bytes);
    }
  }
  if (bz_err != BZ_STREAM_END) {
    LOG(ERROR) << "Finishing compressing data, bz_error=" << bz_err;
    return false;
  }
  bz_err = BZ2_bzCompressEnd(&bz_strm_);
  bz_strm_initialized_ = false;
  if (bz_err != BZ_OK) {
    LOG(ERROR) << "Deleting the compressor stream, bz_error=" << bz_err;
    return false;
  }
  return true;
}

const std::vector<uint8_t>& BZ2Compressor::GetCompressedData() {
  return comp_buffer_.GetCompressedData();
}

}  // namespace bsdiff