Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / courgette / ensemble.cc
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
3 // found in the LICENSE file.
4
5 #include "courgette/ensemble.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
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"
15
16 namespace courgette {
17
18 Element::Element(ExecutableType kind,
19                  Ensemble* ensemble,
20                  const Region& region)
21     : kind_(kind), ensemble_(ensemble), region_(region) {
22 }
23
24 Element::~Element() = default;
25
26 std::string Element::Name() const {
27   return ensemble_->name() + "(" + base::IntToString(kind()) + "," +
28          base::NumberToString(offset_in_ensemble()) + "," +
29          base::NumberToString(region().length()) + ")";
30 }
31
32 // Scans the Ensemble's region, sniffing out Elements.  We assume that the
33 // elements do not overlap.
34 Status Ensemble::FindEmbeddedElements() {
35
36   size_t length = region_.length();
37   const uint8_t* start = region_.start();
38
39   size_t position = 0;
40   while (position < length) {
41     ExecutableType type;
42     size_t detected_length;
43     Status result = DetectExecutableType(start + position,
44                                          length - position,
45                                          &type, &detected_length);
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++;
55     }
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