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 | 73a64ff | 2018-04-30 22:47:52 | [diff] [blame] | 11 | #include <string> |
Samuel Huang | 577ef6c | 2018-03-13 18:19:34 | [diff] [blame] | 12 | #include <utility> |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 13 | |
| 14 | #include "base/command_line.h" |
| 15 | #include "base/files/file.h" |
| 16 | #include "base/files/file_path.h" |
| 17 | #include "base/files/memory_mapped_file.h" |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 18 | #include "base/logging.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"; |
Samuel Huang | 73a64ff | 2018-04-30 22:47:52 | [diff] [blame] | 32 | constexpr char kSwitchImpose[] = "impose"; |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame] | 33 | constexpr char kSwitchKeep[] = "keep"; |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 34 | constexpr char kSwitchRaw[] = "raw"; |
Etienne Pierre-Doray | 73ed423 | 2017-08-10 01:28:41 | [diff] [blame] | 35 | |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 36 | } // namespace |
| 37 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 38 | zucchini::status::Code MainGen(MainParams params) { |
| 39 | CHECK_EQ(3U, params.file_paths.size()); |
Samuel Huang | 9f0f325 | 2018-06-21 15:50:22 | [diff] [blame] | 40 | return zucchini::Generate( |
| 41 | params.file_paths[0], params.file_paths[1], params.file_paths[2], |
| 42 | params.command_line.HasSwitch(kSwitchKeep), |
| 43 | params.command_line.HasSwitch(kSwitchRaw), |
| 44 | params.command_line.GetSwitchValueASCII(kSwitchImpose)); |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 45 | } |
| 46 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 47 | zucchini::status::Code MainApply(MainParams params) { |
| 48 | CHECK_EQ(3U, params.file_paths.size()); |
Etienne Pierre-Doray | 455d1ae | 2017-08-24 01:17:54 | [diff] [blame] | 49 | return zucchini::Apply(params.file_paths[0], params.file_paths[1], |
Samuel Huang | 21879c3 | 2018-03-21 18:54:03 | [diff] [blame] | 50 | params.file_paths[2], |
| 51 | params.command_line.HasSwitch(kSwitchKeep)); |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 52 | } |
| 53 | |
Etienne Pierre-doray | 559d77a | 2021-10-28 21:16:04 | [diff] [blame] | 54 | zucchini::status::Code MainVerify(MainParams params) { |
| 55 | CHECK_EQ(1U, params.file_paths.size()); |
| 56 | return zucchini::VerifyPatch(params.file_paths[0]); |
| 57 | } |
| 58 | |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 59 | zucchini::status::Code MainRead(MainParams params) { |
| 60 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 61 | base::File input_file(params.file_paths[0], |
Greg Thompson | f176eba3 | 2020-07-18 01:29:39 | [diff] [blame] | 62 | base::File::FLAG_OPEN | base::File::FLAG_READ | |
Alexei Svitkine | 9f7f7c8f | 2021-11-16 22:01:32 | [diff] [blame] | 63 | base::File::FLAG_WIN_SHARE_DELETE); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 64 | zucchini::MappedFileReader input(std::move(input_file)); |
| 65 | if (input.HasError()) { |
| 66 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 67 | << input.error(); |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 68 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 69 | } |
Samuel Huang | d943db6 | 2017-08-23 21:46:57 | [diff] [blame] | 70 | |
| 71 | bool do_dump = params.command_line.HasSwitch(kSwitchDump); |
| 72 | zucchini::status::Code status = zucchini::ReadReferences( |
| 73 | {input.data(), input.length()}, do_dump, params.out); |
| 74 | if (status != zucchini::status::kStatusSuccess) |
| 75 | params.err << "Fatal error found when dumping references." << std::endl; |
| 76 | return status; |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 77 | } |
| 78 | |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 79 | zucchini::status::Code MainDetect(MainParams params) { |
| 80 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 81 | base::File input_file(params.file_paths[0], |
Greg Thompson | f176eba3 | 2020-07-18 01:29:39 | [diff] [blame] | 82 | base::File::FLAG_OPEN | base::File::FLAG_READ | |
Alexei Svitkine | 9f7f7c8f | 2021-11-16 22:01:32 | [diff] [blame] | 83 | base::File::FLAG_WIN_SHARE_DELETE); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 84 | zucchini::MappedFileReader input(std::move(input_file)); |
| 85 | if (input.HasError()) { |
| 86 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 87 | << input.error(); |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 88 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 89 | } |
Samuel Huang | dd90d83 | 2017-11-03 18:14:02 | [diff] [blame] | 90 | |
| 91 | std::vector<zucchini::ConstBufferView> sub_image_list; |
| 92 | zucchini::status::Code result = zucchini::DetectAll( |
| 93 | {input.data(), input.length()}, params.out, &sub_image_list); |
| 94 | if (result != zucchini::status::kStatusSuccess) |
| 95 | params.err << "Fatal error found when detecting executables." << std::endl; |
| 96 | return result; |
| 97 | } |
| 98 | |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 99 | zucchini::status::Code MainMatch(MainParams params) { |
| 100 | CHECK_EQ(2U, params.file_paths.size()); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 101 | using base::File; |
Greg Thompson | f176eba3 | 2020-07-18 01:29:39 | [diff] [blame] | 102 | File old_file(params.file_paths[0], File::FLAG_OPEN | File::FLAG_READ | |
Alexei Svitkine | 9f7f7c8f | 2021-11-16 22:01:32 | [diff] [blame] | 103 | base::File::FLAG_WIN_SHARE_DELETE); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 104 | zucchini::MappedFileReader old_image(std::move(old_file)); |
| 105 | if (old_image.HasError()) { |
| 106 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 107 | << old_image.error(); |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 108 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 109 | } |
Greg Thompson | f176eba3 | 2020-07-18 01:29:39 | [diff] [blame] | 110 | File new_file(params.file_paths[1], File::FLAG_OPEN | File::FLAG_READ | |
Alexei Svitkine | 9f7f7c8f | 2021-11-16 22:01:32 | [diff] [blame] | 111 | base::File::FLAG_WIN_SHARE_DELETE); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 112 | zucchini::MappedFileReader new_image(std::move(new_file)); |
Calder Kitagawa | 3135d7d1 | 2018-03-14 19:28:36 | [diff] [blame] | 113 | if (new_image.HasError()) { |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 114 | LOG(ERROR) << "Error with file " << params.file_paths[1].value() << ": " |
| 115 | << new_image.error(); |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 116 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 117 | } |
Samuel Huang | 73a64ff | 2018-04-30 22:47:52 | [diff] [blame] | 118 | |
| 119 | std::string imposed_matches = |
| 120 | params.command_line.GetSwitchValueASCII(kSwitchImpose); |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 121 | zucchini::status::Code status = |
| 122 | zucchini::MatchAll({old_image.data(), old_image.length()}, |
Samuel Huang | 73a64ff | 2018-04-30 22:47:52 | [diff] [blame] | 123 | {new_image.data(), new_image.length()}, |
| 124 | std::move(imposed_matches), params.out); |
Samuel Huang | fdb2f3a | 2017-12-20 17:45:14 | [diff] [blame] | 125 | if (status != zucchini::status::kStatusSuccess) |
| 126 | params.err << "Fatal error found when matching executables." << std::endl; |
| 127 | return status; |
| 128 | } |
| 129 | |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 130 | zucchini::status::Code MainCrc32(MainParams params) { |
| 131 | CHECK_EQ(1U, params.file_paths.size()); |
Calder Kitagawa | 796f2fb | 2018-02-12 17:14:49 | [diff] [blame] | 132 | base::File image_file(params.file_paths[0], |
Greg Thompson | f176eba3 | 2020-07-18 01:29:39 | [diff] [blame] | 133 | base::File::FLAG_OPEN | base::File::FLAG_READ | |
Alexei Svitkine | 9f7f7c8f | 2021-11-16 22:01:32 | [diff] [blame] | 134 | base::File::FLAG_WIN_SHARE_DELETE); |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 135 | zucchini::MappedFileReader image(std::move(image_file)); |
| 136 | if (image.HasError()) { |
| 137 | LOG(ERROR) << "Error with file " << params.file_paths[0].value() << ": " |
| 138 | << image.error(); |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 139 | return zucchini::status::kStatusFileReadError; |
Calder Kitagawa | ab7fbfb | 2018-02-09 18:06:02 | [diff] [blame] | 140 | } |
huangs | 252600a | 2017-07-31 22:19:48 | [diff] [blame] | 141 | |
| 142 | uint32_t crc = |
| 143 | zucchini::CalculateCrc32(image.data(), image.data() + image.length()); |
| 144 | params.out << "CRC32: " << zucchini::AsHex<8>(crc) << std::endl; |
huangs | f953e407 | 2017-07-28 01:16:21 | [diff] [blame] | 145 | return zucchini::status::kStatusSuccess; |
| 146 | } |