Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 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 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 13 | #include "bsdiff/compressor_buffer.h" |
| 14 | #include "bsdiff/compressor_interface.h" |
| 15 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 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 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 23 | class BZ2Compressor : public CompressorInterface { |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 24 | public: |
| 25 | BZ2Compressor(); |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 26 | ~BZ2Compressor() override; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 27 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 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; |
Tianjie Xu | 32b1f21 | 2018-03-06 11:42:45 -0800 | [diff] [blame] | 32 | CompressorType Type() const override { return CompressorType::kBZ2; } |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 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 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 41 | CompressorBuffer comp_buffer_; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | } // namespace bsdiff |
| 45 | |
| 46 | #endif // _BSDIFF_BZ2_COMPRESSOR_H_ |