summaryrefslogtreecommitdiffstats
path: root/courgette/ensemble.cc
blob: a2bea8f38b7d6086ed8ace733927e27739a5fbdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "courgette/ensemble.h"

#include "base/basictypes.h"
#include "base/string_number_conversions.h"

#include "courgette/image_info.h"
#include "courgette/region.h"
#include "courgette/streams.h"
#include "courgette/simple_delta.h"

namespace courgette {

Element::Element(ExecutableType kind,
                 Ensemble* ensemble,
                 const Region& region,
                 PEInfo* info)
    : kind_(kind), ensemble_(ensemble), region_(region), info_(info) {
}

Element::~Element() {
  delete info_;
}

std::string Element::Name() const {
  return ensemble_->name() + "("
      + base::IntToString(kind()) + ","
      + base::Uint64ToString(offset_in_ensemble()) + ","
      + base::Uint64ToString(region().length()) + ")";
}

// Scans the Ensemble's region, sniffing out Elements.  We assume that the
// elements do not overlap.
Status Ensemble::FindEmbeddedElements() {

  size_t length = region_.length();
  const uint8* start = region_.start();

  size_t position = 0;
  while (position < length) {
    ExecutableType type = DetectExecutableType(start + position,
                                               length - position);

    //
    // TODO(dgarrett) This switch can go away totally after two things.
    //
    // Make ImageInfo generic for all executable types.
    // Find a generic way to handle length detection for executables.
    //
    // When this switch is gone, that's one less piece of code that is
    // executable type aware.
    //
    switch (type) {
      case UNKNOWN: {
        // No Element found at current position.
        ++position;
        break;
      }
      case WIN32_X86: {
        // The Info is only created to detect the length of the executable
        courgette::PEInfo* info(new courgette::PEInfo());
        info->Init(start + position, length - position);
        if (!info->ParseHeader()) {
          delete info;
          position++;
          break;
        }
        Region region(start + position, info->length());

        Element* element = new Element(type, this, region, info);
        owned_elements_.push_back(element);
        elements_.push_back(element);
        position += region.length();
        break;
      }
    }
  }
  return C_OK;
}

Ensemble::~Ensemble() {
  for (size_t i = 0;  i < owned_elements_.size();  ++i)
    delete owned_elements_[i];
}

}  // namespace