| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COURGETTE_WIN32_X86_PATCHER_H_ | 5 #ifndef COURGETTE_PATCHER_X86_32_H_ |
| 6 #define COURGETTE_WIN32_X86_PATCHER_H_ | 6 #define COURGETTE_PATCHER_X86_32_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "courgette/assembly_program.h" | 13 #include "courgette/assembly_program.h" |
| 14 #include "courgette/courgette_flow.h" |
| 14 #include "courgette/encoded_program.h" | 15 #include "courgette/encoded_program.h" |
| 15 #include "courgette/ensemble.h" | 16 #include "courgette/ensemble.h" |
| 16 #include "courgette/program_detector.h" | 17 #include "courgette/program_detector.h" |
| 17 | 18 |
| 18 namespace courgette { | 19 namespace courgette { |
| 19 | 20 |
| 20 // PatcherX86_32 is the universal patcher for all executables. The executable | 21 // PatcherX86_32 is the universal patcher for all executables. The executable |
| 21 // type is determined by ParseDetectedExecutable function. | 22 // type is determined by the program detector. |
| 22 // | |
| 23 class PatcherX86_32 : public TransformationPatcher { | 23 class PatcherX86_32 : public TransformationPatcher { |
| 24 public: | 24 public: |
| 25 explicit PatcherX86_32(const Region& region) | 25 explicit PatcherX86_32(const Region& region) |
| 26 : ensemble_region_(region), | 26 : ensemble_region_(region), |
| 27 base_offset_(0), | 27 base_offset_(0), |
| 28 base_length_(0) { | 28 base_length_(0) { |
| 29 } | 29 } |
| 30 | 30 |
| 31 Status Init(SourceStream* parameter_stream) { | 31 Status Init(SourceStream* parameter_stream) { |
| 32 if (!parameter_stream->ReadVarint32(&base_offset_)) | 32 if (!parameter_stream->ReadVarint32(&base_offset_)) |
| 33 return C_BAD_TRANSFORM; | 33 return C_BAD_TRANSFORM; |
| 34 if (!parameter_stream->ReadVarint32(&base_length_)) | 34 if (!parameter_stream->ReadVarint32(&base_length_)) |
| 35 return C_BAD_TRANSFORM; | 35 return C_BAD_TRANSFORM; |
| 36 | 36 |
| 37 if (base_offset_ > ensemble_region_.length()) | 37 if (base_offset_ > ensemble_region_.length()) |
| 38 return C_BAD_TRANSFORM; | 38 return C_BAD_TRANSFORM; |
| 39 if (base_length_ > ensemble_region_.length() - base_offset_) | 39 if (base_length_ > ensemble_region_.length() - base_offset_) |
| 40 return C_BAD_TRANSFORM; | 40 return C_BAD_TRANSFORM; |
| 41 | 41 |
| 42 return C_OK; | 42 return C_OK; |
| 43 } | 43 } |
| 44 | 44 |
| 45 Status PredictTransformParameters(SinkStreamSet* predicted_parameters) { | 45 Status PredictTransformParameters(SinkStreamSet* predicted_parameters) { |
| 46 // No code needed to write an 'empty' predicted parameter set. | 46 // No code needed to write an 'empty' predicted parameter set. |
| 47 return C_OK; | 47 return C_OK; |
| 48 } | 48 } |
| 49 | 49 |
| 50 Status Transform(SourceStreamSet* corrected_parameters, | 50 Status Transform(SourceStreamSet* corrected_parameters, |
| 51 SinkStreamSet* transformed_element) { | 51 SinkStreamSet* transformed_element) { |
| 52 Status status; | 52 CourgetteFlow flow; |
| 53 if (!corrected_parameters->Empty()) | 53 RegionBuffer only_buffer( |
| 54 return C_GENERAL_ERROR; // Don't expect any corrected parameters. | 54 Region(ensemble_region_.start() + base_offset_, base_length_)); |
| 55 | 55 flow.ReadAssemblyProgramFromBuffer(flow.ONLY, only_buffer, false); |
| 56 std::unique_ptr<AssemblyProgram> program; | 56 flow.CreateEncodedProgramFromAssemblyProgram(flow.ONLY); |
| 57 status = ParseDetectedExecutable(ensemble_region_.start() + base_offset_, | 57 flow.DestroyAssemblyProgram(flow.ONLY); |
| 58 base_length_, | 58 flow.WriteSinkStreamSetFromEncodedProgram(flow.ONLY, transformed_element); |
| 59 &program); | 59 if (flow.failed()) |
| 60 if (status != C_OK) | 60 LOG(ERROR) << flow.message(); |
| 61 return status; | 61 return flow.status(); |
| 62 | |
| 63 std::unique_ptr<EncodedProgram> encoded; | |
| 64 status = Encode(*program, &encoded); | |
| 65 if (status != C_OK) | |
| 66 return status; | |
| 67 | |
| 68 program.reset(); | |
| 69 | |
| 70 return WriteEncodedProgram(encoded.get(), transformed_element); | |
| 71 } | 62 } |
| 72 | 63 |
| 73 Status Reform(SourceStreamSet* transformed_element, | 64 Status Reform(SourceStreamSet* transformed_element, |
| 74 SinkStream* reformed_element) { | 65 SinkStream* reformed_element) { |
| 75 Status status; | 66 CourgetteFlow flow; |
| 76 std::unique_ptr<EncodedProgram> encoded_program; | 67 flow.ReadEncodedProgramFromSourceStreamSet(flow.ONLY, transformed_element); |
| 77 status = ReadEncodedProgram(transformed_element, &encoded_program); | 68 flow.WriteExecutableFromEncodedProgram(flow.ONLY, reformed_element); |
| 78 if (status != C_OK) | 69 if (flow.failed()) |
| 79 return status; | 70 LOG(ERROR) << flow.message(); |
| 80 | 71 return flow.status(); |
| 81 return Assemble(encoded_program.get(), reformed_element); | |
| 82 } | 72 } |
| 83 | 73 |
| 84 private: | 74 private: |
| 85 Region ensemble_region_; | 75 Region ensemble_region_; |
| 86 | 76 |
| 87 uint32_t base_offset_; | 77 uint32_t base_offset_; |
| 88 uint32_t base_length_; | 78 uint32_t base_length_; |
| 89 | 79 |
| 90 DISALLOW_COPY_AND_ASSIGN(PatcherX86_32); | 80 DISALLOW_COPY_AND_ASSIGN(PatcherX86_32); |
| 91 }; | 81 }; |
| 92 | 82 |
| 93 } // namespace | 83 } // namespace courgette |
| 94 | 84 |
| 95 #endif // COURGETTE_WIN32_X86_PATCHER_H_ | 85 #endif // COURGETTE_PATCHER_X86_32_H_ |
| OLD | NEW |