blob: ea38c37e202982e3ae9fa4311ca18a354505f554 [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 Xu65288122017-10-13 15:10:58 -070011#include "bsdiff/control_entry.h"
Alex Deymodcd423b2017-09-13 20:54:24 +020012#include "bsdiff/logging.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020013
14using std::endl;
15
16namespace {
17
Alex Deymoa28e0192017-09-08 14:21:05 +020018constexpr uint8_t kMagicHeader[] = "BSDIFF40";
19
20void EncodeInt64(int64_t x, uint8_t* buf) {
21 uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
22 for (int i = 0; i < 8; ++i) {
23 buf[i] = y & 0xff;
24 y /= 256;
25 }
26}
27
28} // namespace
29
30namespace bsdiff {
31
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070032BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename,
33 CompressorType type)
34 : patch_filename_(patch_filename) {
35 ctrl_stream_ = CreateCompressor(type);
36 diff_stream_ = CreateCompressor(type);
37 extra_stream_ = CreateCompressor(type);
38}
39
Alex Deymo4dadd8b2017-10-26 16:19:33 +020040bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Alex Deymo538a75d2017-09-27 15:34:59 +020041 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020042 if (!fp_) {
Alex Deymo538a75d2017-09-27 15:34:59 +020043 LOG(ERROR) << "Opening " << patch_filename_ << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020044 return false;
45 }
46 return true;
47}
48
Alex Deymo68c0e7f2017-10-02 20:38:12 +020049bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070050 return diff_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020051}
52
53bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070054 return extra_stream_->Write(data, size);
Alex Deymo68c0e7f2017-10-02 20:38:12 +020055}
56
Alex Deymoa28e0192017-09-08 14:21:05 +020057bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +020058 // Generate the 24 byte control entry.
59 uint8_t buf[24];
60 EncodeInt64(entry.diff_size, buf);
61 EncodeInt64(entry.extra_size, buf + 8);
62 EncodeInt64(entry.offset_increment, buf + 16);
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070063 if (!ctrl_stream_->Write(buf, sizeof(buf)))
Alex Deymoa28e0192017-09-08 14:21:05 +020064 return false;
Alex Deymoa28e0192017-09-08 14:21:05 +020065 written_output_ += entry.diff_size + entry.extra_size;
66 return true;
67}
68
69bool BsdiffPatchWriter::Close() {
70 if (!fp_) {
71 LOG(ERROR) << "File not open." << endl;
72 return false;
73 }
Alex Deymoa28e0192017-09-08 14:21:05 +020074
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070075 if (!ctrl_stream_->Finish() || !diff_stream_->Finish() ||
76 !extra_stream_->Finish()) {
Alex Deymo68c0e7f2017-10-02 20:38:12 +020077 LOG(ERROR) << "Finalizing compressed streams." << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020078 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020079 }
Alex Deymoa28e0192017-09-08 14:21:05 +020080
Tianjie Xu1c26e2e2017-10-26 17:19:41 -070081 auto ctrl_data = ctrl_stream_->GetCompressedData();
82 auto diff_data = diff_stream_->GetCompressedData();
83 auto extra_data = extra_stream_->GetCompressedData();
Alex Deymoa28e0192017-09-08 14:21:05 +020084
85 if (!WriteHeader(ctrl_data.size(), diff_data.size()))
86 return false;
87
88 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
89 LOG(ERROR) << "Writing ctrl_data." << endl;
90 return false;
91 }
92 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
93 LOG(ERROR) << "Writing diff_data." << endl;
94 return false;
95 }
96 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
97 extra_data.size()) {
98 LOG(ERROR) << "Writing extra_data." << endl;
99 return false;
100 }
101 if (fclose(fp_) != 0) {
102 LOG(ERROR) << "Closing the patch file." << endl;
103 return false;
104 }
105 fp_ = nullptr;
106 return true;
107}
108
109bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) {
110 /* Header format is
111 * 0 8 "BSDIFF40"
112 * 8 8 length of bzip2ed ctrl block
113 * 16 8 length of bzip2ed diff block
114 * 24 8 length of new file
115 *
116 * File format is
117 * 0 32 Header
118 * 32 ?? Bzip2ed ctrl block
119 * ?? ?? Bzip2ed diff block
120 * ?? ?? Bzip2ed extra block
121 */
122 uint8_t header[32];
123 memcpy(header, kMagicHeader, 8);
124 EncodeInt64(ctrl_size, header + 8);
125 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200126 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200127 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
128 LOG(ERROR) << "writing to the patch file" << endl;
129 return false;
130 }
131 return true;
132}
133
134} // namespace bsdiff