blob: 07332055e8a2e1cbec88c3c4d580690a36d1ddae [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
Tianjie Xu1c26e2e2017-10-26 17:19:41 -07008#include <memory>
Alex Deymoa28e0192017-09-08 14:21:05 +02009#include <string>
10#include <vector>
11
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070012#include "bsdiff/compressor_interface.h"
Alex Deymo538a75d2017-09-27 15:34:59 +020013#include "bsdiff/patch_writer_interface.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020014
15namespace bsdiff {
16
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080017// A PatchWriterInterface class with three compressors and a 32-byte header.
Alex Deymo538a75d2017-09-27 15:34:59 +020018class BsdiffPatchWriter : public PatchWriterInterface {
Alex Deymoa28e0192017-09-08 14:21:05 +020019 public:
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080020 // Create the patch writer using the upstream's "BSDIFF40" format. It uses
21 // bz2 as the compression algorithm and the file |patch_filename| to write
22 // the patch data.
23 explicit BsdiffPatchWriter(const std::string& patch_filename);
24
25 // Create the patch writer using the "BSDF2" format. It uses the compressor
26 // with algorithm |type| and quality |quality|. This writer also writes the
27 // patch data to the file |patch_filename|.
28 BsdiffPatchWriter(const std::string& patch_filename,
29 CompressorType type,
30 int quality);
Alex Deymoa28e0192017-09-08 14:21:05 +020031
Alex Deymo538a75d2017-09-27 15:34:59 +020032 // PatchWriterInterface overrides.
Alex Deymo4dadd8b2017-10-26 16:19:33 +020033 bool Init(size_t new_size) override;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020034 bool WriteDiffStream(const uint8_t* data, size_t size) override;
35 bool WriteExtraStream(const uint8_t* data, size_t size) override;
Alex Deymo538a75d2017-09-27 15:34:59 +020036 bool AddControlEntry(const ControlEntry& entry) override;
37 bool Close() override;
Alex Deymoa28e0192017-09-08 14:21:05 +020038
39 private:
40 // Write the BSDIFF patch header to the |fp_| given the size of the compressed
41 // control block and the compressed diff block.
42 bool WriteHeader(uint64_t ctrl_size, uint64_t diff_size);
43
Alex Deymo68c0e7f2017-10-02 20:38:12 +020044 // Bytes of the new files already written. Needed to store the new length in
45 // the header of the file.
Alex Deymoa28e0192017-09-08 14:21:05 +020046 uint64_t written_output_{0};
47
Alex Deymoa28e0192017-09-08 14:21:05 +020048 // The current file we are writing to.
49 FILE* fp_{nullptr};
Alex Deymo538a75d2017-09-27 15:34:59 +020050 std::string patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020051
Tianjie Xub4cba642017-11-14 22:46:38 -080052 // The format of bsdiff we're using.
53 BsdiffFormat format_;
54
Alex Deymoa28e0192017-09-08 14:21:05 +020055 // The three internal compressed streams.
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070056 std::unique_ptr<CompressorInterface> ctrl_stream_{nullptr};
57 std::unique_ptr<CompressorInterface> diff_stream_{nullptr};
58 std::unique_ptr<CompressorInterface> extra_stream_{nullptr};
Alex Deymoa28e0192017-09-08 14:21:05 +020059};
60
61} // namespace bsdiff
62
63#endif // _BSDIFF_PATCH_WRITER_H_