1 // Copyright 2017 The Chromium 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 "components/zucchini/patch_writer.h"
6 
7 #include <algorithm>
8 #include <iterator>
9 
10 #include "base/numerics/checked_math.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "components/zucchini/crc32.h"
13 
14 namespace zucchini {
15 
16 namespace patch {
17 
SerializeElementMatch(const ElementMatch & element_match,BufferSink * sink)18 bool SerializeElementMatch(const ElementMatch& element_match,
19                            BufferSink* sink) {
20   if (!element_match.IsValid())
21     return false;
22 
23   PatchElementHeader element_header;
24   element_header.old_offset =
25       base::checked_cast<uint32_t>(element_match.old_element.offset);
26   element_header.old_length =
27       base::checked_cast<uint32_t>(element_match.old_element.size);
28   element_header.new_offset =
29       base::checked_cast<uint32_t>(element_match.new_element.offset);
30   element_header.new_length =
31       base::checked_cast<uint32_t>(element_match.new_element.size);
32   element_header.exe_type = element_match.exe_type();
33 
34   return sink->PutValue<PatchElementHeader>(element_header);
35 }
36 
SerializedElementMatchSize(const ElementMatch & element_match)37 size_t SerializedElementMatchSize(const ElementMatch& element_match) {
38   return sizeof(PatchElementHeader);
39 }
40 
SerializeBuffer(const std::vector<uint8_t> & buffer,BufferSink * sink)41 bool SerializeBuffer(const std::vector<uint8_t>& buffer, BufferSink* sink) {
42   // buffer.size() is not encoded as varint to simplify SerializedBufferSize().
43   base::CheckedNumeric<uint32_t> size = buffer.size();
44   if (!size.IsValid())
45     return false;
46   return sink->PutValue<uint32_t>(size.ValueOrDie()) &&
47          sink->PutRange(buffer.begin(), buffer.end());
48 }
49 
SerializedBufferSize(const std::vector<uint8_t> & buffer)50 size_t SerializedBufferSize(const std::vector<uint8_t>& buffer) {
51   return sizeof(uint32_t) + buffer.size();
52 }
53 
54 }  // namespace patch
55 
56 /******** EquivalenceSink ********/
57 
58 EquivalenceSink::EquivalenceSink() = default;
EquivalenceSink(const std::vector<uint8_t> & src_skip,const std::vector<uint8_t> & dst_skip,const std::vector<uint8_t> & copy_count)59 EquivalenceSink::EquivalenceSink(const std::vector<uint8_t>& src_skip,
60                                  const std::vector<uint8_t>& dst_skip,
61                                  const std::vector<uint8_t>& copy_count)
62     : src_skip_(src_skip), dst_skip_(dst_skip), copy_count_(copy_count) {}
63 
64 EquivalenceSink::EquivalenceSink(EquivalenceSink&&) = default;
65 EquivalenceSink::~EquivalenceSink() = default;
66 
PutNext(const Equivalence & equivalence)67 void EquivalenceSink::PutNext(const Equivalence& equivalence) {
68   // Equivalences are expected to be given ordered by |dst_offset|.
69   DCHECK_GE(equivalence.dst_offset, dst_offset_);
70   // Unsigned values are ensured by above check.
71 
72   // Result of substracting 2 unsigned integers is unsigned. Overflow is allowed
73   // for negative values, as long as uint32_t can hold the result.
74   uint32_t src_offset_diff =
75       base::strict_cast<uint32_t>(equivalence.src_offset - src_offset_);
76   EncodeVarInt<int32_t>(static_cast<int32_t>(src_offset_diff),
77                         std::back_inserter(src_skip_));
78 
79   EncodeVarUInt<uint32_t>(
80       base::strict_cast<uint32_t>(equivalence.dst_offset - dst_offset_),
81       std::back_inserter(dst_skip_));
82 
83   EncodeVarUInt<uint32_t>(base::strict_cast<uint32_t>(equivalence.length),
84                           std::back_inserter(copy_count_));
85 
86   src_offset_ = equivalence.src_offset + equivalence.length;
87   dst_offset_ = equivalence.dst_offset + equivalence.length;
88 }
89 
SerializedSize() const90 size_t EquivalenceSink::SerializedSize() const {
91   return patch::SerializedBufferSize(src_skip_) +
92          patch::SerializedBufferSize(dst_skip_) +
93          patch::SerializedBufferSize(copy_count_);
94 }
95 
SerializeInto(BufferSink * sink) const96 bool EquivalenceSink::SerializeInto(BufferSink* sink) const {
97   return patch::SerializeBuffer(src_skip_, sink) &&
98          patch::SerializeBuffer(dst_skip_, sink) &&
99          patch::SerializeBuffer(copy_count_, sink);
100 }
101 
102 /******** ExtraDataSink ********/
103 
104 ExtraDataSink::ExtraDataSink() = default;
ExtraDataSink(const std::vector<uint8_t> & extra_data)105 ExtraDataSink::ExtraDataSink(const std::vector<uint8_t>& extra_data)
106     : extra_data_(extra_data) {}
107 
108 ExtraDataSink::ExtraDataSink(ExtraDataSink&&) = default;
109 ExtraDataSink::~ExtraDataSink() = default;
110 
PutNext(ConstBufferView region)111 void ExtraDataSink::PutNext(ConstBufferView region) {
112   extra_data_.insert(extra_data_.end(), region.begin(), region.end());
113 }
114 
SerializedSize() const115 size_t ExtraDataSink::SerializedSize() const {
116   return patch::SerializedBufferSize(extra_data_);
117 }
118 
SerializeInto(BufferSink * sink) const119 bool ExtraDataSink::SerializeInto(BufferSink* sink) const {
120   return patch::SerializeBuffer(extra_data_, sink);
121 }
122 
123 /******** RawDeltaSink ********/
124 
125 RawDeltaSink::RawDeltaSink() = default;
RawDeltaSink(const std::vector<uint8_t> & raw_delta_skip,const std::vector<uint8_t> & raw_delta_diff)126 RawDeltaSink::RawDeltaSink(const std::vector<uint8_t>& raw_delta_skip,
127                            const std::vector<uint8_t>& raw_delta_diff)
128     : raw_delta_skip_(raw_delta_skip), raw_delta_diff_(raw_delta_diff) {}
129 
130 RawDeltaSink::RawDeltaSink(RawDeltaSink&&) = default;
131 RawDeltaSink::~RawDeltaSink() = default;
132 
PutNext(const RawDeltaUnit & delta)133 void RawDeltaSink::PutNext(const RawDeltaUnit& delta) {
134   DCHECK_GE(delta.copy_offset, copy_offset_compensation_);
135   EncodeVarUInt<uint32_t>(base::strict_cast<uint32_t>(
136                               delta.copy_offset - copy_offset_compensation_),
137                           std::back_inserter(raw_delta_skip_));
138 
139   copy_offset_compensation_ = delta.copy_offset + 1;
140 
141   raw_delta_diff_.push_back(delta.diff);
142 }
143 
SerializedSize() const144 size_t RawDeltaSink::SerializedSize() const {
145   return patch::SerializedBufferSize(raw_delta_skip_) +
146          patch::SerializedBufferSize(raw_delta_diff_);
147 }
148 
SerializeInto(BufferSink * sink) const149 bool RawDeltaSink::SerializeInto(BufferSink* sink) const {
150   return patch::SerializeBuffer(raw_delta_skip_, sink) &&
151          patch::SerializeBuffer(raw_delta_diff_, sink);
152 }
153 
154 /******** ReferenceDeltaSink ********/
155 
156 ReferenceDeltaSink::ReferenceDeltaSink() = default;
ReferenceDeltaSink(const std::vector<uint8_t> & reference_delta)157 ReferenceDeltaSink::ReferenceDeltaSink(
158     const std::vector<uint8_t>& reference_delta)
159     : reference_delta_(reference_delta) {}
160 
161 ReferenceDeltaSink::ReferenceDeltaSink(ReferenceDeltaSink&&) = default;
162 ReferenceDeltaSink::~ReferenceDeltaSink() = default;
163 
PutNext(int32_t diff)164 void ReferenceDeltaSink::PutNext(int32_t diff) {
165   EncodeVarInt<int32_t>(diff, std::back_inserter(reference_delta_));
166 }
167 
SerializedSize() const168 size_t ReferenceDeltaSink::SerializedSize() const {
169   return patch::SerializedBufferSize(reference_delta_);
170 }
171 
SerializeInto(BufferSink * sink) const172 bool ReferenceDeltaSink::SerializeInto(BufferSink* sink) const {
173   return patch::SerializeBuffer(reference_delta_, sink);
174 }
175 
176 /******** TargetSink ********/
177 
178 TargetSink::TargetSink() = default;
TargetSink(const std::vector<uint8_t> & extra_targets)179 TargetSink::TargetSink(const std::vector<uint8_t>& extra_targets)
180     : extra_targets_(extra_targets) {}
181 
182 TargetSink::TargetSink(TargetSink&&) = default;
183 TargetSink::~TargetSink() = default;
184 
PutNext(uint32_t target)185 void TargetSink::PutNext(uint32_t target) {
186   DCHECK_GE(target, target_compensation_);
187 
188   EncodeVarUInt<uint32_t>(
189       base::strict_cast<uint32_t>(target - target_compensation_),
190       std::back_inserter(extra_targets_));
191 
192   target_compensation_ = target + 1;
193 }
194 
SerializedSize() const195 size_t TargetSink::SerializedSize() const {
196   return patch::SerializedBufferSize(extra_targets_);
197 }
198 
SerializeInto(BufferSink * sink) const199 bool TargetSink::SerializeInto(BufferSink* sink) const {
200   return patch::SerializeBuffer(extra_targets_, sink);
201 }
202 
203 /******** PatchElementWriter ********/
204 
205 PatchElementWriter::PatchElementWriter() = default;
PatchElementWriter(ElementMatch element_match)206 PatchElementWriter::PatchElementWriter(ElementMatch element_match)
207     : element_match_(element_match) {}
208 
209 PatchElementWriter::PatchElementWriter(PatchElementWriter&&) = default;
210 PatchElementWriter::~PatchElementWriter() = default;
211 
SerializedSize() const212 size_t PatchElementWriter::SerializedSize() const {
213   size_t serialized_size =
214       patch::SerializedElementMatchSize(element_match_) +
215       equivalences_->SerializedSize() + extra_data_->SerializedSize() +
216       raw_delta_->SerializedSize() + reference_delta_->SerializedSize();
217 
218   serialized_size += sizeof(uint32_t);
219   for (const auto& extra_symbols : extra_targets_)
220     serialized_size += extra_symbols.second.SerializedSize() + 1;
221   return serialized_size;
222 }
223 
SerializeInto(BufferSink * sink) const224 bool PatchElementWriter::SerializeInto(BufferSink* sink) const {
225   bool ok =
226       patch::SerializeElementMatch(element_match_, sink) &&
227       equivalences_->SerializeInto(sink) && extra_data_->SerializeInto(sink) &&
228       raw_delta_->SerializeInto(sink) && reference_delta_->SerializeInto(sink);
229   if (!ok)
230     return false;
231 
232   if (!sink->PutValue<uint32_t>(
233           base::checked_cast<uint32_t>(extra_targets_.size())))
234     return false;
235   for (const auto& extra_target_sink : extra_targets_) {
236     if (!sink->PutValue<uint8_t>(extra_target_sink.first.value()))
237       return false;
238     if (!extra_target_sink.second.SerializeInto(sink))
239       return false;
240   }
241   return true;
242 }
243 
244 /******** EnsemblePatchWriter ********/
245 
246 EnsemblePatchWriter::~EnsemblePatchWriter() = default;
247 
EnsemblePatchWriter(const PatchHeader & header)248 EnsemblePatchWriter::EnsemblePatchWriter(const PatchHeader& header)
249     : header_(header) {
250   DCHECK_EQ(header_.magic, PatchHeader::kMagic);
251 }
252 
EnsemblePatchWriter(ConstBufferView old_image,ConstBufferView new_image)253 EnsemblePatchWriter::EnsemblePatchWriter(ConstBufferView old_image,
254                                          ConstBufferView new_image) {
255   header_.magic = PatchHeader::kMagic;
256   header_.old_size = base::checked_cast<uint32_t>(old_image.size());
257   header_.old_crc = CalculateCrc32(old_image.begin(), old_image.end());
258   header_.new_size = base::checked_cast<uint32_t>(new_image.size());
259   header_.new_crc = CalculateCrc32(new_image.begin(), new_image.end());
260 }
261 
AddElement(PatchElementWriter && patch_element)262 void EnsemblePatchWriter::AddElement(PatchElementWriter&& patch_element) {
263   DCHECK(patch_element.new_element().offset == current_dst_offset_);
264   current_dst_offset_ = patch_element.new_element().EndOffset();
265   elements_.push_back(std::move(patch_element));
266 }
267 
SerializedSize() const268 size_t EnsemblePatchWriter::SerializedSize() const {
269   size_t serialized_size = sizeof(PatchHeader) + sizeof(uint32_t);
270   for (const auto& patch_element : elements_) {
271     serialized_size += patch_element.SerializedSize();
272   }
273   return serialized_size;
274 }
275 
SerializeInto(BufferSink * sink) const276 bool EnsemblePatchWriter::SerializeInto(BufferSink* sink) const {
277   DCHECK_EQ(current_dst_offset_, header_.new_size);
278   bool ok =
279       sink->PutValue<PatchHeader>(header_) &&
280       sink->PutValue<uint32_t>(base::checked_cast<uint32_t>(elements_.size()));
281   if (!ok)
282     return false;
283 
284   for (const auto& element : elements_) {
285     if (!element.SerializeInto(sink))
286       return false;
287   }
288   return true;
289 }
290 
291 }  // namespace zucchini
292