blob: a479c813524be79792e4996f9e27817d281a2e2e [file] [log] [blame]
Alex Deymo68c0e7f2017-10-02 20:38:12 +02001// 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_DIFF_ENCODER_H_
6#define _BSDIFF_DIFF_ENCODER_H_
7
8#include <stdint.h>
9
10#include "bsdiff/bz2_compressor.h"
11#include "bsdiff/patch_writer_interface.h"
12
13namespace bsdiff {
14
15// Helper class to encapsulate the diff and extra stream generation logic
16// derived from the old and new file buffers. Using this class is impossible to
17// produce an invalid or incomplete bsdiff patch, since it has checks in place
18// verifying its correct usage.
19
20class DiffEncoder {
21 public:
22 // Initialize the DiffEncoder with the old and new file buffers, as well as
23 // the path writer used. The |patch| must be already initialized.
24 DiffEncoder(PatchWriterInterface* patch,
25 const uint8_t* old_buf,
26 uint64_t old_size,
27 const uint8_t* new_buf,
28 uint64_t new_size)
29 : patch_(patch),
30 old_buf_(old_buf),
31 old_size_(old_size),
32 new_buf_(new_buf),
33 new_size_(new_size) {}
34
35 // Add a new control triplet entry to the patch. The |entry.diff_size| bytes
36 // for the diff stream and the |entry.extra_size| bytes for the extra stream
37 // will be computed and added to the corresponding streams in the patch.
38 // Returns whether the operation succeeded. The operation can fail if either
39 // the old or new files are referenced out of bounds.
40 bool AddControlEntry(const ControlEntry& entry);
41
42 // Finalize the patch writing process and close the underlying patch writer.
43 bool Close();
44
45 private:
46 // Pointer to the patch we are writing to.
47 PatchWriterInterface* patch_;
48
49 // Old and new file buffers.
50 const uint8_t* old_buf_;
51 uint64_t old_size_;
52 const uint8_t* new_buf_;
53 uint64_t new_size_;
54
55 // Bytes of the new_buf_ already written.
56 uint64_t written_output_{0};
57
58 // The current position in the old buf.
59 int64_t old_pos_{0};
60};
61
62} // namespace bsdiff
63
64#endif // _BSDIFF_DIFF_ENCODER_H_