Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [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 | |
| 5 | #include "bsdiff/patch_reader.h" |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include <limits> |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 10 | #include <vector> |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 11 | |
Tianjie Xu | 4d10c3e | 2017-10-26 14:02:06 -0700 | [diff] [blame] | 12 | #include "bsdiff/brotli_decompressor.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 13 | #include "bsdiff/bspatch.h" |
| 14 | #include "bsdiff/bz2_decompressor.h" |
Tianjie Xu | 4d10c3e | 2017-10-26 14:02:06 -0700 | [diff] [blame] | 15 | #include "bsdiff/constants.h" |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 16 | #include "bsdiff/logging.h" |
| 17 | #include "bsdiff/utils.h" |
| 18 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 19 | namespace bsdiff { |
| 20 | |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 21 | bool BsdiffPatchReader::Init(const uint8_t* patch_data, size_t patch_size) { |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 22 | // File format: |
| 23 | // 0 8 magic header |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 24 | // 8 8 X |
| 25 | // 16 8 Y |
| 26 | // 24 8 new_file_size |
| 27 | // 32 X compressed control block |
| 28 | // 32+X Y compressed diff block |
| 29 | // 32+X+Y ??? compressed extra block |
| 30 | // with control block a set of triples (x,y,z) meaning "add x bytes |
| 31 | // from oldfile to x bytes from the diff block; copy y bytes from the |
| 32 | // extra block; seek forwards in oldfile by z bytes". |
| 33 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 34 | if (patch_size < 32) { |
Kelvin Zhang | 67386a2 | 2021-12-20 15:18:02 -0800 | [diff] [blame] | 35 | LOG(ERROR) << "Too small to be a bspatch. " << patch_size; |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 36 | return false; |
| 37 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 38 | // Check for appropriate magic. |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 39 | std::vector<CompressorType> compression_type; |
| 40 | if (memcmp(patch_data, kLegacyMagicHeader, 8) == 0) { |
| 41 | // The magic header is "BSDIFF40" for legacy format. |
| 42 | compression_type = {CompressorType::kBZ2, CompressorType::kBZ2, |
| 43 | CompressorType::kBZ2}; |
| 44 | } else if (memcmp(patch_data, kBSDF2MagicHeader, 5) == 0) { |
| 45 | // The magic header for BSDF2 format: |
| 46 | // 0 5 BSDF2 |
| 47 | // 5 1 compressed type for control stream |
| 48 | // 6 1 compressed type for diff stream |
| 49 | // 7 1 compressed type for extra stream |
| 50 | for (size_t i = 5; i < 8; i++) { |
| 51 | uint8_t type = patch_data[i]; |
| 52 | switch (type) { |
| 53 | case static_cast<uint8_t>(CompressorType::kBZ2): |
| 54 | compression_type.push_back(CompressorType::kBZ2); |
| 55 | break; |
| 56 | case static_cast<uint8_t>(CompressorType::kBrotli): |
| 57 | compression_type.push_back(CompressorType::kBrotli); |
| 58 | break; |
| 59 | default: |
| 60 | LOG(ERROR) << "Unsupported compression type: " |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 61 | << static_cast<int>(type); |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | } else { |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 66 | LOG(ERROR) << "Not a bsdiff patch."; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Read lengths from header. |
| 71 | int64_t ctrl_len = ParseInt64(patch_data + 8); |
| 72 | int64_t diff_len = ParseInt64(patch_data + 16); |
| 73 | int64_t signed_newsize = ParseInt64(patch_data + 24); |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 74 | // We already checked that the patch_size is at least 32 bytes. |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 75 | if ((ctrl_len < 0) || (diff_len < 0) || (signed_newsize < 0) || |
Alex Deymo | 7c4bb57 | 2018-03-19 20:25:08 +0100 | [diff] [blame] | 76 | (static_cast<int64_t>(patch_size) - 32 < ctrl_len) || |
| 77 | (static_cast<int64_t>(patch_size) - 32 - ctrl_len < diff_len)) { |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 78 | LOG(ERROR) << "Corrupt patch. ctrl_len: " << ctrl_len |
| 79 | << ", data_len: " << diff_len |
| 80 | << ", new_file_size: " << signed_newsize |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 81 | << ", patch_size: " << patch_size; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | new_file_size_ = signed_newsize; |
| 85 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 86 | ctrl_stream_ = CreateDecompressor(compression_type[0]); |
| 87 | diff_stream_ = CreateDecompressor(compression_type[1]); |
| 88 | extra_stream_ = CreateDecompressor(compression_type[2]); |
| 89 | if (!(ctrl_stream_ && diff_stream_ && extra_stream_)) { |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 90 | LOG(ERROR) << "uninitialized decompressor stream"; |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 91 | return false; |
| 92 | } |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 93 | |
Tianjie Xu | b4cba64 | 2017-11-14 22:46:38 -0800 | [diff] [blame] | 94 | size_t offset = 32; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 95 | if (!ctrl_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset, |
| 96 | ctrl_len)) { |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 97 | LOG(ERROR) << "Failed to init ctrl stream, ctrl_len: " << ctrl_len; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | |
| 101 | offset += ctrl_len; |
| 102 | if (!diff_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset, |
| 103 | diff_len)) { |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 104 | LOG(ERROR) << "Failed to init ctrl stream, diff_len: " << diff_len; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
| 108 | offset += diff_len; |
| 109 | if (!extra_stream_->SetInputData(const_cast<uint8_t*>(patch_data) + offset, |
| 110 | patch_size - offset)) { |
| 111 | LOG(ERROR) << "Failed to init extra stream, extra_offset: " << offset |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 112 | << ", patch_size: " << patch_size; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool BsdiffPatchReader::ParseControlEntry(ControlEntry* control_entry) { |
| 119 | if (!control_entry) |
| 120 | return false; |
| 121 | |
| 122 | uint8_t buf[8]; |
| 123 | if (!ctrl_stream_->Read(buf, 8)) |
| 124 | return false; |
| 125 | int64_t diff_size = ParseInt64(buf); |
| 126 | |
| 127 | if (!ctrl_stream_->Read(buf, 8)) |
| 128 | return false; |
| 129 | int64_t extra_size = ParseInt64(buf); |
| 130 | |
| 131 | // Sanity check. |
| 132 | if (diff_size < 0 || extra_size < 0) { |
| 133 | LOG(ERROR) << "Corrupt patch; diff_size: " << diff_size |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 134 | << ", extra_size: " << extra_size; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | control_entry->diff_size = diff_size; |
| 139 | control_entry->extra_size = extra_size; |
| 140 | |
| 141 | if (!ctrl_stream_->Read(buf, 8)) |
| 142 | return false; |
| 143 | control_entry->offset_increment = ParseInt64(buf); |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | bool BsdiffPatchReader::ReadDiffStream(uint8_t* buf, size_t size) { |
| 149 | return diff_stream_->Read(buf, size); |
| 150 | } |
| 151 | |
| 152 | bool BsdiffPatchReader::ReadExtraStream(uint8_t* buf, size_t size) { |
| 153 | return extra_stream_->Read(buf, size); |
| 154 | } |
| 155 | |
| 156 | bool BsdiffPatchReader::Finish() { |
| 157 | if (!ctrl_stream_->Close()) { |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 158 | LOG(ERROR) << "Failed to close the control stream"; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if (!diff_stream_->Close()) { |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 163 | LOG(ERROR) << "Failed to close the diff stream"; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
| 167 | if (!extra_stream_->Close()) { |
Tianjie Xu | 18480eb | 2017-11-29 16:21:43 -0800 | [diff] [blame] | 168 | LOG(ERROR) << "Failed to close the extra stream"; |
Tianjie Xu | 6528812 | 2017-10-13 15:10:58 -0700 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | } // namespace bsdiff |