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.
6 // The same patcher can be used for Windows X64 executables.
8 #ifndef COURGETTE_WIN32_X86_PATCHER_H_
9 #define COURGETTE_WIN32_X86_PATCHER_H_
11 #include "courgette/ensemble.h"
15 // Courgette32X86Patcher is a TransformationPatcher for Windows 32-bit
16 // and 64-bit executables. We can use the same patcher for both.
18 class PatcherX86_32
: public TransformationPatcher
{
20 explicit PatcherX86_32(const Region
& region
)
21 : ensemble_region_(region
),
26 Status
Init(SourceStream
* parameter_stream
) {
27 if (!parameter_stream
->ReadVarint32(&base_offset_
))
28 return C_BAD_TRANSFORM
;
29 if (!parameter_stream
->ReadVarint32(&base_length_
))
30 return C_BAD_TRANSFORM
;
32 if (base_offset_
> ensemble_region_
.length())
33 return C_BAD_TRANSFORM
;
34 if (base_length_
> ensemble_region_
.length() - base_offset_
)
35 return C_BAD_TRANSFORM
;
40 Status
PredictTransformParameters(SinkStreamSet
* predicted_parameters
) {
41 // No code needed to write an 'empty' predicted parameter set.
45 Status
Transform(SourceStreamSet
* corrected_parameters
,
46 SinkStreamSet
* transformed_element
) {
48 if (!corrected_parameters
->Empty())
49 return C_GENERAL_ERROR
; // Don't expect any corrected parameters.
51 AssemblyProgram
* program
= NULL
;
52 status
= ParseDetectedExecutable(ensemble_region_
.start() + base_offset_
,
58 // Trim labels below a certain threshold
59 Status trim_status
= TrimLabels(program
);
60 if (trim_status
!= C_OK
) {
61 DeleteAssemblyProgram(program
);
65 EncodedProgram
* encoded
= NULL
;
66 status
= Encode(program
, &encoded
);
67 DeleteAssemblyProgram(program
);
71 status
= WriteEncodedProgram(encoded
, transformed_element
);
72 DeleteEncodedProgram(encoded
);
77 Status
Reform(SourceStreamSet
* transformed_element
,
78 SinkStream
* reformed_element
) {
80 EncodedProgram
* encoded_program
= NULL
;
81 status
= ReadEncodedProgram(transformed_element
, &encoded_program
);
85 status
= Assemble(encoded_program
, reformed_element
);
86 DeleteEncodedProgram(encoded_program
);
94 Region ensemble_region_
;
99 DISALLOW_COPY_AND_ASSIGN(PatcherX86_32
);
103 #endif // COURGETTE_WIN32_X86_PATCHER_H_