| 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 <memory> |
| 8 #include <string> | 9 #include <string> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/at_exit.h" | 12 #include "base/at_exit.h" |
| 12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 15 #include "base/logging.h" | 16 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "courgette/assembly_program.h" | 20 #include "courgette/assembly_program.h" |
| 21 #include "courgette/courgette.h" | 21 #include "courgette/courgette.h" |
| 22 #include "courgette/encoded_program.h" | 22 #include "courgette/encoded_program.h" |
| 23 #include "courgette/program_detector.h" | 23 #include "courgette/program_detector.h" |
| 24 #include "courgette/streams.h" | 24 #include "courgette/streams.h" |
| 25 #include "courgette/third_party/bsdiff.h" | 25 #include "courgette/third_party/bsdiff.h" |
| 26 | 26 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 if (count == -1) | 73 if (count == -1) |
| 74 Problem("Can't write output."); | 74 Problem("Can't write output."); |
| 75 if (static_cast<size_t>(count) != sink->Length()) | 75 if (static_cast<size_t>(count) != sink->Length()) |
| 76 Problem("Incomplete write."); | 76 Problem("Incomplete write."); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void Disassemble(const base::FilePath& input_file, | 79 void Disassemble(const base::FilePath& input_file, |
| 80 const base::FilePath& output_file) { | 80 const base::FilePath& output_file) { |
| 81 std::string buffer = ReadOrFail(input_file, "input"); | 81 std::string buffer = ReadOrFail(input_file, "input"); |
| 82 | 82 |
| 83 scoped_ptr<courgette::AssemblyProgram> program; | 83 std::unique_ptr<courgette::AssemblyProgram> program; |
| 84 const courgette::Status parse_status = | 84 const courgette::Status parse_status = |
| 85 courgette::ParseDetectedExecutable(buffer.c_str(), buffer.length(), | 85 courgette::ParseDetectedExecutable(buffer.c_str(), buffer.length(), |
| 86 &program); | 86 &program); |
| 87 if (parse_status != courgette::C_OK) | 87 if (parse_status != courgette::C_OK) |
| 88 Problem("Can't parse input (code = %d).", parse_status); | 88 Problem("Can't parse input (code = %d).", parse_status); |
| 89 | 89 |
| 90 scoped_ptr<courgette::EncodedProgram> encoded; | 90 std::unique_ptr<courgette::EncodedProgram> encoded; |
| 91 const courgette::Status encode_status = Encode(*program, &encoded); | 91 const courgette::Status encode_status = Encode(*program, &encoded); |
| 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(); | 95 program.reset(); |
| 96 | 96 |
| 97 courgette::SinkStreamSet sinks; | 97 courgette::SinkStreamSet sinks; |
| 98 const courgette::Status write_status = | 98 const courgette::Status write_status = |
| 99 courgette::WriteEncodedProgram(encoded.get(), &sinks); | 99 courgette::WriteEncodedProgram(encoded.get(), &sinks); |
| 100 if (write_status != courgette::C_OK) | 100 if (write_status != courgette::C_OK) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 printf("%s Executable\n", format.c_str()); | 153 printf("%s Executable\n", format.c_str()); |
| 154 return result; | 154 return result; |
| 155 } | 155 } |
| 156 | 156 |
| 157 void DisassembleAndAdjust(const base::FilePath& program_file, | 157 void DisassembleAndAdjust(const base::FilePath& program_file, |
| 158 const base::FilePath& model_file, | 158 const base::FilePath& model_file, |
| 159 const base::FilePath& output_file) { | 159 const base::FilePath& output_file) { |
| 160 std::string program_buffer = ReadOrFail(program_file, "program"); | 160 std::string program_buffer = ReadOrFail(program_file, "program"); |
| 161 std::string model_buffer = ReadOrFail(model_file, "reference"); | 161 std::string model_buffer = ReadOrFail(model_file, "reference"); |
| 162 | 162 |
| 163 scoped_ptr<courgette::AssemblyProgram> program; | 163 std::unique_ptr<courgette::AssemblyProgram> program; |
| 164 const courgette::Status parse_program_status = | 164 const courgette::Status parse_program_status = |
| 165 courgette::ParseDetectedExecutable(program_buffer.c_str(), | 165 courgette::ParseDetectedExecutable(program_buffer.c_str(), |
| 166 program_buffer.length(), | 166 program_buffer.length(), |
| 167 &program); | 167 &program); |
| 168 if (parse_program_status != courgette::C_OK) | 168 if (parse_program_status != courgette::C_OK) |
| 169 Problem("Can't parse program input (code = %d).", parse_program_status); | 169 Problem("Can't parse program input (code = %d).", parse_program_status); |
| 170 | 170 |
| 171 scoped_ptr<courgette::AssemblyProgram> model; | 171 std::unique_ptr<courgette::AssemblyProgram> model; |
| 172 const courgette::Status parse_model_status = | 172 const courgette::Status parse_model_status = |
| 173 courgette::ParseDetectedExecutable(model_buffer.c_str(), | 173 courgette::ParseDetectedExecutable(model_buffer.c_str(), |
| 174 model_buffer.length(), | 174 model_buffer.length(), |
| 175 &model); | 175 &model); |
| 176 if (parse_model_status != courgette::C_OK) | 176 if (parse_model_status != courgette::C_OK) |
| 177 Problem("Can't parse model input (code = %d).", parse_model_status); | 177 Problem("Can't parse model input (code = %d).", parse_model_status); |
| 178 | 178 |
| 179 const courgette::Status adjust_status = Adjust(*model, program.get()); | 179 const courgette::Status adjust_status = Adjust(*model, program.get()); |
| 180 if (adjust_status != courgette::C_OK) | 180 if (adjust_status != courgette::C_OK) |
| 181 Problem("Can't adjust program."); | 181 Problem("Can't adjust program."); |
| 182 | 182 |
| 183 model.reset(); | 183 model.reset(); |
| 184 | 184 |
| 185 scoped_ptr<courgette::EncodedProgram> encoded; | 185 std::unique_ptr<courgette::EncodedProgram> encoded; |
| 186 const courgette::Status encode_status = Encode(*program, &encoded); | 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(); | 190 program.reset(); |
| 191 | 191 |
| 192 courgette::SinkStreamSet sinks; | 192 courgette::SinkStreamSet sinks; |
| 193 const courgette::Status write_status = | 193 const courgette::Status write_status = |
| 194 courgette::WriteEncodedProgram(encoded.get(), &sinks); | 194 courgette::WriteEncodedProgram(encoded.get(), &sinks); |
| 195 if (write_status != courgette::C_OK) | 195 if (write_status != courgette::C_OK) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 209 // 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 |
| 210 // 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 |
| 211 // streams are contributing to the final file size. Adjustment is optional. | 211 // streams are contributing to the final file size. Adjustment is optional. |
| 212 void DisassembleAdjustDiff(const base::FilePath& model_file, | 212 void DisassembleAdjustDiff(const base::FilePath& model_file, |
| 213 const base::FilePath& program_file, | 213 const base::FilePath& program_file, |
| 214 const base::FilePath& output_file_root, | 214 const base::FilePath& output_file_root, |
| 215 bool adjust) { | 215 bool adjust) { |
| 216 std::string model_buffer = ReadOrFail(model_file, "'old'"); | 216 std::string model_buffer = ReadOrFail(model_file, "'old'"); |
| 217 std::string program_buffer = ReadOrFail(program_file, "'new'"); | 217 std::string program_buffer = ReadOrFail(program_file, "'new'"); |
| 218 | 218 |
| 219 scoped_ptr<courgette::AssemblyProgram> model; | 219 std::unique_ptr<courgette::AssemblyProgram> model; |
| 220 const courgette::Status parse_model_status = | 220 const courgette::Status parse_model_status = |
| 221 courgette::ParseDetectedExecutable(model_buffer.c_str(), | 221 courgette::ParseDetectedExecutable(model_buffer.c_str(), |
| 222 model_buffer.length(), | 222 model_buffer.length(), |
| 223 &model); | 223 &model); |
| 224 if (parse_model_status != courgette::C_OK) | 224 if (parse_model_status != courgette::C_OK) |
| 225 Problem("Can't parse model input (code = %d).", parse_model_status); | 225 Problem("Can't parse model input (code = %d).", parse_model_status); |
| 226 | 226 |
| 227 scoped_ptr<courgette::AssemblyProgram> program; | 227 std::unique_ptr<courgette::AssemblyProgram> program; |
| 228 const courgette::Status parse_program_status = | 228 const courgette::Status parse_program_status = |
| 229 courgette::ParseDetectedExecutable(program_buffer.c_str(), | 229 courgette::ParseDetectedExecutable(program_buffer.c_str(), |
| 230 program_buffer.length(), | 230 program_buffer.length(), |
| 231 &program); | 231 &program); |
| 232 if (parse_program_status != courgette::C_OK) | 232 if (parse_program_status != courgette::C_OK) |
| 233 Problem("Can't parse program input (code = %d).", parse_program_status); | 233 Problem("Can't parse program input (code = %d).", parse_program_status); |
| 234 | 234 |
| 235 if (adjust) { | 235 if (adjust) { |
| 236 const courgette::Status adjust_status = Adjust(*model, program.get()); | 236 const courgette::Status adjust_status = Adjust(*model, program.get()); |
| 237 if (adjust_status != courgette::C_OK) | 237 if (adjust_status != courgette::C_OK) |
| 238 Problem("Can't adjust program."); | 238 Problem("Can't adjust program."); |
| 239 } | 239 } |
| 240 | 240 |
| 241 scoped_ptr<courgette::EncodedProgram> encoded_program; | 241 std::unique_ptr<courgette::EncodedProgram> encoded_program; |
| 242 const courgette::Status encode_program_status = | 242 const courgette::Status encode_program_status = |
| 243 Encode(*program, &encoded_program); | 243 Encode(*program, &encoded_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 program.reset(); | 247 program.reset(); |
| 248 | 248 |
| 249 scoped_ptr<courgette::EncodedProgram> encoded_model; | 249 std::unique_ptr<courgette::EncodedProgram> encoded_model; |
| 250 const courgette::Status encode_model_status = Encode(*model, &encoded_model); | 250 const courgette::Status encode_model_status = Encode(*model, &encoded_model); |
| 251 if (encode_model_status != courgette::C_OK) | 251 if (encode_model_status != courgette::C_OK) |
| 252 Problem("Can't encode model."); | 252 Problem("Can't encode model."); |
| 253 | 253 |
| 254 model.reset(); | 254 model.reset(); |
| 255 | 255 |
| 256 courgette::SinkStreamSet program_sinks; | 256 courgette::SinkStreamSet program_sinks; |
| 257 const courgette::Status write_program_status = | 257 const courgette::Status write_program_status = |
| 258 courgette::WriteEncodedProgram(encoded_program.get(), &program_sinks); | 258 courgette::WriteEncodedProgram(encoded_program.get(), &program_sinks); |
| 259 if (write_program_status != courgette::C_OK) | 259 if (write_program_status != courgette::C_OK) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 } | 293 } |
| 294 | 294 |
| 295 void Assemble(const base::FilePath& input_file, | 295 void Assemble(const base::FilePath& input_file, |
| 296 const base::FilePath& output_file) { | 296 const base::FilePath& output_file) { |
| 297 std::string buffer = ReadOrFail(input_file, "input"); | 297 std::string buffer = ReadOrFail(input_file, "input"); |
| 298 | 298 |
| 299 courgette::SourceStreamSet sources; | 299 courgette::SourceStreamSet sources; |
| 300 if (!sources.Init(buffer.c_str(), buffer.length())) | 300 if (!sources.Init(buffer.c_str(), buffer.length())) |
| 301 Problem("Bad input file."); | 301 Problem("Bad input file."); |
| 302 | 302 |
| 303 scoped_ptr<courgette::EncodedProgram> encoded; | 303 std::unique_ptr<courgette::EncodedProgram> encoded; |
| 304 const courgette::Status read_status = | 304 const courgette::Status read_status = |
| 305 courgette::ReadEncodedProgram(&sources, &encoded); | 305 courgette::ReadEncodedProgram(&sources, &encoded); |
| 306 if (read_status != courgette::C_OK) | 306 if (read_status != courgette::C_OK) |
| 307 Problem("Bad encoded program."); | 307 Problem("Bad encoded program."); |
| 308 | 308 |
| 309 courgette::SinkStream sink; | 309 courgette::SinkStream sink; |
| 310 | 310 |
| 311 const courgette::Status assemble_status = | 311 const courgette::Status assemble_status = |
| 312 courgette::Assemble(encoded.get(), &sink); | 312 courgette::Assemble(encoded.get(), &sink); |
| 313 if (assemble_status != courgette::C_OK) | 313 if (assemble_status != courgette::C_OK) |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 UsageProblem("-gen1[au] <old_file> <new_file> <patch_files_root>"); | 517 UsageProblem("-gen1[au] <old_file> <new_file> <patch_files_root>"); |
| 518 DisassembleAdjustDiff(values[0], values[1], values[2], | 518 DisassembleAdjustDiff(values[0], values[1], values[2], |
| 519 cmd_spread_1_adjusted); | 519 cmd_spread_1_adjusted); |
| 520 } else { | 520 } else { |
| 521 UsageProblem("No operation specified"); | 521 UsageProblem("No operation specified"); |
| 522 } | 522 } |
| 523 } | 523 } |
| 524 | 524 |
| 525 return 0; | 525 return 0; |
| 526 } | 526 } |
| OLD | NEW |