blob: 4b6abbba9e9ea76baa245db96727155336aaa1fa [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
Alex Deymodcd423b2017-09-13 20:54:24 +02009#include "bsdiff/logging.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020010
11using std::endl;
12
13namespace {
14
Alex Deymoa28e0192017-09-08 14:21:05 +020015constexpr uint8_t kMagicHeader[] = "BSDIFF40";
16
17void 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
Alex Deymo68c0e7f2017-10-02 20:38:12 +020029bool BsdiffPatchWriter::Init() {
Alex Deymo538a75d2017-09-27 15:34:59 +020030 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020031 if (!fp_) {
Alex Deymo538a75d2017-09-27 15:34:59 +020032 LOG(ERROR) << "Opening " << patch_filename_ << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020033 return false;
34 }
35 return true;
36}
37
Alex Deymo68c0e7f2017-10-02 20:38:12 +020038bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
39 return diff_stream_.Write(data, size);
40}
41
42bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
43 return extra_stream_.Write(data, size);
44}
45
Alex Deymoa28e0192017-09-08 14:21:05 +020046bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +020047 // Generate the 24 byte control entry.
48 uint8_t buf[24];
49 EncodeInt64(entry.diff_size, buf);
50 EncodeInt64(entry.extra_size, buf + 8);
51 EncodeInt64(entry.offset_increment, buf + 16);
52 if (!ctrl_stream_.Write(buf, sizeof(buf)))
53 return false;
Alex Deymoa28e0192017-09-08 14:21:05 +020054 written_output_ += entry.diff_size + entry.extra_size;
55 return true;
56}
57
58bool BsdiffPatchWriter::Close() {
59 if (!fp_) {
60 LOG(ERROR) << "File not open." << endl;
61 return false;
62 }
Alex Deymoa28e0192017-09-08 14:21:05 +020063
64 if (!ctrl_stream_.Finish() || !diff_stream_.Finish() ||
Alex Deymo68c0e7f2017-10-02 20:38:12 +020065 !extra_stream_.Finish()) {
66 LOG(ERROR) << "Finalizing compressed streams." << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020067 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020068 }
Alex Deymoa28e0192017-09-08 14:21:05 +020069
70 auto ctrl_data = ctrl_stream_.GetCompressedData();
71 auto diff_data = diff_stream_.GetCompressedData();
72 auto extra_data = extra_stream_.GetCompressedData();
73
74 if (!WriteHeader(ctrl_data.size(), diff_data.size()))
75 return false;
76
77 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
78 LOG(ERROR) << "Writing ctrl_data." << endl;
79 return false;
80 }
81 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
82 LOG(ERROR) << "Writing diff_data." << endl;
83 return false;
84 }
85 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
86 extra_data.size()) {
87 LOG(ERROR) << "Writing extra_data." << endl;
88 return false;
89 }
90 if (fclose(fp_) != 0) {
91 LOG(ERROR) << "Closing the patch file." << endl;
92 return false;
93 }
94 fp_ = nullptr;
95 return true;
96}
97
98bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) {
99 /* Header format is
100 * 0 8 "BSDIFF40"
101 * 8 8 length of bzip2ed ctrl block
102 * 16 8 length of bzip2ed diff block
103 * 24 8 length of new file
104 *
105 * File format is
106 * 0 32 Header
107 * 32 ?? Bzip2ed ctrl block
108 * ?? ?? Bzip2ed diff block
109 * ?? ?? Bzip2ed extra block
110 */
111 uint8_t header[32];
112 memcpy(header, kMagicHeader, 8);
113 EncodeInt64(ctrl_size, header + 8);
114 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200115 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200116 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
117 LOG(ERROR) << "writing to the patch file" << endl;
118 return false;
119 }
120 return true;
121}
122
123} // namespace bsdiff