| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "courgette/ensemble.h" | 5 #include "courgette/ensemble.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 | 9 |
| 10 #include "courgette/image_info.h" | 10 #include "courgette/image_info.h" |
| 11 #include "courgette/region.h" | 11 #include "courgette/region.h" |
| 12 #include "courgette/streams.h" | 12 #include "courgette/streams.h" |
| 13 #include "courgette/simple_delta.h" | 13 #include "courgette/simple_delta.h" |
| 14 | 14 |
| 15 namespace courgette { | 15 namespace courgette { |
| 16 | 16 |
| 17 Element::Element(Kind kind, Ensemble* ensemble, const Region& region) | 17 Element::Element(ExecutableType kind, |
| 18 : kind_(kind), ensemble_(ensemble), region_(region) { | 18 Ensemble* ensemble, |
| 19 const Region& region, |
| 20 PEInfo* info) |
| 21 : kind_(kind), ensemble_(ensemble), region_(region), info_(info) { |
| 22 } |
| 23 |
| 24 Element::~Element() { |
| 25 delete info_; |
| 19 } | 26 } |
| 20 | 27 |
| 21 std::string Element::Name() const { | 28 std::string Element::Name() const { |
| 22 return ensemble_->name() + "(" | 29 return ensemble_->name() + "(" |
| 23 + base::IntToString(kind()) + "," | 30 + base::IntToString(kind()) + "," |
| 24 + base::Uint64ToString(offset_in_ensemble()) + "," | 31 + base::Uint64ToString(offset_in_ensemble()) + "," |
| 25 + base::Uint64ToString(region().length()) + ")"; | 32 + base::Uint64ToString(region().length()) + ")"; |
| 26 } | 33 } |
| 27 | 34 |
| 28 // A subclass of Element that has a PEInfo. | |
| 29 class ElementWinPE : public Element { | |
| 30 public: | |
| 31 ElementWinPE(Kind kind, Ensemble* ensemble, const Region& region, | |
| 32 PEInfo* info) | |
| 33 : Element(kind, ensemble, region), | |
| 34 pe_info_(info) { | |
| 35 } | |
| 36 | |
| 37 virtual PEInfo* GetPEInfo() const { return pe_info_; } | |
| 38 | |
| 39 protected: | |
| 40 ~ElementWinPE() { delete pe_info_; } | |
| 41 | |
| 42 private: | |
| 43 PEInfo* pe_info_; // Owned by |this|. | |
| 44 }; | |
| 45 | |
| 46 // Scans the Ensemble's region, sniffing out Elements. We assume that the | 35 // Scans the Ensemble's region, sniffing out Elements. We assume that the |
| 47 // elements do not overlap. | 36 // elements do not overlap. |
| 48 Status Ensemble::FindEmbeddedElements() { | 37 Status Ensemble::FindEmbeddedElements() { |
| 38 |
| 49 size_t length = region_.length(); | 39 size_t length = region_.length(); |
| 50 const uint8* start = region_.start(); | 40 const uint8* start = region_.start(); |
| 51 | 41 |
| 52 size_t position = 0; | 42 size_t position = 0; |
| 53 while (position < length) { | 43 while (position < length) { |
| 54 // Quick test; Windows executables begin with 'MZ'. | 44 ExecutableType type = DetectExecutableType(start + position, |
| 55 if (start[position] == 'M' && | 45 length - position); |
| 56 position + 1 < length && start[position + 1] == 'Z') { | 46 |
| 57 courgette::PEInfo *info = new courgette::PEInfo(); | 47 // |
| 58 info->Init(start + position, length - position); | 48 // TODO(dgarrett) This switch can go away totally after two things. |
| 59 if (info->ParseHeader()) { | 49 // |
| 50 // Make ImageInfo generic for all executable types. |
| 51 // Find a generic way to handle length detection for executables. |
| 52 // |
| 53 // When this switch is gone, that's one less piece of code that is |
| 54 // executable type aware. |
| 55 // |
| 56 switch (type) { |
| 57 case UNKNOWN: { |
| 58 // No Element found at current position. |
| 59 ++position; |
| 60 break; |
| 61 } |
| 62 case WIN32_X86: { |
| 63 // The Info is only created to detect the length of the executable |
| 64 courgette::PEInfo* info(new courgette::PEInfo()); |
| 65 info->Init(start + position, length - position); |
| 66 if (!info->ParseHeader()) { |
| 67 delete info; |
| 68 position++; |
| 69 break; |
| 70 } |
| 60 Region region(start + position, info->length()); | 71 Region region(start + position, info->length()); |
| 61 | 72 |
| 62 if (info->has_text_section()) { | 73 Element* element = new Element(type, this, region, info); |
| 63 if (info->is_32bit()) { | 74 owned_elements_.push_back(element); |
| 64 Element* element = new ElementWinPE(Element::WIN32_X86_WITH_CODE, | 75 elements_.push_back(element); |
| 65 this, region, info); | 76 position += region.length(); |
| 66 owned_elements_.push_back(element); | 77 break; |
| 67 elements_.push_back(element); | |
| 68 position += region.length(); | |
| 69 continue; | |
| 70 } | |
| 71 // TODO(sra): Extend to 64-bit executables. | |
| 72 } | |
| 73 | |
| 74 // If we had a clever transformation for resource-only executables we | |
| 75 // should identify the suitable elements here: | |
| 76 if (!info->has_text_section() && false) { | |
| 77 Element* element = new ElementWinPE(Element::WIN32_NOCODE, | |
| 78 this, region, info); | |
| 79 owned_elements_.push_back(element); | |
| 80 elements_.push_back(element); | |
| 81 position += region.length(); | |
| 82 continue; | |
| 83 } | |
| 84 } | 78 } |
| 85 delete info; | |
| 86 } | 79 } |
| 87 | |
| 88 // This is where to add new formats, e.g. Linux executables, Dalvik | |
| 89 // executables etc. | |
| 90 | |
| 91 // No Element found at current position. | |
| 92 ++position; | |
| 93 } | 80 } |
| 94 return C_OK; | 81 return C_OK; |
| 95 } | 82 } |
| 96 | 83 |
| 97 Ensemble::~Ensemble() { | 84 Ensemble::~Ensemble() { |
| 98 for (size_t i = 0; i < owned_elements_.size(); ++i) | 85 for (size_t i = 0; i < owned_elements_.size(); ++i) |
| 99 delete owned_elements_[i]; | 86 delete owned_elements_[i]; |
| 100 } | 87 } |
| 101 | 88 |
| 102 } // namespace | 89 } // namespace |
| OLD | NEW |