summaryrefslogtreecommitdiff
path: root/endsley_patch_writer.cc
blob: 0e8602c311e094e91a6b6ed9e49044ca4db026ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "bsdiff/endsley_patch_writer.h"

#include <string.h>

#include <algorithm>

#include "bsdiff/brotli_compressor.h"
#include "bsdiff/bz2_compressor.h"
#include "bsdiff/logging.h"

namespace {

constexpr uint8_t kEndsleyMagicHeader[] = "ENDSLEY/BSDIFF43";

void EncodeInt64(int64_t x, uint8_t* buf) {
  uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
  for (int i = 0; i < 8; ++i) {
    buf[i] = y & 0xff;
    y /= 256;
  }
}

// The minimum size that we would consider flushing out.
constexpr size_t kMinimumFlushSize = 1024 * 1024;  // 1 MiB

}  // namespace

namespace bsdiff {

bool EndsleyPatchWriter::Init(size_t new_size) {
  switch (compressor_type_) {
    case CompressorType::kNoCompression:
      // The patch is uncompressed and it will need exactly:
      //   new_size + 24 * len(control_entries) + sizeof(header)
      // We don't know the length of the control entries yet, but we can reserve
      // enough space to hold at least |new_size|.
      patch_->clear();
      patch_->reserve(new_size);
      break;
    case CompressorType::kBrotli:
      compressor_.reset(new BrotliCompressor(brotli_quality_));
      if (!compressor_) {
        LOG(ERROR) << "Error creating brotli compressor.";
        return false;
      }
      break;
    case CompressorType::kBZ2:
      compressor_.reset(new BZ2Compressor());
      if (!compressor_) {
        LOG(ERROR) << "Error creating BZ2 compressor.";
        return false;
      }
      break;
  }

  // Header is the magic followed by the new length.
  uint8_t header[24];
  memcpy(header, kEndsleyMagicHeader, 16);
  EncodeInt64(new_size, header + 16);
  EmitBuffer(header, sizeof(header));
  return true;
}

bool EndsleyPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
  if (!size)
    return true;
  // Speed-up the common case where the diff stream data is added right after
  // the control entry that refers to it.
  if (control_.empty() && pending_diff_ >= size) {
    pending_diff_ -= size;
    EmitBuffer(data, size);
    return true;
  }

  diff_data_.insert(diff_data_.end(), data, data + size);
  return true;
}

bool EndsleyPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
  if (!size)
    return true;
  // Speed-up the common case where the extra stream data is added right after
  // the diff stream data and the control entry that refers to it. Note that
  // the diff data comes first so we need to make sure it is all out.
  if (control_.empty() && !pending_diff_ && pending_extra_ >= size) {
    pending_extra_ -= size;
    EmitBuffer(data, size);
    return true;
  }

  extra_data_.insert(extra_data_.end(), data, data + size);
  return true;
}

bool EndsleyPatchWriter::AddControlEntry(const ControlEntry& entry) {
  // Speed-up the common case where the control entry is added when there's
  // nothing else pending.
  if (control_.empty() && diff_data_.empty() && extra_data_.empty() &&
      !pending_diff_ && !pending_extra_) {
    pending_diff_ = entry.diff_size;
    pending_extra_ = entry.extra_size;
    EmitControlEntry(entry);
    return true;
  }

  control_.push_back(entry);
  pending_control_data_ += entry.diff_size + entry.extra_size;

  // Check whether it is worth Flushing the entries now that the we have more
  // control entries. We need control entries to write enough output data, and
  // we need that output data to be at least 50% of the available diff and extra
  // data. This last requirement is to reduce the overhead of removing the
  // flushed data.
  if (pending_control_data_ > kMinimumFlushSize &&
      (diff_data_.size() + extra_data_.size()) / 2 <= pending_control_data_) {
    Flush();
  }

  return true;
}

bool EndsleyPatchWriter::Close() {
  // Flush any pending data.
  Flush();

  if (pending_diff_ || pending_extra_ || !control_.empty()) {
    LOG(ERROR) << "Insufficient data sent to diff/extra streams";
    return false;
  }

  if (!diff_data_.empty() || !extra_data_.empty()) {
    LOG(ERROR) << "Pending data to diff/extra not flushed out on Close()";
    return false;
  }

  if (compressor_) {
    if (!compressor_->Finish())
      return false;
    *patch_ = compressor_->GetCompressedData();
  }

  return true;
}

void EndsleyPatchWriter::EmitControlEntry(const ControlEntry& entry) {
  // Generate the 24 byte control entry.
  uint8_t buf[24];
  EncodeInt64(entry.diff_size, buf);
  EncodeInt64(entry.extra_size, buf + 8);
  EncodeInt64(entry.offset_increment, buf + 16);
  EmitBuffer(buf, sizeof(buf));
}

void EndsleyPatchWriter::EmitBuffer(const uint8_t* data, size_t size) {
  if (compressor_) {
    compressor_->Write(data, size);
  } else {
    patch_->insert(patch_->end(), data, data + size);
  }
}

void EndsleyPatchWriter::Flush() {
  size_t used_diff = 0;
  size_t used_extra = 0;
  size_t used_control = 0;
  do {
    if (!pending_diff_ && !pending_extra_ && used_control < control_.size()) {
      // We can emit a control entry in these conditions.
      const ControlEntry& entry = control_[used_control];
      used_control++;

      pending_diff_ = entry.diff_size;
      pending_extra_ = entry.extra_size;
      pending_control_data_ -= entry.extra_size + entry.diff_size;
      EmitControlEntry(entry);
    }

    if (pending_diff_) {
      size_t diff_size = std::min(diff_data_.size() - used_diff, pending_diff_);
      EmitBuffer(diff_data_.data() + used_diff, diff_size);
      pending_diff_ -= diff_size;
      used_diff += diff_size;
    }

    if (!pending_diff_ && pending_extra_) {
      size_t extra_size =
          std::min(extra_data_.size() - used_extra, pending_extra_);
      EmitBuffer(extra_data_.data() + used_extra, extra_size);
      pending_extra_ -= extra_size;
      used_extra += extra_size;
    }
  } while (!pending_diff_ && !pending_extra_ && used_control < control_.size());

  if (used_diff)
    diff_data_.erase(diff_data_.begin(), diff_data_.begin() + used_diff);
  if (used_extra)
    extra_data_.erase(extra_data_.begin(), extra_data_.begin() + used_extra);
  if (used_control)
    control_.erase(control_.begin(), control_.begin() + used_control);
}

}  // namespace bsdiff