Update device/bluetooth/OWNERS
[chromium-blink-merge.git] / courgette / patch_generator_x86_32.h
blob5ac017b6788e4b3f9b77ce2f82525a8ad07dfc64
1 // Copyright (c) 2011 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 and adjustment for Windows X86 executables.
7 #ifndef COURGETTE_WIN32_X86_GENERATOR_H_
8 #define COURGETTE_WIN32_X86_GENERATOR_H_
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
13 #include "courgette/ensemble.h"
15 namespace courgette {
17 class PatchGeneratorX86_32 : public TransformationPatchGenerator {
18 public:
19 PatchGeneratorX86_32(Element* old_element,
20 Element* new_element,
21 PatcherX86_32* patcher,
22 ExecutableType kind)
23 : TransformationPatchGenerator(old_element, new_element, patcher),
24 kind_(kind) {
27 virtual ExecutableType Kind() { return kind_; }
29 Status WriteInitialParameters(SinkStream* parameter_stream) {
30 if (!parameter_stream->WriteSizeVarint32(
31 old_element_->offset_in_ensemble()) ||
32 !parameter_stream->WriteSizeVarint32(old_element_->region().length())) {
33 return C_STREAM_ERROR;
35 return C_OK;
36 // TODO(sra): Initialize |patcher_| with these parameters.
39 Status PredictTransformParameters(SinkStreamSet* prediction) {
40 return TransformationPatchGenerator::PredictTransformParameters(prediction);
43 Status CorrectedTransformParameters(SinkStreamSet* parameters) {
44 // No code needed to write an 'empty' parameter set.
45 return C_OK;
48 // The format of a transformed_element is a serialized EncodedProgram. We
49 // first disassemble the original old and new Elements into AssemblyPrograms.
50 // Then we adjust the new AssemblyProgram to make it as much like the old one
51 // as possible, before converting the AssemblyPrograms to EncodedPrograms and
52 // serializing them.
53 Status Transform(SourceStreamSet* corrected_parameters,
54 SinkStreamSet* old_transformed_element,
55 SinkStreamSet* new_transformed_element) {
56 // Don't expect any corrected parameters.
57 if (!corrected_parameters->Empty())
58 return C_GENERAL_ERROR;
60 // Generate old version of program using |corrected_parameters|.
61 // TODO(sra): refactor to use same code from patcher_.
62 AssemblyProgram* old_program = NULL;
63 Status old_parse_status =
64 ParseDetectedExecutable(old_element_->region().start(),
65 old_element_->region().length(),
66 &old_program);
67 if (old_parse_status != C_OK) {
68 LOG(ERROR) << "Cannot parse as Win32X86PE " << old_element_->Name();
69 return old_parse_status;
72 AssemblyProgram* new_program = NULL;
73 Status new_parse_status =
74 ParseDetectedExecutable(new_element_->region().start(),
75 new_element_->region().length(),
76 &new_program);
77 if (new_parse_status != C_OK) {
78 DeleteAssemblyProgram(old_program);
79 LOG(ERROR) << "Cannot parse as Win32X86PE " << new_element_->Name();
80 return new_parse_status;
83 EncodedProgram* old_encoded = NULL;
84 Status old_encode_status = Encode(old_program, &old_encoded);
85 if (old_encode_status != C_OK) {
86 DeleteAssemblyProgram(old_program);
87 return old_encode_status;
90 Status old_write_status =
91 WriteEncodedProgram(old_encoded, old_transformed_element);
92 DeleteEncodedProgram(old_encoded);
93 if (old_write_status != C_OK) {
94 DeleteAssemblyProgram(old_program);
95 return old_write_status;
98 Status adjust_status = Adjust(*old_program, new_program);
99 DeleteAssemblyProgram(old_program);
100 if (adjust_status != C_OK) {
101 DeleteAssemblyProgram(new_program);
102 return adjust_status;
105 EncodedProgram* new_encoded = NULL;
106 Status new_encode_status = Encode(new_program, &new_encoded);
107 DeleteAssemblyProgram(new_program);
108 if (new_encode_status != C_OK)
109 return new_encode_status;
111 Status new_write_status =
112 WriteEncodedProgram(new_encoded, new_transformed_element);
113 DeleteEncodedProgram(new_encoded);
114 if (new_write_status != C_OK)
115 return new_write_status;
117 return C_OK;
120 Status Reform(SourceStreamSet* transformed_element,
121 SinkStream* reformed_element) {
122 return TransformationPatchGenerator::Reform(transformed_element,
123 reformed_element);
126 private:
127 virtual ~PatchGeneratorX86_32() { }
129 ExecutableType kind_;
131 DISALLOW_COPY_AND_ASSIGN(PatchGeneratorX86_32);
134 } // namespace courgette
135 #endif // COURGETTE_WIN32_X86_GENERATOR_H_