xref: /aosp_15_r20/external/bsdiff/diff_encoder.h (revision a3a45f308bd90ef1a6e6a5e8fb92fe449b895909)
1*a3a45f30SXin Li // Copyright 2017 The Chromium OS Authors. All rights reserved.
2*a3a45f30SXin Li // Use of this source code is governed by a BSD-style license that can be
3*a3a45f30SXin Li // found in the LICENSE file.
4*a3a45f30SXin Li 
5*a3a45f30SXin Li #ifndef _BSDIFF_DIFF_ENCODER_H_
6*a3a45f30SXin Li #define _BSDIFF_DIFF_ENCODER_H_
7*a3a45f30SXin Li 
8*a3a45f30SXin Li #include <stdint.h>
9*a3a45f30SXin Li 
10*a3a45f30SXin Li #include "bsdiff/bz2_compressor.h"
11*a3a45f30SXin Li #include "bsdiff/patch_writer_interface.h"
12*a3a45f30SXin Li 
13*a3a45f30SXin Li namespace bsdiff {
14*a3a45f30SXin Li 
15*a3a45f30SXin Li // Helper class to encapsulate the diff and extra stream generation logic
16*a3a45f30SXin Li // derived from the old and new file buffers. Using this class is impossible to
17*a3a45f30SXin Li // produce an invalid or incomplete bsdiff patch, since it has checks in place
18*a3a45f30SXin Li // verifying its correct usage.
19*a3a45f30SXin Li 
20*a3a45f30SXin Li class DiffEncoder {
21*a3a45f30SXin Li  public:
22*a3a45f30SXin Li   // Initialize the DiffEncoder with the old and new file buffers, as well as
23*a3a45f30SXin Li   // 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*a3a45f30SXin Li   DiffEncoder(PatchWriterInterface* patch,
25*a3a45f30SXin Li               const uint8_t* old_buf,
26*a3a45f30SXin Li               uint64_t old_size,
27*a3a45f30SXin Li               const uint8_t* new_buf,
28*a3a45f30SXin Li               uint64_t new_size)
29*a3a45f30SXin Li       : patch_(patch),
30*a3a45f30SXin Li         old_buf_(old_buf),
31*a3a45f30SXin Li         old_size_(old_size),
32*a3a45f30SXin Li         new_buf_(new_buf),
33*a3a45f30SXin Li         new_size_(new_size) {}
34*a3a45f30SXin Li 
35*a3a45f30SXin Li   // Initialize the diff encoder and the underlying patch.
36*a3a45f30SXin Li   bool Init();
37*a3a45f30SXin Li 
38*a3a45f30SXin Li   // Add a new control triplet entry to the patch. The |entry.diff_size| bytes
39*a3a45f30SXin Li   // for the diff stream and the |entry.extra_size| bytes for the extra stream
40*a3a45f30SXin Li   // will be computed and added to the corresponding streams in the patch.
41*a3a45f30SXin Li   // Returns whether the operation succeeded. The operation can fail if either
42*a3a45f30SXin Li   // the old or new files are referenced out of bounds.
43*a3a45f30SXin Li   bool AddControlEntry(const ControlEntry& entry);
44*a3a45f30SXin Li 
45*a3a45f30SXin Li   // Finalize the patch writing process and close the underlying patch writer.
46*a3a45f30SXin Li   bool Close();
47*a3a45f30SXin Li 
48*a3a45f30SXin Li  private:
49*a3a45f30SXin Li   // Pointer to the patch we are writing to.
50*a3a45f30SXin Li   PatchWriterInterface* patch_;
51*a3a45f30SXin Li 
52*a3a45f30SXin Li   // Old and new file buffers.
53*a3a45f30SXin Li   const uint8_t* old_buf_;
54*a3a45f30SXin Li   uint64_t old_size_;
55*a3a45f30SXin Li   const uint8_t* new_buf_;
56*a3a45f30SXin Li   uint64_t new_size_;
57*a3a45f30SXin Li 
58*a3a45f30SXin Li   // Bytes of the new_buf_ already written.
59*a3a45f30SXin Li   uint64_t written_output_{0};
60*a3a45f30SXin Li 
61*a3a45f30SXin Li   // The current position in the old buf.
62*a3a45f30SXin Li   int64_t old_pos_{0};
63*a3a45f30SXin Li };
64*a3a45f30SXin Li 
65*a3a45f30SXin Li }  // namespace bsdiff
66*a3a45f30SXin Li 
67*a3a45f30SXin Li #endif  // _BSDIFF_DIFF_ENCODER_H_
68