Don't reload twice in key_mobile_sites
[chromium-blink-merge.git] / courgette / patcher_x86_32.h
blobe984579f040e04ce556e8689a10de40cf866b52a
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"
10 namespace courgette {
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 {
16 public:
17 explicit PatcherX86_32(const Region& region)
18 : ensemble_region_(region),
19 base_offset_(0),
20 base_length_(0) {
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;
34 return C_OK;
37 Status PredictTransformParameters(SinkStreamSet* predicted_parameters) {
38 // No code needed to write an 'empty' predicted parameter set.
39 return C_OK;
42 Status Transform(SourceStreamSet* corrected_parameters,
43 SinkStreamSet* transformed_element) {
44 Status status;
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_,
50 base_length_,
51 &program);
52 if (status != C_OK)
53 return status;
55 // Trim labels below a certain threshold
56 Status trim_status = TrimLabels(program);
57 if (trim_status != C_OK) {
58 DeleteAssemblyProgram(program);
59 return trim_status;
62 EncodedProgram* encoded = NULL;
63 status = Encode(program, &encoded);
64 DeleteAssemblyProgram(program);
65 if (status != C_OK)
66 return status;
68 status = WriteEncodedProgram(encoded, transformed_element);
69 DeleteEncodedProgram(encoded);
71 return status;
74 Status Reform(SourceStreamSet* transformed_element,
75 SinkStream* reformed_element) {
76 Status status;
77 EncodedProgram* encoded_program = NULL;
78 status = ReadEncodedProgram(transformed_element, &encoded_program);
79 if (status != C_OK)
80 return status;
82 status = Assemble(encoded_program, reformed_element);
83 DeleteEncodedProgram(encoded_program);
84 if (status != C_OK)
85 return status;
87 return C_OK;
90 private:
91 Region ensemble_region_;
93 uint32 base_offset_;
94 uint32 base_length_;
96 DISALLOW_COPY_AND_ASSIGN(PatcherX86_32);
99 } // namespace
100 #endif // COURGETTE_WIN32_X86_PATCHER_H_