blob: 52982e0f9e041da7753e0f8a9dc3a0f554b996f1 [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 +020018void EncodeInt64(int64_t x, uint8_t* buf) {
19 uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
20 for (int i = 0; i < 8; ++i) {
21 buf[i] = y & 0xff;
22 y /= 256;
23 }
24}
25
26} // namespace
27
28namespace bsdiff {
29
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080030BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename)
Tianjie Xu32b1f212018-03-06 11:42:45 -080031 : patch_filename_(patch_filename),
32 format_(BsdiffFormat::kLegacy),
Tianjie Xu77833b62018-03-07 18:13:47 -080033 brotli_quality_(-1) {
34 types_.emplace_back(CompressorType::kBZ2);
35}
Tianjie Xu32b1f212018-03-06 11:42:45 -080036
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080037
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070038BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
Tianjie Xu77833b62018-03-07 18:13:47 -080039 const std::vector<CompressorType>& types,
Tianjie Xu2e70b552018-03-02 16:22:10 -080040 int brotli_quality)
Tianjie Xu32b1f212018-03-06 11:42:45 -080041 : patch_filename_(patch_filename),
42 format_(BsdiffFormat::kBsdf2),
Tianjie Xu77833b62018-03-07 18:13:47 -080043 types_(types),
Tianjie Xu32b1f212018-03-06 11:42:45 -080044 brotli_quality_(brotli_quality) {}
45
46bool BsdiffPatchWriter::InitializeCompressorList(
47 std::vector<std::unique_ptr<bsdiff::CompressorInterface>>*
48 compressor_list) {
Tianjie Xu77833b62018-03-07 18:13:47 -080049 if (types_.empty()) {
50 LOG(ERROR) << "Patch writer expects at least one compressor.";
51 return false;
52 }
53
54 for (const auto& type : types_) {
55 switch (type) {
56 case CompressorType::kBZ2:
57 compressor_list->emplace_back(new BZ2Compressor());
58 break;
59 case CompressorType::kBrotli:
60 compressor_list->emplace_back(new BrotliCompressor(brotli_quality_));
61 break;
62 case CompressorType::kNoCompression:
63 LOG(ERROR) << "Unsupported compression type " << static_cast<int>(type);
Sen Jiangeb95b7e2018-11-26 13:32:54 -080064 return false;
Tianjie Xu77833b62018-03-07 18:13:47 -080065 }
Tianjie Xub4cba642017-11-14 22:46:38 -080066 }
Tianjie Xu32b1f212018-03-06 11:42:45 -080067
68 for (const auto& compressor : *compressor_list) {
69 if (!compressor) {
70 return false;
71 }
72 }
73
74 return true;
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070075}
76
Alex Deymo4dadd8b2017-10-26 16:19:33 +020077bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Tianjie Xu32b1f212018-03-06 11:42:45 -080078 if (!InitializeCompressorList(&ctrl_stream_list_)) {
79 LOG(ERROR) << "Failed to initialize control stream compressors.";
80 return false;
81 }
82
83 if (!InitializeCompressorList(&diff_stream_list_)) {
84 LOG(ERROR) << "Failed to initialize diff stream compressors.";
85 return false;
86 }
87
88 if (!InitializeCompressorList(&extra_stream_list_)) {
89 LOG(ERROR) << "Failed to initialize extra stream compressors.";
Tianjie Xub4cba642017-11-14 22:46:38 -080090 return false;
91 }
92
Alex Deymo538a75d2017-09-27 15:34:59 +020093 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020094 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080095 LOG(ERROR) << "Opening " << patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020096 return false;
97 }
98 return true;
99}
100
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200101bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -0800102 for (const auto& compressor : diff_stream_list_) {
103 if (!compressor->Write(data, size)) {
104 return false;
105 }
106 }
107
108 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200109}
110
111bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -0800112 for (const auto& compressor : extra_stream_list_) {
113 if (!compressor->Write(data, size)) {
114 return false;
115 }
116 }
117
118 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200119}
120
Alex Deymoa28e0192017-09-08 14:21:05 +0200121bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200122 // Generate the 24 byte control entry.
123 uint8_t buf[24];
124 EncodeInt64(entry.diff_size, buf);
125 EncodeInt64(entry.extra_size, buf + 8);
126 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800127
128 for (const auto& compressor : ctrl_stream_list_) {
129 if (!compressor->Write(buf, sizeof(buf))) {
130 return false;
131 }
132 }
133
Alex Deymoa28e0192017-09-08 14:21:05 +0200134 written_output_ += entry.diff_size + entry.extra_size;
135 return true;
136}
137
Tianjie Xu32b1f212018-03-06 11:42:45 -0800138bool BsdiffPatchWriter::SelectSmallestResult(
139 const std::vector<std::unique_ptr<CompressorInterface>>& compressor_list,
140 CompressorInterface** smallest_compressor) {
141 size_t min_size = std::numeric_limits<size_t>::max();
142 for (const auto& compressor : compressor_list) {
143 if (!compressor->Finish()) {
144 LOG(ERROR) << "Failed to finalize compressed streams.";
145 return false;
146 }
147
148 // Update |smallest_compressor| if the current compressor produces a
149 // smaller result.
150 if (compressor->GetCompressedData().size() < min_size) {
151 min_size = compressor->GetCompressedData().size();
152 *smallest_compressor = compressor.get();
153 }
154 }
155
156 return true;
157}
158
Alex Deymoa28e0192017-09-08 14:21:05 +0200159bool BsdiffPatchWriter::Close() {
160 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800161 LOG(ERROR) << "File not open.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200162 return false;
163 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200164
Tianjie Xu32b1f212018-03-06 11:42:45 -0800165 CompressorInterface* ctrl_stream = nullptr;
166 if (!SelectSmallestResult(ctrl_stream_list_, &ctrl_stream) || !ctrl_stream) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200167 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200168 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200169
Tianjie Xu32b1f212018-03-06 11:42:45 -0800170 CompressorInterface* diff_stream = nullptr;
171 if (!SelectSmallestResult(diff_stream_list_, &diff_stream) || !diff_stream) {
172 return false;
173 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200174
Tianjie Xu32b1f212018-03-06 11:42:45 -0800175 CompressorInterface* extra_stream = nullptr;
176 if (!SelectSmallestResult(extra_stream_list_, &extra_stream) ||
177 !extra_stream) {
178 return false;
179 }
180
181 auto ctrl_data = ctrl_stream->GetCompressedData();
182 auto diff_data = diff_stream->GetCompressedData();
183 auto extra_data = extra_stream->GetCompressedData();
184
185 uint8_t types[3] = {static_cast<uint8_t>(ctrl_stream->Type()),
186 static_cast<uint8_t>(diff_stream->Type()),
187 static_cast<uint8_t>(extra_stream->Type())};
188
189 if (!WriteHeader(types, ctrl_data.size(), diff_data.size()))
Alex Deymoa28e0192017-09-08 14:21:05 +0200190 return false;
191
192 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800193 LOG(ERROR) << "Writing ctrl_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200194 return false;
195 }
196 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800197 LOG(ERROR) << "Writing diff_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200198 return false;
199 }
200 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
201 extra_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800202 LOG(ERROR) << "Writing extra_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200203 return false;
204 }
205 if (fclose(fp_) != 0) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800206 LOG(ERROR) << "Closing the patch file.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200207 return false;
208 }
209 fp_ = nullptr;
210 return true;
211}
212
Tianjie Xu32b1f212018-03-06 11:42:45 -0800213bool BsdiffPatchWriter::WriteHeader(uint8_t types[3],
214 uint64_t ctrl_size,
215 uint64_t diff_size) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200216 /* Header format is
Tianjie Xub4cba642017-11-14 22:46:38 -0800217 * 0 8 magic header
218 * 8 8 length of compressed ctrl block
219 * 16 8 length of compressed diff block
Alex Deymoa28e0192017-09-08 14:21:05 +0200220 * 24 8 length of new file
221 *
222 * File format is
223 * 0 32 Header
Tianjie Xub4cba642017-11-14 22:46:38 -0800224 * 32 ?? compressed ctrl block
225 * ?? ?? compressed diff block
226 * ?? ?? compressed extra block
Alex Deymoa28e0192017-09-08 14:21:05 +0200227 */
228 uint8_t header[32];
Tianjie Xub4cba642017-11-14 22:46:38 -0800229 if (format_ == BsdiffFormat::kLegacy) {
230 // The magic header is "BSDIFF40" for legacy format.
231 memcpy(header, kLegacyMagicHeader, 8);
232 } else if (format_ == BsdiffFormat::kBsdf2) {
233 // The magic header for BSDF2 format:
234 // 0 5 BSDF2
235 // 5 1 compressed type for control stream
236 // 6 1 compressed type for diff stream
237 // 7 1 compressed type for extra stream
238 memcpy(header, kBSDF2MagicHeader, 5);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800239 memcpy(header + 5, types, 3);
Tianjie Xub4cba642017-11-14 22:46:38 -0800240 } else {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800241 LOG(ERROR) << "Unsupported bsdiff format.";
Tianjie Xub4cba642017-11-14 22:46:38 -0800242 return false;
243 }
244
Alex Deymoa28e0192017-09-08 14:21:05 +0200245 EncodeInt64(ctrl_size, header + 8);
246 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200247 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200248 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800249 LOG(ERROR) << "writing to the patch file";
Alex Deymoa28e0192017-09-08 14:21:05 +0200250 return false;
251 }
252 return true;
253}
254
255} // namespace bsdiff