roll skia 1111->1115
[chromium-blink-merge.git] / courgette / ensemble.cc
blob69e07a7a52567970bc4da53466d18a0e160da718
1 // Copyright (c) 2010 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.
5 #include "courgette/ensemble.h"
7 #include "base/basictypes.h"
8 #include "base/string_number_conversions.h"
10 #include "courgette/image_info.h"
11 #include "courgette/region.h"
12 #include "courgette/streams.h"
13 #include "courgette/simple_delta.h"
15 namespace courgette {
17 Element::Element(Kind kind, Ensemble* ensemble, const Region& region)
18 : kind_(kind), ensemble_(ensemble), region_(region) {
21 std::string Element::Name() const {
22 return ensemble_->name() + "("
23 + base::IntToString(kind()) + ","
24 + base::Uint64ToString(offset_in_ensemble()) + ","
25 + base::Uint64ToString(region().length()) + ")";
28 // A subclass of Element that has a PEInfo.
29 class ElementWinPE : public Element {
30 public:
31 ElementWinPE(Kind kind, Ensemble* ensemble, const Region& region,
32 PEInfo* info)
33 : Element(kind, ensemble, region),
34 pe_info_(info) {
37 virtual PEInfo* GetPEInfo() const { return pe_info_; }
39 protected:
40 ~ElementWinPE() { delete pe_info_; }
42 private:
43 PEInfo* pe_info_; // Owned by |this|.
46 // Scans the Ensemble's region, sniffing out Elements. We assume that the
47 // elements do not overlap.
48 Status Ensemble::FindEmbeddedElements() {
49 size_t length = region_.length();
50 const uint8* start = region_.start();
52 size_t position = 0;
53 while (position < length) {
54 // Quick test; Windows executables begin with 'MZ'.
55 if (start[position] == 'M' &&
56 position + 1 < length && start[position + 1] == 'Z') {
57 courgette::PEInfo *info = new courgette::PEInfo();
58 info->Init(start + position, length - position);
59 if (info->ParseHeader()) {
60 Region region(start + position, info->length());
62 if (info->has_text_section()) {
63 if (info->is_32bit()) {
64 Element* element = new ElementWinPE(Element::WIN32_X86_WITH_CODE,
65 this, region, info);
66 owned_elements_.push_back(element);
67 elements_.push_back(element);
68 position += region.length();
69 continue;
71 // TODO(sra): Extend to 64-bit executables.
74 // If we had a clever transformation for resource-only executables we
75 // should identify the suitable elements here:
76 if (!info->has_text_section() && false) {
77 Element* element = new ElementWinPE(Element::WIN32_NOCODE,
78 this, region, info);
79 owned_elements_.push_back(element);
80 elements_.push_back(element);
81 position += region.length();
82 continue;
85 delete info;
88 // This is where to add new formats, e.g. Linux executables, Dalvik
89 // executables etc.
91 // No Element found at current position.
92 ++position;
94 return C_OK;
97 Ensemble::~Ensemble() {
98 for (size_t i = 0; i < owned_elements_.size(); ++i)
99 delete owned_elements_[i];
102 } // namespace