+#ifndef LDID_NOTOOLS
+static void copy(std::streambuf &source, std::streambuf &target, size_t length, const Progress &progress) {
+ progress(0);
+ size_t total(0);
+ for (;;) {
+ char data[4096 * 4];
+ size_t writ(source.sgetn(data, sizeof(data)));
+ if (writ == 0)
+ break;
+ _assert(target.sputn(data, writ) == writ);
+ total += writ;
+ progress(double(total) / length);
+ }
+}
+
+#ifndef LDID_NOPLIST
+static plist_t plist(const std::string &data) {
+ plist_t plist(NULL);
+ if (Starts(data, "bplist00"))
+ plist_from_bin(data.data(), data.size(), &plist);
+ else
+ plist_from_xml(data.data(), data.size(), &plist);
+ _assert(plist != NULL);
+ return plist;
+}
+
+static void plist_d(std::streambuf &buffer, size_t length, const Functor<void (plist_t)> &code) {
+ std::stringbuf data;
+ copy(buffer, data, length, dummy_);
+ auto node(plist(data.str()));
+ _scope({ plist_free(node); });
+ _assert(plist_get_node_type(node) == PLIST_DICT);
+ code(node);
+}
+
+static std::string plist_s(plist_t node) {
+ _assert(node != NULL);
+ _assert(plist_get_node_type(node) == PLIST_STRING);
+ char *data;
+ plist_get_string_val(node, &data);
+ _scope({ free(data); });
+ return data;
+}
+#endif
+
+enum Mode {
+ NoMode,
+ OptionalMode,
+ OmitMode,
+ NestedMode,
+ TopMode,
+};
+
+class Expression {
+ private:
+ regex_t regex_;
+ std::vector<std::string> matches_;
+
+ public:
+ Expression(const std::string &code) {
+ _assert_(regcomp(®ex_, code.c_str(), REG_EXTENDED) == 0, "regcomp()");
+ matches_.resize(regex_.re_nsub + 1);
+ }
+
+ ~Expression() {
+ regfree(®ex_);
+ }
+
+ bool operator ()(const std::string &data) {
+ regmatch_t matches[matches_.size()];
+ auto value(regexec(®ex_, data.c_str(), matches_.size(), matches, 0));
+ if (value == REG_NOMATCH)
+ return false;
+ _assert_(value == 0, "regexec()");
+ for (size_t i(0); i != matches_.size(); ++i)
+ matches_[i].assign(data.data() + matches[i].rm_so, matches[i].rm_eo - matches[i].rm_so);
+ return true;
+ }
+
+ const std::string &operator [](size_t index) const {
+ return matches_[index];
+ }
+};
+
+struct Rule {
+ unsigned weight_;
+ Mode mode_;
+ std::string code_;
+
+ mutable std::auto_ptr<Expression> regex_;
+
+ Rule(unsigned weight, Mode mode, const std::string &code) :
+ weight_(weight),
+ mode_(mode),
+ code_(code)
+ {
+ }
+
+ Rule(const Rule &rhs) :
+ weight_(rhs.weight_),
+ mode_(rhs.mode_),
+ code_(rhs.code_)
+ {
+ }
+
+ void Compile() const {
+ regex_.reset(new Expression(code_));
+ }
+
+ bool operator ()(const std::string &data) const {
+ _assert(regex_.get() != NULL);
+ return (*regex_)(data);
+ }
+
+ bool operator <(const Rule &rhs) const {
+ if (weight_ > rhs.weight_)
+ return true;
+ if (weight_ < rhs.weight_)
+ return false;
+ return mode_ > rhs.mode_;
+ }
+};
+
+struct RuleCode {
+ bool operator ()(const Rule *lhs, const Rule *rhs) const {
+ return lhs->code_ < rhs->code_;
+ }
+};
+
+#ifndef LDID_NOPLIST
+static Hash Sign(const uint8_t *prefix, size_t size, std::streambuf &buffer, Hash &hash, std::streambuf &save, const std::string &identifier, const std::string &entitlements, bool merge, const std::string &requirements, const std::string &key, const Slots &slots, size_t length, uint32_t flags, bool platform, const Progress &progress) {
+ // XXX: this is a miserable fail
+ std::stringbuf temp;
+ put(temp, prefix, size);
+ copy(buffer, temp, length - size, progress);
+ // XXX: this is a stupid hack
+ pad(temp, 0x10 - (length & 0xf));
+ auto data(temp.str());
+
+ HashProxy proxy(hash, save);
+ return Sign(data.data(), data.size(), proxy, identifier, entitlements, merge, requirements, key, slots, flags, platform, progress);
+}
+
+struct State {
+ std::map<std::string, Hash> files;
+ std::map<std::string, std::string> links;
+
+ void Merge(const std::string &root, const State &state) {
+ for (const auto &entry : state.files)
+ files[root + entry.first] = entry.second;
+ for (const auto &entry : state.links)
+ links[root + entry.first] = entry.second;
+ }
+};
+
+Bundle Sign(const std::string &root, Folder &parent, const std::string &key, State &remote, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, const Progress &progress) {
+ std::string executable;
+ std::string identifier;
+
+ bool mac(false);
+
+ std::string info("Info.plist");
+
+ SubFolder folder(parent, [&]() {
+ if (parent.Look(info))
+ return "";
+ mac = true;
+ if (false);
+ else if (parent.Look("Contents/" + info))
+ return "Contents/";
+ else if (parent.Look("Resources/" + info)) {
+ info = "Resources/" + info;
+ return "";
+ } else _assert_(false, "cannot find Info.plist");
+ }());
+
+ folder.Open(info, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
+ plist_d(buffer, length, fun([&](plist_t node) {
+ executable = plist_s(plist_dict_get_item(node, "CFBundleExecutable"));
+ identifier = plist_s(plist_dict_get_item(node, "CFBundleIdentifier"));
+ }));
+ }));
+
+ if (mac && info == "Info.plist")
+ executable = "MacOS/" + executable;
+
+ progress(root + "*");
+
+ std::string entitlements;
+ folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
+ // XXX: this is a miserable fail
+ std::stringbuf temp;
+ copy(buffer, temp, length, progress);
+ // XXX: this is a stupid hack
+ pad(temp, 0x10 - (length & 0xf));
+ auto data(temp.str());
+ entitlements = alter(root, Analyze(data.data(), data.size()));
+ }));
+
+ static const std::string directory("_CodeSignature/");
+ static const std::string signature(directory + "CodeResources");
+
+ std::map<std::string, std::multiset<Rule>> versions;
+
+ auto &rules1(versions[""]);
+ auto &rules2(versions["2"]);
+
+ const std::string resources(mac ? "Resources/" : "");
+
+ if (true) {
+ rules1.insert(Rule{1, NoMode, "^" + (resources == "" ? ".*" : resources)});
+ rules1.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"});
+ rules1.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"});
+ rules1.insert(Rule{1010, NoMode, "^" + resources + "Base\\.lproj/"});
+ rules1.insert(Rule{1, NoMode, "^version.plist$"});
+ }
+
+ if (true) {
+ rules2.insert(Rule{11, NoMode, ".*\\.dSYM($|/)"});
+ if (mac) rules2.insert(Rule{20, NoMode, "^" + resources});
+ rules2.insert(Rule{2000, OmitMode, "^(.*/)?\\.DS_Store$"});
+ if (mac) rules2.insert(Rule{10, NestedMode, "^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/"});
+ rules2.insert(Rule{1, NoMode, "^.*"});
+ rules2.insert(Rule{1000, OptionalMode, "^" + resources + ".*\\.lproj/"});
+ rules2.insert(Rule{1100, OmitMode, "^" + resources + ".*\\.lproj/locversion.plist$"});
+ if (!mac) rules2.insert(Rule{1010, NoMode, "^Base\\.lproj/"});
+ rules2.insert(Rule{20, OmitMode, "^Info\\.plist$"});
+ rules2.insert(Rule{20, OmitMode, "^PkgInfo$"});
+ if (mac) rules2.insert(Rule{10, NestedMode, "^[^/]+$"});
+ rules2.insert(Rule{20, NoMode, "^embedded\\.provisionprofile$"});
+ if (mac) rules2.insert(Rule{1010, NoMode, "^" + resources + "Base\\.lproj/"});
+ rules2.insert(Rule{20, NoMode, "^version\\.plist$"});
+ }
+
+ State local;
+
+ std::string failure(mac ? "Contents/|Versions/[^/]*/Resources/" : "");
+ Expression nested("^(Frameworks/[^/]*\\.framework|PlugIns/[^/]*\\.appex(()|/[^/]*.app))/(" + failure + ")Info\\.plist$");
+ std::map<std::string, Bundle> bundles;
+
+ folder.Find("", fun([&](const std::string &name) {
+ if (!nested(name))
+ return;
+ auto bundle(root + Split(name).dir);
+ if (mac) {
+ _assert(!bundle.empty());
+ bundle = Split(bundle.substr(0, bundle.size() - 1)).dir;
+ }
+ SubFolder subfolder(folder, bundle);
+
+ bundles[nested[1]] = Sign(bundle, subfolder, key, local, "", Starts(name, "PlugIns/") ? alter :
+ static_cast<const Functor<std::string (const std::string &, const std::string &)> &>(fun([&](const std::string &, const std::string &) -> std::string { return entitlements; }))
+ , progress);
+ }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
+ }));
+
+ std::set<std::string> excludes;
+
+ auto exclude([&](const std::string &name) {
+ // BundleDiskRep::adjustResources -> builder.addExclusion
+ if (name == executable || Starts(name, directory) || Starts(name, "_MASReceipt/") || name == "CodeResources")
+ return true;
+
+ for (const auto &bundle : bundles)
+ if (Starts(name, bundle.first + "/")) {
+ excludes.insert(name);
+ return true;
+ }
+
+ return false;
+ });
+
+ folder.Find("", fun([&](const std::string &name) {
+ if (exclude(name))
+ return;
+
+ if (local.files.find(name) != local.files.end())
+ return;
+ auto &hash(local.files[name]);
+
+ folder.Open(name, fun([&](std::streambuf &data, size_t length, const void *flag) {
+ progress(root + name);
+
+ union {
+ struct {
+ uint32_t magic;
+ uint32_t count;
+ };
+
+ uint8_t bytes[8];
+ } header;
+
+ auto size(most(data, &header.bytes, sizeof(header.bytes)));
+
+ if (name != "_WatchKitStub/WK" && size == sizeof(header.bytes))
+ switch (Swap(header.magic)) {
+ case FAT_MAGIC:
+ // Java class file format
+ if (Swap(header.count) >= 40)
+ break;
+ case FAT_CIGAM:
+ case MH_MAGIC: case MH_MAGIC_64:
+ case MH_CIGAM: case MH_CIGAM_64:
+ folder.Save(name, true, flag, fun([&](std::streambuf &save) {
+ Slots slots;
+ Sign(header.bytes, size, data, hash, save, identifier, "", false, "", key, slots, length, 0, false, Progression(progress, root + name));
+ }));
+ return;
+ }
+
+ folder.Save(name, false, flag, fun([&](std::streambuf &save) {
+ HashProxy proxy(hash, save);
+ put(proxy, header.bytes, size);
+ copy(data, proxy, length - size, progress);
+ }));
+ }));
+ }), fun([&](const std::string &name, const Functor<std::string ()> &read) {
+ if (exclude(name))
+ return;
+
+ local.links[name] = read();
+ }));
+
+ auto plist(plist_new_dict());
+ _scope({ plist_free(plist); });
+
+ for (const auto &version : versions) {
+ auto files(plist_new_dict());
+ plist_dict_set_item(plist, ("files" + version.first).c_str(), files);
+
+ for (const auto &rule : version.second)
+ rule.Compile();
+
+ bool old(&version.second == &rules1);
+
+ for (const auto &hash : local.files)
+ for (const auto &rule : version.second)
+ if (rule(hash.first)) {
+ if (!old && mac && excludes.find(hash.first) != excludes.end());
+ else if (old && rule.mode_ == NoMode)
+ plist_dict_set_item(files, hash.first.c_str(), plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_)));
+ else if (rule.mode_ != OmitMode) {
+ auto entry(plist_new_dict());
+ plist_dict_set_item(entry, "hash", plist_new_data(reinterpret_cast<const char *>(hash.second.sha1_), sizeof(hash.second.sha1_)));
+ if (!old)
+ plist_dict_set_item(entry, "hash2", plist_new_data(reinterpret_cast<const char *>(hash.second.sha256_), sizeof(hash.second.sha256_)));
+ if (rule.mode_ == OptionalMode)
+ plist_dict_set_item(entry, "optional", plist_new_bool(true));
+ plist_dict_set_item(files, hash.first.c_str(), entry);
+ }
+
+ break;
+ }
+
+ if (!old)
+ for (const auto &link : local.links)
+ for (const auto &rule : version.second)
+ if (rule(link.first)) {
+ if (rule.mode_ != OmitMode) {
+ auto entry(plist_new_dict());
+ plist_dict_set_item(entry, "symlink", plist_new_string(link.second.c_str()));
+ if (rule.mode_ == OptionalMode)
+ plist_dict_set_item(entry, "optional", plist_new_bool(true));
+ plist_dict_set_item(files, link.first.c_str(), entry);
+ }
+
+ break;
+ }
+
+ if (!old && mac)
+ for (const auto &bundle : bundles) {
+ auto entry(plist_new_dict());
+ plist_dict_set_item(entry, "cdhash", plist_new_data(reinterpret_cast<const char *>(bundle.second.hash.sha256_), sizeof(bundle.second.hash.sha256_)));
+ plist_dict_set_item(entry, "requirement", plist_new_string("anchor apple generic"));
+ plist_dict_set_item(files, bundle.first.c_str(), entry);
+ }
+ }
+
+ for (const auto &version : versions) {
+ auto rules(plist_new_dict());
+ plist_dict_set_item(plist, ("rules" + version.first).c_str(), rules);
+
+ std::multiset<const Rule *, RuleCode> ordered;
+ for (const auto &rule : version.second)
+ ordered.insert(&rule);
+
+ for (const auto &rule : ordered)
+ if (rule->weight_ == 1 && rule->mode_ == NoMode)
+ plist_dict_set_item(rules, rule->code_.c_str(), plist_new_bool(true));
+ else {
+ auto entry(plist_new_dict());
+ plist_dict_set_item(rules, rule->code_.c_str(), entry);
+
+ switch (rule->mode_) {
+ case NoMode:
+ break;
+ case OmitMode:
+ plist_dict_set_item(entry, "omit", plist_new_bool(true));
+ break;
+ case OptionalMode:
+ plist_dict_set_item(entry, "optional", plist_new_bool(true));
+ break;
+ case NestedMode:
+ plist_dict_set_item(entry, "nested", plist_new_bool(true));
+ break;
+ case TopMode:
+ plist_dict_set_item(entry, "top", plist_new_bool(true));
+ break;
+ }
+
+ if (rule->weight_ >= 10000)
+ plist_dict_set_item(entry, "weight", plist_new_uint(rule->weight_));
+ else if (rule->weight_ != 1)
+ plist_dict_set_item(entry, "weight", plist_new_real(rule->weight_));
+ }
+ }
+
+ folder.Save(signature, true, NULL, fun([&](std::streambuf &save) {
+ HashProxy proxy(local.files[signature], save);
+ char *xml(NULL);
+ uint32_t size;
+ plist_to_xml(plist, &xml, &size);
+ _scope({ free(xml); });
+ put(proxy, xml, size);
+ }));
+
+ Bundle bundle;
+ bundle.path = folder.Path(executable);
+
+ folder.Open(executable, fun([&](std::streambuf &buffer, size_t length, const void *flag) {
+ progress(root + executable);
+ folder.Save(executable, true, flag, fun([&](std::streambuf &save) {
+ Slots slots;
+ slots[1] = local.files.at(info);
+ slots[3] = local.files.at(signature);
+ bundle.hash = Sign(NULL, 0, buffer, local.files[executable], save, identifier, entitlements, false, requirements, key, slots, length, 0, false, Progression(progress, root + executable));
+ }));
+ }));
+
+ remote.Merge(root, local);
+ return bundle;
+}
+
+Bundle Sign(const std::string &root, Folder &folder, const std::string &key, const std::string &requirements, const Functor<std::string (const std::string &, const std::string &)> &alter, const Progress &progress) {
+ State local;
+ return Sign(root, folder, key, local, requirements, alter, progress);
+}
+#endif
+
+#endif
+}
+
+std::string Hex(const uint8_t *data, size_t size) {
+ std::string hex;
+ hex.reserve(size * 2);
+ for (size_t i(0); i != size; ++i) {
+ hex += "0123456789abcdef"[data[i] >> 4];
+ hex += "0123456789abcdef"[data[i] & 0xf];
+ }
+ return hex;
+}
+
+static void usage(const char *argv0) {
+ fprintf(stderr, "usage: %s -S[entitlements.xml] <binary>\n", argv0);
+ fprintf(stderr, " %s -e MobileSafari\n", argv0);
+ fprintf(stderr, " %s -S cat\n", argv0);
+ fprintf(stderr, " %s -Stfp.xml gdb\n", argv0);
+}
+
+#ifndef LDID_NOTOOLS