Home | History | Annotate | Download | only in courgette
      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
      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 // Courgette32X86Patcher is a TransformationPatcher for Windows 32-bit
     15 // executables.
     16 //
     17 class PatcherX86_32 : public TransformationPatcher {
     18  public:
     19   explicit PatcherX86_32(const Region& region)
     20       : ensemble_region_(region),
     21         base_offset_(0),
     22         base_length_(0) {
     23   }
     24 
     25   Status Init(SourceStream* parameter_stream) {
     26     if (!parameter_stream->ReadVarint32(&base_offset_))
     27       return C_BAD_TRANSFORM;
     28     if (!parameter_stream->ReadVarint32(&base_length_))
     29       return C_BAD_TRANSFORM;
     30 
     31     if (base_offset_ > ensemble_region_.length())
     32       return C_BAD_TRANSFORM;
     33     if (base_length_ > ensemble_region_.length() - base_offset_)
     34       return C_BAD_TRANSFORM;
     35 
     36     return C_OK;
     37   }
     38 
     39   Status PredictTransformParameters(SinkStreamSet* predicted_parameters) {
     40     // No code needed to write an 'empty' predicted parameter set.
     41     return C_OK;
     42   }
     43 
     44   Status Transform(SourceStreamSet* corrected_parameters,
     45                    SinkStreamSet* transformed_element) {
     46     Status status;
     47     if (!corrected_parameters->Empty())
     48       return C_GENERAL_ERROR;   // Don't expect any corrected parameters.
     49 
     50     AssemblyProgram* program = NULL;
     51     status = ParseDetectedExecutable(ensemble_region_.start() + base_offset_,
     52                                      base_length_,
     53                                      &program);
     54     if (status != C_OK)
     55       return status;
     56 
     57     // Trim labels below a certain threshold
     58     Status trim_status = TrimLabels(program);
     59     if (trim_status != C_OK) {
     60       DeleteAssemblyProgram(program);
     61       return trim_status;
     62     }
     63 
     64     EncodedProgram* encoded = NULL;
     65     status = Encode(program, &encoded);
     66     DeleteAssemblyProgram(program);
     67     if (status != C_OK)
     68       return status;
     69 
     70     status = WriteEncodedProgram(encoded, transformed_element);
     71     DeleteEncodedProgram(encoded);
     72 
     73     return status;
     74   }
     75 
     76   Status Reform(SourceStreamSet* transformed_element,
     77                 SinkStream* reformed_element) {
     78     Status status;
     79     EncodedProgram* encoded_program = NULL;
     80     status = ReadEncodedProgram(transformed_element, &encoded_program);
     81     if (status != C_OK)
     82       return status;
     83 
     84     status = Assemble(encoded_program, reformed_element);
     85     DeleteEncodedProgram(encoded_program);
     86     if (status != C_OK)
     87       return status;
     88 
     89     return C_OK;
     90   }
     91 
     92  private:
     93   Region ensemble_region_;
     94 
     95   uint32 base_offset_;
     96   uint32 base_length_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(PatcherX86_32);
     99 };
    100 
    101 }  // namespace
    102 #endif  // COURGETTE_WIN32_X86_PATCHER_H_
    103