[Zucchini] Move Zucchini from /chrome/installer/ to /components/.

(Use "git log --follow" to see older revisions of files).

/components/ is the most logical place to put Zucchini, which only
depends on /base and /testing/gtest. This move also enables Zucchini to
be used by the Component Updater. Details:
- Move all files; run the following to change deps and guards:
  sed 's/chrome\/installer/components/' *.cc *.h -i
  sed 's/CHROME_INSTALLER/COMPONENTS/' *.cc *.h -i
  - Sorting works out pretty well!
- Change all 'chrome/installer/zucchini' to 'components/zucchini'
  throughout other parts of the repo; sort if necessary.
- Fix 6 'git cl lint' errors.
- Change 1 Bind() usage to BindRepeated().
- Update OWNER.

Bug: 729154
Change-Id: I50c5a7d411ea85f707b5994ab319dfb2a1acccf7
Reviewed-on: https://chromium-review.googlesource.com/954923
Reviewed-by: Greg Thompson <grt@chromium.org>
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: Samuel Huang <huangs@chromium.org>
Commit-Queue: Samuel Huang <huangs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542857}
NOKEYCHECK=True
GitOrigin-RevId: 577ef6c435e8d43be6e3e60ccbcbd1881780f4ec
diff --git a/image_index.cc b/image_index.cc
new file mode 100644
index 0000000..6c7a28b
--- /dev/null
+++ b/image_index.cc
@@ -0,0 +1,78 @@
+// Copyright 2017 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 "components/zucchini/image_index.h"
+
+#include <algorithm>
+#include <utility>
+
+#include "components/zucchini/algorithm.h"
+#include "components/zucchini/disassembler.h"
+
+namespace zucchini {
+
+ImageIndex::ImageIndex(ConstBufferView image)
+    : image_(image), type_tags_(image.size(), kNoTypeTag) {}
+
+ImageIndex::ImageIndex(ImageIndex&&) = default;
+
+ImageIndex::~ImageIndex() = default;
+
+bool ImageIndex::Initialize(Disassembler* disasm) {
+  std::vector<ReferenceGroup> ref_groups = disasm->MakeReferenceGroups();
+  for (const auto& group : ref_groups) {
+    // Build pool-to-type mapping.
+    DCHECK_NE(kNoPoolTag, group.pool_tag());
+    TargetPool& target_pool = target_pools_[group.pool_tag()];
+    target_pool.AddType(group.type_tag());
+    target_pool.InsertTargets(std::move(*group.GetReader(disasm)));
+  }
+  for (const auto& group : ref_groups) {
+    // Find and store all references for each type, returns false on finding
+    // any overlap, to signal error.
+    if (!InsertReferences(group.traits(),
+                          std::move(*group.GetReader(disasm)))) {
+      return false;
+    }
+  }
+  return true;
+}
+
+bool ImageIndex::IsToken(offset_t location) const {
+  TypeTag type = LookupType(location);
+
+  // |location| points into raw data.
+  if (type == kNoTypeTag)
+    return true;
+
+  // |location| points into a Reference.
+  IndirectReference reference = refs(type).at(location);
+  // Only the first byte of a reference is a token.
+  return location == reference.location;
+}
+
+bool ImageIndex::InsertReferences(const ReferenceTypeTraits& traits,
+                                  ReferenceReader&& ref_reader) {
+  // Store ReferenceSet for current type (of |group|).
+  DCHECK_NE(kNoTypeTag, traits.type_tag);
+  auto result = reference_sets_.emplace(
+      traits.type_tag, ReferenceSet(traits, pool(traits.pool_tag)));
+  DCHECK(result.second);
+
+  result.first->second.InitReferences(std::move(ref_reader));
+  for (auto ref : reference_sets_.at(traits.type_tag)) {
+    DCHECK(RangeIsBounded(ref.location, traits.width, size()));
+    auto cur_type_tag = type_tags_.begin() + ref.location;
+
+    // Check for overlap with existing reference. If found, then invalidate.
+    if (std::any_of(cur_type_tag, cur_type_tag + traits.width,
+                    [](TypeTag type) { return type != kNoTypeTag; })) {
+      return false;
+    }
+    std::fill(cur_type_tag, cur_type_tag + traits.width, traits.type_tag);
+  }
+  return true;
+}
+
+}  // namespace zucchini