Revert 224458 "Enabling MediaStreamInfoBarTest.DenyingCameraDoes..."
[chromium-blink-merge.git] / courgette / patcher_x86_32.h
blob9488270ba4764cb3344cd0105f452159c759f236
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.
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 // Courgette32X86Patcher is a TransformationPatcher for Windows 32-bit
15 // executables.
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) {
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;
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;
36 return C_OK;
39 Status PredictTransformParameters(SinkStreamSet* predicted_parameters) {
40 // No code needed to write an 'empty' predicted parameter set.
41 return C_OK;
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.
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;
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;
64 EncodedProgram* encoded = NULL;
65 status = Encode(program, &encoded);
66 DeleteAssemblyProgram(program);
67 if (status != C_OK)
68 return status;
70 status = WriteEncodedProgram(encoded, transformed_element);
71 DeleteEncodedProgram(encoded);
73 return status;
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;
84 status = Assemble(encoded_program, reformed_element);
85 DeleteEncodedProgram(encoded_program);
86 if (status != C_OK)
87 return status;
89 return C_OK;
92 private:
93 Region ensemble_region_;
95 uint32 base_offset_;
96 uint32 base_length_;
98 DISALLOW_COPY_AND_ASSIGN(PatcherX86_32);
101 } // namespace
102 #endif // COURGETTE_WIN32_X86_PATCHER_H_