blob: 06f11ad63dadf6ce3e247b9654d8de6c7a42df84 [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 Xu65288122017-10-13 15:10:58 -07009#include "bsdiff/control_entry.h"
Alex Deymodcd423b2017-09-13 20:54:24 +020010#include "bsdiff/logging.h"
Alex Deymoa28e0192017-09-08 14:21:05 +020011
12using std::endl;
13
14namespace {
15
Alex Deymoa28e0192017-09-08 14:21:05 +020016constexpr uint8_t kMagicHeader[] = "BSDIFF40";
17
18void EncodeInt64(int64_t x, uint8_t* buf) {
19 uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x;
20 for (int i = 0; i < 8; ++i) {
21 buf[i] = y & 0xff;
22 y /= 256;
23 }
24}
25
26} // namespace
27
28namespace bsdiff {
29
Alex Deymo4dadd8b2017-10-26 16:19:33 +020030bool BsdiffPatchWriter::Init(size_t /* new_size */) {
Alex Deymo538a75d2017-09-27 15:34:59 +020031 fp_ = fopen(patch_filename_.c_str(), "w");
Alex Deymoa28e0192017-09-08 14:21:05 +020032 if (!fp_) {
Alex Deymo538a75d2017-09-27 15:34:59 +020033 LOG(ERROR) << "Opening " << patch_filename_ << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020034 return false;
35 }
36 return true;
37}
38
Alex Deymo68c0e7f2017-10-02 20:38:12 +020039bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) {
40 return diff_stream_.Write(data, size);
41}
42
43bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) {
44 return extra_stream_.Write(data, size);
45}
46
Alex Deymoa28e0192017-09-08 14:21:05 +020047bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) {
Alex Deymoa28e0192017-09-08 14:21:05 +020048 // Generate the 24 byte control entry.
49 uint8_t buf[24];
50 EncodeInt64(entry.diff_size, buf);
51 EncodeInt64(entry.extra_size, buf + 8);
52 EncodeInt64(entry.offset_increment, buf + 16);
53 if (!ctrl_stream_.Write(buf, sizeof(buf)))
54 return false;
Alex Deymoa28e0192017-09-08 14:21:05 +020055 written_output_ += entry.diff_size + entry.extra_size;
56 return true;
57}
58
59bool BsdiffPatchWriter::Close() {
60 if (!fp_) {
61 LOG(ERROR) << "File not open." << endl;
62 return false;
63 }
Alex Deymoa28e0192017-09-08 14:21:05 +020064
65 if (!ctrl_stream_.Finish() || !diff_stream_.Finish() ||
Alex Deymo68c0e7f2017-10-02 20:38:12 +020066 !extra_stream_.Finish()) {
67 LOG(ERROR) << "Finalizing compressed streams." << endl;
Alex Deymoa28e0192017-09-08 14:21:05 +020068 return false;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020069 }
Alex Deymoa28e0192017-09-08 14:21:05 +020070
71 auto ctrl_data = ctrl_stream_.GetCompressedData();
72 auto diff_data = diff_stream_.GetCompressedData();
73 auto extra_data = extra_stream_.GetCompressedData();
74
75 if (!WriteHeader(ctrl_data.size(), diff_data.size()))
76 return false;
77
78 if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) {
79 LOG(ERROR) << "Writing ctrl_data." << endl;
80 return false;
81 }
82 if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) {
83 LOG(ERROR) << "Writing diff_data." << endl;
84 return false;
85 }
86 if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) !=
87 extra_data.size()) {
88 LOG(ERROR) << "Writing extra_data." << endl;
89 return false;
90 }
91 if (fclose(fp_) != 0) {
92 LOG(ERROR) << "Closing the patch file." << endl;
93 return false;
94 }
95 fp_ = nullptr;
96 return true;
97}
98
99bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) {
100 /* Header format is
101 * 0 8 "BSDIFF40"
102 * 8 8 length of bzip2ed ctrl block
103 * 16 8 length of bzip2ed diff block
104 * 24 8 length of new file
105 *
106 * File format is
107 * 0 32 Header
108 * 32 ?? Bzip2ed ctrl block
109 * ?? ?? Bzip2ed diff block
110 * ?? ?? Bzip2ed extra block
111 */
112 uint8_t header[32];
113 memcpy(header, kMagicHeader, 8);
114 EncodeInt64(ctrl_size, header + 8);
115 EncodeInt64(diff_size, header + 16);
Alex Deymo68c0e7f2017-10-02 20:38:12 +0200116 EncodeInt64(written_output_, header + 24);
Alex Deymoa28e0192017-09-08 14:21:05 +0200117 if (fwrite(header, sizeof(header), 1, fp_) != 1) {
118 LOG(ERROR) << "writing to the patch file" << endl;
119 return false;
120 }
121 return true;
122}
123
124} // namespace bsdiff