forked from win32ss/supermium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
courgette_flow.cc
212 lines (179 loc) · 5.46 KB
/
courgette_flow.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "courgette/courgette_flow.h"
#include <stdarg.h>
#include <memory>
#include "base/check.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/strings/stringprintf.h"
#include "courgette/assembly_program.h"
#include "courgette/disassembler.h"
#include "courgette/encoded_program.h"
#include "courgette/program_detector.h"
namespace courgette {
/******** CourgetteFlow::Data ********/
CourgetteFlow::Data::Data() = default;
CourgetteFlow::Data::~Data() = default;
/******** CourgetteFlow ********/
CourgetteFlow::CourgetteFlow() = default;
CourgetteFlow::~CourgetteFlow() = default;
// static
const char* CourgetteFlow::name(Group group) {
switch (group) {
case ONLY:
return "input";
case OLD:
return "'old' input";
case NEW:
return "'new' input";
default:
NOTREACHED();
break;
}
return nullptr;
}
CourgetteFlow::Data* CourgetteFlow::data(Group group) {
switch (group) {
case ONLY:
return &data_only_;
case OLD:
return &data_old_;
case NEW:
return &data_new_;
default:
NOTREACHED();
break;
}
return nullptr;
}
bool CourgetteFlow::ok() {
return status_ == C_OK;
}
bool CourgetteFlow::failed() {
return status_ != C_OK;
}
Status CourgetteFlow::status() {
return status_;
}
const std::string& CourgetteFlow::message() {
return message_;
}
void CourgetteFlow::ReadSourceStreamSetFromBuffer(Group group,
const BasicBuffer& buffer) {
if (failed())
return;
Data* d = data(group);
if (!check(d->sources.Init(buffer.data(), buffer.length()),
C_GENERAL_ERROR)) {
setMessage("Cannot read %s as SourceStreamSet.", name(group));
}
}
void CourgetteFlow::ReadDisassemblerFromBuffer(Group group,
const BasicBuffer& buffer) {
if (failed())
return;
Data* d = data(group);
d->disassembler = DetectDisassembler(buffer.data(), buffer.length());
if (!check(d->disassembler.get() != nullptr, C_INPUT_NOT_RECOGNIZED))
setMessage("Cannot detect program for %s.", name(group));
}
void CourgetteFlow::ReadEncodedProgramFromSourceStreamSet(
Group group,
SourceStreamSet* opt_sources /* nullptr */) {
if (failed())
return;
Data* d = data(group);
SourceStreamSet* sources = opt_sources ? opt_sources : &d->sources;
if (!check(ReadEncodedProgram(sources, &d->encoded)))
setMessage("Cannot read %s as encoded program.", name(group));
}
void CourgetteFlow::CreateAssemblyProgramFromDisassembler(Group group,
bool annotate) {
if (failed())
return;
Data* d = data(group);
d->program = d->disassembler->CreateProgram(annotate);
if (!check(d->program.get() != nullptr, C_DISASSEMBLY_FAILED))
setMessage("Cannot create AssemblyProgram for %s.", name(group));
}
void CourgetteFlow::CreateEncodedProgramFromDisassemblerAndAssemblyProgram(
Group group) {
if (failed())
return;
Data* d = data(group);
d->encoded = std::make_unique<EncodedProgram>();
if (!check(d->disassembler->DisassembleAndEncode(d->program.get(),
d->encoded.get()))) {
setMessage("Cannot disassemble to form EncodedProgram for %s.",
name(group));
}
}
void CourgetteFlow::WriteSinkStreamFromSinkStreamSet(Group group,
SinkStream* sink) {
DCHECK(sink);
if (failed())
return;
if (!check(data(group)->sinks.CopyTo(sink), C_GENERAL_ERROR))
setMessage("Cannnot combine serialized streams for %s.", name(group));
}
void CourgetteFlow::WriteSinkStreamSetFromEncodedProgram(
Group group,
SinkStreamSet* opt_sinks /* nullptr */) {
if (failed())
return;
Data* d = data(group);
SinkStreamSet* sinks = opt_sinks ? opt_sinks : &d->sinks;
if (!check(WriteEncodedProgram(d->encoded.get(), sinks)))
setMessage("Cannot serialize encoded %s.", name(group));
}
void CourgetteFlow::WriteExecutableFromEncodedProgram(Group group,
SinkStream* sink) {
DCHECK(sink);
if (failed())
return;
if (!check(Assemble(data(group)->encoded.get(), sink)))
setMessage("Cannot assemble %s.", name(group));
}
void CourgetteFlow::AdjustNewAssemblyProgramToMatchOld() {
if (failed())
return;
if (!check(Adjust(*data_old_.program, data_new_.program.get())))
setMessage("Cannot adjust %s to match %s.", name(OLD), name(NEW));
}
void CourgetteFlow::DestroyDisassembler(Group group) {
if (failed())
return;
data(group)->disassembler.reset();
}
void CourgetteFlow::DestroyAssemblyProgram(Group group) {
if (failed())
return;
data(group)->program.reset();
}
void CourgetteFlow::DestroyEncodedProgram(Group group) {
if (failed())
return;
data(group)->encoded.reset();
}
bool CourgetteFlow::check(Status new_status) {
if (new_status == C_OK)
return true;
status_ = new_status;
return false;
}
bool CourgetteFlow::check(bool success, Status failure_mode) {
if (success)
return true;
status_ = failure_mode;
return false;
}
void CourgetteFlow::setMessage(const char* format, ...) {
va_list args;
va_start(args, format);
message_ = base::StringPrintV(format, args);
va_end(args);
}
} // namespace courgette