blob: b6f4dfec81e5fce6ac695126dd509229cdd72cb1 [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;
Alex Deymoa28e0192017-09-08 14:21:05 +020032
33 private:
34 // The low-level bzip2 stream.
35 bz_stream bz_strm_;
36
37 // Whether the bz_strm_ is initialized.
38 bool bz_strm_initialized_{false};
39
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070040 CompressorBuffer comp_buffer_;
Alex Deymoa28e0192017-09-08 14:21:05 +020041};
42
43} // namespace bsdiff
44
45#endif // _BSDIFF_BZ2_COMPRESSOR_H_