blob: 3e4d3b4bba2126b327f9e55a889e51f4ffe7e243 [file] [log] [blame]
Kelvin Zhangee481e42021-12-20 12:59:10 -08001//
2// Copyright (C) 2021 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "lz4diff.h"
18#include "lz4diff_compress.h"
19
20#include <bsdiff/bsdiff.h>
21#include <bsdiff/constants.h>
22#include <bsdiff/patch_writer_factory.h>
23#include <bsdiff/patch_writer.h>
24#include <puffin/common.h>
25#include <puffin/puffdiff.h>
26#include <lz4.h>
27#include <lz4hc.h>
28
29#include "update_engine/common/utils.h"
30#include "update_engine/common/hash_calculator.h"
31#include "update_engine/payload_generator/deflate_utils.h"
32#include "update_engine/payload_generator/delta_diff_generator.h"
33#include "lz4diff/lz4diff.pb.h"
34#include "lz4diff_format.h"
35
36namespace chromeos_update_engine {
37
38bool StoreDstCompressedFileInfo(std::string_view recompressed_blob,
39 std::string_view target_blob,
40 const CompressedFile& dst_file_info,
41 Lz4diffHeader* output) {
42 *output->mutable_dst_info()->mutable_algo() = dst_file_info.algo;
43 output->mutable_dst_info()->set_zero_padding_enabled(
44 dst_file_info.zero_padding_enabled);
45 const auto& block_info = dst_file_info.blocks;
46 auto& dst_block_info = *output->mutable_dst_info()->mutable_block_info();
47 dst_block_info.Clear();
48 size_t offset = 0;
49 for (const auto& block : block_info) {
50 auto& pb_block = *dst_block_info.Add();
51 pb_block.set_uncompressed_offset(block.uncompressed_offset);
52 pb_block.set_uncompressed_length(block.uncompressed_length);
53 pb_block.set_compressed_length(block.compressed_length);
54 CHECK_LT(offset, recompressed_blob.size());
55 auto s1 = recompressed_blob.substr(offset, block.compressed_length);
56 auto s2 = target_blob.substr(offset, block.compressed_length);
57 if (s1 != s2) {
58 ScopedTempFile patch;
59 int err =
60 bsdiff::bsdiff(reinterpret_cast<const unsigned char*>(s1.data()),
61 s1.size(),
62 reinterpret_cast<const unsigned char*>(s2.data()),
63 s2.size(),
64 patch.path().c_str(),
65 nullptr);
66 CHECK_EQ(err, 0);
67 LOG(WARNING) << "Recompress Postfix patch size: "
68 << utils::FileSize(patch.path());
69 std::string patch_content;
70 TEST_AND_RETURN_FALSE(utils::ReadFile(patch.path(), &patch_content));
71 pb_block.set_postfix_bspatch(std::move(patch_content));
72 }
73 // Include recompressed blob hash, so we can determine if the device
74 // produces same compressed output
75 Blob recompressed_blob_hash;
76 TEST_AND_RETURN_FALSE(HashCalculator::RawHashOfBytes(
77 s1.data(), s1.length(), &recompressed_blob_hash));
78 pb_block.set_sha256_hash(recompressed_blob_hash.data(),
79 recompressed_blob_hash.size());
80
81 offset += block.compressed_length;
82 }
83 return true;
84}
85
86template <typename Blob>
87static bool TryBsdiff(Blob src, Blob dst, Blob* output) noexcept {
88 static constexpr auto kLz4diffDefaultBrotliQuality = 9;
89 CHECK_NE(output, nullptr);
90 ScopedTempFile patch;
91
92 Blob bsdiff_delta;
93 bsdiff::BsdiffPatchWriter patch_writer(patch.path(),
94 {bsdiff::CompressorType::kBrotli},
95 kLz4diffDefaultBrotliQuality);
96 TEST_AND_RETURN_FALSE(0 == bsdiff::bsdiff(src.data(),
97 src.size(),
98 dst.data(),
99 dst.size(),
100 &patch_writer,
101 nullptr));
102
103 TEST_AND_RETURN_FALSE(utils::ReadFile(patch.path(), &bsdiff_delta));
104 TEST_AND_RETURN_FALSE(!bsdiff_delta.empty());
105 *output = std::move(bsdiff_delta);
106 return true;
107}
108
109bool TryFindDeflates(puffin::Buffer data,
110 std::vector<puffin::BitExtent>* deflates) {
111 if (puffin::LocateDeflatesInZipArchive(data, deflates)) {
112 return true;
113 }
114 deflates->clear();
115 if (puffin::LocateDeflatesInGzip(data, deflates)) {
116 return true;
117 }
118 deflates->clear();
119 return false;
120}
121
122static bool ConstructLz4diffPatch(Blob inner_patch,
123 const Lz4diffHeader& header,
124 Blob* output) {
125 Blob patch;
126 patch.resize(kLz4diffHeaderSize);
127 std::memcpy(patch.data(), kLz4diffMagic.data(), kLz4diffMagic.size());
128 *reinterpret_cast<uint32_t*>(patch.data() + kLz4diffMagic.size()) =
129 htobe32(kLz4diffVersion);
130
131 std::string serialized_pb;
132 TEST_AND_RETURN_FALSE(header.SerializeToString(&serialized_pb));
133 *reinterpret_cast<uint32_t*>(patch.data() + kLz4diffMagic.size() + 4) =
134 htobe32(serialized_pb.size());
135 patch.insert(patch.end(), serialized_pb.begin(), serialized_pb.end());
136 patch.insert(patch.end(), inner_patch.begin(), inner_patch.end());
137
138 *output = std::move(patch);
139 return true;
140}
141
142static bool TryPuffdiff(puffin::Buffer src,
143 puffin::Buffer dst,
144 Blob* output) noexcept {
145 CHECK_NE(output, nullptr);
146 std::vector<puffin::BitExtent> src_deflates;
147 TEST_AND_RETURN_FALSE(TryFindDeflates(src, &src_deflates));
148 std::vector<puffin::BitExtent> dst_deflates;
149 TEST_AND_RETURN_FALSE(TryFindDeflates(dst, &dst_deflates));
150 if (src_deflates.empty() || dst_deflates.empty()) {
151 return false;
152 }
153
154 Blob puffdiff_delta;
155 ScopedTempFile temp_file("puffdiff-delta.XXXXXX");
156 // Perform PuffDiff operation.
157 TEST_AND_RETURN_FALSE(puffin::PuffDiff(
158 src, dst, src_deflates, dst_deflates, temp_file.path(), &puffdiff_delta));
159 TEST_AND_RETURN_FALSE(!puffdiff_delta.empty());
160
161 *output = std::move(puffdiff_delta);
162 return true;
163}
164
165static void StoreSrcCompressedFileInfo(const CompressedFile& src_file_info,
166 Lz4diffHeader* header) {
167 *header->mutable_src_info()->mutable_algo() = src_file_info.algo;
Kelvin Zhang4eae81e2021-12-09 17:07:17 -0800168 header->mutable_src_info()->set_zero_padding_enabled(
169 src_file_info.zero_padding_enabled);
Kelvin Zhangee481e42021-12-20 12:59:10 -0800170 auto& src_blocks = *header->mutable_src_info()->mutable_block_info();
171 src_blocks.Clear();
172 for (const auto& block : src_file_info.blocks) {
173 auto& block_info = *src_blocks.Add();
174 block_info.set_uncompressed_length(block.uncompressed_length);
175 block_info.set_uncompressed_offset(block.uncompressed_offset);
176 block_info.set_compressed_length(block.compressed_length);
177 }
178 return;
179}
180
181bool Lz4Diff(std::string_view src,
182 std::string_view dst,
183 const CompressedFile& src_file_info,
184 const CompressedFile& dst_file_info,
185 const bool zero_padding_enabled,
186 Blob* output,
187 InstallOperation::Type* op_type) noexcept {
188 const auto& src_block_info = src_file_info.blocks;
189 const auto& dst_block_info = dst_file_info.blocks;
190
191 auto decompressed_src =
192 TryDecompressBlob(src, src_block_info, zero_padding_enabled);
193 auto decompressed_dst =
194 TryDecompressBlob(dst, dst_block_info, zero_padding_enabled);
195 if (decompressed_src.empty() || decompressed_dst.empty()) {
196 LOG(ERROR) << "Failed to decompress input data";
197 return false;
198 }
199
200 Lz4diffHeader header;
201 // BSDIFF isn't supposed to fail, so return error if BSDIFF failed.
202 Blob patch_data;
203 TEST_AND_RETURN_FALSE(
204 TryBsdiff(decompressed_src, decompressed_dst, &patch_data));
205 header.set_inner_type(InnerPatchType::BSDIFF);
206 if (op_type) {
207 *op_type = InstallOperation::LZ4DIFF_BSDIFF;
208 }
209 // PUFFDIFF might fail, as the input data might not be deflate compressed.
210
211 Blob puffdiff_delta;
212 if (TryPuffdiff(decompressed_src, decompressed_dst, &puffdiff_delta) &&
213 puffdiff_delta.size() < patch_data.size()) {
214 patch_data = std::move(puffdiff_delta);
215 header.set_inner_type(InnerPatchType::PUFFDIFF);
216 if (op_type) {
217 *op_type = InstallOperation::LZ4DIFF_PUFFDIFF;
218 }
219 }
220 // Free up memory used by |decompressed_src| , as we don't need it anymore.
221 decompressed_src = {};
222
223 auto recompressed_blob = TryCompressBlob(ToStringView(decompressed_dst),
224 dst_block_info,
225 zero_padding_enabled,
226 dst_file_info.algo);
227 TEST_AND_RETURN_FALSE(recompressed_blob.size() > 0);
228
229 StoreSrcCompressedFileInfo(src_file_info, &header);
230 StoreDstCompressedFileInfo(
231 ToStringView(recompressed_blob), dst, dst_file_info, &header);
232 return ConstructLz4diffPatch(std::move(patch_data), header, output);
233}
234
235bool Lz4Diff(const Blob& src,
236 const Blob& dst,
237 const CompressedFile& src_file_info,
238 const CompressedFile& dst_file_info,
239 const bool zero_padding_enabled,
240 Blob* output,
241 InstallOperation::Type* op_type) noexcept {
242 return Lz4Diff(ToStringView(src),
243 ToStringView(dst),
244 src_file_info,
245 dst_file_info,
246 zero_padding_enabled,
247 output,
248 op_type);
249}
250
251} // namespace chromeos_update_engine