blob: 7bed2ca83ec7d1211923227aba67c73f4d5eb745 [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 Xu1f1cdb22017-11-20 11:05:55 -080029BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename)
30 : patch_filename_(patch_filename), format_(BsdiffFormat::kLegacy) {
31 ctrl_stream_.reset(new BZ2Compressor());
32 diff_stream_.reset(new BZ2Compressor());
33 extra_stream_.reset(new BZ2Compressor());
34}
35
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070036BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
Tianjie Xu1f1cdb22017-11-20 11:05:55 -080037 CompressorType type,
38 int quality)
39 : patch_filename_(patch_filename), format_(BsdiffFormat::kBsdf2) {
40 if (type == CompressorType::kBZ2) {
41 ctrl_stream_.reset(new BZ2Compressor());
42 diff_stream_.reset(new BZ2Compressor());
43 extra_stream_.reset(new BZ2Compressor());
44 } else if (type == CompressorType::kBrotli) {
45 ctrl_stream_.reset(new BrotliCompressor(quality));
46 diff_stream_.reset(new BrotliCompressor(quality));
47 extra_stream_.reset(new BrotliCompressor(quality));
Tianjie Xub4cba642017-11-14 22:46:38 -080048 }
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070049}
50
Alex Deymo4dadd8b2017-10-26 16:19:33 +020051bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Tianjie Xub4cba642017-11-14 22:46:38 -080052 if (!(ctrl_stream_ && diff_stream_ && extra_stream_)) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080053 LOG(ERROR) << "uninitialized compressor stream";
Tianjie Xub4cba642017-11-14 22:46:38 -080054 return false;
55 }
56
Alex Deymo538a75d2017-09-27 15:34:59 +020057 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020058 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080059 LOG(ERROR) << "Opening " << patch_filename_;
Alex Deymoa28e0192017-09-08 14:21:05 +020060 return false;
61 }
62 return true;
63}
64
Alex Deymo68c0e7f2017-10-02 20:38:12 +020065bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070066 return diff_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020067}
68
69bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070070 return extra_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020071}
72
Alex Deymoa28e0192017-09-08 14:21:05 +020073bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +020074 // Generate the 24 byte control entry.
75 uint8_t buf[24];
76 EncodeInt64(entry.diff_size, buf);
77 EncodeInt64(entry.extra_size, buf + 8);
78 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070079 if (!ctrl_stream_->Write(buf, sizeof(buf)))
Alex Deymoa28e0192017-09-08 14:21:05 +020080 return false;
Alex Deymoa28e0192017-09-08 14:21:05 +020081 written_output_ += entry.diff_size + entry.extra_size;
82 return true;
83}
84
85bool BsdiffPatchWriter::Close() {
86 if (!fp_) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080087 LOG(ERROR) << "File not open.";
Alex Deymoa28e0192017-09-08 14:21:05 +020088 return false;
89 }
Alex Deymoa28e0192017-09-08 14:21:05 +020090
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070091 if (!ctrl_stream_->Finish() || !diff_stream_->Finish() ||
92 !extra_stream_->Finish()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -080093 LOG(ERROR) << "Finalizing compressed streams.";
Alex Deymoa28e0192017-09-08 14:21:05 +020094 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020095 }
Alex Deymoa28e0192017-09-08 14:21:05 +020096
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070097 auto ctrl_data = ctrl_stream_->GetCompressedData();
98 auto diff_data = diff_stream_->GetCompressedData();
99 auto extra_data = extra_stream_->GetCompressedData();
Alex Deymoa28e0192017-09-08 14:21:05 +0200100
101 if (!WriteHeader(ctrl_data.size(), diff_data.size()))
102 return false;
103
104 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800105 LOG(ERROR) << "Writing ctrl_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200106 return false;
107 }
108 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800109 LOG(ERROR) << "Writing diff_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200110 return false;
111 }
112 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
113 extra_data.size()) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800114 LOG(ERROR) << "Writing extra_data.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200115 return false;
116 }
117 if (fclose(fp_) != 0) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800118 LOG(ERROR) << "Closing the patch file.";
Alex Deymoa28e0192017-09-08 14:21:05 +0200119 return false;
120 }
121 fp_ = nullptr;
122 return true;
123}
124
125bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) {
126 /* Header format is
Tianjie Xub4cba642017-11-14 22:46:38 -0800127 * 0 8 magic header
128 * 8 8 length of compressed ctrl block
129 * 16 8 length of compressed diff block
Alex Deymoa28e0192017-09-08 14:21:05 +0200130 * 24 8 length of new file
131 *
132 * File format is
133 * 0 32 Header
Tianjie Xub4cba642017-11-14 22:46:38 -0800134 * 32 ?? compressed ctrl block
135 * ?? ?? compressed diff block
136 * ?? ?? compressed extra block
Alex Deymoa28e0192017-09-08 14:21:05 +0200137 */
138 uint8_t header[32];
Tianjie Xub4cba642017-11-14 22:46:38 -0800139 if (format_ == BsdiffFormat::kLegacy) {
140 // The magic header is "BSDIFF40" for legacy format.
141 memcpy(header, kLegacyMagicHeader, 8);
142 } else if (format_ == BsdiffFormat::kBsdf2) {
143 // The magic header for BSDF2 format:
144 // 0 5 BSDF2
145 // 5 1 compressed type for control stream
146 // 6 1 compressed type for diff stream
147 // 7 1 compressed type for extra stream
148 memcpy(header, kBSDF2MagicHeader, 5);
149 header[5] = static_cast<uint8_t>(ctrl_stream_->Type());
150 header[6] = static_cast<uint8_t>(diff_stream_->Type());
151 header[7] = static_cast<uint8_t>(extra_stream_->Type());
152 } else {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800153 LOG(ERROR) << "Unsupported bsdiff format.";
Tianjie Xub4cba642017-11-14 22:46:38 -0800154 return false;
155 }
156
Alex Deymoa28e0192017-09-08 14:21:05 +0200157 EncodeInt64(ctrl_size, header + 8);
158 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200159 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200160 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
Tianjie Xu18480eb2017-11-29 16:21:43 -0800161 LOG(ERROR) << "writing to the patch file";
Alex Deymoa28e0192017-09-08 14:21:05 +0200162 return false;
163 }
164 return true;
165}
166
167} // namespace bsdiff