blob: f4f79303f4db1189535db4d16200bef662ae9ced [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
Alex Deymodcd423b2017-09-13 20:54:24 +02005#include "bsdiff/patch_writer.h"
Alex Deymoa28e0192017-09-08 14:21:05 +02006
7#include <string.h>
8
Tianjie Xu1c26e2e2017-10-26 17:19:41 -07009#include "bsdiff/brotli_compressor.h"
10#include "bsdiff/bz2_compressor.h"
Tianjie Xu4d10c3e2017-10-26 14:02:06 -070011#include "bsdiff/constants.h"
Tianjie Xu65288122017-10-13 15:10:58 -070012#include "bsdiff/control_entry.h"
Alex Deymodcd423b2017-09-13 20:54:24 +020013#include "bsdiff/logging.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020014
15using std::endl;
16
17namespace {
18
Alex Deymoa28e0192017-09-08 14:21:05 +020019void EncodeInt64(int64_t x, uint8_t* buf) {
20 uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
21 for (int i = 0; i < 8; ++i) {
22 buf[i] = y & 0xff;
23 y /= 256;
24 }
25}
26
27} // namespace
28
29namespace bsdiff {
30
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070031BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
Tianjie Xub4cba642017-11-14 22:46:38 -080032 BsdiffFormat format)
33 : patch_filename_(patch_filename), format_(format) {
34 if (format_ == BsdiffFormat::kLegacy) {
35 ctrl_stream_ = CreateCompressor(CompressorType::kBZ2);
36 diff_stream_ = CreateCompressor(CompressorType::kBZ2);
37 extra_stream_ = CreateCompressor(CompressorType::kBZ2);
38 } else {
39 // TODO(xunchang) set different compression method according to the
40 // compressed size for these streams.
41 ctrl_stream_ = CreateCompressor(CompressorType::kBrotli);
42 diff_stream_ = CreateCompressor(CompressorType::kBrotli);
43 extra_stream_ = CreateCompressor(CompressorType::kBrotli);
44 }
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070045}
46
Alex Deymo4dadd8b2017-10-26 16:19:33 +020047bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Tianjie Xub4cba642017-11-14 22:46:38 -080048 if (!(ctrl_stream_ && diff_stream_ && extra_stream_)) {
49 LOG(ERROR) << "uninitialized compressor stream" << endl;
50 return false;
51 }
52
Alex Deymo538a75d2017-09-27 15:34:59 +020053 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020054 if (!fp_) {
Alex Deymo538a75d2017-09-27 15:34:59 +020055 LOG(ERROR) << "Opening " << patch_filename_ << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020056 return false;
57 }
58 return true;
59}
60
Alex Deymo68c0e7f2017-10-02 20:38:12 +020061bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070062 return diff_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020063}
64
65bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070066 return extra_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020067}
68
Alex Deymoa28e0192017-09-08 14:21:05 +020069bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +020070 // Generate the 24 byte control entry.
71 uint8_t buf[24];
72 EncodeInt64(entry.diff_size, buf);
73 EncodeInt64(entry.extra_size, buf + 8);
74 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070075 if (!ctrl_stream_->Write(buf, sizeof(buf)))
Alex Deymoa28e0192017-09-08 14:21:05 +020076 return false;
Alex Deymoa28e0192017-09-08 14:21:05 +020077 written_output_ += entry.diff_size + entry.extra_size;
78 return true;
79}
80
81bool BsdiffPatchWriter::Close() {
82 if (!fp_) {
83 LOG(ERROR) << "File not open." << endl;
84 return false;
85 }
Alex Deymoa28e0192017-09-08 14:21:05 +020086
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070087 if (!ctrl_stream_->Finish() || !diff_stream_->Finish() ||
88 !extra_stream_->Finish()) {
Alex Deymo68c0e7f2017-10-02 20:38:12 +020089 LOG(ERROR) << "Finalizing compressed streams." << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020090 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020091 }
Alex Deymoa28e0192017-09-08 14:21:05 +020092
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070093 auto ctrl_data = ctrl_stream_->GetCompressedData();
94 auto diff_data = diff_stream_->GetCompressedData();
95 auto extra_data = extra_stream_->GetCompressedData();
Alex Deymoa28e0192017-09-08 14:21:05 +020096
97 if (!WriteHeader(ctrl_data.size(), diff_data.size()))
98 return false;
99
100 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
101 LOG(ERROR) << "Writing ctrl_data." << endl;
102 return false;
103 }
104 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
105 LOG(ERROR) << "Writing diff_data." << endl;
106 return false;
107 }
108 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
109 extra_data.size()) {
110 LOG(ERROR) << "Writing extra_data." << endl;
111 return false;
112 }
113 if (fclose(fp_) != 0) {
114 LOG(ERROR) << "Closing the patch file." << endl;
115 return false;
116 }
117 fp_ = nullptr;
118 return true;
119}
120
121bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) {
122 /* Header format is
Tianjie Xub4cba642017-11-14 22:46:38 -0800123 * 0 8 magic header
124 * 8 8 length of compressed ctrl block
125 * 16 8 length of compressed diff block
Alex Deymoa28e0192017-09-08 14:21:05 +0200126 * 24 8 length of new file
127 *
128 * File format is
129 * 0 32 Header
Tianjie Xub4cba642017-11-14 22:46:38 -0800130 * 32 ?? compressed ctrl block
131 * ?? ?? compressed diff block
132 * ?? ?? compressed extra block
Alex Deymoa28e0192017-09-08 14:21:05 +0200133 */
134 uint8_t header[32];
Tianjie Xub4cba642017-11-14 22:46:38 -0800135 if (format_ == BsdiffFormat::kLegacy) {
136 // The magic header is "BSDIFF40" for legacy format.
137 memcpy(header, kLegacyMagicHeader, 8);
138 } else if (format_ == BsdiffFormat::kBsdf2) {
139 // The magic header for BSDF2 format:
140 // 0 5 BSDF2
141 // 5 1 compressed type for control stream
142 // 6 1 compressed type for diff stream
143 // 7 1 compressed type for extra stream
144 memcpy(header, kBSDF2MagicHeader, 5);
145 header[5] = static_cast<uint8_t>(ctrl_stream_->Type());
146 header[6] = static_cast<uint8_t>(diff_stream_->Type());
147 header[7] = static_cast<uint8_t>(extra_stream_->Type());
148 } else {
149 LOG(ERROR) << "Unsupported bsdiff format." << endl;
150 return false;
151 }
152
Alex Deymoa28e0192017-09-08 14:21:05 +0200153 EncodeInt64(ctrl_size, header + 8);
154 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200155 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200156 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
157 LOG(ERROR) << "writing to the patch file" << endl;
158 return false;
159 }
160 return true;
161}
162
163} // namespace bsdiff