blob: 6a53486e7dba0f4d0cf78b45745828f340ea1749 [file] [log] [blame]
Alex Deymoa28e0192017-09-08 14:21:05 +02001// Copyright 2017 The Chromium OS 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.
4
5#ifndef _BSDIFF_BZ2_COMPRESSOR_H_
6#define _BSDIFF_BZ2_COMPRESSOR_H_
7
8#include <bzlib.h>
9#include <stdint.h>
10
11#include <vector>
12
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070013#include "bsdiff/compressor_buffer.h"
14#include "bsdiff/compressor_interface.h"
15
Alex Deymoa28e0192017-09-08 14:21:05 +020016namespace bsdiff {
17
18// An in-memory class to wrap the low-level bzip2 compress functions. This class
19// allows to stream uncompressed data to it and then retrieve all the compressed
20// data at the end of the compression step. For that, all the compressed data
21// is stored in memory.
22
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070023class BZ2Compressor : public CompressorInterface {
Alex Deymoa28e0192017-09-08 14:21:05 +020024 public:
25 BZ2Compressor();
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070026 ~BZ2Compressor() override;
Alex Deymoa28e0192017-09-08 14:21:05 +020027
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070028 // CompressorInterface overrides.
29 bool Write(const uint8_t* buf, size_t size) override;
30 bool Finish() override;
31 const std::vector<uint8_t>& GetCompressedData() override;
Tianjie Xu32b1f212018-03-06 11:42:45 -080032 CompressorType Type() const override { return CompressorType::kBZ2; }
Alex Deymoa28e0192017-09-08 14:21:05 +020033
34 private:
35 // The low-level bzip2 stream.
36 bz_stream bz_strm_;
37
38 // Whether the bz_strm_ is initialized.
39 bool bz_strm_initialized_{false};
40
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070041 CompressorBuffer comp_buffer_;
Alex Deymoa28e0192017-09-08 14:21:05 +020042};
43
44} // namespace bsdiff
45
46#endif // _BSDIFF_BZ2_COMPRESSOR_H_