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_PATCH_WRITER_H_ |
| 6 | #define _BSDIFF_PATCH_WRITER_H_ |
| 7 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 8 | #include <memory> |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 12 | #include "bsdiff/compressor_interface.h" |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 13 | #include "bsdiff/patch_writer_interface.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 14 | |
| 15 | namespace bsdiff { |
| 16 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 17 | // A PatchWriterInterface class using the upstream's BSDIFF40 format: three |
| 18 | // BZ2-compressors and a 32-byte header. |
| 19 | class BsdiffPatchWriter : public PatchWriterInterface { |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 20 | public: |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 21 | // Create the patch writer using |type| as the compression algorithm and the |
| 22 | // file |patch_filename| to write the patch data. |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame^] | 23 | BsdiffPatchWriter(const std::string& patch_filename, BsdiffFormat format); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 24 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 25 | // PatchWriterInterface overrides. |
Alex Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 26 | bool Init(size_t new_size) override; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 27 | bool WriteDiffStream(const uint8_t* data, size_t size) override; |
| 28 | bool WriteExtraStream(const uint8_t* data, size_t size) override; |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 29 | bool AddControlEntry(const ControlEntry& entry) override; |
| 30 | bool Close() override; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 31 | |
| 32 | private: |
| 33 | // Write the BSDIFF patch header to the |fp_| given the size of the compressed |
| 34 | // control block and the compressed diff block. |
| 35 | bool WriteHeader(uint64_t ctrl_size, uint64_t diff_size); |
| 36 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 37 | // Bytes of the new files already written. Needed to store the new length in |
| 38 | // the header of the file. |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 39 | uint64_t written_output_{0}; |
| 40 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 41 | // The current file we are writing to. |
| 42 | FILE* fp_{nullptr}; |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 43 | std::string patch_filename_; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 44 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame^] | 45 | // The format of bsdiff we're using. |
| 46 | BsdiffFormat format_; |
| 47 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 48 | // The three internal compressed streams. |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 49 | std::unique_ptr<CompressorInterface> ctrl_stream_{nullptr}; |
| 50 | std::unique_ptr<CompressorInterface> diff_stream_{nullptr}; |
| 51 | std::unique_ptr<CompressorInterface> extra_stream_{nullptr}; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | } // namespace bsdiff |
| 55 | |
| 56 | #endif // _BSDIFF_PATCH_WRITER_H_ |