blob: b1f45faf607be90c4eb575a8790440cba112210d [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);
64 }
Tianjie Xub4cba642017-11-14 22:46:38 -080065 }
Tianjie Xu32b1f212018-03-06 11:42:45 -080066
67 for (const auto& compressor : *compressor_list) {
68 if (!compressor) {
69 return false;
70 }
71 }
72
73 return true;
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070074}
75
Alex Deymo4dadd8b2017-10-26 16:19:33 +020076bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Tianjie Xu32b1f212018-03-06 11:42:45 -080077 if (!InitializeCompressorList(&ctrl_stream_list_)) {
78 LOG(ERROR) << "Failed to initialize control stream compressors.";
79 return false;
80 }
81
82 if (!InitializeCompressorList(&diff_stream_list_)) {
83 LOG(ERROR) << "Failed to initialize diff stream compressors.";
84 return false;
85 }
86
87 if (!InitializeCompressorList(&extra_stream_list_)) {
88 LOG(ERROR) << "Failed to initialize extra stream compressors.";
Tianjie Xub4cba642017-11-14 22:46:38 -080089 return false;
90 }
91
Alex Deymo538a75d2017-09-27 15:34:59 +020092 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020093 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080094 LOG(ERROR) << "Opening " << patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020095 return false;
96 }
97 return true;
98}
99
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200100bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -0800101 for (const auto& compressor : diff_stream_list_) {
102 if (!compressor->Write(data, size)) {
103 return false;
104 }
105 }
106
107 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200108}
109
110bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -0800111 for (const auto& compressor : extra_stream_list_) {
112 if (!compressor->Write(data, size)) {
113 return false;
114 }
115 }
116
117 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200118}
119
Alex Deymoa28e0192017-09-08 14:21:05 +0200120bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200121 // Generate the 24 byte control entry.
122 uint8_t buf[24];
123 EncodeInt64(entry.diff_size, buf);
124 EncodeInt64(entry.extra_size, buf + 8);
125 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800126
127 for (const auto& compressor : ctrl_stream_list_) {
128 if (!compressor->Write(buf, sizeof(buf))) {
129 return false;
130 }
131 }
132
Alex Deymoa28e0192017-09-08 14:21:05 +0200133 written_output_ += entry.diff_size + entry.extra_size;
134 return true;
135}
136
Tianjie Xu32b1f212018-03-06 11:42:45 -0800137bool BsdiffPatchWriter::SelectSmallestResult(
138 const std::vector<std::unique_ptr<CompressorInterface>>& compressor_list,
139 CompressorInterface** smallest_compressor) {
140 size_t min_size = std::numeric_limits<size_t>::max();
141 for (const auto& compressor : compressor_list) {
142 if (!compressor->Finish()) {
143 LOG(ERROR) << "Failed to finalize compressed streams.";
144 return false;
145 }
146
147 // Update |smallest_compressor| if the current compressor produces a
148 // smaller result.
149 if (compressor->GetCompressedData().size() < min_size) {
150 min_size = compressor->GetCompressedData().size();
151 *smallest_compressor = compressor.get();
152 }
153 }
154
155 return true;
156}
157
Alex Deymoa28e0192017-09-08 14:21:05 +0200158bool BsdiffPatchWriter::Close() {
159 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800160 LOG(ERROR) << "File not open.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200161 return false;
162 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200163
Tianjie Xu32b1f212018-03-06 11:42:45 -0800164 CompressorInterface* ctrl_stream = nullptr;
165 if (!SelectSmallestResult(ctrl_stream_list_, &ctrl_stream) || !ctrl_stream) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200166 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200167 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200168
Tianjie Xu32b1f212018-03-06 11:42:45 -0800169 CompressorInterface* diff_stream = nullptr;
170 if (!SelectSmallestResult(diff_stream_list_, &diff_stream) || !diff_stream) {
171 return false;
172 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200173
Tianjie Xu32b1f212018-03-06 11:42:45 -0800174 CompressorInterface* extra_stream = nullptr;
175 if (!SelectSmallestResult(extra_stream_list_, &extra_stream) ||
176 !extra_stream) {
177 return false;
178 }
179
180 auto ctrl_data = ctrl_stream->GetCompressedData();
181 auto diff_data = diff_stream->GetCompressedData();
182 auto extra_data = extra_stream->GetCompressedData();
183
184 uint8_t types[3] = {static_cast<uint8_t>(ctrl_stream->Type()),
185 static_cast<uint8_t>(diff_stream->Type()),
186 static_cast<uint8_t>(extra_stream->Type())};
187
188 if (!WriteHeader(types, ctrl_data.size(), diff_data.size()))
Alex Deymoa28e0192017-09-08 14:21:05 +0200189 return false;
190
191 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800192 LOG(ERROR) << "Writing ctrl_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200193 return false;
194 }
195 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800196 LOG(ERROR) << "Writing diff_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200197 return false;
198 }
199 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
200 extra_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800201 LOG(ERROR) << "Writing extra_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200202 return false;
203 }
204 if (fclose(fp_) != 0) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800205 LOG(ERROR) << "Closing the patch file.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200206 return false;
207 }
208 fp_ = nullptr;
209 return true;
210}
211
Tianjie Xu32b1f212018-03-06 11:42:45 -0800212bool BsdiffPatchWriter::WriteHeader(uint8_t types[3],
213 uint64_t ctrl_size,
214 uint64_t diff_size) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200215 /* Header format is
Tianjie Xub4cba642017-11-14 22:46:38 -0800216 * 0 8 magic header
217 * 8 8 length of compressed ctrl block
218 * 16 8 length of compressed diff block
Alex Deymoa28e0192017-09-08 14:21:05 +0200219 * 24 8 length of new file
220 *
221 * File format is
222 * 0 32 Header
Tianjie Xub4cba642017-11-14 22:46:38 -0800223 * 32 ?? compressed ctrl block
224 * ?? ?? compressed diff block
225 * ?? ?? compressed extra block
Alex Deymoa28e0192017-09-08 14:21:05 +0200226 */
227 uint8_t header[32];
Tianjie Xub4cba642017-11-14 22:46:38 -0800228 if (format_ == BsdiffFormat::kLegacy) {
229 // The magic header is "BSDIFF40" for legacy format.
230 memcpy(header, kLegacyMagicHeader, 8);
231 } else if (format_ == BsdiffFormat::kBsdf2) {
232 // The magic header for BSDF2 format:
233 // 0 5 BSDF2
234 // 5 1 compressed type for control stream
235 // 6 1 compressed type for diff stream
236 // 7 1 compressed type for extra stream
237 memcpy(header, kBSDF2MagicHeader, 5);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800238 memcpy(header + 5, types, 3);
Tianjie Xub4cba642017-11-14 22:46:38 -0800239 } else {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800240 LOG(ERROR) << "Unsupported bsdiff format.";
Tianjie Xub4cba642017-11-14 22:46:38 -0800241 return false;
242 }
243
Alex Deymoa28e0192017-09-08 14:21:05 +0200244 EncodeInt64(ctrl_size, header + 8);
245 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200246 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200247 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800248 LOG(ERROR) << "writing to the patch file";
Alex Deymoa28e0192017-09-08 14:21:05 +0200249 return false;
250 }
251 return true;
252}
253
254} // namespace bsdiff