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 | |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 17 | // A PatchWriterInterface class with three compressors and a 32-byte header. |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 18 | class BsdiffPatchWriter : public PatchWriterInterface { |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 19 | public: |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 20 | // Create the patch writer using the upstream's "BSDIFF40" format. It uses |
| 21 | // bz2 as the compression algorithm and the file |patch_filename| to write |
| 22 | // the patch data. |
| 23 | explicit BsdiffPatchWriter(const std::string& patch_filename); |
| 24 | |
| 25 | // Create the patch writer using the "BSDF2" format. It uses the compressor |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 26 | // with algorithm |type|; and quality |brotli_quality| if it's brotli. This |
| 27 | // writer also writes the patch data to the file |patch_filename|. |
Tianjie Xu | 1f1cdb2 | 2017-11-20 11:05:55 -0800 | [diff] [blame] | 28 | BsdiffPatchWriter(const std::string& patch_filename, |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 29 | const std::vector<CompressorType>& types, |
Tianjie Xu | 2e70b55 | 2018-03-02 16:22:10 -0800 | [diff] [blame] | 30 | int brotli_quality); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 31 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 32 | // PatchWriterInterface overrides. |
Alex Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 33 | bool Init(size_t new_size) override; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 34 | bool WriteDiffStream(const uint8_t* data, size_t size) override; |
| 35 | bool WriteExtraStream(const uint8_t* data, size_t size) override; |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 36 | bool AddControlEntry(const ControlEntry& entry) override; |
| 37 | bool Close() override; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 38 | |
| 39 | private: |
Tianjie Xu | 32b1f21 | 2018-03-06 11:42:45 -0800 | [diff] [blame] | 40 | // Add supported compressors to |compressor_list|; return false if we failed |
| 41 | // to initialize one of them. |
| 42 | bool InitializeCompressorList( |
| 43 | std::vector<std::unique_ptr<CompressorInterface>>* compressor_list); |
| 44 | |
| 45 | // Select the compressor in |compressor_list| that produces the smallest |
| 46 | // patch, and put the result in |smallest_compressor|. |
| 47 | bool SelectSmallestResult( |
| 48 | const std::vector<std::unique_ptr<CompressorInterface>>& compressor_list, |
| 49 | CompressorInterface** smallest_compressor); |
| 50 | |
| 51 | |
| 52 | // Write the BSDIFF patch header to the |fp_|. |
| 53 | // Arguments: |
| 54 | // A three bytes array with the compressor types of ctrl|diff|extra stream |
| 55 | // Size of the compressed control block |
| 56 | // Size of the compressed diff block. |
| 57 | bool WriteHeader(uint8_t types[3], uint64_t ctrl_size, uint64_t diff_size); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 58 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 59 | // Bytes of the new files already written. Needed to store the new length in |
| 60 | // the header of the file. |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 61 | uint64_t written_output_{0}; |
| 62 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 63 | // The current file we are writing to. |
| 64 | FILE* fp_{nullptr}; |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 65 | std::string patch_filename_; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 66 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 67 | // The format of bsdiff we're using. |
| 68 | BsdiffFormat format_; |
| 69 | |
Tianjie Xu | 77833b6 | 2018-03-07 18:13:47 -0800 | [diff] [blame] | 70 | // The compressors we're using. |
| 71 | std::vector<CompressorType> types_; |
Tianjie Xu | 32b1f21 | 2018-03-06 11:42:45 -0800 | [diff] [blame] | 72 | |
| 73 | // The compression quality of the brotli compressor. |
| 74 | int brotli_quality_; |
| 75 | |
| 76 | // The list of compressors to try for each stream. |
| 77 | std::vector<std::unique_ptr<CompressorInterface>> ctrl_stream_list_; |
| 78 | std::vector<std::unique_ptr<CompressorInterface>> diff_stream_list_; |
| 79 | std::vector<std::unique_ptr<CompressorInterface>> extra_stream_list_; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // namespace bsdiff |
| 83 | |
| 84 | #endif // _BSDIFF_PATCH_WRITER_H_ |