Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "courgette/courgette_flow.h" | |
| 6 | |
| 7 #include <stdarg.h> | |
| 8 | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "courgette/assembly_program.h" | |
| 14 #include "courgette/encoded_program.h" | |
| 15 #include "courgette/program_detector.h" | |
| 16 | |
| 17 namespace courgette { | |
| 18 | |
| 19 /******** CourgetteFlow::Data ********/ | |
| 20 | |
| 21 CourgetteFlow::Data::Data() = default; | |
| 22 | |
| 23 CourgetteFlow::Data::~Data() = default; | |
| 24 | |
| 25 /******** CourgetteFlow ********/ | |
| 26 | |
| 27 CourgetteFlow::CourgetteFlow() = default; | |
| 28 | |
| 29 CourgetteFlow::~CourgetteFlow() = default; | |
| 30 | |
| 31 // static | |
| 32 const char* CourgetteFlow::name(Group group) { | |
| 33 switch (group) { | |
| 34 case ONLY: | |
| 35 return "input"; | |
| 36 case OLD: | |
| 37 return "'old' input"; | |
| 38 case NEW: | |
| 39 return "'new' input"; | |
| 40 default: | |
| 41 NOTREACHED(); | |
| 42 break; | |
| 43 } | |
| 44 return nullptr; | |
| 45 } | |
| 46 | |
| 47 CourgetteFlow::Data* CourgetteFlow::data(Group group) { | |
| 48 switch (group) { | |
| 49 case ONLY: | |
| 50 return &data_only_; | |
| 51 case OLD: | |
| 52 return &data_old_; | |
| 53 case NEW: | |
| 54 return &data_new_; | |
| 55 default: | |
| 56 NOTREACHED(); | |
| 57 break; | |
| 58 } | |
| 59 return nullptr; | |
| 60 } | |
| 61 | |
| 62 bool CourgetteFlow::ok() { | |
| 63 return status_ == C_OK; | |
| 64 } | |
| 65 | |
| 66 bool CourgetteFlow::failed() { | |
| 67 return status_ != C_OK; | |
| 68 } | |
| 69 | |
| 70 Status CourgetteFlow::status() { | |
| 71 return status_; | |
| 72 } | |
| 73 | |
| 74 const std::string& CourgetteFlow::message() { | |
| 75 return message_; | |
| 76 } | |
| 77 | |
| 78 void CourgetteFlow::ReadSourceStreamSetFromBuffer(Group group, | |
| 79 const BasicBuffer& buffer) { | |
| 80 if (ok()) { | |
|
chrisha
2017/04/24 18:15:49
if (failed())
return;
Then the rest of the logi
huangs
2017/04/24 20:08:14
Initially I did that, then I got spooked by the bu
| |
| 81 Data* d = data(group); | |
| 82 if (!check(d->sources.Init(buffer.data(), buffer.length()), | |
| 83 C_GENERAL_ERROR)) { | |
| 84 setMessage("Cannot read %s as SourceStreamSet.", name(group)); | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void CourgetteFlow::ReadAssemblyProgramFromBuffer(Group group, | |
| 90 const BasicBuffer& buffer, | |
| 91 bool annotate) { | |
| 92 if (ok()) { | |
| 93 Data* d = data(group); | |
| 94 auto parser = annotate ? ParseDetectedExecutableWithAnnotation | |
| 95 : ParseDetectedExecutable; | |
| 96 if (!check(parser(buffer.data(), buffer.length(), &d->program))) | |
| 97 setMessage("Cannot parse %s (code = %d).", name(group), status_); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 void CourgetteFlow::ReadEncodedProgramFromSourceStreamSet( | |
| 102 Group group, | |
| 103 SourceStreamSet* opt_sources /* nullptr */) { | |
| 104 if (ok()) { | |
| 105 Data* d = data(group); | |
| 106 SourceStreamSet* sources = opt_sources ? opt_sources : &d->sources; | |
| 107 if (!check(ReadEncodedProgram(sources, &d->encoded))) | |
| 108 setMessage("Cannot read %s as encoded program.", name(group)); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void CourgetteFlow::CreateEncodedProgramFromAssemblyProgram(Group group) { | |
| 113 if (ok()) { | |
| 114 Data* d = data(group); | |
| 115 if (!check(Encode(*d->program, &d->encoded))) | |
| 116 setMessage("Cannot encode %s (code = %d).", name(group), status_); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void CourgetteFlow::WriteSinkStreamFromSinkStreamSet(Group group, | |
| 121 SinkStream* sink) { | |
| 122 DCHECK(sink); | |
| 123 if (ok()) { | |
| 124 if (!check(data(group)->sinks.CopyTo(sink), C_GENERAL_ERROR)) | |
| 125 setMessage("Cannnot combine serialized streams for %s.", name(group)); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void CourgetteFlow::WriteSinkStreamSetFromEncodedProgram( | |
| 130 Group group, | |
| 131 SinkStreamSet* opt_sinks /* nullptr */) { | |
| 132 if (ok()) { | |
| 133 Data* d = data(group); | |
| 134 SinkStreamSet* sinks = opt_sinks ? opt_sinks : &d->sinks; | |
| 135 if (!check(WriteEncodedProgram(d->encoded.get(), sinks))) | |
| 136 setMessage("Cannot serialize encoded %s.", name(group)); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void CourgetteFlow::WriteExecutableFromEncodedProgram(Group group, | |
| 141 SinkStream* sink) { | |
| 142 DCHECK(sink); | |
| 143 if (ok()) { | |
| 144 if (!check(Assemble(data(group)->encoded.get(), sink))) | |
| 145 setMessage("Cannot assemble %s.", name(group)); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 void CourgetteFlow::AdjustNewAssemblyProgramToMatchOld() { | |
| 150 if (ok()) { | |
| 151 if (!check(Adjust(*data_old_.program, data_new_.program.get()))) | |
| 152 setMessage("Cannot adjust %s to match %s.", name(OLD), name(NEW)); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 void CourgetteFlow::DestroyAssemblyProgram(Group group) { | |
| 157 if (ok()) | |
| 158 data(group)->program.reset(); | |
| 159 } | |
| 160 | |
| 161 void CourgetteFlow::DestroyEncodedProgram(Group group) { | |
| 162 if (ok()) | |
| 163 data(group)->encoded.reset(); | |
| 164 } | |
| 165 | |
| 166 bool CourgetteFlow::check(Status new_status) { | |
| 167 if (new_status == C_OK) | |
| 168 return true; | |
| 169 status_ = new_status; | |
| 170 return false; | |
| 171 } | |
| 172 | |
| 173 bool CourgetteFlow::check(bool success, Status failure_mode) { | |
| 174 if (success) | |
| 175 return true; | |
| 176 status_ = failure_mode; | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 void CourgetteFlow::setMessage(const char* format, ...) { | |
| 181 va_list args; | |
| 182 va_start(args, format); | |
| 183 message_ = base::StringPrintV(format, args); | |
| 184 va_end(args); | |
| 185 } | |
| 186 | |
| 187 } // namespace courgette | |
| OLD | NEW |