• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 
13 namespace 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 
20 class DiffEncoder {
21  public:
22   // Initialize the DiffEncoder with the old and new file buffers, as well as
23   // the path writer used. The |patch| will be initialized when calling Init().
DiffEncoder(PatchWriterInterface * patch,const uint8_t * old_buf,uint64_t old_size,const uint8_t * new_buf,uint64_t new_size)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   // Initialize the diff encoder and the underlying patch.
36   bool Init();
37 
38   // Add a new control triplet entry to the patch. The |entry.diff_size| bytes
39   // for the diff stream and the |entry.extra_size| bytes for the extra stream
40   // will be computed and added to the corresponding streams in the patch.
41   // Returns whether the operation succeeded. The operation can fail if either
42   // the old or new files are referenced out of bounds.
43   bool AddControlEntry(const ControlEntry& entry);
44 
45   // Finalize the patch writing process and close the underlying patch writer.
46   bool Close();
47 
48  private:
49   // Pointer to the patch we are writing to.
50   PatchWriterInterface* patch_;
51 
52   // Old and new file buffers.
53   const uint8_t* old_buf_;
54   uint64_t old_size_;
55   const uint8_t* new_buf_;
56   uint64_t new_size_;
57 
58   // Bytes of the new_buf_ already written.
59   uint64_t written_output_{0};
60 
61   // The current position in the old buf.
62   int64_t old_pos_{0};
63 };
64 
65 }  // namespace bsdiff
66 
67 #endif  // _BSDIFF_DIFF_ENCODER_H_
68