blob: b7d9b08f5cb97a399df998f56cead60a63ca0bb6 [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>
Tianjie Xu32b1f212018-03-06 11:42:45 -08008#include <limits>
Alex Deymoa28e0192017-09-08 14:21:05 +02009
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070010#include "bsdiff/brotli_compressor.h"
11#include "bsdiff/bz2_compressor.h"
Tianjie Xu4d10c3e2017-10-26 14:02:06 -070012#include "bsdiff/constants.h"
Tianjie Xu65288122017-10-13 15:10:58 -070013#include "bsdiff/control_entry.h"
Alex Deymodcd423b2017-09-13 20:54:24 +020014#include "bsdiff/logging.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020015
Alex Deymoa28e0192017-09-08 14:21:05 +020016namespace {
17
Alex Deymoa28e0192017-09-08 14:21:05 +020018
19} // namespace
20
21namespace bsdiff {
22
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080023BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename)
Tianjie Xu32b1f212018-03-06 11:42:45 -080024 : patch_filename_(patch_filename),
25 format_(BsdiffFormat::kLegacy),
Tianjie Xu77833b62018-03-07 18:13:47 -080026 brotli_quality_(-1) {
27 types_.emplace_back(CompressorType::kBZ2);
28}
Tianjie Xu32b1f212018-03-06 11:42:45 -080029
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080030
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070031BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
Tianjie Xu77833b62018-03-07 18:13:47 -080032 const std::vector<CompressorType>& types,
Tianjie Xu2e70b552018-03-02 16:22:10 -080033 int brotli_quality)
Tianjie Xu32b1f212018-03-06 11:42:45 -080034 : patch_filename_(patch_filename),
35 format_(BsdiffFormat::kBsdf2),
Tianjie Xu77833b62018-03-07 18:13:47 -080036 types_(types),
Tianjie Xu32b1f212018-03-06 11:42:45 -080037 brotli_quality_(brotli_quality) {}
38
39bool BsdiffPatchWriter::InitializeCompressorList(
40 std::vector<std::unique_ptr<bsdiff::CompressorInterface>>*
41 compressor_list) {
Tianjie Xu77833b62018-03-07 18:13:47 -080042 if (types_.empty()) {
43 LOG(ERROR) << "Patch writer expects at least one compressor.";
44 return false;
45 }
46
47 for (const auto& type : types_) {
48 switch (type) {
49 case CompressorType::kBZ2:
50 compressor_list->emplace_back(new BZ2Compressor());
51 break;
52 case CompressorType::kBrotli:
53 compressor_list->emplace_back(new BrotliCompressor(brotli_quality_));
54 break;
55 case CompressorType::kNoCompression:
56 LOG(ERROR) << "Unsupported compression type " << static_cast<int>(type);
Sen Jiangeb95b7e2018-11-26 13:32:54 -080057 return false;
Tianjie Xu77833b62018-03-07 18:13:47 -080058 }
Tianjie Xub4cba642017-11-14 22:46:38 -080059 }
Tianjie Xu32b1f212018-03-06 11:42:45 -080060
61 for (const auto& compressor : *compressor_list) {
62 if (!compressor) {
63 return false;
64 }
65 }
66
67 return true;
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070068}
69
Alex Deymo4dadd8b2017-10-26 16:19:33 +020070bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Tianjie Xu32b1f212018-03-06 11:42:45 -080071 if (!InitializeCompressorList(&ctrl_stream_list_)) {
72 LOG(ERROR) << "Failed to initialize control stream compressors.";
73 return false;
74 }
75
76 if (!InitializeCompressorList(&diff_stream_list_)) {
77 LOG(ERROR) << "Failed to initialize diff stream compressors.";
78 return false;
79 }
80
81 if (!InitializeCompressorList(&extra_stream_list_)) {
82 LOG(ERROR) << "Failed to initialize extra stream compressors.";
Tianjie Xub4cba642017-11-14 22:46:38 -080083 return false;
84 }
85
Alex Deymo538a75d2017-09-27 15:34:59 +020086 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020087 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080088 LOG(ERROR) << "Opening " << patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020089 return false;
90 }
91 return true;
92}
93
Alex Deymo68c0e7f2017-10-02 20:38:12 +020094bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -080095 for (const auto& compressor : diff_stream_list_) {
96 if (!compressor->Write(data, size)) {
97 return false;
98 }
99 }
100
101 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200102}
103
104bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -0800105 for (const auto& compressor : extra_stream_list_) {
106 if (!compressor->Write(data, size)) {
107 return false;
108 }
109 }
110
111 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200112}
113
Alex Deymoa28e0192017-09-08 14:21:05 +0200114bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200115 // Generate the 24 byte control entry.
116 uint8_t buf[24];
117 EncodeInt64(entry.diff_size, buf);
118 EncodeInt64(entry.extra_size, buf + 8);
119 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800120
121 for (const auto& compressor : ctrl_stream_list_) {
122 if (!compressor->Write(buf, sizeof(buf))) {
123 return false;
124 }
125 }
126
Alex Deymoa28e0192017-09-08 14:21:05 +0200127 written_output_ += entry.diff_size + entry.extra_size;
128 return true;
129}
130
Tianjie Xu32b1f212018-03-06 11:42:45 -0800131bool BsdiffPatchWriter::SelectSmallestResult(
132 const std::vector<std::unique_ptr<CompressorInterface>>& compressor_list,
133 CompressorInterface** smallest_compressor) {
134 size_t min_size = std::numeric_limits<size_t>::max();
135 for (const auto& compressor : compressor_list) {
136 if (!compressor->Finish()) {
137 LOG(ERROR) << "Failed to finalize compressed streams.";
138 return false;
139 }
140
141 // Update |smallest_compressor| if the current compressor produces a
142 // smaller result.
143 if (compressor->GetCompressedData().size() < min_size) {
144 min_size = compressor->GetCompressedData().size();
145 *smallest_compressor = compressor.get();
146 }
147 }
148
149 return true;
150}
151
Alex Deymoa28e0192017-09-08 14:21:05 +0200152bool BsdiffPatchWriter::Close() {
153 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800154 LOG(ERROR) << "File not open.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200155 return false;
156 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200157
Tianjie Xu32b1f212018-03-06 11:42:45 -0800158 CompressorInterface* ctrl_stream = nullptr;
159 if (!SelectSmallestResult(ctrl_stream_list_, &ctrl_stream) || !ctrl_stream) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200160 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200161 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200162
Tianjie Xu32b1f212018-03-06 11:42:45 -0800163 CompressorInterface* diff_stream = nullptr;
164 if (!SelectSmallestResult(diff_stream_list_, &diff_stream) || !diff_stream) {
165 return false;
166 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200167
Tianjie Xu32b1f212018-03-06 11:42:45 -0800168 CompressorInterface* extra_stream = nullptr;
169 if (!SelectSmallestResult(extra_stream_list_, &extra_stream) ||
170 !extra_stream) {
171 return false;
172 }
173
174 auto ctrl_data = ctrl_stream->GetCompressedData();
175 auto diff_data = diff_stream->GetCompressedData();
176 auto extra_data = extra_stream->GetCompressedData();
177
178 uint8_t types[3] = {static_cast<uint8_t>(ctrl_stream->Type()),
179 static_cast<uint8_t>(diff_stream->Type()),
180 static_cast<uint8_t>(extra_stream->Type())};
181
182 if (!WriteHeader(types, ctrl_data.size(), diff_data.size()))
Alex Deymoa28e0192017-09-08 14:21:05 +0200183 return false;
184
185 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800186 LOG(ERROR) << "Writing ctrl_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200187 return false;
188 }
189 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800190 LOG(ERROR) << "Writing diff_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200191 return false;
192 }
193 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
194 extra_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800195 LOG(ERROR) << "Writing extra_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200196 return false;
197 }
198 if (fclose(fp_) != 0) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800199 LOG(ERROR) << "Closing the patch file.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200200 return false;
201 }
202 fp_ = nullptr;
203 return true;
204}
205
Tianjie Xu32b1f212018-03-06 11:42:45 -0800206bool BsdiffPatchWriter::WriteHeader(uint8_t types[3],
207 uint64_t ctrl_size,
208 uint64_t diff_size) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200209 /* Header format is
Tianjie Xub4cba642017-11-14 22:46:38 -0800210 * 0 8 magic header
211 * 8 8 length of compressed ctrl block
212 * 16 8 length of compressed diff block
Alex Deymoa28e0192017-09-08 14:21:05 +0200213 * 24 8 length of new file
214 *
215 * File format is
216 * 0 32 Header
Tianjie Xub4cba642017-11-14 22:46:38 -0800217 * 32 ?? compressed ctrl block
218 * ?? ?? compressed diff block
219 * ?? ?? compressed extra block
Alex Deymoa28e0192017-09-08 14:21:05 +0200220 */
221 uint8_t header[32];
Tianjie Xub4cba642017-11-14 22:46:38 -0800222 if (format_ == BsdiffFormat::kLegacy) {
223 // The magic header is "BSDIFF40" for legacy format.
224 memcpy(header, kLegacyMagicHeader, 8);
225 } else if (format_ == BsdiffFormat::kBsdf2) {
226 // The magic header for BSDF2 format:
227 // 0 5 BSDF2
228 // 5 1 compressed type for control stream
229 // 6 1 compressed type for diff stream
230 // 7 1 compressed type for extra stream
231 memcpy(header, kBSDF2MagicHeader, 5);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800232 memcpy(header + 5, types, 3);
Tianjie Xub4cba642017-11-14 22:46:38 -0800233 } else {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800234 LOG(ERROR) << "Unsupported bsdiff format.";
Tianjie Xub4cba642017-11-14 22:46:38 -0800235 return false;
236 }
237
Alex Deymoa28e0192017-09-08 14:21:05 +0200238 EncodeInt64(ctrl_size, header + 8);
239 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200240 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200241 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800242 LOG(ERROR) << "writing to the patch file";
Alex Deymoa28e0192017-09-08 14:21:05 +0200243 return false;
244 }
245 return true;
246}
247
248} // namespace bsdiff