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