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 #ifndef COURGETTE_WIN32_X86_PATCHER_H_
6 #define COURGETTE_WIN32_X86_PATCHER_H_
8 #include "courgette/ensemble.h"
12 // PatcherX86_32 is the universal patcher for all executables. The executable
13 // type is determined by ParseDetectedExecutable function.
15 class PatcherX86_32
: public TransformationPatcher
{
17 explicit PatcherX86_32(const Region
& region
)
18 : ensemble_region_(region
),
23 Status
Init(SourceStream
* parameter_stream
) {
24 if (!parameter_stream
->ReadVarint32(&base_offset_
))
25 return C_BAD_TRANSFORM
;
26 if (!parameter_stream
->ReadVarint32(&base_length_
))
27 return C_BAD_TRANSFORM
;
29 if (base_offset_
> ensemble_region_
.length())
30 return C_BAD_TRANSFORM
;
31 if (base_length_
> ensemble_region_
.length() - base_offset_
)
32 return C_BAD_TRANSFORM
;
37 Status
PredictTransformParameters(SinkStreamSet
* predicted_parameters
) {
38 // No code needed to write an 'empty' predicted parameter set.
42 Status
Transform(SourceStreamSet
* corrected_parameters
,
43 SinkStreamSet
* transformed_element
) {
45 if (!corrected_parameters
->Empty())
46 return C_GENERAL_ERROR
; // Don't expect any corrected parameters.
48 AssemblyProgram
* program
= NULL
;
49 status
= ParseDetectedExecutable(ensemble_region_
.start() + base_offset_
,
55 // Trim labels below a certain threshold
56 Status trim_status
= TrimLabels(program
);
57 if (trim_status
!= C_OK
) {
58 DeleteAssemblyProgram(program
);
62 EncodedProgram
* encoded
= NULL
;
63 status
= Encode(program
, &encoded
);
64 DeleteAssemblyProgram(program
);
68 status
= WriteEncodedProgram(encoded
, transformed_element
);
69 DeleteEncodedProgram(encoded
);
74 Status
Reform(SourceStreamSet
* transformed_element
,
75 SinkStream
* reformed_element
) {
77 EncodedProgram
* encoded_program
= NULL
;
78 status
= ReadEncodedProgram(transformed_element
, &encoded_program
);
82 status
= Assemble(encoded_program
, reformed_element
);
83 DeleteEncodedProgram(encoded_program
);
91 Region ensemble_region_
;
96 DISALLOW_COPY_AND_ASSIGN(PatcherX86_32
);
100 #endif // COURGETTE_WIN32_X86_PATCHER_H_