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 | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 9 | #include "bsdiff/control_entry.h" |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 10 | #include "bsdiff/logging.h" |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 11 | |
| 12 | using std::endl; |
| 13 | |
| 14 | namespace { |
| 15 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 16 | constexpr uint8_t kMagicHeader[] = "BSDIFF40"; |
| 17 | |
| 18 | void 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 | |
| 28 | namespace bsdiff { |
| 29 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 30 | bool BsdiffPatchWriter::Init() { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 31 | fp_ = fopen(patch_filename_.c_str(), "w"); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 32 | if (!fp_) { |
Alex Deymo | 538a75d | 2017-09-27 15:34:59 +0200 | [diff] [blame] | 33 | LOG(ERROR) << "Opening " << patch_filename_ << endl; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | return true; |
| 37 | } |
| 38 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 39 | bool BsdiffPatchWriter::WriteDiffStream(const uint8_t* data, size_t size) { |
| 40 | return diff_stream_.Write(data, size); |
| 41 | } |
| 42 | |
| 43 | bool BsdiffPatchWriter::WriteExtraStream(const uint8_t* data, size_t size) { |
| 44 | return extra_stream_.Write(data, size); |
| 45 | } |
| 46 | |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 47 | bool BsdiffPatchWriter::AddControlEntry(const ControlEntry& entry) { |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 48 | // 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 Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 55 | written_output_ += entry.diff_size + entry.extra_size; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool BsdiffPatchWriter::Close() { |
| 60 | if (!fp_) { |
| 61 | LOG(ERROR) << "File not open." << endl; |
| 62 | return false; |
| 63 | } |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 64 | |
| 65 | if (!ctrl_stream_.Finish() || !diff_stream_.Finish() || |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 66 | !extra_stream_.Finish()) { |
| 67 | LOG(ERROR) << "Finalizing compressed streams." << endl; |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 68 | return false; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 69 | } |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 70 | |
| 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 | |
| 99 | bool 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 Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 116 | EncodeInt64(written_output_, header + 24); |
Alex Deymo | a28e019 | 2017-09-08 14:21:05 +0200 | [diff] [blame] | 117 | 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 |