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