blob: 9c906c615602b1e5de1c66022e6b4c056d5f2836 [file] [log] [blame]
Alex Deymoa28e0192017-09-08 14:21:05 +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
Alex Deymodcd423b2017-09-13 20:54:24 +02005#include "bsdiff/patch_writer.h"
Alex Deymoa28e0192017-09-08 14:21:05 +02006
7#include <string.h>
8
Tianjie Xu1c26e2e2017-10-26 17:19:41 -07009#include "bsdiff/brotli_compressor.h"
10#include "bsdiff/bz2_compressor.h"
Tianjie Xu4d10c3e2017-10-26 14:02:06 -070011#include "bsdiff/constants.h"
Tianjie Xu65288122017-10-13 15:10:58 -070012#include "bsdiff/control_entry.h"
Alex Deymodcd423b2017-09-13 20:54:24 +020013#include "bsdiff/logging.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020014
Alex Deymoa28e0192017-09-08 14:21:05 +020015namespace {
16
Alex Deymoa28e0192017-09-08 14:21:05 +020017void EncodeInt64(int64_t x, uint8_t* buf) {
18 uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
19 for (int i = 0; i < 8; ++i) {
20 buf[i] = y & 0xff;
21 y /= 256;
22 }
23}
24
25} // namespace
26
27namespace bsdiff {
28
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070029BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
Tianjie Xub4cba642017-11-14 22:46:38 -080030 BsdiffFormat format)
31 : patch_filename_(patch_filename), format_(format) {
32 if (format_ == BsdiffFormat::kLegacy) {
33 ctrl_stream_ = CreateCompressor(CompressorType::kBZ2);
34 diff_stream_ = CreateCompressor(CompressorType::kBZ2);
35 extra_stream_ = CreateCompressor(CompressorType::kBZ2);
36 } else {
37 // TODO(xunchang) set different compression method according to the
38 // compressed size for these streams.
39 ctrl_stream_ = CreateCompressor(CompressorType::kBrotli);
40 diff_stream_ = CreateCompressor(CompressorType::kBrotli);
41 extra_stream_ = CreateCompressor(CompressorType::kBrotli);
42 }
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070043}
44
Alex Deymo4dadd8b2017-10-26 16:19:33 +020045bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Tianjie Xub4cba642017-11-14 22:46:38 -080046 if (!(ctrl_stream_ && diff_stream_ && extra_stream_)) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080047 LOG(ERROR) << "uninitialized compressor stream";
Tianjie Xub4cba642017-11-14 22:46:38 -080048 return false;
49 }
50
Alex Deymo538a75d2017-09-27 15:34:59 +020051 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020052 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080053 LOG(ERROR) << "Opening " << patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020054 return false;
55 }
56 return true;
57}
58
Alex Deymo68c0e7f2017-10-02 20:38:12 +020059bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070060 return diff_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020061}
62
63bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070064 return extra_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020065}
66
Alex Deymoa28e0192017-09-08 14:21:05 +020067bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +020068 // Generate the 24 byte control entry.
69 uint8_t buf[24];
70 EncodeInt64(entry.diff_size, buf);
71 EncodeInt64(entry.extra_size, buf + 8);
72 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070073 if (!ctrl_stream_->Write(buf, sizeof(buf)))
Alex Deymoa28e0192017-09-08 14:21:05 +020074 return false;
Alex Deymoa28e0192017-09-08 14:21:05 +020075 written_output_ += entry.diff_size + entry.extra_size;
76 return true;
77}
78
79bool BsdiffPatchWriter::Close() {
80 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080081 LOG(ERROR) << "File not open.";
Alex Deymoa28e0192017-09-08 14:21:05 +020082 return false;
83 }
Alex Deymoa28e0192017-09-08 14:21:05 +020084
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070085 if (!ctrl_stream_->Finish() || !diff_stream_->Finish() ||
86 !extra_stream_->Finish()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080087 LOG(ERROR) << "Finalizing compressed streams.";
Alex Deymoa28e0192017-09-08 14:21:05 +020088 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020089 }
Alex Deymoa28e0192017-09-08 14:21:05 +020090
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070091 auto ctrl_data = ctrl_stream_->GetCompressedData();
92 auto diff_data = diff_stream_->GetCompressedData();
93 auto extra_data = extra_stream_->GetCompressedData();
Alex Deymoa28e0192017-09-08 14:21:05 +020094
95 if (!WriteHeader(ctrl_data.size(), diff_data.size()))
96 return false;
97
98 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080099 LOG(ERROR) << "Writing ctrl_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200100 return false;
101 }
102 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800103 LOG(ERROR) << "Writing diff_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200104 return false;
105 }
106 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
107 extra_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800108 LOG(ERROR) << "Writing extra_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200109 return false;
110 }
111 if (fclose(fp_) != 0) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800112 LOG(ERROR) << "Closing the patch file.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200113 return false;
114 }
115 fp_ = nullptr;
116 return true;
117}
118
119bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) {
120 /* Header format is
Tianjie Xub4cba642017-11-14 22:46:38 -0800121 * 0 8 magic header
122 * 8 8 length of compressed ctrl block
123 * 16 8 length of compressed diff block
Alex Deymoa28e0192017-09-08 14:21:05 +0200124 * 24 8 length of new file
125 *
126 * File format is
127 * 0 32 Header
Tianjie Xub4cba642017-11-14 22:46:38 -0800128 * 32 ?? compressed ctrl block
129 * ?? ?? compressed diff block
130 * ?? ?? compressed extra block
Alex Deymoa28e0192017-09-08 14:21:05 +0200131 */
132 uint8_t header[32];
Tianjie Xub4cba642017-11-14 22:46:38 -0800133 if (format_ == BsdiffFormat::kLegacy) {
134 // The magic header is "BSDIFF40" for legacy format.
135 memcpy(header, kLegacyMagicHeader, 8);
136 } else if (format_ == BsdiffFormat::kBsdf2) {
137 // The magic header for BSDF2 format:
138 // 0 5 BSDF2
139 // 5 1 compressed type for control stream
140 // 6 1 compressed type for diff stream
141 // 7 1 compressed type for extra stream
142 memcpy(header, kBSDF2MagicHeader, 5);
143 header[5] = static_cast<uint8_t>(ctrl_stream_->Type());
144 header[6] = static_cast<uint8_t>(diff_stream_->Type());
145 header[7] = static_cast<uint8_t>(extra_stream_->Type());
146 } else {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800147 LOG(ERROR) << "Unsupported bsdiff format.";
Tianjie Xub4cba642017-11-14 22:46:38 -0800148 return false;
149 }
150
Alex Deymoa28e0192017-09-08 14:21:05 +0200151 EncodeInt64(ctrl_size, header + 8);
152 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200153 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200154 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800155 LOG(ERROR) << "writing to the patch file";
Alex Deymoa28e0192017-09-08 14:21:05 +0200156 return false;
157 }
158 return true;
159}
160
161} // namespace bsdiff