Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [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_READER_H_ |
| 6 | #define _BSDIFF_PATCH_READER_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <memory> |
| 12 | |
| 13 | #include "bsdiff/control_entry.h" |
| 14 | #include "bsdiff/decompressor_interface.h" |
| 15 | #include "bsdiff/file_interface.h" |
| 16 | |
| 17 | namespace bsdiff { |
| 18 | |
| 19 | extern const uint8_t kMagicHeader[]; |
| 20 | |
| 21 | // A wrapper class to read and parse the data in a bsdiff patch. The reader |
| 22 | // class contains the concatenated streams of control, diff, and extra data. |
| 23 | // After initialization, this class provides the function to read the metadata |
| 24 | // embedded in the control stream sequentially and decompresses the diff & extra |
| 25 | // stream based on this metadata. |
| 26 | |
| 27 | class BsdiffPatchReader { |
| 28 | public: |
| 29 | BsdiffPatchReader() = default; |
| 30 | |
| 31 | // Initialize the control stream, diff stream and extra stream from the |
| 32 | // corresponding offset of |patch_data|. |
Alex Deymo | 279409d | 2017-11-07 20:48:18 +0100 | [diff] [blame^] | 33 | bool Init(const uint8_t* patch_data, size_t patch_size); |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 34 | |
| 35 | // Read the control stream and parse the metadata of |diff_size_|, |
| 36 | // |extra_size_| and |offset_incremental_|. |
| 37 | bool ParseControlEntry(ControlEntry* control_entry); |
| 38 | |
| 39 | // Read the data in |diff_stream_| and write |size| decompressed data to |
| 40 | // |buf|. |
| 41 | bool ReadDiffStream(uint8_t* buf, size_t size); |
| 42 | |
| 43 | // Read the data in |extra_stream_| and write |size| decompressed data to |
| 44 | // |buf|. |
| 45 | bool ReadExtraStream(uint8_t* buf, size_t size); |
| 46 | |
| 47 | uint64_t new_file_size() const { return new_file_size_; } |
| 48 | |
| 49 | // Close the control/diff/extra stream. Return false if errors occur when |
| 50 | // closing any of these streams. |
| 51 | bool Finish(); |
| 52 | |
| 53 | private: |
| 54 | // The compressed stream that contains the control data; i.e. length of each |
| 55 | // diff/extra block and the corresponding offset to read in the source file. |
| 56 | std::unique_ptr<DecompressorInterface> ctrl_stream_{nullptr}; |
| 57 | // The compressed stream that contains the concatenated diff blocks. |
| 58 | std::unique_ptr<DecompressorInterface> diff_stream_{nullptr}; |
| 59 | // The compressed stream that contains the concatenated extra blocks. |
| 60 | std::unique_ptr<DecompressorInterface> extra_stream_{nullptr}; |
| 61 | |
| 62 | // Size of the target file. |
| 63 | uint64_t new_file_size_; |
| 64 | }; |
| 65 | |
| 66 | } // namespace bsdiff |
| 67 | |
| 68 | #endif // _BSDIFF_PATCH_READER_H_ |