blob: 8fb4cd26ec78bff1055e5d47851c23e6047c1cb0 [file] [log] [blame]
Alex Deymoa28e0192017-09-08 14:21:05 +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_PATCH_WRITER_H_
6#define _BSDIFF_PATCH_WRITER_H_
7
Alex Deymoa28e0192017-09-08 14:21:05 +02008#include <string>
9#include <vector>
10
Alex Deymo538a75d2017-09-27 15:34:59 +020011#include "bsdiff/bz2_compressor.h"
12#include "bsdiff/patch_writer_interface.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020013
14namespace bsdiff {
15
Alex Deymo538a75d2017-09-27 15:34:59 +020016// A PatchWriterInterface class using the upstream's BSDIFF40 format: three
17// BZ2-compressors and a 32-byte header.
18class BsdiffPatchWriter : public PatchWriterInterface {
Alex Deymoa28e0192017-09-08 14:21:05 +020019 public:
Alex Deymo538a75d2017-09-27 15:34:59 +020020 // Create the patch writer using the file |patch_filename| where the patch
21 // data will be written to.
22 explicit BsdiffPatchWriter(const std::string& patch_filename)
23 : patch_filename_(patch_filename) {}
Alex Deymoa28e0192017-09-08 14:21:05 +020024
Alex Deymo538a75d2017-09-27 15:34:59 +020025 // PatchWriterInterface overrides.
26 bool InitializeBuffers(const uint8_t* old_buf,
27 uint64_t old_size,
28 const uint8_t* new_buf,
29 uint64_t new_size) override;
30 bool AddControlEntry(const ControlEntry& entry) override;
31 bool Close() override;
Alex Deymoa28e0192017-09-08 14:21:05 +020032
33 private:
34 // Write the BSDIFF patch header to the |fp_| given the size of the compressed
35 // control block and the compressed diff block.
36 bool WriteHeader(uint64_t ctrl_size, uint64_t diff_size);
37
Alex Deymo538a75d2017-09-27 15:34:59 +020038 // Old and new file buffers.
39 const uint8_t* old_buf_{nullptr};
40 uint64_t old_size_{0};
41 const uint8_t* new_buf_{nullptr};
42 uint64_t new_size_{0};
Alex Deymoa28e0192017-09-08 14:21:05 +020043
44 // Bytes of the new_buf_ already written.
45 uint64_t written_output_{0};
46
47 // The current position in the old buf.
48 int64_t old_pos_{0};
49
50 // The current file we are writing to.
51 FILE* fp_{nullptr};
Alex Deymo538a75d2017-09-27 15:34:59 +020052 std::string patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020053
54 // The three internal compressed streams.
55 BZ2Compressor ctrl_stream_;
56 BZ2Compressor diff_stream_;
57 BZ2Compressor extra_stream_;
58};
59
60} // namespace bsdiff
61
62#endif // _BSDIFF_PATCH_WRITER_H_