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 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 11 | #include "bsdiff/bz2_compressor.h" |
| 12 | #include "bsdiff/patch_writer_interface.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 13 | |
| 14 | namespace bsdiff { |
| 15 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 16 | // A PatchWriterInterface class using the upstream's BSDIFF40 format: three |
| 17 | // BZ2-compressors and a 32-byte header. |
| 18 | class BsdiffPatchWriter : public PatchWriterInterface { |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 19 | public: |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 20 | // Create the patch writer using the file |patch_filename| where the patch |
| 21 | // data will be written to. |
| 22 | explicit BsdiffPatchWriter(const std::string& patch_filename) |
| 23 | : patch_filename_(patch_filename) {} |
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. |
| 26 | bool InitializeBuffers(const uint8_t* old_buf, |
| 27 | uint64_t old_size, |
| 28 | const uint8_t* new_buf, |
| 29 | uint64_t new_size) override; |
| 30 | bool AddControlEntry(const ControlEntry& entry) override; |
| 31 | bool Close() override; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 32 | |
| 33 | private: |
| 34 | // Write the BSDIFF patch header to the |fp_| given the size of the compressed |
| 35 | // control block and the compressed diff block. |
| 36 | bool WriteHeader(uint64_t ctrl_size, uint64_t diff_size); |
| 37 | |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 38 | // Old and new file buffers. |
| 39 | const uint8_t* old_buf_{nullptr}; |
| 40 | uint64_t old_size_{0}; |
| 41 | const uint8_t* new_buf_{nullptr}; |
| 42 | uint64_t new_size_{0}; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 43 | |
| 44 | // Bytes of the new_buf_ already written. |
| 45 | uint64_t written_output_{0}; |
| 46 | |
| 47 | // The current position in the old buf. |
| 48 | int64_t old_pos_{0}; |
| 49 | |
| 50 | // The current file we are writing to. |
| 51 | FILE* fp_{nullptr}; |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 52 | std::string patch_filename_; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 53 | |
| 54 | // The three internal compressed streams. |
| 55 | BZ2Compressor ctrl_stream_; |
| 56 | BZ2Compressor diff_stream_; |
| 57 | BZ2Compressor extra_stream_; |
| 58 | }; |
| 59 | |
| 60 | } // namespace bsdiff |
| 61 | |
| 62 | #endif // _BSDIFF_PATCH_WRITER_H_ |