1 // Copyright (c) 2012 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.
5 // This is the transformation for Windows X86 executables.
7 #ifndef COURGETTE_WIN32_X86_PATCHER_H_
8 #define COURGETTE_WIN32_X86_PATCHER_H_
10 #include "courgette/ensemble.h"
14 // Courgette32X86Patcher is a TransformationPatcher for Windows 32-bit
17 class PatcherX86_32
: public TransformationPatcher
{
19 explicit PatcherX86_32(const Region
& region
)
20 : ensemble_region_(region
),
25 Status
Init(SourceStream
* parameter_stream
) {
26 if (!parameter_stream
->ReadVarint32(&base_offset_
))
27 return C_BAD_TRANSFORM
;
28 if (!parameter_stream
->ReadVarint32(&base_length_
))
29 return C_BAD_TRANSFORM
;
31 if (base_offset_
> ensemble_region_
.length())
32 return C_BAD_TRANSFORM
;
33 if (base_length_
> ensemble_region_
.length() - base_offset_
)
34 return C_BAD_TRANSFORM
;
39 Status
PredictTransformParameters(SinkStreamSet
* predicted_parameters
) {
40 // No code needed to write an 'empty' predicted parameter set.
44 Status
Transform(SourceStreamSet
* corrected_parameters
,
45 SinkStreamSet
* transformed_element
) {
47 if (!corrected_parameters
->Empty())
48 return C_GENERAL_ERROR
; // Don't expect any corrected parameters.
50 AssemblyProgram
* program
= NULL
;
51 status
= ParseDetectedExecutable(ensemble_region_
.start() + base_offset_
,
57 // Trim labels below a certain threshold
58 Status trim_status
= TrimLabels(program
);
59 if (trim_status
!= C_OK
) {
60 DeleteAssemblyProgram(program
);
64 EncodedProgram
* encoded
= NULL
;
65 status
= Encode(program
, &encoded
);
66 DeleteAssemblyProgram(program
);
70 status
= WriteEncodedProgram(encoded
, transformed_element
);
71 DeleteEncodedProgram(encoded
);
76 Status
Reform(SourceStreamSet
* transformed_element
,
77 SinkStream
* reformed_element
) {
79 EncodedProgram
* encoded_program
= NULL
;
80 status
= ReadEncodedProgram(transformed_element
, &encoded_program
);
84 status
= Assemble(encoded_program
, reformed_element
);
85 DeleteEncodedProgram(encoded_program
);
93 Region ensemble_region_
;
98 DISALLOW_COPY_AND_ASSIGN(PatcherX86_32
);
102 #endif // COURGETTE_WIN32_X86_PATCHER_H_