1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/ensemble.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "courgette/program_detector.h"
12 #include "courgette/region.h"
13 #include "courgette/simple_delta.h"
14 #include "courgette/streams.h"
18 Element::Element(ExecutableType kind,
21 : kind_(kind), ensemble_(ensemble), region_(region) {
24 Element::~Element() = default;
26 std::string Element::Name() const {
27 return ensemble_->name() + "(" + base::NumberToString(kind()) + "," +
28 base::NumberToString(offset_in_ensemble()) + "," +
29 base::NumberToString(region().length()) + ")";
32 // Scans the Ensemble's region, sniffing out Elements. We assume that the
33 // elements do not overlap.
34 Status Ensemble::FindEmbeddedElements() {
36 size_t length = region_.length();
37 const uint8_t* start = region_.start();
40 while (position < length) {
42 size_t detected_length;
43 Status result = DetectExecutableType(start + position,
45 &type, &detected_length);
47 Region region(start + position, detected_length);
49 Element* element = new Element(type, this, region);
50 owned_elements_.push_back(element);
51 elements_.push_back(element);
52 position += region.length();
60 Ensemble::~Ensemble() {
61 for (size_t i = 0; i < owned_elements_.size(); ++i)
62 delete owned_elements_[i];