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 | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 11 | #include "bsdiff/control_entry.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 12 | #include "bsdiff/logging.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 13 | |
| 14 | using std::endl; |
| 15 | |
| 16 | namespace { |
| 17 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 18 | constexpr uint8_t kMagicHeader[] = "BSDIFF40"; |
| 19 | |
| 20 | void 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 | |
| 30 | namespace bsdiff { |
| 31 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 32 | BsdiffPatchWriter::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 Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 40 | bool BsdiffPatchWriter::Init(size_t /* new_size */) { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 41 | fp_ = fopen(patch_filename_.c_str(), "w"); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 42 | if (!fp_) { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 43 | LOG(ERROR) << "Opening " << patch_filename_ << endl; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 49 | bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) { |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 50 | return diff_stream_->Write(data, size); |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) { |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 54 | return extra_stream_->Write(data, size); |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 57 | bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) { |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 58 | // 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 Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 63 | if (!ctrl_stream_->Write(buf, sizeof(buf))) |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 64 | return false; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 65 | written_output_ += entry.diff_size + entry.extra_size; |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | bool BsdiffPatchWriter::Close() { |
| 70 | if (!fp_) { |
| 71 | LOG(ERROR) << "File not open." << endl; |
| 72 | return false; |
| 73 | } |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 74 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 75 | if (!ctrl_stream_->Finish() || !diff_stream_->Finish() || |
| 76 | !extra_stream_->Finish()) { |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 77 | LOG(ERROR) << "Finalizing compressed streams." << endl; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 78 | return false; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 79 | } |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 80 | |
Tianjie Xu | 1c26e2e | 2017-10-26 17:19:41 -0700 | [diff] [blame] | 81 | auto ctrl_data = ctrl_stream_->GetCompressedData(); |
| 82 | auto diff_data = diff_stream_->GetCompressedData(); |
| 83 | auto extra_data = extra_stream_->GetCompressedData(); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 84 | |
| 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 | |
| 109 | bool 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 Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 126 | EncodeInt64(written_output_, header + 24); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 127 | 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 |