summaryrefslogtreecommitdiffstats
path: root/courgette/patch_generator_x86_32.h
diff options
context:
space:
mode:
authordgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-31 22:09:40 +0000
committerdgarrett@chromium.org <dgarrett@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-31 22:09:40 +0000
commitc5fc1df3631f016cc609a2a022fea324a0f0e4bd (patch)
tree903589aec8931178fba92e4099b6fb3cd1691c9d /courgette/patch_generator_x86_32.h
parent6915c26061c9a6e67602d72ebc7bb7e02a593e9d (diff)
downloadchromium_src-c5fc1df3631f016cc609a2a022fea324a0f0e4bd.zip
chromium_src-c5fc1df3631f016cc609a2a022fea324a0f0e4bd.tar.gz
chromium_src-c5fc1df3631f016cc609a2a022fea324a0f0e4bd.tar.bz2
Last small bit of refactoring.
Move the Win32X86Generator and Patcher classes to non-windows specific names since they can be reused untouched for Elf. Move them from one file to files with matching names while at it. Store the transformation kind on the generator class so that it can be know if it's PE or Elf. Unified the TransformationId and ExecutableType enums into a single enum used everywhere (defined in courgette.h since it's now part of the external API). BUG=chromium-os:22149 TEST=Unittests Review URL: http://codereview.chromium.org/8417045 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@108019 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'courgette/patch_generator_x86_32.h')
-rw-r--r--courgette/patch_generator_x86_32.h135
1 files changed, 135 insertions, 0 deletions
diff --git a/courgette/patch_generator_x86_32.h b/courgette/patch_generator_x86_32.h
new file mode 100644
index 0000000..0e3b0a8
--- /dev/null
+++ b/courgette/patch_generator_x86_32.h
@@ -0,0 +1,135 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This is the transformation and adjustment for Windows X86 executables.
+
+#ifndef COURGETTE_WIN32_X86_GENERATOR_H_
+#define COURGETTE_WIN32_X86_GENERATOR_H_
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+
+#include "courgette/ensemble.h"
+
+namespace courgette {
+
+class PatchGeneratorX86_32 : public TransformationPatchGenerator {
+ public:
+ PatchGeneratorX86_32(Element* old_element,
+ Element* new_element,
+ PatcherX86_32* patcher,
+ ExecutableType kind)
+ : TransformationPatchGenerator(old_element, new_element, patcher),
+ kind_(kind) {
+ }
+
+ virtual ExecutableType Kind() { return kind_; }
+
+ Status WriteInitialParameters(SinkStream* parameter_stream) {
+ if (!parameter_stream->WriteSizeVarint32(
+ old_element_->offset_in_ensemble()) ||
+ !parameter_stream->WriteSizeVarint32(old_element_->region().length())) {
+ return C_STREAM_ERROR;
+ }
+ return C_OK;
+ // TODO(sra): Initialize |patcher_| with these parameters.
+ }
+
+ Status PredictTransformParameters(SinkStreamSet* prediction) {
+ return TransformationPatchGenerator::PredictTransformParameters(prediction);
+ }
+
+ Status CorrectedTransformParameters(SinkStreamSet* parameters) {
+ // No code needed to write an 'empty' parameter set.
+ return C_OK;
+ }
+
+ // The format of a transformed_element is a serialized EncodedProgram. We
+ // first disassemble the original old and new Elements into AssemblyPrograms.
+ // Then we adjust the new AssemblyProgram to make it as much like the old one
+ // as possible, before converting the AssemblyPrograms to EncodedPrograms and
+ // serializing them.
+ Status Transform(SourceStreamSet* corrected_parameters,
+ SinkStreamSet* old_transformed_element,
+ SinkStreamSet* new_transformed_element) {
+ // Don't expect any corrected parameters.
+ if (!corrected_parameters->Empty())
+ return C_GENERAL_ERROR;
+
+ // Generate old version of program using |corrected_parameters|.
+ // TODO(sra): refactor to use same code from patcher_.
+ AssemblyProgram* old_program = NULL;
+ Status old_parse_status =
+ ParseDetectedExecutable(old_element_->region().start(),
+ old_element_->region().length(),
+ &old_program);
+ if (old_parse_status != C_OK) {
+ LOG(ERROR) << "Cannot parse as Win32X86PE " << old_element_->Name();
+ return old_parse_status;
+ }
+
+ AssemblyProgram* new_program = NULL;
+ Status new_parse_status =
+ ParseDetectedExecutable(new_element_->region().start(),
+ new_element_->region().length(),
+ &new_program);
+ if (new_parse_status != C_OK) {
+ DeleteAssemblyProgram(old_program);
+ LOG(ERROR) << "Cannot parse as Win32X86PE " << new_element_->Name();
+ return new_parse_status;
+ }
+
+ EncodedProgram* old_encoded = NULL;
+ Status old_encode_status = Encode(old_program, &old_encoded);
+ if (old_encode_status != C_OK) {
+ DeleteAssemblyProgram(old_program);
+ return old_encode_status;
+ }
+
+ Status old_write_status =
+ WriteEncodedProgram(old_encoded, old_transformed_element);
+ DeleteEncodedProgram(old_encoded);
+ if (old_write_status != C_OK) {
+ DeleteAssemblyProgram(old_program);
+ return old_write_status;
+ }
+
+ Status adjust_status = Adjust(*old_program, new_program);
+ DeleteAssemblyProgram(old_program);
+ if (adjust_status != C_OK) {
+ DeleteAssemblyProgram(new_program);
+ return adjust_status;
+ }
+
+ EncodedProgram* new_encoded = NULL;
+ Status new_encode_status = Encode(new_program, &new_encoded);
+ DeleteAssemblyProgram(new_program);
+ if (new_encode_status != C_OK)
+ return new_encode_status;
+
+ Status new_write_status =
+ WriteEncodedProgram(new_encoded, new_transformed_element);
+ DeleteEncodedProgram(new_encoded);
+ if (new_write_status != C_OK)
+ return new_write_status;
+
+ return C_OK;
+ }
+
+ Status Reform(SourceStreamSet* transformed_element,
+ SinkStream* reformed_element) {
+ return TransformationPatchGenerator::Reform(transformed_element,
+ reformed_element);
+ }
+
+ private:
+ virtual ~PatchGeneratorX86_32() { }
+
+ ExecutableType kind_;
+
+ DISALLOW_COPY_AND_ASSIGN(PatchGeneratorX86_32);
+};
+
+} // namespace courgette
+#endif // COURGETTE_WIN32_X86_GENERATOR_H_