- add sources.
[platform/framework/web/crosswalk.git] / src / 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 "base/basictypes.h"
8 #include "base/strings/string_number_conversions.h"
9
10 #include "courgette/region.h"
11 #include "courgette/simple_delta.h"
12 #include "courgette/streams.h"
13
14 namespace courgette {
15
16 Element::Element(ExecutableType kind,
17                  Ensemble* ensemble,
18                  const Region& region)
19     : kind_(kind), ensemble_(ensemble), region_(region) {
20 }
21
22 Element::~Element() {}
23
24 std::string Element::Name() const {
25   return ensemble_->name() + "("
26       + base::IntToString(kind()) + ","
27       + base::Uint64ToString(offset_in_ensemble()) + ","
28       + base::Uint64ToString(region().length()) + ")";
29 }
30
31 // Scans the Ensemble's region, sniffing out Elements.  We assume that the
32 // elements do not overlap.
33 Status Ensemble::FindEmbeddedElements() {
34
35   size_t length = region_.length();
36   const uint8* start = region_.start();
37
38   size_t position = 0;
39   while (position < length) {
40     ExecutableType type;
41     size_t detected_length;
42
43     Status result = DetectExecutableType(start + position,
44                                          length - position,
45                                          &type, &detected_length);
46
47     if (result == C_OK) {
48       Region region(start + position, detected_length);
49
50       Element* element = new Element(type, this, region);
51       owned_elements_.push_back(element);
52       elements_.push_back(element);
53       position += region.length();
54     } else {
55       position++;
56     }
57   }
58   return C_OK;
59 }
60
61 Ensemble::~Ensemble() {
62   for (size_t i = 0;  i < owned_elements_.size();  ++i)
63     delete owned_elements_[i];
64 }
65
66 }  // namespace