blob: baa5565f7eac449819323e5f0dec8cec8b8c1b7f [file] [log] [blame]
Alex Deymo68c0e7f2017-10-02 20:38:12 +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
5#include "bsdiff/diff_encoder.h"
6
7#include <vector>
8
9#include "bsdiff/logging.h"
10
11using std::endl;
12
13namespace {
14
15// The maximum positive number that we should encode. A number larger than this
16// for unsigned fields will be interpreted as a negative value and thus a
17// corrupt patch.
18const uint64_t kMaxEncodedUint64Value = (1ULL << 63) - 1;
19
20} // namespace
21
22namespace bsdiff {
23
Alex Deymo4dadd8b2017-10-26 16:19:33 +020024bool DiffEncoder::Init() {
25 return patch_->Init(new_size_);
26}
27
Alex Deymo68c0e7f2017-10-02 20:38:12 +020028bool DiffEncoder::AddControlEntry(const ControlEntry& entry) {
29 if (entry.diff_size > kMaxEncodedUint64Value) {
30 LOG(ERROR) << "Encoding value out of range " << entry.diff_size << endl;
31 return false;
32 }
33
34 if (entry.extra_size > kMaxEncodedUint64Value) {
35 LOG(ERROR) << "Encoding value out of range " << entry.extra_size << endl;
36 return false;
37 }
38
Alex Deymoaefc2532017-10-11 17:50:12 +020039 // entry.diff_size + entry.extra_size don't overflow in uint64_t since we
40 // checked the kMaxEncodedUint64Value limit before.
41 if (entry.diff_size + entry.extra_size > new_size_ - written_output_) {
Alex Deymo68c0e7f2017-10-02 20:38:12 +020042 LOG(ERROR) << "Wrote more output than the declared new_size" << endl;
43 return false;
44 }
45
46 if (entry.diff_size > 0 &&
Alex Deymoaefc2532017-10-11 17:50:12 +020047 (old_pos_ < 0 ||
48 static_cast<uint64_t>(old_pos_) + entry.diff_size > old_size_)) {
49 LOG(ERROR) << "The pointer in the old stream [" << old_pos_ << ", "
50 << (static_cast<uint64_t>(old_pos_) + entry.diff_size)
Alex Deymo68c0e7f2017-10-02 20:38:12 +020051 << ") is out of bounds [0, " << old_size_ << ")" << endl;
52 return false;
53 }
54
55 // Pass down the control entry.
56 if (!patch_->AddControlEntry(entry))
57 return false;
58
59 // Generate the diff stream.
60 std::vector<uint8_t> diff(entry.diff_size);
61 for (uint64_t i = 0; i < entry.diff_size; ++i) {
62 diff[i] = new_buf_[written_output_ + i] - old_buf_[old_pos_ + i];
63 }
64 if (!patch_->WriteDiffStream(diff.data(), diff.size())) {
65 LOG(ERROR) << "Writing " << diff.size() << " bytes to the diff stream"
66 << endl;
67 return false;
68 }
69
70 if (!patch_->WriteExtraStream(new_buf_ + written_output_ + entry.diff_size,
71 entry.extra_size)) {
72 LOG(ERROR) << "Writing " << entry.extra_size << " bytes to the extra stream"
73 << endl;
74 return false;
75 }
76
77 old_pos_ += entry.diff_size + entry.offset_increment;
78 written_output_ += entry.diff_size + entry.extra_size;
79
80 return true;
81}
82
83bool DiffEncoder::Close() {
84 if (written_output_ != new_size_) {
85 LOG(ERROR) << "Close() called but not all the output was written" << endl;
86 return false;
87 }
88 return patch_->Close();
89}
90
91} // namespace bsdiff