huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 1 | // Copyright 2017 The Chromium 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 | |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 5 | #include "components/zucchini/zucchini_commands.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 6 | |
Samuel Huang | e8d07b75 | 2017-08-21 16:05:25 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 10 | #include <ostream> |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 11 | #include <utility> |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 12 | |
| 13 | #include "base/command_line.h" |
| 14 | #include "base/files/file.h" |
| 15 | #include "base/files/file_path.h" |
| 16 | #include "base/files/memory_mapped_file.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 17 | #include "base/logging.h" |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 18 | #include "base/macros.h" |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 19 | #include "components/zucchini/buffer_view.h" |
| 20 | #include "components/zucchini/crc32.h" |
| 21 | #include "components/zucchini/io_utils.h" |
| 22 | #include "components/zucchini/mapped_file.h" |
| 23 | #include "components/zucchini/patch_writer.h" |
| 24 | #include "components/zucchini/zucchini_integration.h" |
| 25 | #include "components/zucchini/zucchini_tools.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 29 | /******** Command-line Switches ********/ |
| 30 | |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 31 | constexpr char kSwitchDump[] = "dump"; |
| 32 | constexpr char kSwitchRaw[] = "raw"; |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 33 | |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 34 | } // namespace |
| 35 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 36 | zucchini::status::Code MainGen(MainParams params) { |
| 37 | CHECK_EQ(3U, params.file_paths.size()); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 38 | |
| 39 | // TODO(huangs): Move implementation to zucchini_integration.cc. |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 40 | using base::File; |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 41 | File old_file(params.file_paths[0], File::FLAG_OPEN | File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 42 | zucchini::MappedFileReader old_image(std::move(old_file)); |
| 43 | if (old_image.HasError()) { |
| 44 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 45 | << old_image.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 46 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 47 | } |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 48 | File new_file(params.file_paths[1], File::FLAG_OPEN | File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 49 | zucchini::MappedFileReader new_image(std::move(new_file)); |
| 50 | if (new_image.HasError()) { |
| 51 | LOG(ERROR) << "Error with file " << params.file_paths[1].value() << ": " |
| 52 | << new_image.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 53 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 54 | } |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 55 | zucchini::EnsemblePatchWriter patch_writer(old_image.region(), |
| 56 | new_image.region()); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 57 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 58 | auto generate = params.command_line.HasSwitch(kSwitchRaw) |
| 59 | ? zucchini::GenerateRaw |
| 60 | : zucchini::GenerateEnsemble; |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 61 | zucchini::status::Code result = |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 62 | generate(old_image.region(), new_image.region(), &patch_writer); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 63 | if (result != zucchini::status::kStatusSuccess) { |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 64 | params.out << "Fatal error encountered when generating patch." << std::endl; |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 65 | return result; |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 66 | } |
| 67 | |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 68 | // By default, delete patch on destruction, to avoid having lingering files in |
| 69 | // case of a failure. On Windows deletion can be done by the OS. |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 70 | File patch_file(params.file_paths[2], File::FLAG_CREATE_ALWAYS | |
| 71 | File::FLAG_READ | File::FLAG_WRITE | |
| 72 | File::FLAG_SHARE_DELETE | |
| 73 | File::FLAG_CAN_DELETE_ON_CLOSE); |
| 74 | zucchini::MappedFileWriter patch(params.file_paths[2], std::move(patch_file), |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 75 | patch_writer.SerializedSize()); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 76 | if (patch.HasError()) { |
| 77 | LOG(ERROR) << "Error with file " << params.file_paths[2].value() << ": " |
| 78 | << patch.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 79 | return zucchini::status::kStatusFileWriteError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 80 | } |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 81 | |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 82 | if (!patch_writer.SerializeInto(patch.region())) |
| 83 | return zucchini::status::kStatusPatchWriteError; |
| 84 | |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 85 | // Successfully created patch. Explicitly request file to be kept. |
| 86 | if (!patch.Keep()) |
| 87 | return zucchini::status::kStatusFileWriteError; |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 88 | return zucchini::status::kStatusSuccess; |
| 89 | } |
| 90 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 91 | zucchini::status::Code MainApply(MainParams params) { |
| 92 | CHECK_EQ(3U, params.file_paths.size()); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 93 | return zucchini::Apply(params.file_paths[0], params.file_paths[1], |
| 94 | params.file_paths[2]); |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | zucchini::status::Code MainRead(MainParams params) { |
| 98 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 99 | base::File input_file(params.file_paths[0], |
| 100 | base::File::FLAG_OPEN | base::File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 101 | zucchini::MappedFileReader input(std::move(input_file)); |
| 102 | if (input.HasError()) { |
| 103 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 104 | << input.error(); |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 105 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 106 | } |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 107 | |
| 108 | bool do_dump = params.command_line.HasSwitch(kSwitchDump); |
| 109 | zucchini::status::Code status = zucchini::ReadReferences( |
| 110 | {input.data(), input.length()}, do_dump, params.out); |
| 111 | if (status != zucchini::status::kStatusSuccess) |
| 112 | params.err << "Fatal error found when dumping references." << std::endl; |
| 113 | return status; |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 114 | } |
| 115 | |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 116 | zucchini::status::Code MainDetect(MainParams params) { |
| 117 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 118 | base::File input_file(params.file_paths[0], |
| 119 | base::File::FLAG_OPEN | base::File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 120 | zucchini::MappedFileReader input(std::move(input_file)); |
| 121 | if (input.HasError()) { |
| 122 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 123 | << input.error(); |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 124 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 125 | } |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 126 | |
| 127 | std::vector<zucchini::ConstBufferView> sub_image_list; |
| 128 | zucchini::status::Code result = zucchini::DetectAll( |
| 129 | {input.data(), input.length()}, params.out, &sub_image_list); |
| 130 | if (result != zucchini::status::kStatusSuccess) |
| 131 | params.err << "Fatal error found when detecting executables." << std::endl; |
| 132 | return result; |
| 133 | } |
| 134 | |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 135 | zucchini::status::Code MainMatch(MainParams params) { |
| 136 | CHECK_EQ(2U, params.file_paths.size()); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 137 | using base::File; |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 138 | File old_file(params.file_paths[0], File::FLAG_OPEN | File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 139 | zucchini::MappedFileReader old_image(std::move(old_file)); |
| 140 | if (old_image.HasError()) { |
| 141 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 142 | << old_image.error(); |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 143 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 144 | } |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 145 | File new_file(params.file_paths[1], File::FLAG_OPEN | File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 146 | zucchini::MappedFileReader new_image(std::move(new_file)); |
Calder Kitagawa | 3135d7d1 | 2018-03-14 19:28:36 | [diff] [blame] | 147 | if (new_image.HasError()) { |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 148 | LOG(ERROR) << "Error with file " << params.file_paths[1].value() << ": " |
| 149 | << new_image.error(); |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 150 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 151 | } |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 152 | zucchini::status::Code status = |
| 153 | zucchini::MatchAll({old_image.data(), old_image.length()}, |
| 154 | {new_image.data(), new_image.length()}, params.out); |
| 155 | if (status != zucchini::status::kStatusSuccess) |
| 156 | params.err << "Fatal error found when matching executables." << std::endl; |
| 157 | return status; |
| 158 | } |
| 159 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 160 | zucchini::status::Code MainCrc32(MainParams params) { |
| 161 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 162 | base::File image_file(params.file_paths[0], |
| 163 | base::File::FLAG_OPEN | base::File::FLAG_READ); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 164 | zucchini::MappedFileReader image(std::move(image_file)); |
| 165 | if (image.HasError()) { |
| 166 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 167 | << image.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 168 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 169 | } |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 170 | |
| 171 | uint32_t crc = |
| 172 | zucchini::CalculateCrc32(image.data(), image.data() + image.length()); |
| 173 | params.out << "CRC32: " << zucchini::AsHex<8>(crc) << std::endl; |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 174 | return zucchini::status::kStatusSuccess; |
| 175 | } |