Make BsdiffPatchWriter into an Interface.
Currently, all bsdiff() functions take a filename for the patch, which
is called with a temporary file in all cases. To help expose an
interface that allows to write bsdiff patches in different formats (for
example, changing the compressor and header format) we expose the
PatchWriterInterface class in the public interface so callers can use
a different one or define their own to help experimenting with bsdiff
enconding improvements.
Bug: 34220646
Test: make bsdiff and update_engine; ran bsdiff_unittest
Change-Id: Ie450b2790137665bc033cb36d037171090b18a4b
diff --git a/patch_writer.h b/patch_writer.h
index 2dbf8be..8fb4cd2 100644
--- a/patch_writer.h
+++ b/patch_writer.h
@@ -5,67 +5,41 @@
#ifndef _BSDIFF_PATCH_WRITER_H_
#define _BSDIFF_PATCH_WRITER_H_
-#include <stdio.h>
-
#include <string>
#include <vector>
-#include "bsdiff/common.h"
-#include "bz2_compressor.h"
+#include "bsdiff/bz2_compressor.h"
+#include "bsdiff/patch_writer_interface.h"
namespace bsdiff {
-struct ControlEntry {
- ControlEntry(uint64_t diff_size,
- uint64_t extra_size,
- int64_t offset_increment)
- : diff_size(diff_size),
- extra_size(extra_size),
- offset_increment(offset_increment) {}
-
- // The number of bytes to copy from the source and diff stream.
- uint64_t diff_size;
-
- // The number of bytes to copy from the extra stream.
- uint64_t extra_size;
-
- // The value to add to the source pointer after patching from the diff stream.
- int64_t offset_increment;
-};
-
-class BsdiffPatchWriter {
+// A PatchWriterInterface class using the upstream's BSDIFF40 format: three
+// BZ2-compressors and a 32-byte header.
+class BsdiffPatchWriter : public PatchWriterInterface {
public:
- BsdiffPatchWriter(const uint8_t* old_buf,
- uint64_t old_size,
- const uint8_t* new_buf,
- uint64_t new_size)
- : old_buf_(old_buf),
- old_size_(old_size),
- new_buf_(new_buf),
- new_size_(new_size) {}
+ // Create the patch writer using the file |patch_filename| where the patch
+ // data will be written to.
+ explicit BsdiffPatchWriter(const std::string& patch_filename)
+ : patch_filename_(patch_filename) {}
- // Create the file |patch_filename| where the patch will be written to.
- bool Open(const std::string& patch_filename);
-
- // Add a new control triplet entry to the patch. The |entry.diff_size| bytes
- // for the diff stream and the |entry.extra_size| bytes for the extra stream
- // will be computed and added to the corresponding streams in the patch.
- // Returns whether the operation succeeded. The operation can fail if either
- // the old or new files are referenced out of bounds.
- bool AddControlEntry(const ControlEntry& entry);
-
- bool Close();
+ // PatchWriterInterface overrides.
+ bool InitializeBuffers(const uint8_t* old_buf,
+ uint64_t old_size,
+ const uint8_t* new_buf,
+ uint64_t new_size) override;
+ bool AddControlEntry(const ControlEntry& entry) override;
+ bool Close() override;
private:
// Write the BSDIFF patch header to the |fp_| given the size of the compressed
// control block and the compressed diff block.
bool WriteHeader(uint64_t ctrl_size, uint64_t diff_size);
-
- const uint8_t* old_buf_;
- uint64_t old_size_;
- const uint8_t* new_buf_;
- uint64_t new_size_;
+ // Old and new file buffers.
+ const uint8_t* old_buf_{nullptr};
+ uint64_t old_size_{0};
+ const uint8_t* new_buf_{nullptr};
+ uint64_t new_size_{0};
// Bytes of the new_buf_ already written.
uint64_t written_output_{0};
@@ -75,6 +49,7 @@
// The current file we are writing to.
FILE* fp_{nullptr};
+ std::string patch_filename_;
// The three internal compressed streams.
BZ2Compressor ctrl_stream_;