Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "courgette/assembly_program.h" | |
| 19 #include "courgette/courgette.h" | 21 #include "courgette/courgette.h" |
| 22 #include "courgette/encoded_program.h" | |
| 23 #include "courgette/program_detector.h" | |
| 20 #include "courgette/streams.h" | 24 #include "courgette/streams.h" |
| 21 #include "courgette/third_party/bsdiff.h" | 25 #include "courgette/third_party/bsdiff.h" |
| 22 | 26 |
| 23 | 27 |
| 24 void PrintHelp() { | 28 void PrintHelp() { |
| 25 fprintf(stderr, | 29 fprintf(stderr, |
| 26 "Usage:\n" | 30 "Usage:\n" |
| 27 " courgette -supported <executable_file>\n" | 31 " courgette -supported <executable_file>\n" |
| 28 " courgette -dis <executable_file> <binary_assembly_file>\n" | 32 " courgette -dis <executable_file> <binary_assembly_file>\n" |
| 29 " courgette -asm <binary_assembly_file> <executable_file>\n" | 33 " courgette -asm <binary_assembly_file> <executable_file>\n" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 69 if (count == -1) | 73 if (count == -1) |
| 70 Problem("Can't write output."); | 74 Problem("Can't write output."); |
| 71 if (static_cast<size_t>(count) != sink->Length()) | 75 if (static_cast<size_t>(count) != sink->Length()) |
| 72 Problem("Incomplete write."); | 76 Problem("Incomplete write."); |
| 73 } | 77 } |
| 74 | 78 |
| 75 void Disassemble(const base::FilePath& input_file, | 79 void Disassemble(const base::FilePath& input_file, |
| 76 const base::FilePath& output_file) { | 80 const base::FilePath& output_file) { |
| 77 std::string buffer = ReadOrFail(input_file, "input"); | 81 std::string buffer = ReadOrFail(input_file, "input"); |
| 78 | 82 |
| 79 courgette::AssemblyProgram* program = NULL; | 83 scoped_ptr<courgette::AssemblyProgram> program; |
|
huangs
2016/01/25 19:22:48
Style: I prefer to put error checking immediately
| |
| 80 const courgette::Status parse_status = | 84 const courgette::Status parse_status = |
| 81 courgette::ParseDetectedExecutable(buffer.c_str(), buffer.length(), | 85 courgette::ParseDetectedExecutable(buffer.c_str(), buffer.length(), |
| 82 &program); | 86 &program); |
| 83 | |
| 84 if (parse_status != courgette::C_OK) | 87 if (parse_status != courgette::C_OK) |
| 85 Problem("Can't parse input (code = %d).", parse_status); | 88 Problem("Can't parse input (code = %d).", parse_status); |
| 86 | 89 |
| 87 courgette::EncodedProgram* encoded = NULL; | 90 scoped_ptr<courgette::EncodedProgram> encoded; |
|
grt (UTC plus 2)
2016/01/26 16:39:54
yeah, i'm not sure why this shouldn't be:
scoped
Will Harris
2016/01/26 18:40:20
Just to comment on this point, courgette doesn't e
huangs
2016/01/26 18:42:24
Excellent point! Two issues of debate are:
A. Al
| |
| 88 const courgette::Status encode_status = Encode(program, &encoded); | 91 const courgette::Status encode_status = Encode(*program, &encoded); |
| 89 | |
| 90 courgette::DeleteAssemblyProgram(program); | |
| 91 | |
| 92 if (encode_status != courgette::C_OK) | 92 if (encode_status != courgette::C_OK) |
| 93 Problem("Can't encode program."); | 93 Problem("Can't encode program."); |
| 94 | 94 |
| 95 program.reset(nullptr); | |
|
grt (UTC plus 2)
2016/01/26 16:39:54
reset() here and everywhere
huangs
2016/01/26 18:42:24
Done.
| |
| 96 | |
| 95 courgette::SinkStreamSet sinks; | 97 courgette::SinkStreamSet sinks; |
| 96 | |
| 97 const courgette::Status write_status = | 98 const courgette::Status write_status = |
| 98 courgette::WriteEncodedProgram(encoded, &sinks); | 99 courgette::WriteEncodedProgram(encoded.get(), &sinks); |
| 99 if (write_status != courgette::C_OK) | 100 if (write_status != courgette::C_OK) |
| 100 Problem("Can't serialize encoded program."); | 101 Problem("Can't serialize encoded program."); |
| 101 | 102 |
| 102 courgette::DeleteEncodedProgram(encoded); | 103 encoded.reset(nullptr); |
| 103 | 104 |
| 104 courgette::SinkStream sink; | 105 courgette::SinkStream sink; |
| 105 if (!sinks.CopyTo(&sink)) | 106 if (!sinks.CopyTo(&sink)) |
| 106 Problem("Can't combine serialized encoded program streams."); | 107 Problem("Can't combine serialized encoded program streams."); |
| 107 | 108 |
| 108 WriteSinkToFile(&sink, output_file); | 109 WriteSinkToFile(&sink, output_file); |
| 109 } | 110 } |
| 110 | 111 |
| 111 bool Supported(const base::FilePath& input_file) { | 112 bool Supported(const base::FilePath& input_file) { |
| 112 bool result = false; | 113 bool result = false; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 printf("%s Executable\n", format.c_str()); | 153 printf("%s Executable\n", format.c_str()); |
| 153 return result; | 154 return result; |
| 154 } | 155 } |
| 155 | 156 |
| 156 void DisassembleAndAdjust(const base::FilePath& program_file, | 157 void DisassembleAndAdjust(const base::FilePath& program_file, |
| 157 const base::FilePath& model_file, | 158 const base::FilePath& model_file, |
| 158 const base::FilePath& output_file) { | 159 const base::FilePath& output_file) { |
| 159 std::string program_buffer = ReadOrFail(program_file, "program"); | 160 std::string program_buffer = ReadOrFail(program_file, "program"); |
| 160 std::string model_buffer = ReadOrFail(model_file, "reference"); | 161 std::string model_buffer = ReadOrFail(model_file, "reference"); |
| 161 | 162 |
| 162 courgette::AssemblyProgram* program = NULL; | 163 scoped_ptr<courgette::AssemblyProgram> program; |
| 163 const courgette::Status parse_program_status = | 164 const courgette::Status parse_program_status = |
| 164 courgette::ParseDetectedExecutable(program_buffer.c_str(), | 165 courgette::ParseDetectedExecutable(program_buffer.c_str(), |
| 165 program_buffer.length(), | 166 program_buffer.length(), |
| 166 &program); | 167 &program); |
| 167 if (parse_program_status != courgette::C_OK) | 168 if (parse_program_status != courgette::C_OK) |
| 168 Problem("Can't parse program input (code = %d).", parse_program_status); | 169 Problem("Can't parse program input (code = %d).", parse_program_status); |
| 169 | 170 |
| 170 courgette::AssemblyProgram* model = NULL; | 171 scoped_ptr<courgette::AssemblyProgram> model; |
| 171 const courgette::Status parse_model_status = | 172 const courgette::Status parse_model_status = |
| 172 courgette::ParseDetectedExecutable(model_buffer.c_str(), | 173 courgette::ParseDetectedExecutable(model_buffer.c_str(), |
| 173 model_buffer.length(), | 174 model_buffer.length(), |
| 174 &model); | 175 &model); |
| 175 if (parse_model_status != courgette::C_OK) | 176 if (parse_model_status != courgette::C_OK) |
| 176 Problem("Can't parse model input (code = %d).", parse_model_status); | 177 Problem("Can't parse model input (code = %d).", parse_model_status); |
| 177 | 178 |
| 178 const courgette::Status adjust_status = Adjust(*model, program); | 179 const courgette::Status adjust_status = Adjust(*model, program.get()); |
| 179 if (adjust_status != courgette::C_OK) | 180 if (adjust_status != courgette::C_OK) |
| 180 Problem("Can't adjust program."); | 181 Problem("Can't adjust program."); |
| 181 | 182 |
| 182 courgette::EncodedProgram* encoded = NULL; | 183 model.reset(nullptr); |
| 183 const courgette::Status encode_status = Encode(program, &encoded); | |
| 184 | 184 |
| 185 courgette::DeleteAssemblyProgram(program); | 185 scoped_ptr<courgette::EncodedProgram> encoded; |
| 186 | 186 const courgette::Status encode_status = Encode(*program, &encoded); |
| 187 if (encode_status != courgette::C_OK) | 187 if (encode_status != courgette::C_OK) |
| 188 Problem("Can't encode program."); | 188 Problem("Can't encode program."); |
| 189 | 189 |
| 190 program.reset(nullptr); | |
| 191 | |
| 190 courgette::SinkStreamSet sinks; | 192 courgette::SinkStreamSet sinks; |
| 191 | |
| 192 const courgette::Status write_status = | 193 const courgette::Status write_status = |
| 193 courgette::WriteEncodedProgram(encoded, &sinks); | 194 courgette::WriteEncodedProgram(encoded.get(), &sinks); |
| 194 if (write_status != courgette::C_OK) | 195 if (write_status != courgette::C_OK) |
| 195 Problem("Can't serialize encoded program."); | 196 Problem("Can't serialize encoded program."); |
| 196 | 197 |
| 197 courgette::DeleteEncodedProgram(encoded); | 198 encoded.reset(nullptr); |
| 198 | 199 |
| 199 courgette::SinkStream sink; | 200 courgette::SinkStream sink; |
| 200 if (!sinks.CopyTo(&sink)) | 201 if (!sinks.CopyTo(&sink)) |
| 201 Problem("Can't combine serialized encoded program streams."); | 202 Problem("Can't combine serialized encoded program streams."); |
| 202 | 203 |
| 203 WriteSinkToFile(&sink, output_file); | 204 WriteSinkToFile(&sink, output_file); |
| 204 } | 205 } |
| 205 | 206 |
| 206 // Diffs two executable files, write a set of files for the diff, one file per | 207 // Diffs two executable files, write a set of files for the diff, one file per |
| 207 // stream of the EncodedProgram format. Each file is the bsdiff between the | 208 // stream of the EncodedProgram format. Each file is the bsdiff between the |
| 208 // original file's stream and the new file's stream. This is completely | 209 // original file's stream and the new file's stream. This is completely |
| 209 // uninteresting to users, but it is handy for seeing how much each which | 210 // uninteresting to users, but it is handy for seeing how much each which |
| 210 // streams are contributing to the final file size. Adjustment is optional. | 211 // streams are contributing to the final file size. Adjustment is optional. |
| 211 void DisassembleAdjustDiff(const base::FilePath& model_file, | 212 void DisassembleAdjustDiff(const base::FilePath& model_file, |
| 212 const base::FilePath& program_file, | 213 const base::FilePath& program_file, |
| 213 const base::FilePath& output_file_root, | 214 const base::FilePath& output_file_root, |
| 214 bool adjust) { | 215 bool adjust) { |
| 215 std::string model_buffer = ReadOrFail(model_file, "'old'"); | 216 std::string model_buffer = ReadOrFail(model_file, "'old'"); |
| 216 std::string program_buffer = ReadOrFail(program_file, "'new'"); | 217 std::string program_buffer = ReadOrFail(program_file, "'new'"); |
| 217 | 218 |
| 218 courgette::AssemblyProgram* model = NULL; | 219 scoped_ptr<courgette::AssemblyProgram> model; |
| 219 const courgette::Status parse_model_status = | 220 const courgette::Status parse_model_status = |
| 220 courgette::ParseDetectedExecutable(model_buffer.c_str(), | 221 courgette::ParseDetectedExecutable(model_buffer.c_str(), |
| 221 model_buffer.length(), | 222 model_buffer.length(), |
| 222 &model); | 223 &model); |
| 223 if (parse_model_status != courgette::C_OK) | 224 if (parse_model_status != courgette::C_OK) |
| 224 Problem("Can't parse model input (code = %d).", parse_model_status); | 225 Problem("Can't parse model input (code = %d).", parse_model_status); |
| 225 | 226 |
| 226 courgette::AssemblyProgram* program = NULL; | 227 scoped_ptr<courgette::AssemblyProgram> program; |
| 227 const courgette::Status parse_program_status = | 228 const courgette::Status parse_program_status = |
| 228 courgette::ParseDetectedExecutable(program_buffer.c_str(), | 229 courgette::ParseDetectedExecutable(program_buffer.c_str(), |
| 229 program_buffer.length(), | 230 program_buffer.length(), |
| 230 &program); | 231 &program); |
| 231 if (parse_program_status != courgette::C_OK) | 232 if (parse_program_status != courgette::C_OK) |
| 232 Problem("Can't parse program input (code = %d).", parse_program_status); | 233 Problem("Can't parse program input (code = %d).", parse_program_status); |
| 233 | 234 |
| 234 if (adjust) { | 235 if (adjust) { |
| 235 const courgette::Status adjust_status = Adjust(*model, program); | 236 const courgette::Status adjust_status = Adjust(*model, program.get()); |
| 236 if (adjust_status != courgette::C_OK) | 237 if (adjust_status != courgette::C_OK) |
| 237 Problem("Can't adjust program."); | 238 Problem("Can't adjust program."); |
| 238 } | 239 } |
| 239 | 240 |
| 240 courgette::EncodedProgram* encoded_program = NULL; | 241 scoped_ptr<courgette::EncodedProgram> encoded_program; |
| 241 const courgette::Status encode_program_status = | 242 const courgette::Status encode_program_status = |
| 242 Encode(program, &encoded_program); | 243 Encode(*program, &encoded_program); |
| 243 courgette::DeleteAssemblyProgram(program); | |
| 244 if (encode_program_status != courgette::C_OK) | 244 if (encode_program_status != courgette::C_OK) |
| 245 Problem("Can't encode program."); | 245 Problem("Can't encode program."); |
| 246 | 246 |
| 247 courgette::EncodedProgram* encoded_model = NULL; | 247 program.reset(nullptr); |
| 248 const courgette::Status encode_model_status = Encode(model, &encoded_model); | 248 |
| 249 courgette::DeleteAssemblyProgram(model); | 249 scoped_ptr<courgette::EncodedProgram> encoded_model; |
| 250 const courgette::Status encode_model_status = Encode(*model, &encoded_model); | |
| 250 if (encode_model_status != courgette::C_OK) | 251 if (encode_model_status != courgette::C_OK) |
| 251 Problem("Can't encode model."); | 252 Problem("Can't encode model."); |
| 252 | 253 |
| 254 model.reset(nullptr); | |
| 255 | |
| 253 courgette::SinkStreamSet program_sinks; | 256 courgette::SinkStreamSet program_sinks; |
| 254 const courgette::Status write_program_status = | 257 const courgette::Status write_program_status = |
| 255 courgette::WriteEncodedProgram(encoded_program, &program_sinks); | 258 courgette::WriteEncodedProgram(encoded_program.get(), &program_sinks); |
| 256 if (write_program_status != courgette::C_OK) | 259 if (write_program_status != courgette::C_OK) |
| 257 Problem("Can't serialize encoded program."); | 260 Problem("Can't serialize encoded program."); |
| 258 courgette::DeleteEncodedProgram(encoded_program); | 261 |
| 262 encoded_program.reset(); | |
| 259 | 263 |
| 260 courgette::SinkStreamSet model_sinks; | 264 courgette::SinkStreamSet model_sinks; |
| 261 const courgette::Status write_model_status = | 265 const courgette::Status write_model_status = |
| 262 courgette::WriteEncodedProgram(encoded_model, &model_sinks); | 266 courgette::WriteEncodedProgram(encoded_model.get(), &model_sinks); |
| 263 if (write_model_status != courgette::C_OK) | 267 if (write_model_status != courgette::C_OK) |
| 264 Problem("Can't serialize encoded model."); | 268 Problem("Can't serialize encoded model."); |
| 265 courgette::DeleteEncodedProgram(encoded_model); | 269 |
| 270 encoded_model.reset(nullptr); | |
| 266 | 271 |
| 267 courgette::SinkStream empty_sink; | 272 courgette::SinkStream empty_sink; |
| 268 for (int i = 0; ; ++i) { | 273 for (int i = 0; ; ++i) { |
| 269 courgette::SinkStream* old_stream = model_sinks.stream(i); | 274 courgette::SinkStream* old_stream = model_sinks.stream(i); |
| 270 courgette::SinkStream* new_stream = program_sinks.stream(i); | 275 courgette::SinkStream* new_stream = program_sinks.stream(i); |
| 271 if (old_stream == NULL && new_stream == NULL) | 276 if (old_stream == NULL && new_stream == NULL) |
| 272 break; | 277 break; |
| 273 | 278 |
| 274 courgette::SourceStream old_source; | 279 courgette::SourceStream old_source; |
| 275 courgette::SourceStream new_source; | 280 courgette::SourceStream new_source; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 288 } | 293 } |
| 289 | 294 |
| 290 void Assemble(const base::FilePath& input_file, | 295 void Assemble(const base::FilePath& input_file, |
| 291 const base::FilePath& output_file) { | 296 const base::FilePath& output_file) { |
| 292 std::string buffer = ReadOrFail(input_file, "input"); | 297 std::string buffer = ReadOrFail(input_file, "input"); |
| 293 | 298 |
| 294 courgette::SourceStreamSet sources; | 299 courgette::SourceStreamSet sources; |
| 295 if (!sources.Init(buffer.c_str(), buffer.length())) | 300 if (!sources.Init(buffer.c_str(), buffer.length())) |
| 296 Problem("Bad input file."); | 301 Problem("Bad input file."); |
| 297 | 302 |
| 298 courgette::EncodedProgram* encoded = NULL; | 303 scoped_ptr<courgette::EncodedProgram> encoded; |
| 299 const courgette::Status read_status = ReadEncodedProgram(&sources, &encoded); | 304 const courgette::Status read_status = |
| 305 courgette::ReadEncodedProgram(&sources, &encoded); | |
| 300 if (read_status != courgette::C_OK) | 306 if (read_status != courgette::C_OK) |
| 301 Problem("Bad encoded program."); | 307 Problem("Bad encoded program."); |
| 302 | 308 |
| 303 courgette::SinkStream sink; | 309 courgette::SinkStream sink; |
| 304 | 310 |
| 305 const courgette::Status assemble_status = courgette::Assemble(encoded, &sink); | 311 const courgette::Status assemble_status = |
| 312 courgette::Assemble(encoded.get(), &sink); | |
| 306 if (assemble_status != courgette::C_OK) | 313 if (assemble_status != courgette::C_OK) |
| 307 Problem("Can't assemble."); | 314 Problem("Can't assemble."); |
| 308 | 315 |
| 309 WriteSinkToFile(&sink, output_file); | 316 WriteSinkToFile(&sink, output_file); |
| 310 } | 317 } |
| 311 | 318 |
| 312 void GenerateEnsemblePatch(const base::FilePath& old_file, | 319 void GenerateEnsemblePatch(const base::FilePath& old_file, |
| 313 const base::FilePath& new_file, | 320 const base::FilePath& new_file, |
| 314 const base::FilePath& patch_file) { | 321 const base::FilePath& patch_file) { |
| 315 std::string old_buffer = ReadOrFail(old_file, "'old' input"); | 322 std::string old_buffer = ReadOrFail(old_file, "'old' input"); |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 510 UsageProblem("-gen1[au] <old_file> <new_file> <patch_files_root>"); | 517 UsageProblem("-gen1[au] <old_file> <new_file> <patch_files_root>"); |
| 511 DisassembleAdjustDiff(values[0], values[1], values[2], | 518 DisassembleAdjustDiff(values[0], values[1], values[2], |
| 512 cmd_spread_1_adjusted); | 519 cmd_spread_1_adjusted); |
| 513 } else { | 520 } else { |
| 514 UsageProblem("No operation specified"); | 521 UsageProblem("No operation specified"); |
| 515 } | 522 } |
| 516 } | 523 } |
| 517 | 524 |
| 518 return 0; | 525 return 0; |
| 519 } | 526 } |
| OLD | NEW |