[email protected] | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 1 | // Copyright (c) 2009 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. |
| 4 | |
| 5 | // This is the transformation for Windows X86 executables. |
| 6 | |
| 7 | #ifndef COURGETTE_WIN32_X86_PATCHER_H_ |
| 8 | #define COURGETTE_WIN32_X86_PATCHER_H_ |
| 9 | |
| 10 | #include "courgette/ensemble.h" |
| 11 | |
| 12 | namespace courgette { |
| 13 | |
| 14 | // CourgetteWin32X86Patcher is a TransformationPatcher for Windows 32-bit |
| 15 | // executables. |
| 16 | // |
| 17 | class CourgetteWin32X86Patcher : public TransformationPatcher { |
| 18 | public: |
| 19 | explicit CourgetteWin32X86Patcher(const Region& region) |
| 20 | : ensemble_region_(region) { |
| 21 | } |
| 22 | |
| 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; |
| 28 | |
| 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; |
| 33 | |
| 34 | return C_OK; |
| 35 | } |
| 36 | |
| 37 | Status PredictTransformParameters(SinkStreamSet* predicted_parameters) { |
| 38 | // No code needed to write an 'empty' predicted parameter set. |
| 39 | return C_OK; |
| 40 | } |
| 41 | |
| 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. |
| 47 | |
| 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; |
| 54 | |
| 55 | EncodedProgram* encoded = NULL; |
| 56 | status = Encode(program, &encoded); |
| 57 | DeleteAssemblyProgram(program); |
| 58 | if (status != C_OK) |
| 59 | return status; |
| 60 | |
| 61 | status = WriteEncodedProgram(encoded, transformed_element); |
| 62 | DeleteEncodedProgram(encoded); |
| 63 | if (status != C_OK) |
| 64 | return status; |
| 65 | |
| 66 | return status; |
| 67 | } |
| 68 | |
| 69 | Status Reform(SourceStreamSet* transformed_element, |
| 70 | SinkStream* reformed_element) { |
| 71 | Status status; |
| 72 | EncodedProgram* encoded_program = NULL; |
| 73 | status = ReadEncodedProgram(transformed_element, &encoded_program); |
| 74 | if (status != C_OK) |
| 75 | return status; |
| 76 | |
| 77 | status = Assemble(encoded_program, reformed_element); |
| 78 | DeleteEncodedProgram(encoded_program); |
| 79 | if (status != C_OK) |
| 80 | return status; |
| 81 | |
| 82 | return C_OK; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | Region ensemble_region_; |
| 87 | |
| 88 | uint32 base_offset_; |
| 89 | uint32 base_length_; |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(CourgetteWin32X86Patcher); |
| 92 | }; |
| 93 | |
| 94 | } // namespace |
| 95 | #endif // COURGETTE_WIN32_X86_PATCHER_H_ |