Avi Drissman | 0ff8a7e | 2022-09-13 18:35:42 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 5 | #include "courgette/ensemble.h" |
| 6 | |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
avi@chromium.org | 68c986e | 2013-06-11 13:26:25 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
huangs | 8d5be25 | 2016-01-29 22:14:18 | [diff] [blame] | 11 | #include "courgette/program_detector.h" |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 12 | #include "courgette/region.h" |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 13 | #include "courgette/simple_delta.h" |
avi@chromium.org | 68c986e | 2013-06-11 13:26:25 | [diff] [blame] | 14 | #include "courgette/streams.h" |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 15 | |
| 16 | namespace courgette { |
| 17 | |
dgarrett@chromium.org | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 18 | Element::Element(ExecutableType kind, |
| 19 | Ensemble* ensemble, |
dgarrett@chromium.org | 423a381 | 2011-10-26 00:50:20 | [diff] [blame] | 20 | const Region& region) |
| 21 | : kind_(kind), ensemble_(ensemble), region_(region) { |
dgarrett@chromium.org | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 22 | } |
| 23 | |
Chris Watkins | 2903a82 | 2017-11-30 03:19:57 | [diff] [blame] | 24 | Element::~Element() = default; |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 25 | |
| 26 | std::string Element::Name() const { |
Raul Tambre | e7ef5c5 | 2019-02-11 19:54:50 | [diff] [blame] | 27 | return ensemble_->name() + "(" + base::NumberToString(kind()) + "," + |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 28 | base::NumberToString(offset_in_ensemble()) + "," + |
| 29 | base::NumberToString(region().length()) + ")"; |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 30 | } |
| 31 | |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 32 | // Scans the Ensemble's region, sniffing out Elements. We assume that the |
| 33 | // elements do not overlap. |
| 34 | Status Ensemble::FindEmbeddedElements() { |
dgarrett@chromium.org | 93d529ca | 2011-10-21 22:24:19 | [diff] [blame] | 35 | |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 36 | size_t length = region_.length(); |
avi | ab98dcc9 | 2015-12-21 19:35:33 | [diff] [blame] | 37 | const uint8_t* start = region_.start(); |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 38 | |
| 39 | size_t position = 0; |
| 40 | while (position < length) { |
dgarrett@chromium.org | 423a381 | 2011-10-26 00:50:20 | [diff] [blame] | 41 | ExecutableType type; |
| 42 | size_t detected_length; |
dgarrett@chromium.org | 423a381 | 2011-10-26 00:50:20 | [diff] [blame] | 43 | Status result = DetectExecutableType(start + position, |
| 44 | length - position, |
| 45 | &type, &detected_length); |
dgarrett@chromium.org | 423a381 | 2011-10-26 00:50:20 | [diff] [blame] | 46 | if (result == C_OK) { |
| 47 | Region region(start + position, detected_length); |
| 48 | |
| 49 | Element* element = new Element(type, this, region); |
| 50 | owned_elements_.push_back(element); |
| 51 | elements_.push_back(element); |
| 52 | position += region.length(); |
| 53 | } else { |
| 54 | position++; |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 55 | } |
sra@chromium.org | 04ca1bc | 2009-05-08 23:00:29 | [diff] [blame] | 56 | } |
| 57 | return C_OK; |
| 58 | } |
| 59 | |
| 60 | Ensemble::~Ensemble() { |
| 61 | for (size_t i = 0; i < owned_elements_.size(); ++i) |
| 62 | delete owned_elements_[i]; |
| 63 | } |
| 64 | |
| 65 | } // namespace |