1 // 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
5 #include "puffin/src/include/puffin/puffdiff.h"
6
7 #include <endian.h>
8 #include <inttypes.h>
9 #include <unistd.h>
10
11 #include <string>
12 #include <vector>
13
14 #include "bsdiff/bsdiff.h"
15 #include "bsdiff/patch_writer_factory.h"
16
17 #include "puffin/src/file_stream.h"
18 #include "puffin/src/include/puffin/common.h"
19 #include "puffin/src/include/puffin/puffer.h"
20 #include "puffin/src/include/puffin/puffpatch.h"
21 #include "puffin/src/include/puffin/utils.h"
22 #include "puffin/src/logging.h"
23 #include "puffin/src/memory_stream.h"
24 #include "puffin/src/puffin.pb.h"
25 #include "puffin/src/puffin_stream.h"
26
27 using std::string;
28 using std::vector;
29
30 namespace puffin {
31
32 namespace {
33 const int kBrotliCompressionQuality = 11;
34
35 template <typename T>
CopyVectorToRpf(const T & from,google::protobuf::RepeatedPtrField<metadata::BitExtent> * to,size_t coef)36 void CopyVectorToRpf(
37 const T& from,
38 google::protobuf::RepeatedPtrField<metadata::BitExtent>* to,
39 size_t coef) {
40 to->Reserve(from.size());
41 for (const auto& ext : from) {
42 auto tmp = to->Add();
43 tmp->set_offset(ext.offset * coef);
44 tmp->set_length(ext.length * coef);
45 }
46 }
47
48 // Structure of a puffin patch
49 // +-------+------------------+-------------+--------------+
50 // |P|U|F|1| PatchHeader Size | PatchHeader | bsdiff_patch |
51 // +-------+------------------+-------------+--------------+
CreatePatch(const Buffer & bsdiff_patch,const vector<BitExtent> & src_deflates,const vector<BitExtent> & dst_deflates,const vector<ByteExtent> & src_puffs,const vector<ByteExtent> & dst_puffs,uint64_t src_puff_size,uint64_t dst_puff_size,Buffer * patch)52 bool CreatePatch(const Buffer& bsdiff_patch,
53 const vector<BitExtent>& src_deflates,
54 const vector<BitExtent>& dst_deflates,
55 const vector<ByteExtent>& src_puffs,
56 const vector<ByteExtent>& dst_puffs,
57 uint64_t src_puff_size,
58 uint64_t dst_puff_size,
59 Buffer* patch) {
60 metadata::PatchHeader header;
61 header.set_version(1);
62
63 CopyVectorToRpf(src_deflates, header.mutable_src()->mutable_deflates(), 1);
64 CopyVectorToRpf(dst_deflates, header.mutable_dst()->mutable_deflates(), 1);
65 CopyVectorToRpf(src_puffs, header.mutable_src()->mutable_puffs(), 8);
66 CopyVectorToRpf(dst_puffs, header.mutable_dst()->mutable_puffs(), 8);
67
68 header.mutable_src()->set_puff_length(src_puff_size);
69 header.mutable_dst()->set_puff_length(dst_puff_size);
70
71 const uint32_t header_size = header.ByteSize();
72
73 uint64_t offset = 0;
74 patch->resize(kMagicLength + sizeof(header_size) + header_size +
75 bsdiff_patch.size());
76
77 memcpy(patch->data() + offset, kMagic, kMagicLength);
78 offset += kMagicLength;
79
80 // Read header size from big-endian mode.
81 uint32_t be_header_size = htobe32(header_size);
82 memcpy(patch->data() + offset, &be_header_size, sizeof(be_header_size));
83 offset += 4;
84
85 TEST_AND_RETURN_FALSE(
86 header.SerializeToArray(patch->data() + offset, header_size));
87 offset += header_size;
88
89 memcpy(patch->data() + offset, bsdiff_patch.data(), bsdiff_patch.size());
90
91 if (bsdiff_patch.size() > patch->size()) {
92 LOG(ERROR) << "Puffin patch is invalid";
93 }
94 return true;
95 }
96
97 } // namespace
98
PuffDiff(UniqueStreamPtr src,UniqueStreamPtr dst,const vector<BitExtent> & src_deflates,const vector<BitExtent> & dst_deflates,const vector<bsdiff::CompressorType> & compressors,const string & tmp_filepath,Buffer * patch)99 bool PuffDiff(UniqueStreamPtr src,
100 UniqueStreamPtr dst,
101 const vector<BitExtent>& src_deflates,
102 const vector<BitExtent>& dst_deflates,
103 const vector<bsdiff::CompressorType>& compressors,
104 const string& tmp_filepath,
105 Buffer* patch) {
106 auto puffer = std::make_shared<Puffer>();
107 auto puff_deflate_stream =
108 [&puffer](UniqueStreamPtr stream, const vector<BitExtent>& deflates,
109 Buffer* puff_buffer, vector<ByteExtent>* puffs) {
110 uint64_t puff_size;
111 TEST_AND_RETURN_FALSE(stream->Seek(0));
112 TEST_AND_RETURN_FALSE(
113 FindPuffLocations(stream, deflates, puffs, &puff_size));
114 TEST_AND_RETURN_FALSE(stream->Seek(0));
115 auto src_puffin_stream = PuffinStream::CreateForPuff(
116 std::move(stream), puffer, puff_size, deflates, *puffs);
117 puff_buffer->resize(puff_size);
118 TEST_AND_RETURN_FALSE(
119 src_puffin_stream->Read(puff_buffer->data(), puff_buffer->size()));
120 return true;
121 };
122
123 Buffer src_puff_buffer;
124 Buffer dst_puff_buffer;
125 vector<ByteExtent> src_puffs, dst_puffs;
126 TEST_AND_RETURN_FALSE(puff_deflate_stream(std::move(src), src_deflates,
127 &src_puff_buffer, &src_puffs));
128 TEST_AND_RETURN_FALSE(puff_deflate_stream(std::move(dst), dst_deflates,
129 &dst_puff_buffer, &dst_puffs));
130
131 auto bsdiff_patch_writer = bsdiff::CreateBSDF2PatchWriter(
132 tmp_filepath, compressors, kBrotliCompressionQuality);
133
134 TEST_AND_RETURN_FALSE(
135 0 == bsdiff::bsdiff(src_puff_buffer.data(), src_puff_buffer.size(),
136 dst_puff_buffer.data(), dst_puff_buffer.size(),
137 bsdiff_patch_writer.get(), nullptr));
138
139 auto bsdiff_patch = FileStream::Open(tmp_filepath, true, false);
140 TEST_AND_RETURN_FALSE(bsdiff_patch);
141 uint64_t patch_size;
142 TEST_AND_RETURN_FALSE(bsdiff_patch->GetSize(&patch_size));
143 Buffer bsdiff_patch_buf(patch_size);
144 TEST_AND_RETURN_FALSE(
145 bsdiff_patch->Read(bsdiff_patch_buf.data(), bsdiff_patch_buf.size()));
146 TEST_AND_RETURN_FALSE(bsdiff_patch->Close());
147
148 TEST_AND_RETURN_FALSE(CreatePatch(
149 bsdiff_patch_buf, src_deflates, dst_deflates, src_puffs, dst_puffs,
150 src_puff_buffer.size(), dst_puff_buffer.size(), patch));
151 return true;
152 }
153
PuffDiff(const Buffer & src,const Buffer & dst,const vector<BitExtent> & src_deflates,const vector<BitExtent> & dst_deflates,const vector<bsdiff::CompressorType> & compressors,const string & tmp_filepath,Buffer * patch)154 bool PuffDiff(const Buffer& src,
155 const Buffer& dst,
156 const vector<BitExtent>& src_deflates,
157 const vector<BitExtent>& dst_deflates,
158 const vector<bsdiff::CompressorType>& compressors,
159 const string& tmp_filepath,
160 Buffer* patch) {
161 return PuffDiff(MemoryStream::CreateForRead(src),
162 MemoryStream::CreateForRead(dst), src_deflates, dst_deflates,
163 compressors, tmp_filepath, patch);
164 }
165
PuffDiff(const Buffer & src,const Buffer & dst,const vector<BitExtent> & src_deflates,const vector<BitExtent> & dst_deflates,const string & tmp_filepath,Buffer * patch)166 bool PuffDiff(const Buffer& src,
167 const Buffer& dst,
168 const vector<BitExtent>& src_deflates,
169 const vector<BitExtent>& dst_deflates,
170 const string& tmp_filepath,
171 Buffer* patch) {
172 return PuffDiff(
173 src, dst, src_deflates, dst_deflates,
174 {bsdiff::CompressorType::kBZ2, bsdiff::CompressorType::kBrotli},
175 tmp_filepath, patch);
176 }
177
178 } // namespace puffin
179