blob: b4e80d90057fa9db7231631fe8bf8d6958515501 [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),
33 type_(CompressorType::kBZ2),
34 brotli_quality_(-1) {}
35
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080036
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070037BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080038 CompressorType type,
Tianjie Xu2e70b552018-03-02 16:22:10 -080039 int brotli_quality)
Tianjie Xu32b1f212018-03-06 11:42:45 -080040 : patch_filename_(patch_filename),
41 format_(BsdiffFormat::kBsdf2),
42 type_(type),
43 brotli_quality_(brotli_quality) {}
44
45bool BsdiffPatchWriter::InitializeCompressorList(
46 std::vector<std::unique_ptr<bsdiff::CompressorInterface>>*
47 compressor_list) {
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_);
Tianjie Xub4cba642017-11-14 22:46:38 -080057 }
Tianjie Xu32b1f212018-03-06 11:42:45 -080058
59 for (const auto& compressor : *compressor_list) {
60 if (!compressor) {
61 return false;
62 }
63 }
64
65 return true;
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070066}
67
Alex Deymo4dadd8b2017-10-26 16:19:33 +020068bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Tianjie Xu32b1f212018-03-06 11:42:45 -080069 if (!InitializeCompressorList(&ctrl_stream_list_)) {
70 LOG(ERROR) << "Failed to initialize control stream compressors.";
71 return false;
72 }
73
74 if (!InitializeCompressorList(&diff_stream_list_)) {
75 LOG(ERROR) << "Failed to initialize diff stream compressors.";
76 return false;
77 }
78
79 if (!InitializeCompressorList(&extra_stream_list_)) {
80 LOG(ERROR) << "Failed to initialize extra stream compressors.";
Tianjie Xub4cba642017-11-14 22:46:38 -080081 return false;
82 }
83
Alex Deymo538a75d2017-09-27 15:34:59 +020084 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020085 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080086 LOG(ERROR) << "Opening " << patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020087 return false;
88 }
89 return true;
90}
91
Alex Deymo68c0e7f2017-10-02 20:38:12 +020092bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -080093 for (const auto& compressor : diff_stream_list_) {
94 if (!compressor->Write(data, size)) {
95 return false;
96 }
97 }
98
99 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200100}
101
102bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu32b1f212018-03-06 11:42:45 -0800103 for (const auto& compressor : extra_stream_list_) {
104 if (!compressor->Write(data, size)) {
105 return false;
106 }
107 }
108
109 return true;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200110}
111
Alex Deymoa28e0192017-09-08 14:21:05 +0200112bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200113 // Generate the 24 byte control entry.
114 uint8_t buf[24];
115 EncodeInt64(entry.diff_size, buf);
116 EncodeInt64(entry.extra_size, buf + 8);
117 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800118
119 for (const auto& compressor : ctrl_stream_list_) {
120 if (!compressor->Write(buf, sizeof(buf))) {
121 return false;
122 }
123 }
124
Alex Deymoa28e0192017-09-08 14:21:05 +0200125 written_output_ += entry.diff_size + entry.extra_size;
126 return true;
127}
128
Tianjie Xu32b1f212018-03-06 11:42:45 -0800129bool BsdiffPatchWriter::SelectSmallestResult(
130 const std::vector<std::unique_ptr<CompressorInterface>>& compressor_list,
131 CompressorInterface** smallest_compressor) {
132 size_t min_size = std::numeric_limits<size_t>::max();
133 for (const auto& compressor : compressor_list) {
134 if (!compressor->Finish()) {
135 LOG(ERROR) << "Failed to finalize compressed streams.";
136 return false;
137 }
138
139 // Update |smallest_compressor| if the current compressor produces a
140 // smaller result.
141 if (compressor->GetCompressedData().size() < min_size) {
142 min_size = compressor->GetCompressedData().size();
143 *smallest_compressor = compressor.get();
144 }
145 }
146
147 return true;
148}
149
Alex Deymoa28e0192017-09-08 14:21:05 +0200150bool BsdiffPatchWriter::Close() {
151 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800152 LOG(ERROR) << "File not open.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200153 return false;
154 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200155
Tianjie Xu32b1f212018-03-06 11:42:45 -0800156 CompressorInterface* ctrl_stream = nullptr;
157 if (!SelectSmallestResult(ctrl_stream_list_, &ctrl_stream) || !ctrl_stream) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200158 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200159 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200160
Tianjie Xu32b1f212018-03-06 11:42:45 -0800161 CompressorInterface* diff_stream = nullptr;
162 if (!SelectSmallestResult(diff_stream_list_, &diff_stream) || !diff_stream) {
163 return false;
164 }
Alex Deymoa28e0192017-09-08 14:21:05 +0200165
Tianjie Xu32b1f212018-03-06 11:42:45 -0800166 CompressorInterface* extra_stream = nullptr;
167 if (!SelectSmallestResult(extra_stream_list_, &extra_stream) ||
168 !extra_stream) {
169 return false;
170 }
171
172 auto ctrl_data = ctrl_stream->GetCompressedData();
173 auto diff_data = diff_stream->GetCompressedData();
174 auto extra_data = extra_stream->GetCompressedData();
175
176 uint8_t types[3] = {static_cast<uint8_t>(ctrl_stream->Type()),
177 static_cast<uint8_t>(diff_stream->Type()),
178 static_cast<uint8_t>(extra_stream->Type())};
179
180 if (!WriteHeader(types, ctrl_data.size(), diff_data.size()))
Alex Deymoa28e0192017-09-08 14:21:05 +0200181 return false;
182
183 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800184 LOG(ERROR) << "Writing ctrl_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200185 return false;
186 }
187 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800188 LOG(ERROR) << "Writing diff_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200189 return false;
190 }
191 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
192 extra_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800193 LOG(ERROR) << "Writing extra_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200194 return false;
195 }
196 if (fclose(fp_) != 0) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800197 LOG(ERROR) << "Closing the patch file.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200198 return false;
199 }
200 fp_ = nullptr;
201 return true;
202}
203
Tianjie Xu32b1f212018-03-06 11:42:45 -0800204bool BsdiffPatchWriter::WriteHeader(uint8_t types[3],
205 uint64_t ctrl_size,
206 uint64_t diff_size) {
Alex Deymoa28e0192017-09-08 14:21:05 +0200207 /* Header format is
Tianjie Xub4cba642017-11-14 22:46:38 -0800208 * 0 8 magic header
209 * 8 8 length of compressed ctrl block
210 * 16 8 length of compressed diff block
Alex Deymoa28e0192017-09-08 14:21:05 +0200211 * 24 8 length of new file
212 *
213 * File format is
214 * 0 32 Header
Tianjie Xub4cba642017-11-14 22:46:38 -0800215 * 32 ?? compressed ctrl block
216 * ?? ?? compressed diff block
217 * ?? ?? compressed extra block
Alex Deymoa28e0192017-09-08 14:21:05 +0200218 */
219 uint8_t header[32];
Tianjie Xub4cba642017-11-14 22:46:38 -0800220 if (format_ == BsdiffFormat::kLegacy) {
221 // The magic header is "BSDIFF40" for legacy format.
222 memcpy(header, kLegacyMagicHeader, 8);
223 } else if (format_ == BsdiffFormat::kBsdf2) {
224 // The magic header for BSDF2 format:
225 // 0 5 BSDF2
226 // 5 1 compressed type for control stream
227 // 6 1 compressed type for diff stream
228 // 7 1 compressed type for extra stream
229 memcpy(header, kBSDF2MagicHeader, 5);
Tianjie Xu32b1f212018-03-06 11:42:45 -0800230 memcpy(header + 5, types, 3);
Tianjie Xub4cba642017-11-14 22:46:38 -0800231 } else {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800232 LOG(ERROR) << "Unsupported bsdiff format.";
Tianjie Xub4cba642017-11-14 22:46:38 -0800233 return false;
234 }
235
Alex Deymoa28e0192017-09-08 14:21:05 +0200236 EncodeInt64(ctrl_size, header + 8);
237 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200238 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200239 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800240 LOG(ERROR) << "writing to the patch file";
Alex Deymoa28e0192017-09-08 14:21:05 +0200241 return false;
242 }
243 return true;
244}
245
246} // namespace bsdiff