1 // 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 13 #include "bsdiff/compressor_buffer.h" 14 #include "bsdiff/compressor_interface.h" 15 16 namespace 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 23 class BZ2Compressor : public CompressorInterface { 24 public: 25 BZ2Compressor(); 26 ~BZ2Compressor() override; 27 28 // 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; Type()32 CompressorType Type() const override { return CompressorType::kBZ2; } 33 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 41 CompressorBuffer comp_buffer_; 42 }; 43 44 } // namespace bsdiff 45 46 #endif // _BSDIFF_BZ2_COMPRESSOR_H_ 47