Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 1 | // 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 Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 5 | #include "bsdiff/patch_writer.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 6 | |
| 7 | #include <string.h> |
| 8 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 9 | #include "bsdiff/brotli_compressor.h" |
| 10 | #include "bsdiff/bz2_compressor.h" |
Tianjie Xu | 4d10c3e | 2017-10-26 14:02:06 -0700 | [diff] [blame^] | 11 | #include "bsdiff/constants.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 12 | #include "bsdiff/control_entry.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 13 | #include "bsdiff/logging.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 14 | |
| 15 | using std::endl; |
| 16 | |
| 17 | namespace { |
| 18 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 19 | void EncodeInt64(int64_t x, uint8_t* buf) { |
| 20 | uint64_t y = x < 0 ? (1ULL << 63ULL) - x : x; |
| 21 | for (int i = 0; i < 8; ++i) { |
| 22 | buf[i] = y & 0xff; |
| 23 | y /= 256; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | } // namespace |
| 28 | |
| 29 | namespace bsdiff { |
| 30 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 31 | BsdiffPatchWriter::BsdiffPatchWriter(const std::string& patch_filename, |
| 32 | CompressorType type) |
| 33 | : patch_filename_(patch_filename) { |
| 34 | ctrl_stream_ = CreateCompressor(type); |
| 35 | diff_stream_ = CreateCompressor(type); |
| 36 | extra_stream_ = CreateCompressor(type); |
| 37 | } |
| 38 | |
Alex Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 39 | bool BsdiffPatchWriter::Init(size_t /* new_size */) { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 40 | fp_ = fopen(patch_filename_.c_str(), "w"); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 41 | if (!fp_) { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 42 | LOG(ERROR) << "Opening " << patch_filename_ << endl; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 48 | bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) { |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 49 | return diff_stream_->Write(data, size); |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) { |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 53 | return extra_stream_->Write(data, size); |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 56 | bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) { |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 57 | // Generate the 24 byte control entry. |
| 58 | uint8_t buf[24]; |
| 59 | EncodeInt64(entry.diff_size, buf); |
| 60 | EncodeInt64(entry.extra_size, buf + 8); |
| 61 | EncodeInt64(entry.offset_increment, buf + 16); |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 62 | if (!ctrl_stream_->Write(buf, sizeof(buf))) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 63 | return false; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 64 | written_output_ += entry.diff_size + entry.extra_size; |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | bool BsdiffPatchWriter::Close() { |
| 69 | if (!fp_) { |
| 70 | LOG(ERROR) << "File not open." << endl; |
| 71 | return false; |
| 72 | } |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 73 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 74 | if (!ctrl_stream_->Finish() || !diff_stream_->Finish() || |
| 75 | !extra_stream_->Finish()) { |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 76 | LOG(ERROR) << "Finalizing compressed streams." << endl; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 77 | return false; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 78 | } |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 79 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 80 | auto ctrl_data = ctrl_stream_->GetCompressedData(); |
| 81 | auto diff_data = diff_stream_->GetCompressedData(); |
| 82 | auto extra_data = extra_stream_->GetCompressedData(); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 83 | |
| 84 | if (!WriteHeader(ctrl_data.size(), diff_data.size())) |
| 85 | return false; |
| 86 | |
| 87 | if (fwrite(ctrl_data.data(), 1, ctrl_data.size(), fp_) != ctrl_data.size()) { |
| 88 | LOG(ERROR) << "Writing ctrl_data." << endl; |
| 89 | return false; |
| 90 | } |
| 91 | if (fwrite(diff_data.data(), 1, diff_data.size(), fp_) != diff_data.size()) { |
| 92 | LOG(ERROR) << "Writing diff_data." << endl; |
| 93 | return false; |
| 94 | } |
| 95 | if (fwrite(extra_data.data(), 1, extra_data.size(), fp_) != |
| 96 | extra_data.size()) { |
| 97 | LOG(ERROR) << "Writing extra_data." << endl; |
| 98 | return false; |
| 99 | } |
| 100 | if (fclose(fp_) != 0) { |
| 101 | LOG(ERROR) << "Closing the patch file." << endl; |
| 102 | return false; |
| 103 | } |
| 104 | fp_ = nullptr; |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool BsdiffPatchWriter::WriteHeader(uint64_t ctrl_size, uint64_t diff_size) { |
| 109 | /* Header format is |
| 110 | * 0 8 "BSDIFF40" |
| 111 | * 8 8 length of bzip2ed ctrl block |
| 112 | * 16 8 length of bzip2ed diff block |
| 113 | * 24 8 length of new file |
| 114 | * |
| 115 | * File format is |
| 116 | * 0 32 Header |
| 117 | * 32 ?? Bzip2ed ctrl block |
| 118 | * ?? ?? Bzip2ed diff block |
| 119 | * ?? ?? Bzip2ed extra block |
| 120 | */ |
| 121 | uint8_t header[32]; |
| 122 | memcpy(header, kMagicHeader, 8); |
| 123 | EncodeInt64(ctrl_size, header + 8); |
| 124 | EncodeInt64(diff_size, header + 16); |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 125 | EncodeInt64(written_output_, header + 24); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 126 | if (fwrite(header, sizeof(header), 1, fp_) != 1) { |
| 127 | LOG(ERROR) << "writing to the patch file" << endl; |
| 128 | return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | } // namespace bsdiff |