xref: /aosp_15_r20/external/bsdiff/bz2_compressor.h (revision a3a45f308bd90ef1a6e6a5e8fb92fe449b895909)
1*a3a45f30SXin Li // Copyright 2017 The Chromium OS Authors. All rights reserved.
2*a3a45f30SXin Li // Use of this source code is governed by a BSD-style license that can be
3*a3a45f30SXin Li // found in the LICENSE file.
4*a3a45f30SXin Li 
5*a3a45f30SXin Li #ifndef _BSDIFF_BZ2_COMPRESSOR_H_
6*a3a45f30SXin Li #define _BSDIFF_BZ2_COMPRESSOR_H_
7*a3a45f30SXin Li 
8*a3a45f30SXin Li #include <bzlib.h>
9*a3a45f30SXin Li #include <stdint.h>
10*a3a45f30SXin Li 
11*a3a45f30SXin Li #include <vector>
12*a3a45f30SXin Li 
13*a3a45f30SXin Li #include "bsdiff/compressor_buffer.h"
14*a3a45f30SXin Li #include "bsdiff/compressor_interface.h"
15*a3a45f30SXin Li 
16*a3a45f30SXin Li namespace bsdiff {
17*a3a45f30SXin Li 
18*a3a45f30SXin Li // An in-memory class to wrap the low-level bzip2 compress functions. This class
19*a3a45f30SXin Li // allows to stream uncompressed data to it and then retrieve all the compressed
20*a3a45f30SXin Li // data at the end of the compression step. For that, all the compressed data
21*a3a45f30SXin Li // is stored in memory.
22*a3a45f30SXin Li 
23*a3a45f30SXin Li class BZ2Compressor : public CompressorInterface {
24*a3a45f30SXin Li  public:
25*a3a45f30SXin Li   BZ2Compressor();
26*a3a45f30SXin Li   ~BZ2Compressor() override;
27*a3a45f30SXin Li 
28*a3a45f30SXin Li   // CompressorInterface overrides.
29*a3a45f30SXin Li   bool Write(const uint8_t* buf, size_t size) override;
30*a3a45f30SXin Li   bool Finish() override;
31*a3a45f30SXin Li   const std::vector<uint8_t>& GetCompressedData() override;
Type()32*a3a45f30SXin Li   CompressorType Type() const override { return CompressorType::kBZ2; }
33*a3a45f30SXin Li 
34*a3a45f30SXin Li  private:
35*a3a45f30SXin Li   // The low-level bzip2 stream.
36*a3a45f30SXin Li   bz_stream bz_strm_;
37*a3a45f30SXin Li 
38*a3a45f30SXin Li   // Whether the bz_strm_ is initialized.
39*a3a45f30SXin Li   bool bz_strm_initialized_{false};
40*a3a45f30SXin Li 
41*a3a45f30SXin Li   CompressorBuffer comp_buffer_;
42*a3a45f30SXin Li };
43*a3a45f30SXin Li 
44*a3a45f30SXin Li }  // namespace bsdiff
45*a3a45f30SXin Li 
46*a3a45f30SXin Li #endif  // _BSDIFF_BZ2_COMPRESSOR_H_
47