Updating trunk VERSION from 874.0 to 875.0
[chromium-blink-merge.git] / courgette / win32_x86_patcher.h
blob6b850217cf7601d69b107329669ccccf766ce1bf
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 for Windows X86 executables.
7 #ifndef COURGETTE_WIN32_X86_PATCHER_H_
8 #define COURGETTE_WIN32_X86_PATCHER_H_
10 #include "courgette/ensemble.h"
12 namespace courgette {
14 // CourgetteWin32X86Patcher is a TransformationPatcher for Windows 32-bit
15 // executables.
17 class CourgetteWin32X86Patcher : public TransformationPatcher {
18 public:
19 explicit CourgetteWin32X86Patcher(const Region& region)
20 : 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;
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 = ParseWin32X86PE(ensemble_region_.start() + base_offset_,
50 base_length_,
51 &program);
52 if (status != C_OK)
53 return status;
55 EncodedProgram* encoded = NULL;
56 status = Encode(program, &encoded);
57 DeleteAssemblyProgram(program);
58 if (status != C_OK)
59 return status;
61 status = WriteEncodedProgram(encoded, transformed_element);
62 DeleteEncodedProgram(encoded);
64 return status;
67 Status Reform(SourceStreamSet* transformed_element,
68 SinkStream* reformed_element) {
69 Status status;
70 EncodedProgram* encoded_program = NULL;
71 status = ReadEncodedProgram(transformed_element, &encoded_program);
72 if (status != C_OK)
73 return status;
75 status = Assemble(encoded_program, reformed_element);
76 DeleteEncodedProgram(encoded_program);
77 if (status != C_OK)
78 return status;
80 return C_OK;
83 private:
84 Region ensemble_region_;
86 uint32 base_offset_;
87 uint32 base_length_;
89 DISALLOW_COPY_AND_ASSIGN(CourgetteWin32X86Patcher);
92 } // namespace
93 #endif // COURGETTE_WIN32_X86_PATCHER_H_